blob: d38fa4b0231a193fbb8c29da8ea1f53e7af1a607 [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 Moolenaar21f990e2018-08-11 19:20:49 +0200319 || STRNCMP(cmd, "global", p - cmd) == 0
320 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200321 {
Bram Moolenaar167ae422018-08-14 21:32:21 +0200322 if (*cmd == 's' && cmd[1] == 'm')
323 p_magic = TRUE;
324 else if (*cmd == 's' && cmd[1] == 'n')
325 p_magic = FALSE;
326
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200327 // Check for "global!/".
328 if (*cmd == 'g' && *p == '!')
329 {
330 p++;
331 if (*skipwhite(p) == NUL)
332 return FALSE;
333 }
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200334 p = skipwhite(p);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200335 delim = *p++;
336 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200337 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200338 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200339 pos_T save_cursor = curwin->w_cursor;
340
341 // found a non-empty pattern
342 *skiplen = (int)(p - ccline.cmdbuff);
343 *patlen = (int)(end - p);
344
345 // parse the address range
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200346 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200347 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200348 if (ea.addr_count > 0)
349 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200350 // Allow for reverse match.
351 if (ea.line2 < ea.line1)
352 {
353 search_first_line = ea.line2;
354 search_last_line = ea.line1;
355 }
356 else
357 {
358 search_first_line = ea.line1;
359 search_last_line = ea.line2;
360 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200361 }
362 else if (*cmd == 's')
363 {
364 // :s defaults to the current line
365 search_first_line = curwin->w_cursor.lnum;
366 search_last_line = curwin->w_cursor.lnum;
367 }
368
369 curwin->w_cursor = save_cursor;
370 return TRUE;
371 }
372 }
373 }
374 }
375 }
376
377 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200378}
379
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200380 static void
381finish_incsearch_highlighting(
382 int gotesc,
383 incsearch_state_T *is_state,
384 int call_update_screen)
385{
386 if (is_state->did_incsearch)
387 {
388 is_state->did_incsearch = FALSE;
389 if (gotesc)
390 curwin->w_cursor = is_state->save_cursor;
391 else
392 {
393 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
394 {
395 // put the '" mark at the original position
396 curwin->w_cursor = is_state->save_cursor;
397 setpcmark();
398 }
399 curwin->w_cursor = is_state->search_start;
400 }
401 restore_viewstate(&is_state->old_viewstate);
402 highlight_match = FALSE;
403 validate_cursor(); /* needed for TAB */
404 if (call_update_screen)
405 update_screen(SOME_VALID);
406 else
407 redraw_all_later(SOME_VALID);
Bram Moolenaar167ae422018-08-14 21:32:21 +0200408 p_magic = is_state->magic_save;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200409 }
410}
411
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200412/*
413 * Do 'incsearch' highlighting if desired.
414 */
415 static void
416may_do_incsearch_highlighting(
417 int firstc,
418 long count,
419 incsearch_state_T *is_state)
420{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200421 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422 int i;
423 pos_T end_pos;
424 struct cmdline_info save_ccline;
425#ifdef FEAT_RELTIME
426 proftime_T tm;
427#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200428 int next_char;
429 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200430
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200431 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200432 {
433 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200434 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200435 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200436
437 // If there is a character waiting, search and redraw later.
438 if (char_avail())
439 {
440 is_state->incsearch_postponed = TRUE;
441 return;
442 }
443 is_state->incsearch_postponed = FALSE;
444
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200445 if (search_first_line == 0)
446 // start at the original cursor position
447 curwin->w_cursor = is_state->search_start;
448 else
449 {
450 // start at the first line in the range
451 curwin->w_cursor.lnum = search_first_line;
452 curwin->w_cursor.col = 0;
453 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200454 save_last_search_pattern();
455
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200456 // Use the previous pattern for ":s//".
457 next_char = ccline.cmdbuff[skiplen + patlen];
458 use_last_pat = patlen == 0 && skiplen > 0
459 && ccline.cmdbuff[skiplen - 1] == next_char;
460
461 // If there is no pattern, don't do anything.
462 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200463 {
464 i = 0;
465 set_no_hlsearch(TRUE); // turn off previous highlight
466 redraw_all_later(SOME_VALID);
467 }
468 else
469 {
470 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
471
472 cursor_off(); // so the user knows we're busy
473 out_flush();
474 ++emsg_off; // so it doesn't beep if bad expr
475#ifdef FEAT_RELTIME
476 // Set the time limit to half a second.
477 profile_setlimit(500L, &tm);
478#endif
479 if (!p_hls)
480 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200481 if (search_first_line != 0)
482 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200483 ccline.cmdbuff[skiplen + patlen] = NUL;
484 i = do_search(NULL, firstc == ':' ? '/' : firstc,
485 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200486#ifdef FEAT_RELTIME
487 &tm, NULL
488#else
489 NULL, NULL
490#endif
491 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200492 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200493 --emsg_off;
494
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200495 if (curwin->w_cursor.lnum < search_first_line
496 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200497 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200498 // match outside of address range
499 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200500 curwin->w_cursor = is_state->search_start;
501 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200502
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200503 // if interrupted while searching, behave like it failed
504 if (got_int)
505 {
506 (void)vpeekc(); // remove <C-C> from input stream
507 got_int = FALSE; // don't abandon the command line
508 i = 0;
509 }
510 else if (char_avail())
511 // cancelled searching because a char was typed
512 is_state->incsearch_postponed = TRUE;
513 }
514 if (i != 0)
515 highlight_match = TRUE; // highlight position
516 else
517 highlight_match = FALSE; // remove highlight
518
519 // First restore the old curwin values, so the screen is positioned in the
520 // same way as the actual search command.
521 restore_viewstate(&is_state->old_viewstate);
522 changed_cline_bef_curs();
523 update_topline();
524
525 if (i != 0)
526 {
527 pos_T save_pos = curwin->w_cursor;
528
529 is_state->match_start = curwin->w_cursor;
530 set_search_match(&curwin->w_cursor);
531 validate_cursor();
532 end_pos = curwin->w_cursor;
533 is_state->match_end = end_pos;
534 curwin->w_cursor = save_pos;
535 }
536 else
537 end_pos = curwin->w_cursor; // shutup gcc 4
538
539 // Disable 'hlsearch' highlighting if the pattern matches everything.
540 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200541 if (!use_last_pat)
542 {
543 next_char = ccline.cmdbuff[skiplen + patlen];
544 ccline.cmdbuff[skiplen + patlen] = NUL;
545 if (empty_pattern(ccline.cmdbuff))
546 set_no_hlsearch(TRUE);
547 ccline.cmdbuff[skiplen + patlen] = next_char;
548 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549
550 validate_cursor();
551 // May redraw the status line to show the cursor position.
552 if (p_ru && curwin->w_status_height > 0)
553 curwin->w_redr_status = TRUE;
554
555 save_cmdline(&save_ccline);
556 update_screen(SOME_VALID);
557 restore_cmdline(&save_ccline);
558 restore_last_search_pattern();
559
560 // Leave it at the end to make CTRL-R CTRL-W work.
561 if (i != 0)
562 curwin->w_cursor = end_pos;
563
564 msg_starthere();
565 redrawcmdline();
566 is_state->did_incsearch = TRUE;
567}
568
569/*
570 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
571 * or previous match.
572 * Returns FAIL when jumping to cmdline_not_changed;
573 */
574 static int
575may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200576 int firstc,
577 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200579 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200580{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200581 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200582 pos_T t;
583 char_u *pat;
584 int search_flags = SEARCH_NOOF;
585 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200586 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200588 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200589 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200590 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591 return FAIL;
592
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200593 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200594 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200595 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200596 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200597 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200598 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200600 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200601
602 save_last_search_pattern();
603 cursor_off();
604 out_flush();
605 if (c == Ctrl_G)
606 {
607 t = is_state->match_end;
608 if (LT_POS(is_state->match_start, is_state->match_end))
609 // Start searching at the end of the match not at the beginning of
610 // the next column.
611 (void)decl(&t);
612 search_flags += SEARCH_COL;
613 }
614 else
615 t = is_state->match_start;
616 if (!p_hls)
617 search_flags += SEARCH_KEEP;
618 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200619 save = pat[patlen];
620 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200621 i = searchit(curwin, curbuf, &t,
622 c == Ctrl_G ? FORWARD : BACKWARD,
623 pat, count, search_flags,
624 RE_SEARCH, 0, NULL, NULL);
625 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200626 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200627 if (i)
628 {
629 is_state->search_start = is_state->match_start;
630 is_state->match_end = t;
631 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200632 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200633 {
634 // Move just before the current match, so that when nv_search
635 // finishes the cursor will be put back on the match.
636 is_state->search_start = t;
637 (void)decl(&is_state->search_start);
638 }
639 else if (c == Ctrl_G && firstc == '?')
640 {
641 // Move just after the current match, so that when nv_search
642 // finishes the cursor will be put back on the match.
643 is_state->search_start = t;
644 (void)incl(&is_state->search_start);
645 }
646 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
647 {
648 // wrap around
649 is_state->search_start = t;
650 if (firstc == '?')
651 (void)incl(&is_state->search_start);
652 else
653 (void)decl(&is_state->search_start);
654 }
655
656 set_search_match(&is_state->match_end);
657 curwin->w_cursor = is_state->match_start;
658 changed_cline_bef_curs();
659 update_topline();
660 validate_cursor();
661 highlight_match = TRUE;
662 save_viewstate(&is_state->old_viewstate);
663 update_screen(NOT_VALID);
664 redrawcmdline();
665 }
666 else
667 vim_beep(BO_ERROR);
668 restore_last_search_pattern();
669 return FAIL;
670}
671
672/*
673 * When CTRL-L typed: add character from the match to the pattern.
674 * May set "*c" to the added character.
675 * Return OK when jumping to cmdline_not_changed.
676 */
677 static int
678may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
679{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200680 int skiplen, patlen;
681
682 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200683 return FAIL;
684
685 // Add a character from under the cursor for 'incsearch'.
686 if (is_state->did_incsearch)
687 {
688 curwin->w_cursor = is_state->match_end;
689 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
690 {
691 *c = gchar_cursor();
692
693 // If 'ignorecase' and 'smartcase' are set and the
694 // command line has no uppercase characters, convert
695 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200696 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200697 *c = MB_TOLOWER(*c);
698 if (*c != NUL)
699 {
700 if (*c == firstc || vim_strchr((char_u *)(
701 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
702 {
703 // put a backslash before special characters
704 stuffcharReadbuff(*c);
705 *c = '\\';
706 }
707 return FAIL;
708 }
709 }
710 }
711 return OK;
712}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200713#endif
714
Bram Moolenaar66216052017-12-16 16:33:44 +0100715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 * getcmdline() - accept a command line starting with firstc.
717 *
718 * firstc == ':' get ":" command line.
719 * firstc == '/' or '?' get search pattern
720 * firstc == '=' get expression
721 * firstc == '@' get text for input() function
722 * firstc == '>' get text for debug mode
723 * firstc == NUL get text for :insert command
724 * firstc == -1 like NUL, and break on CTRL-C
725 *
726 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
727 * command line.
728 *
729 * Careful: getcmdline() can be called recursively!
730 *
731 * Return pointer to allocated string if there is a commandline, NULL
732 * otherwise.
733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100735getcmdline(
736 int firstc,
737 long count UNUSED, /* only used for incremental search */
738 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 int c;
741 int i;
742 int j;
743 int gotesc = FALSE; /* TRUE when <ESC> just typed */
744 int do_abbr; /* when TRUE check for abbr. */
745#ifdef FEAT_CMDHIST
746 char_u *lookfor = NULL; /* string to match */
747 int hiscnt; /* current history line in use */
748 int histype; /* history type to be used */
749#endif
750#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200751 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#endif
753 int did_wild_list = FALSE; /* did wild_list() recently */
754 int wim_index = 0; /* index in wim_flags[] */
755 int res;
756 int save_msg_scroll = msg_scroll;
757 int save_State = State; /* remember State when called */
758 int some_key_typed = FALSE; /* one of the keys was typed */
759#ifdef FEAT_MOUSE
760 /* mouse drag and release events are ignored, unless they are
761 * preceded with a mouse down event */
762 int ignore_drag_release = TRUE;
763#endif
764#ifdef FEAT_EVAL
765 int break_ctrl_c = FALSE;
766#endif
767 expand_T xpc;
768 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200769#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000770 /* Everything that may work recursively should save and restore the
771 * current command line in save_ccline. That includes update_screen(), a
772 * custom status line may invoke ":normal". */
773 struct cmdline_info save_ccline;
774#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200775 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#ifdef FEAT_EVAL
778 if (firstc == -1)
779 {
780 firstc = NUL;
781 break_ctrl_c = TRUE;
782 }
783#endif
784#ifdef FEAT_RIGHTLEFT
785 /* start without Hebrew mapping for a command line */
786 if (firstc == ':' || firstc == '=' || firstc == '>')
787 cmd_hkmap = 0;
788#endif
789
790 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200793 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794#endif
795
796 /*
797 * set some variables for redrawcmd()
798 */
799 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000800 ccline.cmdindent = (firstc > 0 ? indent : 0);
801
802 /* alloc initial ccline.cmdbuff */
803 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (ccline.cmdbuff == NULL)
805 return NULL; /* out of memory */
806 ccline.cmdlen = ccline.cmdpos = 0;
807 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100808 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000810 /* autoindent for :insert and :append */
811 if (firstc <= 0)
812 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200813 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000814 ccline.cmdbuff[indent] = NUL;
815 ccline.cmdpos = indent;
816 ccline.cmdspos = indent;
817 ccline.cmdlen = indent;
818 }
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000821 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823#ifdef FEAT_RIGHTLEFT
824 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
825 && (firstc == '/' || firstc == '?'))
826 cmdmsg_rl = TRUE;
827 else
828 cmdmsg_rl = FALSE;
829#endif
830
831 redir_off = TRUE; /* don't redirect the typed command */
832 if (!cmd_silent)
833 {
834 i = msg_scrolled;
835 msg_scrolled = 0; /* avoid wait_return message */
836 gotocmdline(TRUE);
837 msg_scrolled += i;
838 redrawcmdprompt(); /* draw prompt or indent */
839 set_cmdspos();
840 }
841 xpc.xp_context = EXPAND_NOTHING;
842 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000843#ifndef BACKSLASH_IN_FILENAME
844 xpc.xp_shell = FALSE;
845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000847#if defined(FEAT_EVAL)
848 if (ccline.input_fn)
849 {
850 xpc.xp_context = ccline.xp_context;
851 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000852# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000853 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000854# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000855 }
856#endif
857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 /*
859 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
860 * doing ":@0" when register 0 doesn't contain a CR.
861 */
862 msg_scroll = FALSE;
863
864 State = CMDLINE;
865
866 if (firstc == '/' || firstc == '?' || firstc == '@')
867 {
868 /* Use ":lmap" mappings for search pattern and input(). */
869 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
870 b_im_ptr = &curbuf->b_p_iminsert;
871 else
872 b_im_ptr = &curbuf->b_p_imsearch;
873 if (*b_im_ptr == B_IMODE_LMAP)
874 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100875#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 im_set_active(*b_im_ptr == B_IMODE_IM);
877#endif
878 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100879#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else if (p_imcmdline)
881 im_set_active(TRUE);
882#endif
883
884#ifdef FEAT_MOUSE
885 setmouse();
886#endif
887#ifdef CURSOR_SHAPE
888 ui_cursor_shape(); /* may show different cursor shape */
889#endif
890
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000891 /* When inside an autocommand for writing "exiting" may be set and
892 * terminal mode set to cooked. Need to set raw mode here then. */
893 settmode(TMODE_RAW);
894
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200895 /* Trigger CmdlineEnter autocommands. */
896 cmdline_type = firstc == NUL ? '-' : firstc;
897 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899#ifdef FEAT_CMDHIST
900 init_history();
901 hiscnt = hislen; /* set hiscnt to impossible history value */
902 histype = hist_char2type(firstc);
903#endif
904
905#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000906 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200909 /* If something above caused an error, reset the flags, we do want to type
910 * and execute commands. Display may be messed up a bit. */
911 if (did_emsg)
912 redrawcmd();
913 did_emsg = FALSE;
914 got_int = FALSE;
915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 /*
917 * Collect the command string, handling editing keys.
918 */
919 for (;;)
920 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000921 redir_off = TRUE; /* Don't redirect the typed command.
922 Repeated, because a ":redir" inside
923 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#ifdef USE_ON_FLY_SCROLL
925 dont_scroll = FALSE; /* allow scrolling here */
926#endif
927 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
928
Bram Moolenaar72532d32018-04-07 19:09:09 +0200929 did_emsg = FALSE; /* There can't really be a reason why an error
930 that occurs while typing a command should
931 cause the command not to be executed. */
932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000934
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100935 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
936 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000937 do
938 {
939 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100940 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000941
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 if (KeyTyped)
943 {
944 some_key_typed = TRUE;
945#ifdef FEAT_RIGHTLEFT
946 if (cmd_hkmap)
947 c = hkmap(c);
948# ifdef FEAT_FKMAP
949 if (cmd_fkmap)
950 c = cmdl_fkmap(c);
951# endif
952 if (cmdmsg_rl && !KeyStuffed)
953 {
954 /* Invert horizontal movements and operations. Only when
955 * typed by the user directly, not when the result of a
956 * mapping. */
957 switch (c)
958 {
959 case K_RIGHT: c = K_LEFT; break;
960 case K_S_RIGHT: c = K_S_LEFT; break;
961 case K_C_RIGHT: c = K_C_LEFT; break;
962 case K_LEFT: c = K_RIGHT; break;
963 case K_S_LEFT: c = K_S_RIGHT; break;
964 case K_C_LEFT: c = K_C_RIGHT; break;
965 }
966 }
967#endif
968 }
969
970 /*
971 * Ignore got_int when CTRL-C was typed here.
972 * Don't ignore it in :global, we really need to break then, e.g., for
973 * ":g/pat/normal /pat" (without the <CR>).
974 * Don't ignore it for the input() function.
975 */
976 if ((c == Ctrl_C
977#ifdef UNIX
978 || c == intr_char
979#endif
980 )
981#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
982 && firstc != '@'
983#endif
984#ifdef FEAT_EVAL
985 && !break_ctrl_c
986#endif
987 && !global_busy)
988 got_int = FALSE;
989
990#ifdef FEAT_CMDHIST
991 /* free old command line when finished moving around in the history
992 * list */
993 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000994 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000995 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 && c != K_PAGEDOWN && c != K_PAGEUP
997 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000998 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001000 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#endif
1002
1003 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001004 * When there are matching completions to select <S-Tab> works like
1005 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001007 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 c = Ctrl_P;
1009
1010#ifdef FEAT_WILDMENU
1011 /* Special translations for 'wildmenu' */
1012 if (did_wild_list && p_wmnu)
1013 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001014 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001016 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 c = Ctrl_N;
1018 }
1019 /* Hitting CR after "emenu Name.": complete submenu */
1020 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1021 && ccline.cmdpos > 1
1022 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1023 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1024 && (c == '\n' || c == '\r' || c == K_KENTER))
1025 c = K_DOWN;
1026#endif
1027
1028 /* free expanded names when finished walking through matches */
1029 if (xpc.xp_numfiles != -1
1030 && !(c == p_wc && KeyTyped) && c != p_wcm
1031 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1032 && c != Ctrl_L)
1033 {
1034 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1035 did_wild_list = FALSE;
1036#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001037 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038#endif
1039 xpc.xp_context = EXPAND_NOTHING;
1040 wim_index = 0;
1041#ifdef FEAT_WILDMENU
1042 if (p_wmnu && wild_menu_showing != 0)
1043 {
1044 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001045 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001046
1047 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001048 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 if (wild_menu_showing == WM_SCROLLED)
1051 {
1052 /* Entered command line, move it up */
1053 cmdline_row--;
1054 redrawcmd();
1055 }
1056 else if (save_p_ls != -1)
1057 {
1058 /* restore 'laststatus' and 'winminheight' */
1059 p_ls = save_p_ls;
1060 p_wmh = save_p_wmh;
1061 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001062 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001064 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 redrawcmd();
1066 save_p_ls = -1;
1067 }
1068 else
1069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 redraw_statuslines();
1072 }
1073 KeyTyped = skt;
1074 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001075 if (ccline.input_fn)
1076 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078#endif
1079 }
1080
1081#ifdef FEAT_WILDMENU
1082 /* Special translations for 'wildmenu' */
1083 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1084 {
1085 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001086 if (c == K_DOWN && ccline.cmdpos > 0
1087 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001089 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {
1091 /* Hitting <Up>: Remove one submenu name in front of the
1092 * cursor */
1093 int found = FALSE;
1094
1095 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1096 i = 0;
1097 while (--j > 0)
1098 {
1099 /* check for start of menu name */
1100 if (ccline.cmdbuff[j] == ' '
1101 && ccline.cmdbuff[j - 1] != '\\')
1102 {
1103 i = j + 1;
1104 break;
1105 }
1106 /* check for start of submenu name */
1107 if (ccline.cmdbuff[j] == '.'
1108 && ccline.cmdbuff[j - 1] != '\\')
1109 {
1110 if (found)
1111 {
1112 i = j + 1;
1113 break;
1114 }
1115 else
1116 found = TRUE;
1117 }
1118 }
1119 if (i > 0)
1120 cmdline_del(i);
1121 c = p_wc;
1122 xpc.xp_context = EXPAND_NOTHING;
1123 }
1124 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001125 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001126 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001127 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
1129 char_u upseg[5];
1130
1131 upseg[0] = PATHSEP;
1132 upseg[1] = '.';
1133 upseg[2] = '.';
1134 upseg[3] = PATHSEP;
1135 upseg[4] = NUL;
1136
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001137 if (c == K_DOWN
1138 && ccline.cmdpos > 0
1139 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1140 && (ccline.cmdpos < 3
1141 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1143 {
1144 /* go down a directory */
1145 c = p_wc;
1146 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001147 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 /* If in a direct ancestor, strip off one ../ to go down */
1150 int found = FALSE;
1151
1152 j = ccline.cmdpos;
1153 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1154 while (--j > i)
1155 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001156#ifdef FEAT_MBYTE
1157 if (has_mbyte)
1158 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (vim_ispathsep(ccline.cmdbuff[j]))
1161 {
1162 found = TRUE;
1163 break;
1164 }
1165 }
1166 if (found
1167 && ccline.cmdbuff[j - 1] == '.'
1168 && ccline.cmdbuff[j - 2] == '.'
1169 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1170 {
1171 cmdline_del(j - 2);
1172 c = p_wc;
1173 }
1174 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001175 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177 /* go up a directory */
1178 int found = FALSE;
1179
1180 j = ccline.cmdpos - 1;
1181 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1182 while (--j > i)
1183 {
1184#ifdef FEAT_MBYTE
1185 if (has_mbyte)
1186 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1187#endif
1188 if (vim_ispathsep(ccline.cmdbuff[j])
1189#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001190 && vim_strchr((char_u *)" *?[{`$%#",
1191 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#endif
1193 )
1194 {
1195 if (found)
1196 {
1197 i = j + 1;
1198 break;
1199 }
1200 else
1201 found = TRUE;
1202 }
1203 }
1204
1205 if (!found)
1206 j = i;
1207 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1208 j += 4;
1209 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1210 && j == i)
1211 j += 3;
1212 else
1213 j = 0;
1214 if (j > 0)
1215 {
1216 /* TODO this is only for DOS/UNIX systems - need to put in
1217 * machine-specific stuff here and in upseg init */
1218 cmdline_del(j);
1219 put_on_cmdline(upseg + 1, 3, FALSE);
1220 }
1221 else if (ccline.cmdpos > i)
1222 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001223
1224 /* Now complete in the new directory. Set KeyTyped in case the
1225 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001227 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231#endif /* FEAT_WILDMENU */
1232
1233 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1234 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1235 if (c == Ctrl_BSL)
1236 {
1237 ++no_mapping;
1238 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001239 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 --no_mapping;
1241 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001242 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1243 * is in a mapping. */
1244 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1245 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 vungetc(c);
1248 c = Ctrl_BSL;
1249 }
1250#ifdef FEAT_EVAL
1251 else if (c == 'e')
1252 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001253 char_u *p = NULL;
1254 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256 /*
1257 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001258 * Need to save and restore the current command line, to be
1259 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 */
1261 if (ccline.cmdpos == ccline.cmdlen)
1262 new_cmdpos = 99999; /* keep it at the end */
1263 else
1264 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001265
1266 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001268 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (c == '=')
1270 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001271 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001272 * to avoid nasty things like going to another buffer when
1273 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001274 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001275 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001277 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001278 restore_cmdline(&save_ccline);
1279
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001280 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001282 len = (int)STRLEN(p);
1283 if (realloc_cmdbuff(len + 1) == OK)
1284 {
1285 ccline.cmdlen = len;
1286 STRCPY(ccline.cmdbuff, p);
1287 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001289 /* Restore the cursor or use the position set with
1290 * set_cmdline_pos(). */
1291 if (new_cmdpos > ccline.cmdlen)
1292 ccline.cmdpos = ccline.cmdlen;
1293 else
1294 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001296 KeyTyped = FALSE; /* Don't do p_wc completion. */
1297 redrawcmd();
1298 goto cmdline_changed;
1299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301 }
1302 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001303 got_int = FALSE; /* don't abandon the command line */
1304 did_emsg = FALSE;
1305 emsg_on_display = FALSE;
1306 redrawcmd();
1307 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309#endif
1310 else
1311 {
1312 if (c == Ctrl_G && p_im && restart_edit == 0)
1313 restart_edit = 'a';
1314 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1315 in history */
1316 goto returncmd; /* back to Normal mode */
1317 }
1318 }
1319
1320#ifdef FEAT_CMDWIN
1321 if (c == cedit_key || c == K_CMDWIN)
1322 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001323 if (ex_normal_busy == 0 && got_int == FALSE)
1324 {
1325 /*
1326 * Open a window to edit the command line (and history).
1327 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001328 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001329 some_key_typed = TRUE;
1330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332# ifdef FEAT_DIGRAPHS
1333 else
1334# endif
1335#endif
1336#ifdef FEAT_DIGRAPHS
1337 c = do_digraph(c);
1338#endif
1339
1340 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1341 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1342 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001343 /* In Ex mode a backslash escapes a newline. */
1344 if (exmode_active
1345 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001346 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001347 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001348 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001350 if (c == K_KENTER)
1351 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001353 else
1354 {
1355 gotesc = FALSE; /* Might have typed ESC previously, don't
1356 truncate the cmdline now. */
1357 if (ccheck_abbr(c + ABBR_OFF))
1358 goto cmdline_changed;
1359 if (!cmd_silent)
1360 {
1361 windgoto(msg_row, 0);
1362 out_flush();
1363 }
1364 break;
1365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367
1368 /*
1369 * Completion for 'wildchar' or 'wildcharm' key.
1370 * - hitting <ESC> twice means: abandon command line.
1371 * - wildcard expansion is only done when the 'wildchar' key is really
1372 * typed, not when it comes from a macro
1373 */
1374 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1375 {
1376 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1377 {
1378 /* if 'wildmode' contains "list" may still need to list */
1379 if (xpc.xp_numfiles > 1
1380 && !did_wild_list
1381 && (wim_flags[wim_index] & WIM_LIST))
1382 {
1383 (void)showmatches(&xpc, FALSE);
1384 redrawcmd();
1385 did_wild_list = TRUE;
1386 }
1387 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001388 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1389 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001391 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1392 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 else
1394 res = OK; /* don't insert 'wildchar' now */
1395 }
1396 else /* typed p_wc first time */
1397 {
1398 wim_index = 0;
1399 j = ccline.cmdpos;
1400 /* if 'wildmode' first contains "longest", get longest
1401 * common part */
1402 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001403 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1404 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001406 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1407 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 /* if interrupted while completing, behave like it failed */
1410 if (got_int)
1411 {
1412 (void)vpeekc(); /* remove <C-C> from input stream */
1413 got_int = FALSE; /* don't abandon the command line */
1414 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1415#ifdef FEAT_WILDMENU
1416 xpc.xp_context = EXPAND_NOTHING;
1417#endif
1418 goto cmdline_changed;
1419 }
1420
1421 /* when more than one match, and 'wildmode' first contains
1422 * "list", or no change and 'wildmode' contains "longest,list",
1423 * list all matches */
1424 if (res == OK && xpc.xp_numfiles > 1)
1425 {
1426 /* a "longest" that didn't do anything is skipped (but not
1427 * "list:longest") */
1428 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1429 wim_index = 1;
1430 if ((wim_flags[wim_index] & WIM_LIST)
1431#ifdef FEAT_WILDMENU
1432 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1433#endif
1434 )
1435 {
1436 if (!(wim_flags[0] & WIM_LONGEST))
1437 {
1438#ifdef FEAT_WILDMENU
1439 int p_wmnu_save = p_wmnu;
1440 p_wmnu = 0;
1441#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001442 /* remove match */
1443 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444#ifdef FEAT_WILDMENU
1445 p_wmnu = p_wmnu_save;
1446#endif
1447 }
1448#ifdef FEAT_WILDMENU
1449 (void)showmatches(&xpc, p_wmnu
1450 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1451#else
1452 (void)showmatches(&xpc, FALSE);
1453#endif
1454 redrawcmd();
1455 did_wild_list = TRUE;
1456 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001457 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1458 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001460 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1461 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001464 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466#ifdef FEAT_WILDMENU
1467 else if (xpc.xp_numfiles == -1)
1468 xpc.xp_context = EXPAND_NOTHING;
1469#endif
1470 }
1471 if (wim_index < 3)
1472 ++wim_index;
1473 if (c == ESC)
1474 gotesc = TRUE;
1475 if (res == OK)
1476 goto cmdline_changed;
1477 }
1478
1479 gotesc = FALSE;
1480
1481 /* <S-Tab> goes to last match, in a clumsy way */
1482 if (c == K_S_TAB && KeyTyped)
1483 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001484 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1485 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1486 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 goto cmdline_changed;
1488 }
1489
1490 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1491 c = NL;
1492
1493 do_abbr = TRUE; /* default: check for abbreviation */
1494
1495 /*
1496 * Big switch for a typed command line character.
1497 */
1498 switch (c)
1499 {
1500 case K_BS:
1501 case Ctrl_H:
1502 case K_DEL:
1503 case K_KDEL:
1504 case Ctrl_W:
1505#ifdef FEAT_FKMAP
1506 if (cmd_fkmap && c == K_BS)
1507 c = K_DEL;
1508#endif
1509 if (c == K_KDEL)
1510 c = K_DEL;
1511
1512 /*
1513 * delete current character is the same as backspace on next
1514 * character, except at end of line
1515 */
1516 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1517 ++ccline.cmdpos;
1518#ifdef FEAT_MBYTE
1519 if (has_mbyte && c == K_DEL)
1520 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1521 ccline.cmdbuff + ccline.cmdpos);
1522#endif
1523 if (ccline.cmdpos > 0)
1524 {
1525 char_u *p;
1526
1527 j = ccline.cmdpos;
1528 p = ccline.cmdbuff + j;
1529#ifdef FEAT_MBYTE
1530 if (has_mbyte)
1531 {
1532 p = mb_prevptr(ccline.cmdbuff, p);
1533 if (c == Ctrl_W)
1534 {
1535 while (p > ccline.cmdbuff && vim_isspace(*p))
1536 p = mb_prevptr(ccline.cmdbuff, p);
1537 i = mb_get_class(p);
1538 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1539 p = mb_prevptr(ccline.cmdbuff, p);
1540 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001541 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 }
1543 }
1544 else
1545#endif
1546 if (c == Ctrl_W)
1547 {
1548 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1549 --p;
1550 i = vim_iswordc(p[-1]);
1551 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1552 && vim_iswordc(p[-1]) == i)
1553 --p;
1554 }
1555 else
1556 --p;
1557 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1558 ccline.cmdlen -= j - ccline.cmdpos;
1559 i = ccline.cmdpos;
1560 while (i < ccline.cmdlen)
1561 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1562
1563 /* Truncate at the end, required for multi-byte chars. */
1564 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001565#ifdef FEAT_SEARCH_EXTRA
1566 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001567 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001568 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001569 /* save view settings, so that the screen
1570 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001571 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001572 }
1573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 redrawcmd();
1575 }
1576 else if (ccline.cmdlen == 0 && c != Ctrl_W
1577 && ccline.cmdprompt == NULL && indent == 0)
1578 {
1579 /* In ex and debug mode it doesn't make sense to return. */
1580 if (exmode_active
1581#ifdef FEAT_EVAL
1582 || ccline.cmdfirstc == '>'
1583#endif
1584 )
1585 goto cmdline_not_changed;
1586
Bram Moolenaard23a8232018-02-10 18:45:26 +01001587 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (!cmd_silent)
1589 {
1590#ifdef FEAT_RIGHTLEFT
1591 if (cmdmsg_rl)
1592 msg_col = Columns;
1593 else
1594#endif
1595 msg_col = 0;
1596 msg_putchar(' '); /* delete ':' */
1597 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001598#ifdef FEAT_SEARCH_EXTRA
1599 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001600 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 redraw_cmdline = TRUE;
1603 goto returncmd; /* back to cmd mode */
1604 }
1605 goto cmdline_changed;
1606
1607 case K_INS:
1608 case K_KINS:
1609#ifdef FEAT_FKMAP
1610 /* if Farsi mode set, we are in reverse insert mode -
1611 Do not change the mode */
1612 if (cmd_fkmap)
1613 beep_flush();
1614 else
1615#endif
1616 ccline.overstrike = !ccline.overstrike;
1617#ifdef CURSOR_SHAPE
1618 ui_cursor_shape(); /* may show different cursor shape */
1619#endif
1620 goto cmdline_not_changed;
1621
1622 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001623 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 /* ":lmap" mappings exists, toggle use of mappings. */
1626 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001627#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 im_set_active(FALSE); /* Disable input method */
1629#endif
1630 if (b_im_ptr != NULL)
1631 {
1632 if (State & LANGMAP)
1633 *b_im_ptr = B_IMODE_LMAP;
1634 else
1635 *b_im_ptr = B_IMODE_NONE;
1636 }
1637 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001638#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 else
1640 {
1641 /* There are no ":lmap" mappings, toggle IM. When
1642 * 'imdisable' is set don't try getting the status, it's
1643 * always off. */
1644 if ((p_imdisable && b_im_ptr != NULL)
1645 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1646 {
1647 im_set_active(FALSE); /* Disable input method */
1648 if (b_im_ptr != NULL)
1649 *b_im_ptr = B_IMODE_NONE;
1650 }
1651 else
1652 {
1653 im_set_active(TRUE); /* Enable input method */
1654 if (b_im_ptr != NULL)
1655 *b_im_ptr = B_IMODE_IM;
1656 }
1657 }
1658#endif
1659 if (b_im_ptr != NULL)
1660 {
1661 if (b_im_ptr == &curbuf->b_p_iminsert)
1662 set_iminsert_global();
1663 else
1664 set_imsearch_global();
1665 }
1666#ifdef CURSOR_SHAPE
1667 ui_cursor_shape(); /* may show different cursor shape */
1668#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001669#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 /* Show/unshow value of 'keymap' in status lines later. */
1671 status_redraw_curbuf();
1672#endif
1673 goto cmdline_not_changed;
1674
1675/* case '@': only in very old vi */
1676 case Ctrl_U:
1677 /* delete all characters left of the cursor */
1678 j = ccline.cmdpos;
1679 ccline.cmdlen -= j;
1680 i = ccline.cmdpos = 0;
1681 while (i < ccline.cmdlen)
1682 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1683 /* Truncate at the end, required for multi-byte chars. */
1684 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001685#ifdef FEAT_SEARCH_EXTRA
1686 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001687 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 redrawcmd();
1690 goto cmdline_changed;
1691
1692#ifdef FEAT_CLIPBOARD
1693 case Ctrl_Y:
1694 /* Copy the modeless selection, if there is one. */
1695 if (clip_star.state != SELECT_CLEARED)
1696 {
1697 if (clip_star.state == SELECT_DONE)
1698 clip_copy_modeless_selection(TRUE);
1699 goto cmdline_not_changed;
1700 }
1701 break;
1702#endif
1703
1704 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1705 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001706 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001707 * ":normal" runs out of characters. */
1708 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001709 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 goto cmdline_not_changed;
1711
1712 gotesc = TRUE; /* will free ccline.cmdbuff after
1713 putting it in history */
1714 goto returncmd; /* back to cmd mode */
1715
1716 case Ctrl_R: /* insert register */
1717#ifdef USE_ON_FLY_SCROLL
1718 dont_scroll = TRUE; /* disallow scrolling here */
1719#endif
1720 putcmdline('"', TRUE);
1721 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001722 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (i == Ctrl_O)
1724 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1725 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001726 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001727 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 --no_mapping;
1729#ifdef FEAT_EVAL
1730 /*
1731 * Insert the result of an expression.
1732 * Need to save the current command line, to be able to enter
1733 * a new one...
1734 */
1735 new_cmdpos = -1;
1736 if (c == '=')
1737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1739 {
1740 beep_flush();
1741 c = ESC;
1742 }
1743 else
1744 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001745 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001747 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749 }
1750#endif
1751 if (c != ESC) /* use ESC to cancel inserting register */
1752 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001753 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001754
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001755#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001756 /* When there was a serious error abort getting the
1757 * command line. */
1758 if (aborting())
1759 {
1760 gotesc = TRUE; /* will free ccline.cmdbuff after
1761 putting it in history */
1762 goto returncmd; /* back to cmd mode */
1763 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 KeyTyped = FALSE; /* Don't do p_wc completion. */
1766#ifdef FEAT_EVAL
1767 if (new_cmdpos >= 0)
1768 {
1769 /* set_cmdline_pos() was used */
1770 if (new_cmdpos > ccline.cmdlen)
1771 ccline.cmdpos = ccline.cmdlen;
1772 else
1773 ccline.cmdpos = new_cmdpos;
1774 }
1775#endif
1776 }
1777 redrawcmd();
1778 goto cmdline_changed;
1779
1780 case Ctrl_D:
1781 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1782 break; /* Use ^D as normal char instead */
1783
1784 redrawcmd();
1785 continue; /* don't do incremental search now */
1786
1787 case K_RIGHT:
1788 case K_S_RIGHT:
1789 case K_C_RIGHT:
1790 do
1791 {
1792 if (ccline.cmdpos >= ccline.cmdlen)
1793 break;
1794 i = cmdline_charsize(ccline.cmdpos);
1795 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1796 break;
1797 ccline.cmdspos += i;
1798#ifdef FEAT_MBYTE
1799 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001800 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 + ccline.cmdpos);
1802 else
1803#endif
1804 ++ccline.cmdpos;
1805 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001806 while ((c == K_S_RIGHT || c == K_C_RIGHT
1807 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1809#ifdef FEAT_MBYTE
1810 if (has_mbyte)
1811 set_cmdspos_cursor();
1812#endif
1813 goto cmdline_not_changed;
1814
1815 case K_LEFT:
1816 case K_S_LEFT:
1817 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001818 if (ccline.cmdpos == 0)
1819 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 do
1821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 --ccline.cmdpos;
1823#ifdef FEAT_MBYTE
1824 if (has_mbyte) /* move to first byte of char */
1825 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1826 ccline.cmdbuff + ccline.cmdpos);
1827#endif
1828 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1829 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001830 while (ccline.cmdpos > 0
1831 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001832 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1834#ifdef FEAT_MBYTE
1835 if (has_mbyte)
1836 set_cmdspos_cursor();
1837#endif
1838 goto cmdline_not_changed;
1839
1840 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001841 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001842 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar4770d092006-01-12 23:22:24 +00001844#ifdef FEAT_GUI_W32
1845 /* On Win32 ignore <M-F4>, we get it when closing the window was
1846 * cancelled. */
1847 case K_F4:
1848 if (mod_mask == MOD_MASK_ALT)
1849 {
1850 redrawcmd(); /* somehow the cmdline is cleared */
1851 goto cmdline_not_changed;
1852 }
1853 break;
1854#endif
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856#ifdef FEAT_MOUSE
1857 case K_MIDDLEDRAG:
1858 case K_MIDDLERELEASE:
1859 goto cmdline_not_changed; /* Ignore mouse */
1860
1861 case K_MIDDLEMOUSE:
1862# ifdef FEAT_GUI
1863 /* When GUI is active, also paste when 'mouse' is empty */
1864 if (!gui.in_use)
1865# endif
1866 if (!mouse_has(MOUSE_COMMAND))
1867 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001868# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001870 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001872# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001873 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 redrawcmd();
1875 goto cmdline_changed;
1876
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001877# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001879 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 redrawcmd();
1881 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 case K_LEFTDRAG:
1885 case K_LEFTRELEASE:
1886 case K_RIGHTDRAG:
1887 case K_RIGHTRELEASE:
1888 /* Ignore drag and release events when the button-down wasn't
1889 * seen before. */
1890 if (ignore_drag_release)
1891 goto cmdline_not_changed;
1892 /* FALLTHROUGH */
1893 case K_LEFTMOUSE:
1894 case K_RIGHTMOUSE:
1895 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1896 ignore_drag_release = TRUE;
1897 else
1898 ignore_drag_release = FALSE;
1899# ifdef FEAT_GUI
1900 /* When GUI is active, also move when 'mouse' is empty */
1901 if (!gui.in_use)
1902# endif
1903 if (!mouse_has(MOUSE_COMMAND))
1904 goto cmdline_not_changed; /* Ignore mouse */
1905# ifdef FEAT_CLIPBOARD
1906 if (mouse_row < cmdline_row && clip_star.available)
1907 {
1908 int button, is_click, is_drag;
1909
1910 /*
1911 * Handle modeless selection.
1912 */
1913 button = get_mouse_button(KEY2TERMCAP1(c),
1914 &is_click, &is_drag);
1915 if (mouse_model_popup() && button == MOUSE_LEFT
1916 && (mod_mask & MOD_MASK_SHIFT))
1917 {
1918 /* Translate shift-left to right button. */
1919 button = MOUSE_RIGHT;
1920 mod_mask &= ~MOD_MASK_SHIFT;
1921 }
1922 clip_modeless(button, is_click, is_drag);
1923 goto cmdline_not_changed;
1924 }
1925# endif
1926
1927 set_cmdspos();
1928 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1929 ++ccline.cmdpos)
1930 {
1931 i = cmdline_charsize(ccline.cmdpos);
1932 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1933 && mouse_col < ccline.cmdspos % Columns + i)
1934 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001935# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (has_mbyte)
1937 {
1938 /* Count ">" for double-wide char that doesn't fit. */
1939 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001940 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 + ccline.cmdpos) - 1;
1942 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 ccline.cmdspos += i;
1945 }
1946 goto cmdline_not_changed;
1947
1948 /* Mouse scroll wheel: ignored here */
1949 case K_MOUSEDOWN:
1950 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001951 case K_MOUSELEFT:
1952 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 /* Alternate buttons ignored here */
1954 case K_X1MOUSE:
1955 case K_X1DRAG:
1956 case K_X1RELEASE:
1957 case K_X2MOUSE:
1958 case K_X2DRAG:
1959 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001960 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 goto cmdline_not_changed;
1962
1963#endif /* FEAT_MOUSE */
1964
1965#ifdef FEAT_GUI
1966 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1967 case K_LEFTRELEASE_NM:
1968 goto cmdline_not_changed;
1969
1970 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001971 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 gui_do_scroll();
1974 redrawcmd();
1975 }
1976 goto cmdline_not_changed;
1977
1978 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001979 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001981 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 redrawcmd();
1983 }
1984 goto cmdline_not_changed;
1985#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001986#ifdef FEAT_GUI_TABLINE
1987 case K_TABLINE:
1988 case K_TABMENU:
1989 /* Don't want to change any tabs here. Make sure the same tab
1990 * is still selected. */
1991 if (gui_use_tabline())
1992 gui_mch_set_curtab(tabpage_index(curtab));
1993 goto cmdline_not_changed;
1994#endif
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 case K_SELECT: /* end of Select mode mapping - ignore */
1997 goto cmdline_not_changed;
1998
1999 case Ctrl_B: /* begin of command line */
2000 case K_HOME:
2001 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 case K_S_HOME:
2003 case K_C_HOME:
2004 ccline.cmdpos = 0;
2005 set_cmdspos();
2006 goto cmdline_not_changed;
2007
2008 case Ctrl_E: /* end of command line */
2009 case K_END:
2010 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 case K_S_END:
2012 case K_C_END:
2013 ccline.cmdpos = ccline.cmdlen;
2014 set_cmdspos_cursor();
2015 goto cmdline_not_changed;
2016
2017 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002018 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 break;
2020 goto cmdline_changed;
2021
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002022 case Ctrl_L:
2023#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002024 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002025 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002026#endif
2027
2028 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002029 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 break;
2031 goto cmdline_changed;
2032
2033 case Ctrl_N: /* next match */
2034 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002035 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002037 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2038 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002040 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002043 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 case K_UP:
2045 case K_DOWN:
2046 case K_S_UP:
2047 case K_S_DOWN:
2048 case K_PAGEUP:
2049 case K_KPAGEUP:
2050 case K_PAGEDOWN:
2051 case K_KPAGEDOWN:
2052 if (hislen == 0 || firstc == NUL) /* no history */
2053 goto cmdline_not_changed;
2054
2055 i = hiscnt;
2056
2057 /* save current command string so it can be restored later */
2058 if (lookfor == NULL)
2059 {
2060 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2061 goto cmdline_not_changed;
2062 lookfor[ccline.cmdpos] = NUL;
2063 }
2064
2065 j = (int)STRLEN(lookfor);
2066 for (;;)
2067 {
2068 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002069 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002070 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 if (hiscnt == hislen) /* first time */
2073 hiscnt = hisidx[histype];
2074 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2075 hiscnt = hislen - 1;
2076 else if (hiscnt != hisidx[histype] + 1)
2077 --hiscnt;
2078 else /* at top of list */
2079 {
2080 hiscnt = i;
2081 break;
2082 }
2083 }
2084 else /* one step forwards */
2085 {
2086 /* on last entry, clear the line */
2087 if (hiscnt == hisidx[histype])
2088 {
2089 hiscnt = hislen;
2090 break;
2091 }
2092
2093 /* not on a history line, nothing to do */
2094 if (hiscnt == hislen)
2095 break;
2096 if (hiscnt == hislen - 1) /* wrap around */
2097 hiscnt = 0;
2098 else
2099 ++hiscnt;
2100 }
2101 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2102 {
2103 hiscnt = i;
2104 break;
2105 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002106 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002107 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 || STRNCMP(history[histype][hiscnt].hisstr,
2109 lookfor, (size_t)j) == 0)
2110 break;
2111 }
2112
2113 if (hiscnt != i) /* jumped to other entry */
2114 {
2115 char_u *p;
2116 int len;
2117 int old_firstc;
2118
2119 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002120 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (hiscnt == hislen)
2122 p = lookfor; /* back to the old one */
2123 else
2124 p = history[histype][hiscnt].hisstr;
2125
2126 if (histype == HIST_SEARCH
2127 && p != lookfor
2128 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2129 {
2130 /* Correct for the separator character used when
2131 * adding the history entry vs the one used now.
2132 * First loop: count length.
2133 * Second loop: copy the characters. */
2134 for (i = 0; i <= 1; ++i)
2135 {
2136 len = 0;
2137 for (j = 0; p[j] != NUL; ++j)
2138 {
2139 /* Replace old sep with new sep, unless it is
2140 * escaped. */
2141 if (p[j] == old_firstc
2142 && (j == 0 || p[j - 1] != '\\'))
2143 {
2144 if (i > 0)
2145 ccline.cmdbuff[len] = firstc;
2146 }
2147 else
2148 {
2149 /* Escape new sep, unless it is already
2150 * escaped. */
2151 if (p[j] == firstc
2152 && (j == 0 || p[j - 1] != '\\'))
2153 {
2154 if (i > 0)
2155 ccline.cmdbuff[len] = '\\';
2156 ++len;
2157 }
2158 if (i > 0)
2159 ccline.cmdbuff[len] = p[j];
2160 }
2161 ++len;
2162 }
2163 if (i == 0)
2164 {
2165 alloc_cmdbuff(len);
2166 if (ccline.cmdbuff == NULL)
2167 goto returncmd;
2168 }
2169 }
2170 ccline.cmdbuff[len] = NUL;
2171 }
2172 else
2173 {
2174 alloc_cmdbuff((int)STRLEN(p));
2175 if (ccline.cmdbuff == NULL)
2176 goto returncmd;
2177 STRCPY(ccline.cmdbuff, p);
2178 }
2179
2180 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2181 redrawcmd();
2182 goto cmdline_changed;
2183 }
2184 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002185#endif
2186 goto cmdline_not_changed;
2187
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002188#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002189 case Ctrl_G: /* next match */
2190 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002191 if (may_adjust_incsearch_highlighting(
2192 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002193 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196
2197 case Ctrl_V:
2198 case Ctrl_Q:
2199#ifdef FEAT_MOUSE
2200 ignore_drag_release = TRUE;
2201#endif
2202 putcmdline('^', TRUE);
2203 c = get_literal(); /* get next (two) character(s) */
2204 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002205 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#ifdef FEAT_MBYTE
2207 /* may need to remove ^ when composing char was typed */
2208 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2209 {
2210 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2211 msg_putchar(' ');
2212 cursorcmd();
2213 }
2214#endif
2215 break;
2216
2217#ifdef FEAT_DIGRAPHS
2218 case Ctrl_K:
2219#ifdef FEAT_MOUSE
2220 ignore_drag_release = TRUE;
2221#endif
2222 putcmdline('?', TRUE);
2223#ifdef USE_ON_FLY_SCROLL
2224 dont_scroll = TRUE; /* disallow scrolling here */
2225#endif
2226 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002227 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (c != NUL)
2229 break;
2230
2231 redrawcmd();
2232 goto cmdline_not_changed;
2233#endif /* FEAT_DIGRAPHS */
2234
2235#ifdef FEAT_RIGHTLEFT
2236 case Ctrl__: /* CTRL-_: switch language mode */
2237 if (!p_ari)
2238 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002239# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (p_altkeymap)
2241 {
2242 cmd_fkmap = !cmd_fkmap;
2243 if (cmd_fkmap) /* in Farsi always in Insert mode */
2244 ccline.overstrike = FALSE;
2245 }
2246 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 cmd_hkmap = !cmd_hkmap;
2249 goto cmdline_not_changed;
2250#endif
2251
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002252 case K_PS:
2253 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2254 goto cmdline_changed;
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 default:
2257#ifdef UNIX
2258 if (c == intr_char)
2259 {
2260 gotesc = TRUE; /* will free ccline.cmdbuff after
2261 putting it in history */
2262 goto returncmd; /* back to Normal mode */
2263 }
2264#endif
2265 /*
2266 * Normal character with no special meaning. Just set mod_mask
2267 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2268 * the string <S-Space>. This should only happen after ^V.
2269 */
2270 if (!IS_SPECIAL(c))
2271 mod_mask = 0x0;
2272 break;
2273 }
2274 /*
2275 * End of switch on command line character.
2276 * We come here if we have a normal character.
2277 */
2278
Bram Moolenaarede3e632013-06-23 16:16:19 +02002279 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280#ifdef FEAT_MBYTE
2281 /* Add ABBR_OFF for characters above 0x100, this is
2282 * what check_abbr() expects. */
2283 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2284#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002285 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 goto cmdline_changed;
2287
2288 /*
2289 * put the character in the command line
2290 */
2291 if (IS_SPECIAL(c) || mod_mask != 0)
2292 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2293 else
2294 {
2295#ifdef FEAT_MBYTE
2296 if (has_mbyte)
2297 {
2298 j = (*mb_char2bytes)(c, IObuff);
2299 IObuff[j] = NUL; /* exclude composing chars */
2300 put_on_cmdline(IObuff, j, TRUE);
2301 }
2302 else
2303#endif
2304 {
2305 IObuff[0] = c;
2306 put_on_cmdline(IObuff, 1, TRUE);
2307 }
2308 }
2309 goto cmdline_changed;
2310
2311/*
2312 * This part implements incremental searches for "/" and "?"
2313 * Jump to cmdline_not_changed when a character has been read but the command
2314 * line did not change. Then we only search and redraw if something changed in
2315 * the past.
2316 * Jump to cmdline_changed when the command line did change.
2317 * (Sorry for the goto's, I know it is ugly).
2318 */
2319cmdline_not_changed:
2320#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002321 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 continue;
2323#endif
2324
2325cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002326 /* Trigger CmdlineChanged autocommands. */
2327 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002330 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332
2333#ifdef FEAT_RIGHTLEFT
2334 if (cmdmsg_rl
2335# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002336 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337# endif
2338 )
2339 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002340 * right-left typing. Not efficient, but it works.
2341 * Do it only when there are no characters left to read
2342 * to avoid useless intermediate redraws. */
2343 if (vpeekc() == NUL)
2344 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345#endif
2346 }
2347
2348returncmd:
2349
2350#ifdef FEAT_RIGHTLEFT
2351 cmdmsg_rl = FALSE;
2352#endif
2353
2354#ifdef FEAT_FKMAP
2355 cmd_fkmap = 0;
2356#endif
2357
2358 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002359 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002362 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#endif
2364
2365 if (ccline.cmdbuff != NULL)
2366 {
2367 /*
2368 * Put line in history buffer (":" and "=" only when it was typed).
2369 */
2370#ifdef FEAT_CMDHIST
2371 if (ccline.cmdlen && firstc != NUL
2372 && (some_key_typed || histype == HIST_SEARCH))
2373 {
2374 add_to_history(histype, ccline.cmdbuff, TRUE,
2375 histype == HIST_SEARCH ? firstc : NUL);
2376 if (firstc == ':')
2377 {
2378 vim_free(new_last_cmdline);
2379 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2380 }
2381 }
2382#endif
2383
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002384 if (gotesc)
2385 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387
2388 /*
2389 * If the screen was shifted up, redraw the whole screen (later).
2390 * If the line is too long, clear it, so ruler and shown command do
2391 * not get printed in the middle of it.
2392 */
2393 msg_check();
2394 msg_scroll = save_msg_scroll;
2395 redir_off = FALSE;
2396
2397 /* When the command line was typed, no need for a wait-return prompt. */
2398 if (some_key_typed)
2399 need_wait_return = FALSE;
2400
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002401 /* Trigger CmdlineLeave autocommands. */
2402 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002405#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2407 im_save_status(b_im_ptr);
2408 im_set_active(FALSE);
2409#endif
2410#ifdef FEAT_MOUSE
2411 setmouse();
2412#endif
2413#ifdef CURSOR_SHAPE
2414 ui_cursor_shape(); /* may show different cursor shape */
2415#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002416 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002418 {
2419 char_u *p = ccline.cmdbuff;
2420
2421 /* Make ccline empty, getcmdline() may try to use it. */
2422 ccline.cmdbuff = NULL;
2423 return p;
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425}
2426
2427#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2428/*
2429 * Get a command line with a prompt.
2430 * This is prepared to be called recursively from getcmdline() (e.g. by
2431 * f_input() when evaluating an expression from CTRL-R =).
2432 * Returns the command line in allocated memory, or NULL.
2433 */
2434 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002435getcmdline_prompt(
2436 int firstc,
2437 char_u *prompt, /* command line prompt */
2438 int attr, /* attributes for prompt */
2439 int xp_context, /* type of expansion */
2440 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 char_u *s;
2443 struct cmdline_info save_ccline;
2444 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002445 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002447 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 ccline.cmdprompt = prompt;
2449 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002450# ifdef FEAT_EVAL
2451 ccline.xp_context = xp_context;
2452 ccline.xp_arg = xp_arg;
2453 ccline.input_fn = (firstc == '@');
2454# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002455 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002457 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002458 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002459 /* Restore msg_col, the prompt from input() may have changed it.
2460 * But only if called recursively and the commandline is therefore being
2461 * restored to an old one; if not, the input() prompt stays on the screen,
2462 * so we need its modified msg_col left intact. */
2463 if (ccline.cmdbuff != NULL)
2464 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 return s;
2467}
2468#endif
2469
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002470/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002471 * Return TRUE when the text must not be changed and we can't switch to
2472 * another window or buffer. Used when editing the command line, evaluating
2473 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002474 */
2475 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002476text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002477{
2478#ifdef FEAT_CMDWIN
2479 if (cmdwin_type != 0)
2480 return TRUE;
2481#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002482 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002483}
2484
2485/*
2486 * Give an error message for a command that isn't allowed while the cmdline
2487 * window is open or editing the cmdline in another way.
2488 */
2489 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002490text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002491{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002492 EMSG(_(get_text_locked_msg()));
2493}
2494
2495 char_u *
2496get_text_locked_msg(void)
2497{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002498#ifdef FEAT_CMDWIN
2499 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002500 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002501#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002502 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002503}
2504
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002505/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002506 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2507 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002508 */
2509 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002510curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002511{
2512 if (curbuf_lock > 0)
2513 {
2514 EMSG(_("E788: Not allowed to edit another buffer now"));
2515 return TRUE;
2516 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002517 return allbuf_locked();
2518}
2519
2520/*
2521 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2522 * message.
2523 */
2524 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002525allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002526{
2527 if (allbuf_lock > 0)
2528 {
2529 EMSG(_("E811: Not allowed to change buffer information now"));
2530 return TRUE;
2531 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002532 return FALSE;
2533}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002536cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2539 if (cmdline_star > 0) /* showing '*', always 1 position */
2540 return 1;
2541#endif
2542 return ptr2cells(ccline.cmdbuff + idx);
2543}
2544
2545/*
2546 * Compute the offset of the cursor on the command line for the prompt and
2547 * indent.
2548 */
2549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002550set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002552 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 ccline.cmdspos = 1 + ccline.cmdindent;
2554 else
2555 ccline.cmdspos = 0 + ccline.cmdindent;
2556}
2557
2558/*
2559 * Compute the screen position for the cursor on the command line.
2560 */
2561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002562set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 int i, m, c;
2565
2566 set_cmdspos();
2567 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002570 if (m < 0) /* overflow, Columns or Rows at weird value */
2571 m = MAXCOL;
2572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 else
2574 m = MAXCOL;
2575 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2576 {
2577 c = cmdline_charsize(i);
2578#ifdef FEAT_MBYTE
2579 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2580 if (has_mbyte)
2581 correct_cmdspos(i, c);
2582#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002583 /* If the cmdline doesn't fit, show cursor on last visible char.
2584 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if ((ccline.cmdspos += c) >= m)
2586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 ccline.cmdspos -= c;
2588 break;
2589 }
2590#ifdef FEAT_MBYTE
2591 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002592 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#endif
2594 }
2595}
2596
2597#ifdef FEAT_MBYTE
2598/*
2599 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2600 * character that doesn't fit, so that a ">" must be displayed.
2601 */
2602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002603correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002605 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2607 && ccline.cmdspos % Columns + cells > Columns)
2608 ccline.cmdspos++;
2609}
2610#endif
2611
2612/*
2613 * Get an Ex command line for the ":" command.
2614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002616getexline(
2617 int c, /* normally ':', NUL for ":append" */
2618 void *cookie UNUSED,
2619 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
2621 /* When executing a register, remove ':' that's in front of each line. */
2622 if (exec_from_reg && vpeekc() == ':')
2623 (void)vgetc();
2624 return getcmdline(c, 1L, indent);
2625}
2626
2627/*
2628 * Get an Ex command line for Ex mode.
2629 * In Ex mode we only use the OS supplied line editing features and no
2630 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002634getexmodeline(
2635 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002636 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002637 void *cookie UNUSED,
2638 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640 garray_T line_ga;
2641 char_u *pend;
2642 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002643 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 int escaped = FALSE; /* CTRL-V typed */
2645 int vcol = 0;
2646 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002647 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002648 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 /* Switch cursor on now. This avoids that it happens after the "\n", which
2651 * confuses the system function that computes tabstops. */
2652 cursor_on();
2653
2654 /* always start in column 0; write a newline if necessary */
2655 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002656 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002658 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002660 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002661 if (p_prompt)
2662 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 while (indent-- > 0)
2664 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667
2668 ga_init2(&line_ga, 1, 30);
2669
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002670 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002671 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002672 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002673 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002674 while (indent >= 8)
2675 {
2676 ga_append(&line_ga, TAB);
2677 msg_puts((char_u *)" ");
2678 indent -= 8;
2679 }
2680 while (indent-- > 0)
2681 {
2682 ga_append(&line_ga, ' ');
2683 msg_putchar(' ');
2684 }
2685 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 ++no_mapping;
2687 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 /*
2690 * Get the line, one character at a time.
2691 */
2692 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002693 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002695 long sw;
2696 char_u *s;
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (ga_grow(&line_ga, 40) == FAIL)
2699 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaarba748c82017-02-26 14:00:07 +01002701 /*
2702 * Get one character at a time.
2703 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002704 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002705
2706 /* Check for a ":normal" command and no more characters left. */
2707 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2708 c1 = '\n';
2709 else
2710 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713 * Handle line editing.
2714 * Previously this was left to the system, putting the terminal in
2715 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002719 msg_putchar('\n');
2720 break;
2721 }
2722
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002723 if (c1 == K_PS)
2724 {
2725 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2726 goto redraw;
2727 }
2728
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 if (!escaped)
2730 {
2731 /* CR typed means "enter", which is NL */
2732 if (c1 == '\r')
2733 c1 = '\n';
2734
2735 if (c1 == BS || c1 == K_BS
2736 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002738 if (line_ga.ga_len > 0)
2739 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002740#ifdef FEAT_MBYTE
2741 if (has_mbyte)
2742 {
2743 p = (char_u *)line_ga.ga_data;
2744 p[line_ga.ga_len] = NUL;
2745 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2746 line_ga.ga_len -= len;
2747 }
2748 else
2749#endif
2750 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002751 goto redraw;
2752 }
2753 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 msg_col = startcol;
2759 msg_clr_eos();
2760 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002761 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002762 }
2763
2764 if (c1 == Ctrl_T)
2765 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002766 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 p = (char_u *)line_ga.ga_data;
2768 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002769 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002770 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002771add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002772 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002774 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002775 p = (char_u *)line_ga.ga_data;
2776 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002777 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2778 *s = ' ';
2779 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781redraw:
2782 /* redraw the line */
2783 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002784 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002785 p = (char_u *)line_ga.ga_data;
2786 p[line_ga.ga_len] = NUL;
2787 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002789 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 msg_putchar(' ');
2794 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002795 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002799 len = MB_PTR2LEN(p);
2800 msg_outtrans_len(p, len);
2801 vcol += ptr2cells(p);
2802 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002806 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002807 continue;
2808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810 if (c1 == Ctrl_D)
2811 {
2812 /* Delete one shiftwidth. */
2813 p = (char_u *)line_ga.ga_data;
2814 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 if (prev_char == '^')
2817 ex_keep_indent = TRUE;
2818 indent = 0;
2819 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
2821 else
2822 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002824 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002825 if (indent > 0)
2826 {
2827 --indent;
2828 indent -= indent % get_sw_value(curbuf);
2829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002831 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002833 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002834 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2835 --line_ga.ga_len;
2836 }
2837 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839
2840 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2841 {
2842 escaped = TRUE;
2843 continue;
2844 }
2845
2846 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2847 if (IS_SPECIAL(c1))
2848 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850
2851 if (IS_SPECIAL(c1))
2852 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002853#ifdef FEAT_MBYTE
2854 if (has_mbyte)
2855 len = (*mb_char2bytes)(c1,
2856 (char_u *)line_ga.ga_data + line_ga.ga_len);
2857 else
2858#endif
2859 {
2860 len = 1;
2861 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2862 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 if (c1 == '\n')
2864 msg_putchar('\n');
2865 else if (c1 == TAB)
2866 {
2867 /* Don't use chartabsize(), 'ts' can be different */
2868 do
2869 {
2870 msg_putchar(' ');
2871 } while (++vcol % 8);
2872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002876 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002879 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880 escaped = FALSE;
2881
2882 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002883 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002884
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002885 /* We are done when a NL is entered, but not when it comes after an
2886 * odd number of backslashes, that results in a NUL. */
2887 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002889 int bcount = 0;
2890
2891 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2892 ++bcount;
2893
2894 if (bcount > 0)
2895 {
2896 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2897 * "\NL", etc. */
2898 line_ga.ga_len -= (bcount + 1) / 2;
2899 pend -= (bcount + 1) / 2;
2900 pend[-1] = '\n';
2901 }
2902
2903 if ((bcount & 1) == 0)
2904 {
2905 --line_ga.ga_len;
2906 --pend;
2907 *pend = NUL;
2908 break;
2909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
2911 }
2912
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002913 --no_mapping;
2914 --allow_keys;
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 /* make following messages go to the next line */
2917 msg_didout = FALSE;
2918 msg_col = 0;
2919 if (msg_row < Rows - 1)
2920 ++msg_row;
2921 emsg_on_display = FALSE; /* don't want ui_delay() */
2922
2923 if (got_int)
2924 ga_clear(&line_ga);
2925
2926 return (char_u *)line_ga.ga_data;
2927}
2928
Bram Moolenaare344bea2005-09-01 20:46:49 +00002929# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2930 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931/*
2932 * Return TRUE if ccline.overstrike is on.
2933 */
2934 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002935cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 return ccline.overstrike;
2938}
2939
2940/*
2941 * Return TRUE if the cursor is at the end of the cmdline.
2942 */
2943 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002944cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 return (ccline.cmdpos >= ccline.cmdlen);
2947}
2948#endif
2949
Bram Moolenaar9372a112005-12-06 19:59:18 +00002950#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951/*
2952 * Return the virtual column number at the current cursor position.
2953 * This is used by the IM code to obtain the start of the preedit string.
2954 */
2955 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002956cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
2958 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2959 return MAXCOL;
2960
2961# ifdef FEAT_MBYTE
2962 if (has_mbyte)
2963 {
2964 colnr_T col;
2965 int i = 0;
2966
2967 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002968 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970 return col;
2971 }
2972 else
2973# endif
2974 return ccline.cmdpos;
2975}
2976#endif
2977
2978#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2979/*
2980 * If part of the command line is an IM preedit string, redraw it with
2981 * IM feedback attributes. The cursor position is restored after drawing.
2982 */
2983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002984redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 if ((State & CMDLINE)
2987 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002988 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 && !p_imdisable
2990 && im_is_preediting())
2991 {
2992 int cmdpos = 0;
2993 int cmdspos;
2994 int old_row;
2995 int old_col;
2996 colnr_T col;
2997
2998 old_row = msg_row;
2999 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003000 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002# ifdef FEAT_MBYTE
3003 if (has_mbyte)
3004 {
3005 for (col = 0; col < preedit_start_col
3006 && cmdpos < ccline.cmdlen; ++col)
3007 {
3008 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003009 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011 }
3012 else
3013# endif
3014 {
3015 cmdspos += preedit_start_col;
3016 cmdpos += preedit_start_col;
3017 }
3018
3019 msg_row = cmdline_row + (cmdspos / (int)Columns);
3020 msg_col = cmdspos % (int)Columns;
3021 if (msg_row >= Rows)
3022 msg_row = Rows - 1;
3023
3024 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3025 {
3026 int char_len;
3027 int char_attr;
3028
3029 char_attr = im_get_feedback_attr(col);
3030 if (char_attr < 0)
3031 break; /* end of preedit string */
3032
3033# ifdef FEAT_MBYTE
3034 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003035 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 else
3037# endif
3038 char_len = 1;
3039
3040 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3041 cmdpos += char_len;
3042 }
3043
3044 msg_row = old_row;
3045 msg_col = old_col;
3046 }
3047}
3048#endif /* FEAT_XIM && FEAT_GUI_GTK */
3049
3050/*
3051 * Allocate a new command line buffer.
3052 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3053 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3054 */
3055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003056alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 /*
3059 * give some extra space to avoid having to allocate all the time
3060 */
3061 if (len < 80)
3062 len = 100;
3063 else
3064 len += 20;
3065
3066 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3067 ccline.cmdbufflen = len;
3068}
3069
3070/*
3071 * Re-allocate the command line to length len + something extra.
3072 * return FAIL for failure, OK otherwise
3073 */
3074 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003075realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 char_u *p;
3078
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003079 if (len < ccline.cmdbufflen)
3080 return OK; /* no need to resize */
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 p = ccline.cmdbuff;
3083 alloc_cmdbuff(len); /* will get some more */
3084 if (ccline.cmdbuff == NULL) /* out of memory */
3085 {
3086 ccline.cmdbuff = p; /* keep the old one */
3087 return FAIL;
3088 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003089 /* There isn't always a NUL after the command, but it may need to be
3090 * there, thus copy up to the NUL and add a NUL. */
3091 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3092 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003094
3095 if (ccline.xpc != NULL
3096 && ccline.xpc->xp_pattern != NULL
3097 && ccline.xpc->xp_context != EXPAND_NOTHING
3098 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3099 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003100 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003101
3102 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3103 * to point into the newly allocated memory. */
3104 if (i >= 0 && i <= ccline.cmdlen)
3105 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3106 }
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 return OK;
3109}
3110
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003111#if defined(FEAT_ARABIC) || defined(PROTO)
3112static char_u *arshape_buf = NULL;
3113
3114# if defined(EXITFREE) || defined(PROTO)
3115 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003116free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003117{
3118 vim_free(arshape_buf);
3119}
3120# endif
3121#endif
3122
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123/*
3124 * Draw part of the cmdline at the current cursor position. But draw stars
3125 * when cmdline_star is TRUE.
3126 */
3127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3131 int i;
3132
3133 if (cmdline_star > 0)
3134 for (i = 0; i < len; ++i)
3135 {
3136 msg_putchar('*');
3137# ifdef FEAT_MBYTE
3138 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003139 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140# endif
3141 }
3142 else
3143#endif
3144#ifdef FEAT_ARABIC
3145 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 static int buflen = 0;
3148 char_u *p;
3149 int j;
3150 int newlen = 0;
3151 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003152 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int prev_c = 0;
3154 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003155 int u8c;
3156 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 /*
3160 * Do arabic shaping into a temporary buffer. This is very
3161 * inefficient!
3162 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003163 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165 /* Re-allocate the buffer. We keep it around to avoid a lot of
3166 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003167 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003168 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003169 arshape_buf = alloc(buflen);
3170 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 return; /* out of memory */
3172 }
3173
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003174 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3175 {
3176 /* Prepend a space to draw the leading composing char on. */
3177 arshape_buf[0] = ' ';
3178 newlen = 1;
3179 }
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 for (j = start; j < start + len; j += mb_l)
3182 {
3183 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003184 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003185 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (ARABIC_CHAR(u8c))
3187 {
3188 /* Do Arabic shaping. */
3189 if (cmdmsg_rl)
3190 {
3191 /* displaying from right to left */
3192 pc = prev_c;
3193 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003194 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (j + mb_l >= start + len)
3196 nc = NUL;
3197 else
3198 nc = utf_ptr2char(p + mb_l);
3199 }
3200 else
3201 {
3202 /* displaying from left to right */
3203 if (j + mb_l >= start + len)
3204 pc = NUL;
3205 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003206 {
3207 int pcc[MAX_MCO];
3208
3209 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003211 pc1 = pcc[0];
3212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 nc = prev_c;
3214 }
3215 prev_c = u8c;
3216
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003217 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003219 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003220 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003222 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3223 if (u8cc[1] != 0)
3224 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003225 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227 }
3228 else
3229 {
3230 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003231 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 newlen += mb_l;
3233 }
3234 }
3235
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003236 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238 else
3239#endif
3240 msg_outtrans_len(ccline.cmdbuff + start, len);
3241}
3242
3243/*
3244 * Put a character on the command line. Shifts the following text to the
3245 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3246 * "c" must be printable (fit in one display cell)!
3247 */
3248 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003249putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 if (cmd_silent)
3252 return;
3253 msg_no_more = TRUE;
3254 msg_putchar(c);
3255 if (shift)
3256 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3257 msg_no_more = FALSE;
3258 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003259 extra_char = c;
3260 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
3263/*
3264 * Undo a putcmdline(c, FALSE).
3265 */
3266 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003267unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 if (cmd_silent)
3270 return;
3271 msg_no_more = TRUE;
3272 if (ccline.cmdlen == ccline.cmdpos)
3273 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003274#ifdef FEAT_MBYTE
3275 else if (has_mbyte)
3276 draw_cmdline(ccline.cmdpos,
3277 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 else
3280 draw_cmdline(ccline.cmdpos, 1);
3281 msg_no_more = FALSE;
3282 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003283 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285
3286/*
3287 * Put the given string, of the given length, onto the command line.
3288 * If len is -1, then STRLEN() is used to calculate the length.
3289 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3290 * part will be redrawn, otherwise it will not. If this function is called
3291 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3292 * called afterwards.
3293 */
3294 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003295put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 int retval;
3298 int i;
3299 int m;
3300 int c;
3301
3302 if (len < 0)
3303 len = (int)STRLEN(str);
3304
3305 /* Check if ccline.cmdbuff needs to be longer */
3306 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003307 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else
3309 retval = OK;
3310 if (retval == OK)
3311 {
3312 if (!ccline.overstrike)
3313 {
3314 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3315 ccline.cmdbuff + ccline.cmdpos,
3316 (size_t)(ccline.cmdlen - ccline.cmdpos));
3317 ccline.cmdlen += len;
3318 }
3319 else
3320 {
3321#ifdef FEAT_MBYTE
3322 if (has_mbyte)
3323 {
3324 /* Count nr of characters in the new string. */
3325 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003326 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 ++m;
3328 /* Count nr of bytes in cmdline that are overwritten by these
3329 * characters. */
3330 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003331 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 --m;
3333 if (i < ccline.cmdlen)
3334 {
3335 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3336 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3337 ccline.cmdlen += ccline.cmdpos + len - i;
3338 }
3339 else
3340 ccline.cmdlen = ccline.cmdpos + len;
3341 }
3342 else
3343#endif
3344 if (ccline.cmdpos + len > ccline.cmdlen)
3345 ccline.cmdlen = ccline.cmdpos + len;
3346 }
3347 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3348 ccline.cmdbuff[ccline.cmdlen] = NUL;
3349
3350#ifdef FEAT_MBYTE
3351 if (enc_utf8)
3352 {
3353 /* When the inserted text starts with a composing character,
3354 * backup to the character before it. There could be two of them.
3355 */
3356 i = 0;
3357 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3358 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3359 {
3360 i = (*mb_head_off)(ccline.cmdbuff,
3361 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3362 ccline.cmdpos -= i;
3363 len += i;
3364 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3365 }
3366# ifdef FEAT_ARABIC
3367 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3368 {
3369 /* Check the previous character for Arabic combining pair. */
3370 i = (*mb_head_off)(ccline.cmdbuff,
3371 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3372 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3373 + ccline.cmdpos - i), c))
3374 {
3375 ccline.cmdpos -= i;
3376 len += i;
3377 }
3378 else
3379 i = 0;
3380 }
3381# endif
3382 if (i != 0)
3383 {
3384 /* Also backup the cursor position. */
3385 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3386 ccline.cmdspos -= i;
3387 msg_col -= i;
3388 if (msg_col < 0)
3389 {
3390 msg_col += Columns;
3391 --msg_row;
3392 }
3393 }
3394 }
3395#endif
3396
3397 if (redraw && !cmd_silent)
3398 {
3399 msg_no_more = TRUE;
3400 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003401 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3403 /* Avoid clearing the rest of the line too often. */
3404 if (cmdline_row != i || ccline.overstrike)
3405 msg_clr_eos();
3406 msg_no_more = FALSE;
3407 }
3408#ifdef FEAT_FKMAP
3409 /*
3410 * If we are in Farsi command mode, the character input must be in
3411 * Insert mode. So do not advance the cmdpos.
3412 */
3413 if (!cmd_fkmap)
3414#endif
3415 {
3416 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003419 if (m < 0) /* overflow, Columns or Rows at weird value */
3420 m = MAXCOL;
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 else
3423 m = MAXCOL;
3424 for (i = 0; i < len; ++i)
3425 {
3426 c = cmdline_charsize(ccline.cmdpos);
3427#ifdef FEAT_MBYTE
3428 /* count ">" for a double-wide char that doesn't fit. */
3429 if (has_mbyte)
3430 correct_cmdspos(ccline.cmdpos, c);
3431#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003432 /* Stop cursor at the end of the screen, but do increment the
3433 * insert position, so that entering a very long command
3434 * works, even though you can't see it. */
3435 if (ccline.cmdspos + c < m)
3436 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437#ifdef FEAT_MBYTE
3438 if (has_mbyte)
3439 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003440 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 if (c > len - i - 1)
3442 c = len - i - 1;
3443 ccline.cmdpos += c;
3444 i += c;
3445 }
3446#endif
3447 ++ccline.cmdpos;
3448 }
3449 }
3450 }
3451 if (redraw)
3452 msg_check();
3453 return retval;
3454}
3455
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003456static struct cmdline_info prev_ccline;
3457static int prev_ccline_used = FALSE;
3458
3459/*
3460 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3461 * and overwrite it. But get_cmdline_str() may need it, thus make it
3462 * available globally in prev_ccline.
3463 */
3464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003465save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003466{
3467 if (!prev_ccline_used)
3468 {
3469 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3470 prev_ccline_used = TRUE;
3471 }
3472 *ccp = prev_ccline;
3473 prev_ccline = ccline;
3474 ccline.cmdbuff = NULL;
3475 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003476 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003477}
3478
3479/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003480 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003481 */
3482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003483restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003484{
3485 ccline = prev_ccline;
3486 prev_ccline = *ccp;
3487}
3488
Bram Moolenaar5a305422006-04-28 22:38:25 +00003489#if defined(FEAT_EVAL) || defined(PROTO)
3490/*
3491 * Save the command line into allocated memory. Returns a pointer to be
3492 * passed to restore_cmdline_alloc() later.
3493 * Returns NULL when failed.
3494 */
3495 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003496save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003497{
3498 struct cmdline_info *p;
3499
3500 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3501 if (p != NULL)
3502 save_cmdline(p);
3503 return (char_u *)p;
3504}
3505
3506/*
3507 * Restore the command line from the return value of save_cmdline_alloc().
3508 */
3509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003510restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003511{
3512 if (p != NULL)
3513 {
3514 restore_cmdline((struct cmdline_info *)p);
3515 vim_free(p);
3516 }
3517}
3518#endif
3519
Bram Moolenaar8299df92004-07-10 09:47:34 +00003520/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003521 * Paste a yank register into the command line.
3522 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003523 * insert_reg() can't be used here, because special characters from the
3524 * register contents will be interpreted as commands.
3525 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003526 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003527 */
3528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003529cmdline_paste(
3530 int regname,
3531 int literally, /* Insert text literally instead of "as typed" */
3532 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003533{
3534 long i;
3535 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003536 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003537 int allocated;
3538 struct cmdline_info save_ccline;
3539
3540 /* check for valid regname; also accept special characters for CTRL-R in
3541 * the command line */
3542 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003543 && regname != Ctrl_A && regname != Ctrl_L
3544 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003545 return FAIL;
3546
3547 /* A register containing CTRL-R can cause an endless loop. Allow using
3548 * CTRL-C to break the loop. */
3549 line_breakcheck();
3550 if (got_int)
3551 return FAIL;
3552
3553#ifdef FEAT_CLIPBOARD
3554 regname = may_get_selection(regname);
3555#endif
3556
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003557 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003558 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003559 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003560 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003561 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003562 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003563 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003564
3565 if (i)
3566 {
3567 /* Got the value of a special register in "arg". */
3568 if (arg == NULL)
3569 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003570
3571 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3572 * part of the word. */
3573 p = arg;
3574 if (p_is && regname == Ctrl_W)
3575 {
3576 char_u *w;
3577 int len;
3578
3579 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003580 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003581 {
3582#ifdef FEAT_MBYTE
3583 if (has_mbyte)
3584 {
3585 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3586 if (!vim_iswordc(mb_ptr2char(w - len)))
3587 break;
3588 w -= len;
3589 }
3590 else
3591#endif
3592 {
3593 if (!vim_iswordc(w[-1]))
3594 break;
3595 --w;
3596 }
3597 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003598 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003599 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3600 p += len;
3601 }
3602
3603 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003604 if (allocated)
3605 vim_free(arg);
3606 return OK;
3607 }
3608
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003609 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003610}
3611
3612/*
3613 * Put a string on the command line.
3614 * When "literally" is TRUE, insert literally.
3615 * When "literally" is FALSE, insert as typed, but don't leave the command
3616 * line.
3617 */
3618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003619cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003620{
3621 int c, cv;
3622
3623 if (literally)
3624 put_on_cmdline(s, -1, TRUE);
3625 else
3626 while (*s != NUL)
3627 {
3628 cv = *s;
3629 if (cv == Ctrl_V && s[1])
3630 ++s;
3631#ifdef FEAT_MBYTE
3632 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003633 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003634 else
3635#endif
3636 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003637 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3638 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003639#ifdef UNIX
3640 || c == intr_char
3641#endif
3642 || (c == Ctrl_BSL && *s == Ctrl_N))
3643 stuffcharReadbuff(Ctrl_V);
3644 stuffcharReadbuff(c);
3645 }
3646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#ifdef FEAT_WILDMENU
3649/*
3650 * Delete characters on the command line, from "from" to the current
3651 * position.
3652 */
3653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003654cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3657 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3658 ccline.cmdlen -= ccline.cmdpos - from;
3659 ccline.cmdpos = from;
3660}
3661#endif
3662
3663/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003664 * This function is called when the screen size changes and with incremental
3665 * search and in other situations where the command line may have been
3666 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003669redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003671 redrawcmdline_ex(TRUE);
3672}
3673
3674 void
3675redrawcmdline_ex(int do_compute_cmdrow)
3676{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (cmd_silent)
3678 return;
3679 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003680 if (do_compute_cmdrow)
3681 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 redrawcmd();
3683 cursorcmd();
3684}
3685
3686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003687redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 int i;
3690
3691 if (cmd_silent)
3692 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003693 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 msg_putchar(ccline.cmdfirstc);
3695 if (ccline.cmdprompt != NULL)
3696 {
3697 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3698 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3699 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003700 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 --ccline.cmdindent;
3702 }
3703 else
3704 for (i = ccline.cmdindent; i > 0; --i)
3705 msg_putchar(' ');
3706}
3707
3708/*
3709 * Redraw what is currently on the command line.
3710 */
3711 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003712redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 if (cmd_silent)
3715 return;
3716
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003717 /* when 'incsearch' is set there may be no command line while redrawing */
3718 if (ccline.cmdbuff == NULL)
3719 {
3720 windgoto(cmdline_row, 0);
3721 msg_clr_eos();
3722 return;
3723 }
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 msg_start();
3726 redrawcmdprompt();
3727
3728 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3729 msg_no_more = TRUE;
3730 draw_cmdline(0, ccline.cmdlen);
3731 msg_clr_eos();
3732 msg_no_more = FALSE;
3733
3734 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003735 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003736 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 /*
3739 * An emsg() before may have set msg_scroll. This is used in normal mode,
3740 * in cmdline mode we can reset them now.
3741 */
3742 msg_scroll = FALSE; /* next message overwrites cmdline */
3743
3744 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3745 * in cmdline mode */
3746 skip_redraw = FALSE;
3747}
3748
3749 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003750compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003752 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 cmdline_row = Rows - 1;
3754 else
3755 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003756 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758
3759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003760cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
3762 if (cmd_silent)
3763 return;
3764
3765#ifdef FEAT_RIGHTLEFT
3766 if (cmdmsg_rl)
3767 {
3768 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3769 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3770 if (msg_row <= 0)
3771 msg_row = Rows - 1;
3772 }
3773 else
3774#endif
3775 {
3776 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3777 msg_col = ccline.cmdspos % (int)Columns;
3778 if (msg_row >= Rows)
3779 msg_row = Rows - 1;
3780 }
3781
3782 windgoto(msg_row, msg_col);
3783#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003784 if (p_imst == IM_ON_THE_SPOT)
3785 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786#endif
3787#ifdef MCH_CURSOR_SHAPE
3788 mch_update_cursor();
3789#endif
3790}
3791
3792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003793gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
3795 msg_start();
3796#ifdef FEAT_RIGHTLEFT
3797 if (cmdmsg_rl)
3798 msg_col = Columns - 1;
3799 else
3800#endif
3801 msg_col = 0; /* always start in column 0 */
3802 if (clr) /* clear the bottom line(s) */
3803 msg_clr_eos(); /* will reset clear_cmdline */
3804 windgoto(cmdline_row, 0);
3805}
3806
3807/*
3808 * Check the word in front of the cursor for an abbreviation.
3809 * Called when the non-id character "c" has been entered.
3810 * When an abbreviation is recognized it is removed from the text with
3811 * backspaces and the replacement string is inserted, followed by "c".
3812 */
3813 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003814ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003816 int spos = 0;
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3819 return FALSE;
3820
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003821 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3822 * Actually accepts any mark. */
3823 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3824 spos++;
3825 if (ccline.cmdlen - spos > 5
3826 && ccline.cmdbuff[spos] == '\''
3827 && ccline.cmdbuff[spos + 2] == ','
3828 && ccline.cmdbuff[spos + 3] == '\'')
3829 spos += 5;
3830 else
3831 /* check abbreviation from the beginning of the commandline */
3832 spos = 0;
3833
3834 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835}
3836
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003837#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3838 static int
3839#ifdef __BORLANDC__
3840_RTLENTRYF
3841#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003842sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003843{
3844 char_u *p1 = *(char_u **)s1;
3845 char_u *p2 = *(char_u **)s2;
3846
3847 if (*p1 != '<' && *p2 == '<') return -1;
3848 if (*p1 == '<' && *p2 != '<') return 1;
3849 return STRCMP(p1, p2);
3850}
3851#endif
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853/*
3854 * Return FAIL if this is not an appropriate context in which to do
3855 * completion of anything, return OK if it is (even if there are no matches).
3856 * For the caller, this means that the character is just passed through like a
3857 * normal character (instead of being expanded). This allows :s/^I^D etc.
3858 */
3859 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003860nextwild(
3861 expand_T *xp,
3862 int type,
3863 int options, /* extra options for ExpandOne() */
3864 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 int i, j;
3867 char_u *p1;
3868 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 int difflen;
3870 int v;
3871
3872 if (xp->xp_numfiles == -1)
3873 {
3874 set_expand_context(xp);
3875 cmd_showtail = expand_showtail(xp);
3876 }
3877
3878 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3879 {
3880 beep_flush();
3881 return OK; /* Something illegal on command line */
3882 }
3883 if (xp->xp_context == EXPAND_NOTHING)
3884 {
3885 /* Caller can use the character as a normal char instead */
3886 return FAIL;
3887 }
3888
3889 MSG_PUTS("..."); /* show that we are busy */
3890 out_flush();
3891
3892 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003893 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
3895 if (type == WILD_NEXT || type == WILD_PREV)
3896 {
3897 /*
3898 * Get next/previous match for a previous expanded pattern.
3899 */
3900 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3901 }
3902 else
3903 {
3904 /*
3905 * Translate string into pattern and expand it.
3906 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003907 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3908 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 p2 = NULL;
3910 else
3911 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003912 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003913 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3914 if (escape)
3915 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003916
3917 if (p_wic)
3918 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003919 p2 = ExpandOne(xp, p1,
3920 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003921 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003923 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (p2 != NULL && type == WILD_LONGEST)
3925 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003926 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if (ccline.cmdbuff[i + j] == '*'
3928 || ccline.cmdbuff[i + j] == '?')
3929 break;
3930 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003931 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 }
3934 }
3935
3936 if (p2 != NULL && !got_int)
3937 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003938 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003939 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003941 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 xp->xp_pattern = ccline.cmdbuff + i;
3943 }
3944 else
3945 v = OK;
3946 if (v == OK)
3947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003948 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3949 &ccline.cmdbuff[ccline.cmdpos],
3950 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3951 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 ccline.cmdlen += difflen;
3953 ccline.cmdpos += difflen;
3954 }
3955 }
3956 vim_free(p2);
3957
3958 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003959 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
3961 /* When expanding a ":map" command and no matches are found, assume that
3962 * the key is supposed to be inserted literally */
3963 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3964 return FAIL;
3965
3966 if (xp->xp_numfiles <= 0 && p2 == NULL)
3967 beep_flush();
3968 else if (xp->xp_numfiles == 1)
3969 /* free expanded pattern */
3970 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3971
3972 return OK;
3973}
3974
3975/*
3976 * Do wildcard expansion on the string 'str'.
3977 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003978 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 * Return NULL for failure.
3980 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003981 * "orig" is the originally expanded string, copied to allocated memory. It
3982 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3983 * WILD_PREV "orig" should be NULL.
3984 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003985 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3986 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 *
3988 * mode = WILD_FREE: just free previously expanded matches
3989 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3990 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3991 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3992 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3993 * mode = WILD_ALL: return all matches concatenated
3994 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003995 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 *
3997 * options = WILD_LIST_NOTFOUND: list entries without a match
3998 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3999 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4000 * options = WILD_NO_BEEP: Don't beep for multiple matches
4001 * options = WILD_ADD_SLASH: add a slash after directory names
4002 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4003 * options = WILD_SILENT: don't print warning messages
4004 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004005 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 *
4007 * The variables xp->xp_context and xp->xp_backslash must have been set!
4008 */
4009 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004010ExpandOne(
4011 expand_T *xp,
4012 char_u *str,
4013 char_u *orig, /* allocated copy of original of expanded string */
4014 int options,
4015 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016{
4017 char_u *ss = NULL;
4018 static int findex;
4019 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004020 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 int i;
4022 long_u len;
4023 int non_suf_match; /* number without matching suffix */
4024
4025 /*
4026 * first handle the case of using an old match
4027 */
4028 if (mode == WILD_NEXT || mode == WILD_PREV)
4029 {
4030 if (xp->xp_numfiles > 0)
4031 {
4032 if (mode == WILD_PREV)
4033 {
4034 if (findex == -1)
4035 findex = xp->xp_numfiles;
4036 --findex;
4037 }
4038 else /* mode == WILD_NEXT */
4039 ++findex;
4040
4041 /*
4042 * When wrapping around, return the original string, set findex to
4043 * -1.
4044 */
4045 if (findex < 0)
4046 {
4047 if (orig_save == NULL)
4048 findex = xp->xp_numfiles - 1;
4049 else
4050 findex = -1;
4051 }
4052 if (findex >= xp->xp_numfiles)
4053 {
4054 if (orig_save == NULL)
4055 findex = 0;
4056 else
4057 findex = -1;
4058 }
4059#ifdef FEAT_WILDMENU
4060 if (p_wmnu)
4061 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4062 findex, cmd_showtail);
4063#endif
4064 if (findex == -1)
4065 return vim_strsave(orig_save);
4066 return vim_strsave(xp->xp_files[findex]);
4067 }
4068 else
4069 return NULL;
4070 }
4071
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004072 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4074 {
4075 FreeWild(xp->xp_numfiles, xp->xp_files);
4076 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004077 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 findex = 0;
4080
4081 if (mode == WILD_FREE) /* only release file name */
4082 return NULL;
4083
4084 if (xp->xp_numfiles == -1)
4085 {
4086 vim_free(orig_save);
4087 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004088 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 /*
4091 * Do the expansion.
4092 */
4093 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4094 options) == FAIL)
4095 {
4096#ifdef FNAME_ILLEGAL
4097 /* Illegal file name has been silently skipped. But when there
4098 * are wildcards, the real problem is that there was no match,
4099 * causing the pattern to be added, which has illegal characters.
4100 */
4101 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4102 EMSG2(_(e_nomatch2), str);
4103#endif
4104 }
4105 else if (xp->xp_numfiles == 0)
4106 {
4107 if (!(options & WILD_SILENT))
4108 EMSG2(_(e_nomatch2), str);
4109 }
4110 else
4111 {
4112 /* Escape the matches for use on the command line. */
4113 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4114
4115 /*
4116 * Check for matching suffixes in file names.
4117 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004118 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4119 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 if (xp->xp_numfiles)
4122 non_suf_match = xp->xp_numfiles;
4123 else
4124 non_suf_match = 1;
4125 if ((xp->xp_context == EXPAND_FILES
4126 || xp->xp_context == EXPAND_DIRECTORIES)
4127 && xp->xp_numfiles > 1)
4128 {
4129 /*
4130 * More than one match; check suffix.
4131 * The files will have been sorted on matching suffix in
4132 * expand_wildcards, only need to check the first two.
4133 */
4134 non_suf_match = 0;
4135 for (i = 0; i < 2; ++i)
4136 if (match_suffix(xp->xp_files[i]))
4137 ++non_suf_match;
4138 }
4139 if (non_suf_match != 1)
4140 {
4141 /* Can we ever get here unless it's while expanding
4142 * interactively? If not, we can get rid of this all
4143 * together. Don't really want to wait for this message
4144 * (and possibly have to hit return to continue!).
4145 */
4146 if (!(options & WILD_SILENT))
4147 EMSG(_(e_toomany));
4148 else if (!(options & WILD_NO_BEEP))
4149 beep_flush();
4150 }
4151 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4152 ss = vim_strsave(xp->xp_files[0]);
4153 }
4154 }
4155 }
4156
4157 /* Find longest common part */
4158 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4159 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004160 int mb_len = 1;
4161 int c0, ci;
4162
4163 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004165#ifdef FEAT_MBYTE
4166 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004168 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4169 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4170 }
4171 else
4172#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004173 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004174 for (i = 1; i < xp->xp_numfiles; ++i)
4175 {
4176#ifdef FEAT_MBYTE
4177 if (has_mbyte)
4178 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4179 else
4180#endif
4181 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004182 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004184 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004185 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004187 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 break;
4189 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004190 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 break;
4192 }
4193 if (i < xp->xp_numfiles)
4194 {
4195 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004196 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 break;
4198 }
4199 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 ss = alloc((unsigned)len + 1);
4202 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004203 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 findex = -1; /* next p_wc gets first one */
4205 }
4206
4207 /* Concatenate all matching names */
4208 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4209 {
4210 len = 0;
4211 for (i = 0; i < xp->xp_numfiles; ++i)
4212 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4213 ss = lalloc(len, TRUE);
4214 if (ss != NULL)
4215 {
4216 *ss = NUL;
4217 for (i = 0; i < xp->xp_numfiles; ++i)
4218 {
4219 STRCAT(ss, xp->xp_files[i]);
4220 if (i != xp->xp_numfiles - 1)
4221 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4222 }
4223 }
4224 }
4225
4226 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4227 ExpandCleanup(xp);
4228
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004229 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004230 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004231 vim_free(orig);
4232
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return ss;
4234}
4235
4236/*
4237 * Prepare an expand structure for use.
4238 */
4239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004240ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004242 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004243 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004245#ifndef BACKSLASH_IN_FILENAME
4246 xp->xp_shell = FALSE;
4247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 xp->xp_numfiles = -1;
4249 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004250#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4251 xp->xp_arg = NULL;
4252#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004253 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254}
4255
4256/*
4257 * Cleanup an expand structure after use.
4258 */
4259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004260ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 if (xp->xp_numfiles >= 0)
4263 {
4264 FreeWild(xp->xp_numfiles, xp->xp_files);
4265 xp->xp_numfiles = -1;
4266 }
4267}
4268
4269 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004270ExpandEscape(
4271 expand_T *xp,
4272 char_u *str,
4273 int numfiles,
4274 char_u **files,
4275 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
4277 int i;
4278 char_u *p;
4279
4280 /*
4281 * May change home directory back to "~"
4282 */
4283 if (options & WILD_HOME_REPLACE)
4284 tilde_replace(str, numfiles, files);
4285
4286 if (options & WILD_ESCAPE)
4287 {
4288 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004289 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004290 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 || xp->xp_context == EXPAND_BUFFERS
4292 || xp->xp_context == EXPAND_DIRECTORIES)
4293 {
4294 /*
4295 * Insert a backslash into a file name before a space, \, %, #
4296 * and wildmatch characters, except '~'.
4297 */
4298 for (i = 0; i < numfiles; ++i)
4299 {
4300 /* for ":set path=" we need to escape spaces twice */
4301 if (xp->xp_backslash == XP_BS_THREE)
4302 {
4303 p = vim_strsave_escaped(files[i], (char_u *)" ");
4304 if (p != NULL)
4305 {
4306 vim_free(files[i]);
4307 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004308#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 p = vim_strsave_escaped(files[i], (char_u *)" ");
4310 if (p != NULL)
4311 {
4312 vim_free(files[i]);
4313 files[i] = p;
4314 }
4315#endif
4316 }
4317 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004318#ifdef BACKSLASH_IN_FILENAME
4319 p = vim_strsave_fnameescape(files[i], FALSE);
4320#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004321 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (p != NULL)
4324 {
4325 vim_free(files[i]);
4326 files[i] = p;
4327 }
4328
4329 /* If 'str' starts with "\~", replace "~" at start of
4330 * files[i] with "\~". */
4331 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004332 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004335
4336 /* If the first file starts with a '+' escape it. Otherwise it
4337 * could be seen as "+cmd". */
4338 if (*files[0] == '+')
4339 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 else if (xp->xp_context == EXPAND_TAGS)
4342 {
4343 /*
4344 * Insert a backslash before characters in a tag name that
4345 * would terminate the ":tag" command.
4346 */
4347 for (i = 0; i < numfiles; ++i)
4348 {
4349 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4350 if (p != NULL)
4351 {
4352 vim_free(files[i]);
4353 files[i] = p;
4354 }
4355 }
4356 }
4357 }
4358}
4359
4360/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004361 * Escape special characters in "fname" for when used as a file name argument
4362 * after a Vim command, or, when "shell" is non-zero, a shell command.
4363 * Returns the result in allocated memory.
4364 */
4365 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004366vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004367{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004368 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004369#ifdef BACKSLASH_IN_FILENAME
4370 char_u buf[20];
4371 int j = 0;
4372
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004373 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004374 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004375 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004376 buf[j++] = *p;
4377 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004378 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004379#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004380 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4381 if (shell && csh_like_shell() && p != NULL)
4382 {
4383 char_u *s;
4384
4385 /* For csh and similar shells need to put two backslashes before '!'.
4386 * One is taken by Vim, one by the shell. */
4387 s = vim_strsave_escaped(p, (char_u *)"!");
4388 vim_free(p);
4389 p = s;
4390 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004391#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004392
4393 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4394 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004395 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004396 escape_fname(&p);
4397
4398 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004399}
4400
4401/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004402 * Put a backslash before the file name in "pp", which is in allocated memory.
4403 */
4404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004405escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004406{
4407 char_u *p;
4408
4409 p = alloc((unsigned)(STRLEN(*pp) + 2));
4410 if (p != NULL)
4411 {
4412 p[0] = '\\';
4413 STRCPY(p + 1, *pp);
4414 vim_free(*pp);
4415 *pp = p;
4416 }
4417}
4418
4419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 * For each file name in files[num_files]:
4421 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4422 */
4423 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004424tilde_replace(
4425 char_u *orig_pat,
4426 int num_files,
4427 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
4429 int i;
4430 char_u *p;
4431
4432 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4433 {
4434 for (i = 0; i < num_files; ++i)
4435 {
4436 p = home_replace_save(NULL, files[i]);
4437 if (p != NULL)
4438 {
4439 vim_free(files[i]);
4440 files[i] = p;
4441 }
4442 }
4443 }
4444}
4445
4446/*
4447 * Show all matches for completion on the command line.
4448 * Returns EXPAND_NOTHING when the character that triggered expansion should
4449 * be inserted like a normal character.
4450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004452showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4455 int num_files;
4456 char_u **files_found;
4457 int i, j, k;
4458 int maxlen;
4459 int lines;
4460 int columns;
4461 char_u *p;
4462 int lastlen;
4463 int attr;
4464 int showtail;
4465
4466 if (xp->xp_numfiles == -1)
4467 {
4468 set_expand_context(xp);
4469 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4470 &num_files, &files_found);
4471 showtail = expand_showtail(xp);
4472 if (i != EXPAND_OK)
4473 return i;
4474
4475 }
4476 else
4477 {
4478 num_files = xp->xp_numfiles;
4479 files_found = xp->xp_files;
4480 showtail = cmd_showtail;
4481 }
4482
4483#ifdef FEAT_WILDMENU
4484 if (!wildmenu)
4485 {
4486#endif
4487 msg_didany = FALSE; /* lines_left will be set */
4488 msg_start(); /* prepare for paging */
4489 msg_putchar('\n');
4490 out_flush();
4491 cmdline_row = msg_row;
4492 msg_didany = FALSE; /* lines_left will be set again */
4493 msg_start(); /* prepare for paging */
4494#ifdef FEAT_WILDMENU
4495 }
4496#endif
4497
4498 if (got_int)
4499 got_int = FALSE; /* only int. the completion, not the cmd line */
4500#ifdef FEAT_WILDMENU
4501 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004502 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503#endif
4504 else
4505 {
4506 /* find the length of the longest file name */
4507 maxlen = 0;
4508 for (i = 0; i < num_files; ++i)
4509 {
4510 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004511 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 || xp->xp_context == EXPAND_BUFFERS))
4513 {
4514 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4515 j = vim_strsize(NameBuff);
4516 }
4517 else
4518 j = vim_strsize(L_SHOWFILE(i));
4519 if (j > maxlen)
4520 maxlen = j;
4521 }
4522
4523 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4524 lines = num_files;
4525 else
4526 {
4527 /* compute the number of columns and lines for the listing */
4528 maxlen += 2; /* two spaces between file names */
4529 columns = ((int)Columns + 2) / maxlen;
4530 if (columns < 1)
4531 columns = 1;
4532 lines = (num_files + columns - 1) / columns;
4533 }
4534
Bram Moolenaar8820b482017-03-16 17:23:31 +01004535 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4538 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004539 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 msg_clr_eos();
4541 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004542 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544
4545 /* list the files line by line */
4546 for (i = 0; i < lines; ++i)
4547 {
4548 lastlen = 999;
4549 for (k = i; k < num_files; k += lines)
4550 {
4551 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4552 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004553 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 p = files_found[k] + STRLEN(files_found[k]) + 1;
4555 msg_advance(maxlen + 1);
4556 msg_puts(p);
4557 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004558 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 break;
4560 }
4561 for (j = maxlen - lastlen; --j >= 0; )
4562 msg_putchar(' ');
4563 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004564 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 || xp->xp_context == EXPAND_BUFFERS)
4566 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004567 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004568 if (xp->xp_numfiles != -1)
4569 {
4570 char_u *halved_slash;
4571 char_u *exp_path;
4572
4573 /* Expansion was done before and special characters
4574 * were escaped, need to halve backslashes. Also
4575 * $HOME has been replaced with ~/. */
4576 exp_path = expand_env_save_opt(files_found[k], TRUE);
4577 halved_slash = backslash_halve_save(
4578 exp_path != NULL ? exp_path : files_found[k]);
4579 j = mch_isdir(halved_slash != NULL ? halved_slash
4580 : files_found[k]);
4581 vim_free(exp_path);
4582 vim_free(halved_slash);
4583 }
4584 else
4585 /* Expansion was done here, file names are literal. */
4586 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if (showtail)
4588 p = L_SHOWFILE(k);
4589 else
4590 {
4591 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4592 TRUE);
4593 p = NameBuff;
4594 }
4595 }
4596 else
4597 {
4598 j = FALSE;
4599 p = L_SHOWFILE(k);
4600 }
4601 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4602 }
4603 if (msg_col > 0) /* when not wrapped around */
4604 {
4605 msg_clr_eos();
4606 msg_putchar('\n');
4607 }
4608 out_flush(); /* show one line at a time */
4609 if (got_int)
4610 {
4611 got_int = FALSE;
4612 break;
4613 }
4614 }
4615
4616 /*
4617 * we redraw the command below the lines that we have just listed
4618 * This is a bit tricky, but it saves a lot of screen updating.
4619 */
4620 cmdline_row = msg_row; /* will put it back later */
4621 }
4622
4623 if (xp->xp_numfiles == -1)
4624 FreeWild(num_files, files_found);
4625
4626 return EXPAND_OK;
4627}
4628
4629/*
4630 * Private gettail for showmatches() (and win_redr_status_matches()):
4631 * Find tail of file name path, but ignore trailing "/".
4632 */
4633 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 char_u *p;
4637 char_u *t = s;
4638 int had_sep = FALSE;
4639
4640 for (p = s; *p != NUL; )
4641 {
4642 if (vim_ispathsep(*p)
4643#ifdef BACKSLASH_IN_FILENAME
4644 && !rem_backslash(p)
4645#endif
4646 )
4647 had_sep = TRUE;
4648 else if (had_sep)
4649 {
4650 t = p;
4651 had_sep = FALSE;
4652 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004653 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 return t;
4656}
4657
4658/*
4659 * Return TRUE if we only need to show the tail of completion matches.
4660 * When not completing file names or there is a wildcard in the path FALSE is
4661 * returned.
4662 */
4663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004664expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
4666 char_u *s;
4667 char_u *end;
4668
4669 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004670 if (xp->xp_context != EXPAND_FILES
4671 && xp->xp_context != EXPAND_SHELLCMD
4672 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 return FALSE;
4674
4675 end = gettail(xp->xp_pattern);
4676 if (end == xp->xp_pattern) /* there is no path separator */
4677 return FALSE;
4678
4679 for (s = xp->xp_pattern; s < end; s++)
4680 {
4681 /* Skip escaped wildcards. Only when the backslash is not a path
4682 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4683 if (rem_backslash(s))
4684 ++s;
4685 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4686 return FALSE;
4687 }
4688 return TRUE;
4689}
4690
4691/*
4692 * Prepare a string for expansion.
4693 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004694 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 * When expanding other names: The string will be used with regcomp(). Copy
4696 * the name into allocated memory and prepend "^".
4697 */
4698 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004699addstar(
4700 char_u *fname,
4701 int len,
4702 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 char_u *retval;
4705 int i, j;
4706 int new_len;
4707 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004708 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004710 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004711 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004712 && context != EXPAND_SHELLCMD
4713 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 /*
4716 * Matching will be done internally (on something other than files).
4717 * So we convert the file-matching-type wildcards into our kind for
4718 * use with vim_regcomp(). First work out how long it will be:
4719 */
4720
4721 /* For help tags the translation is done in find_help_tags().
4722 * For a tag pattern starting with "/" no translation is needed. */
4723 if (context == EXPAND_HELP
4724 || context == EXPAND_COLORS
4725 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004726 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004727 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004728 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004729 || ((context == EXPAND_TAGS_LISTFILES
4730 || context == EXPAND_TAGS)
4731 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 retval = vim_strnsave(fname, len);
4733 else
4734 {
4735 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4736 for (i = 0; i < len; i++)
4737 {
4738 if (fname[i] == '*' || fname[i] == '~')
4739 new_len++; /* '*' needs to be replaced by ".*"
4740 '~' needs to be replaced by "\~" */
4741
4742 /* Buffer names are like file names. "." should be literal */
4743 if (context == EXPAND_BUFFERS && fname[i] == '.')
4744 new_len++; /* "." becomes "\." */
4745
4746 /* Custom expansion takes care of special things, match
4747 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004748 if ((context == EXPAND_USER_DEFINED
4749 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 new_len++; /* '\' becomes "\\" */
4751 }
4752 retval = alloc(new_len);
4753 if (retval != NULL)
4754 {
4755 retval[0] = '^';
4756 j = 1;
4757 for (i = 0; i < len; i++, j++)
4758 {
4759 /* Skip backslash. But why? At least keep it for custom
4760 * expansion. */
4761 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004762 && context != EXPAND_USER_LIST
4763 && fname[i] == '\\'
4764 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 break;
4766
4767 switch (fname[i])
4768 {
4769 case '*': retval[j++] = '.';
4770 break;
4771 case '~': retval[j++] = '\\';
4772 break;
4773 case '?': retval[j] = '.';
4774 continue;
4775 case '.': if (context == EXPAND_BUFFERS)
4776 retval[j++] = '\\';
4777 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004778 case '\\': if (context == EXPAND_USER_DEFINED
4779 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 retval[j++] = '\\';
4781 break;
4782 }
4783 retval[j] = fname[i];
4784 }
4785 retval[j] = NUL;
4786 }
4787 }
4788 }
4789 else
4790 {
4791 retval = alloc(len + 4);
4792 if (retval != NULL)
4793 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004794 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004797 * Don't add a star to *, ~, ~user, $var or `cmd`.
4798 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 * ~ would be at the start of the file name, but not the tail.
4800 * $ could be anywhere in the tail.
4801 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004802 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 */
4804 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004805 ends_in_star = (len > 0 && retval[len - 1] == '*');
4806#ifndef BACKSLASH_IN_FILENAME
4807 for (i = len - 2; i >= 0; --i)
4808 {
4809 if (retval[i] != '\\')
4810 break;
4811 ends_in_star = !ends_in_star;
4812 }
4813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004815 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 && vim_strchr(tail, '$') == NULL
4817 && vim_strchr(retval, '`') == NULL)
4818 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004819 else if (len > 0 && retval[len - 1] == '$')
4820 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 retval[len] = NUL;
4822 }
4823 }
4824 return retval;
4825}
4826
4827/*
4828 * Must parse the command line so far to work out what context we are in.
4829 * Completion can then be done based on that context.
4830 * This routine sets the variables:
4831 * xp->xp_pattern The start of the pattern to be expanded within
4832 * the command line (ends at the cursor).
4833 * xp->xp_context The type of thing to expand. Will be one of:
4834 *
4835 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4836 * the command line, like an unknown command. Caller
4837 * should beep.
4838 * EXPAND_NOTHING Unrecognised context for completion, use char like
4839 * a normal char, rather than for completion. eg
4840 * :s/^I/
4841 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4842 * it.
4843 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4844 * EXPAND_FILES After command with XFILE set, or after setting
4845 * with P_EXPAND set. eg :e ^I, :w>>^I
4846 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4847 * when we know only directories are of interest. eg
4848 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004849 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4851 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4852 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4853 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4854 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4855 * EXPAND_EVENTS Complete event names
4856 * EXPAND_SYNTAX Complete :syntax command arguments
4857 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4858 * EXPAND_AUGROUP Complete autocommand group names
4859 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4860 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4861 * eg :unmap a^I , :cunab x^I
4862 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4863 * eg :call sub^I
4864 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4865 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4866 * names in expressions, eg :while s^I
4867 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004868 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
4870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004871set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004873 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (ccline.cmdfirstc != ':'
4875#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004876 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004877 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#endif
4879 )
4880 {
4881 xp->xp_context = EXPAND_NOTHING;
4882 return;
4883 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004884 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885}
4886
4887 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004888set_cmd_context(
4889 expand_T *xp,
4890 char_u *str, /* start of command line */
4891 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004892 int col, /* position of cursor */
4893 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 int old_char = NUL;
4896 char_u *nextcomm;
4897
4898 /*
4899 * Avoid a UMR warning from Purify, only save the character if it has been
4900 * written before.
4901 */
4902 if (col < len)
4903 old_char = str[col];
4904 str[col] = NUL;
4905 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004906
4907#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004908 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004909 {
4910# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004911 /* pass CMD_SIZE because there is no real command */
4912 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004913# endif
4914 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004915 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004916 {
4917 xp->xp_context = ccline.xp_context;
4918 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004919# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004920 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004921# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004922 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004923 else
4924#endif
4925 while (nextcomm != NULL)
4926 nextcomm = set_one_cmd_context(xp, nextcomm);
4927
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004928 /* Store the string here so that call_user_expand_func() can get to them
4929 * easily. */
4930 xp->xp_line = str;
4931 xp->xp_col = col;
4932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 str[col] = old_char;
4934}
4935
4936/*
4937 * Expand the command line "str" from context "xp".
4938 * "xp" must have been set by set_cmd_context().
4939 * xp->xp_pattern points into "str", to where the text that is to be expanded
4940 * starts.
4941 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4942 * cursor.
4943 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4944 * key that triggered expansion literally.
4945 * Returns EXPAND_OK otherwise.
4946 */
4947 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004948expand_cmdline(
4949 expand_T *xp,
4950 char_u *str, /* start of command line */
4951 int col, /* position of cursor */
4952 int *matchcount, /* return: nr of matches */
4953 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004956 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4959 {
4960 beep_flush();
4961 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4962 }
4963 if (xp->xp_context == EXPAND_NOTHING)
4964 {
4965 /* Caller can use the character as a normal char instead */
4966 return EXPAND_NOTHING;
4967 }
4968
4969 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004970 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4971 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (file_str == NULL)
4973 return EXPAND_UNSUCCESSFUL;
4974
Bram Moolenaar94950a92010-12-02 16:01:29 +01004975 if (p_wic)
4976 options += WILD_ICASE;
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004979 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 *matchcount = 0;
4982 *matches = NULL;
4983 }
4984 vim_free(file_str);
4985
4986 return EXPAND_OK;
4987}
4988
4989#ifdef FEAT_MULTI_LANG
4990/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004991 * Cleanup matches for help tags:
4992 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4993 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004995static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004998cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
5000 int i, j;
5001 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005002 char_u buf[4];
5003 char_u *p = buf;
5004
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005005 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005006 {
5007 *p++ = '@';
5008 *p++ = p_hlg[0];
5009 *p++ = p_hlg[1];
5010 }
5011 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 for (i = 0; i < num_file; ++i)
5014 {
5015 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005016 if (len <= 0)
5017 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005018 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 /* Sorting on priority means the same item in another language may
5021 * be anywhere. Search all items for a match up to the "@en". */
5022 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005023 if (j != i && (int)STRLEN(file[j]) == len + 3
5024 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005027 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 file[i][len] = NUL;
5029 }
5030 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005031
5032 if (*buf != NUL)
5033 for (i = 0; i < num_file; ++i)
5034 {
5035 len = (int)STRLEN(file[i]) - 3;
5036 if (len <= 0)
5037 continue;
5038 if (STRCMP(file[i] + len, buf) == 0)
5039 {
5040 /* remove the default language */
5041 file[i][len] = NUL;
5042 }
5043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044}
5045#endif
5046
5047/*
5048 * Do the expansion based on xp->xp_context and "pat".
5049 */
5050 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005051ExpandFromContext(
5052 expand_T *xp,
5053 char_u *pat,
5054 int *num_file,
5055 char_u ***file,
5056 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058#ifdef FEAT_CMDL_COMPL
5059 regmatch_T regmatch;
5060#endif
5061 int ret;
5062 int flags;
5063
5064 flags = EW_DIR; /* include directories */
5065 if (options & WILD_LIST_NOTFOUND)
5066 flags |= EW_NOTFOUND;
5067 if (options & WILD_ADD_SLASH)
5068 flags |= EW_ADDSLASH;
5069 if (options & WILD_KEEP_ALL)
5070 flags |= EW_KEEPALL;
5071 if (options & WILD_SILENT)
5072 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005073 if (options & WILD_ALLLINKS)
5074 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005076 if (xp->xp_context == EXPAND_FILES
5077 || xp->xp_context == EXPAND_DIRECTORIES
5078 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 /*
5081 * Expand file or directory names.
5082 */
5083 int free_pat = FALSE;
5084 int i;
5085
5086 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5087 * space */
5088 if (xp->xp_backslash != XP_BS_NONE)
5089 {
5090 free_pat = TRUE;
5091 pat = vim_strsave(pat);
5092 for (i = 0; pat[i]; ++i)
5093 if (pat[i] == '\\')
5094 {
5095 if (xp->xp_backslash == XP_BS_THREE
5096 && pat[i + 1] == '\\'
5097 && pat[i + 2] == '\\'
5098 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005099 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (xp->xp_backslash == XP_BS_ONE
5101 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005102 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104 }
5105
5106 if (xp->xp_context == EXPAND_FILES)
5107 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005108 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5109 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 else
5111 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005112 if (options & WILD_ICASE)
5113 flags |= EW_ICASE;
5114
Bram Moolenaard7834d32009-12-02 16:14:36 +00005115 /* Expand wildcards, supporting %:h and the like. */
5116 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (free_pat)
5118 vim_free(pat);
5119 return ret;
5120 }
5121
5122 *file = (char_u **)"";
5123 *num_file = 0;
5124 if (xp->xp_context == EXPAND_HELP)
5125 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005126 /* With an empty argument we would get all the help tags, which is
5127 * very slow. Get matches for "help" instead. */
5128 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5129 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131#ifdef FEAT_MULTI_LANG
5132 cleanup_help_tags(*num_file, *file);
5133#endif
5134 return OK;
5135 }
5136 return FAIL;
5137 }
5138
5139#ifndef FEAT_CMDL_COMPL
5140 return FAIL;
5141#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005142 if (xp->xp_context == EXPAND_SHELLCMD)
5143 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (xp->xp_context == EXPAND_OLD_SETTING)
5145 return ExpandOldSetting(num_file, file);
5146 if (xp->xp_context == EXPAND_BUFFERS)
5147 return ExpandBufnames(pat, num_file, file, options);
5148 if (xp->xp_context == EXPAND_TAGS
5149 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5150 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5151 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005152 {
5153 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005154 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5155 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005158 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005159 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005160 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005161 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005162 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005163 {
5164 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005165 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005166 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005167 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005168 {
5169 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005170 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005171 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005172# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5173 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005174 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005175# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005176 if (xp->xp_context == EXPAND_PACKADD)
5177 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
5179 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5180 if (regmatch.regprog == NULL)
5181 return FAIL;
5182
5183 /* set ignore-case according to p_ic, p_scs and pat */
5184 regmatch.rm_ic = ignorecase(pat);
5185
5186 if (xp->xp_context == EXPAND_SETTINGS
5187 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5188 ret = ExpandSettings(xp, &regmatch, num_file, file);
5189 else if (xp->xp_context == EXPAND_MAPPINGS)
5190 ret = ExpandMappings(&regmatch, num_file, file);
5191# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5192 else if (xp->xp_context == EXPAND_USER_DEFINED)
5193 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5194# endif
5195 else
5196 {
5197 static struct expgen
5198 {
5199 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005200 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005202 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 } tab[] =
5204 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005205 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5206 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005207 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005208 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005209#ifdef FEAT_CMDHIST
5210 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005213 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005214 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005215 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5216 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5217 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#endif
5219#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005220 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5221 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5222 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5223 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#endif
5225#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005226 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5227 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#endif
5229#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005230 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005232#ifdef FEAT_PROFILE
5233 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5234#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005235 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005236 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5237 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005238#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005239 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005240#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005241#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005242 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005243#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005244#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005245 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5248 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005249 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5250 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005252 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005253 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005254 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 };
5256 int i;
5257
5258 /*
5259 * Find a context in the table and call the ExpandGeneric() with the
5260 * right function to do the expansion.
5261 */
5262 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005263 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (xp->xp_context == tab[i].context)
5265 {
5266 if (tab[i].ic)
5267 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005268 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005269 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 break;
5271 }
5272 }
5273
Bram Moolenaar473de612013-06-08 18:19:48 +02005274 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 return ret;
5277#endif /* FEAT_CMDL_COMPL */
5278}
5279
5280#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5281/*
5282 * Expand a list of names.
5283 *
5284 * Generic function for command line completion. It calls a function to
5285 * obtain strings, one by one. The strings are matched against a regexp
5286 * program. Matching strings are copied into an array, which is returned.
5287 *
5288 * Returns OK when no problems encountered, FAIL for error (out of memory).
5289 */
5290 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005291ExpandGeneric(
5292 expand_T *xp,
5293 regmatch_T *regmatch,
5294 int *num_file,
5295 char_u ***file,
5296 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005298 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
5300 int i;
5301 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005302 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 char_u *str;
5304
5305 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005306 * round == 0: count the number of matching names
5307 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005309 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
5311 for (i = 0; ; ++i)
5312 {
5313 str = (*func)(xp, i);
5314 if (str == NULL) /* end of list */
5315 break;
5316 if (*str == NUL) /* skip empty strings */
5317 continue;
5318
5319 if (vim_regexec(regmatch, str, (colnr_T)0))
5320 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005321 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005323 if (escaped)
5324 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5325 else
5326 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 (*file)[count] = str;
5328#ifdef FEAT_MENU
5329 if (func == get_menu_names && str != NULL)
5330 {
5331 /* test for separator added by get_menu_names() */
5332 str += STRLEN(str) - 1;
5333 if (*str == '\001')
5334 *str = '.';
5335 }
5336#endif
5337 }
5338 ++count;
5339 }
5340 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005341 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 if (count == 0)
5344 return OK;
5345 *num_file = count;
5346 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5347 if (*file == NULL)
5348 {
5349 *file = (char_u **)"";
5350 return FAIL;
5351 }
5352 count = 0;
5353 }
5354 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005355
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005356 /* Sort the results. Keep menu's in the specified order. */
5357 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005358 {
5359 if (xp->xp_context == EXPAND_EXPRESSION
5360 || xp->xp_context == EXPAND_FUNCTIONS
5361 || xp->xp_context == EXPAND_USER_FUNC)
5362 /* <SNR> functions should be sorted to the end. */
5363 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5364 sort_func_compare);
5365 else
5366 sort_strings(*file, *num_file);
5367 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005368
Bram Moolenaar4f688582007-07-24 12:34:30 +00005369#ifdef FEAT_CMDL_COMPL
5370 /* Reset the variables used for special highlight names expansion, so that
5371 * they don't show up when getting normal highlight names by ID. */
5372 reset_expand_highlight();
5373#endif
5374
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 return OK;
5376}
5377
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005378/*
5379 * Complete a shell command.
5380 * Returns FAIL or OK;
5381 */
5382 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005383expand_shellcmd(
5384 char_u *filepat, /* pattern to match with command names */
5385 int *num_file, /* return: number of matches */
5386 char_u ***file, /* return: array with matches */
5387 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005388{
5389 char_u *pat;
5390 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005391 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005392 int mustfree = FALSE;
5393 garray_T ga;
5394 char_u *buf = alloc(MAXPATHL);
5395 size_t l;
5396 char_u *s, *e;
5397 int flags = flagsarg;
5398 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005399 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005400 hashtab_T found_ht;
5401 hashitem_T *hi;
5402 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005403
5404 if (buf == NULL)
5405 return FAIL;
5406
5407 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5408 * space */
5409 pat = vim_strsave(filepat);
5410 for (i = 0; pat[i]; ++i)
5411 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005412 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005413
Bram Moolenaarb5971142015-03-21 17:32:19 +01005414 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005415
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005416 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5417 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005418 path = (char_u *)".";
5419 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005420 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005421 /* For an absolute name we don't use $PATH. */
5422 if (!mch_isFullName(pat))
5423 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005424 if (path == NULL)
5425 path = (char_u *)"";
5426 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005427
5428 /*
5429 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005430 * collect them in "ga". When "." is not in $PATH also expand for the
5431 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005432 */
5433 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005434 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005435 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005436 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005437#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005438 e = vim_strchr(s, ';');
5439#else
5440 e = vim_strchr(s, ':');
5441#endif
5442 if (e == NULL)
5443 e = s + STRLEN(s);
5444
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005445 if (*s == NUL)
5446 {
5447 if (did_curdir)
5448 break;
5449 // Find directories in the current directory, path is empty.
5450 did_curdir = TRUE;
5451 flags |= EW_DIR;
5452 }
5453 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5454 {
5455 did_curdir = TRUE;
5456 flags |= EW_DIR;
5457 }
5458 else
5459 // Do not match directories inside a $PATH item.
5460 flags &= ~EW_DIR;
5461
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005462 l = e - s;
5463 if (l > MAXPATHL - 5)
5464 break;
5465 vim_strncpy(buf, s, l);
5466 add_pathsep(buf);
5467 l = STRLEN(buf);
5468 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5469
5470 /* Expand matches in one directory of $PATH. */
5471 ret = expand_wildcards(1, &buf, num_file, file, flags);
5472 if (ret == OK)
5473 {
5474 if (ga_grow(&ga, *num_file) == FAIL)
5475 FreeWild(*num_file, *file);
5476 else
5477 {
5478 for (i = 0; i < *num_file; ++i)
5479 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005480 char_u *name = (*file)[i];
5481
5482 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005483 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005484 // Check if this name was already found.
5485 hash = hash_hash(name + l);
5486 hi = hash_lookup(&found_ht, name + l, hash);
5487 if (HASHITEM_EMPTY(hi))
5488 {
5489 // Remove the path that was prepended.
5490 STRMOVE(name, name + l);
5491 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5492 hash_add_item(&found_ht, hi, name, hash);
5493 name = NULL;
5494 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005495 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005496 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005497 }
5498 vim_free(*file);
5499 }
5500 }
5501 if (*e != NUL)
5502 ++e;
5503 }
5504 *file = ga.ga_data;
5505 *num_file = ga.ga_len;
5506
5507 vim_free(buf);
5508 vim_free(pat);
5509 if (mustfree)
5510 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005511 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005512 return OK;
5513}
5514
5515
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5517/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005518 * Call "user_expand_func()" to invoke a user defined Vim script function and
5519 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005521 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005522call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005523 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005524 expand_T *xp,
5525 int *num_file,
5526 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005528 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005529 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005531 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005532 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005533 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaarb7515462013-06-29 12:58:33 +02005535 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005536 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 *num_file = 0;
5538 *file = NULL;
5539
Bram Moolenaarb7515462013-06-29 12:58:33 +02005540 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005541 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005542 keep = ccline.cmdbuff[ccline.cmdlen];
5543 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005544 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005545
Bram Moolenaarffa96842018-06-12 22:05:14 +02005546 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5547
5548 args[0].v_type = VAR_STRING;
5549 args[0].vval.v_string = pat;
5550 args[1].v_type = VAR_STRING;
5551 args[1].vval.v_string = xp->xp_line;
5552 args[2].v_type = VAR_NUMBER;
5553 args[2].vval.v_number = xp->xp_col;
5554 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005556 /* Save the cmdline, we don't know what the function may do. */
5557 save_ccline = ccline;
5558 ccline.cmdbuff = NULL;
5559 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005561
Bram Moolenaarded27a12018-08-01 19:06:03 +02005562 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005563
5564 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005566 if (ccline.cmdbuff != NULL)
5567 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005568
Bram Moolenaarffa96842018-06-12 22:05:14 +02005569 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005570 return ret;
5571}
5572
5573/*
5574 * Expand names with a function defined by the user.
5575 */
5576 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ExpandUserDefined(
5578 expand_T *xp,
5579 regmatch_T *regmatch,
5580 int *num_file,
5581 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005582{
5583 char_u *retstr;
5584 char_u *s;
5585 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005586 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005587 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005588 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005589
5590 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5591 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 return FAIL;
5593
5594 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005595 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
5597 e = vim_strchr(s, '\n');
5598 if (e == NULL)
5599 e = s + STRLEN(s);
5600 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005601 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005603 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5604 *e = keep;
5605
5606 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005608 if (ga_grow(&ga, 1) == FAIL)
5609 break;
5610 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5611 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 if (*e != NUL)
5615 ++e;
5616 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005617 vim_free(retstr);
5618 *file = ga.ga_data;
5619 *num_file = ga.ga_len;
5620 return OK;
5621}
5622
5623/*
5624 * Expand names with a list returned by a function defined by the user.
5625 */
5626 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005627ExpandUserList(
5628 expand_T *xp,
5629 int *num_file,
5630 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005631{
5632 list_T *retlist;
5633 listitem_T *li;
5634 garray_T ga;
5635
5636 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5637 if (retlist == NULL)
5638 return FAIL;
5639
5640 ga_init2(&ga, (int)sizeof(char *), 3);
5641 /* Loop over the items in the list. */
5642 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5643 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005644 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5645 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005646
5647 if (ga_grow(&ga, 1) == FAIL)
5648 break;
5649
5650 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005651 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005652 ++ga.ga_len;
5653 }
5654 list_unref(retlist);
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 *file = ga.ga_data;
5657 *num_file = ga.ga_len;
5658 return OK;
5659}
5660#endif
5661
5662/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005663 * Expand color scheme, compiler or filetype names.
5664 * Search from 'runtimepath':
5665 * 'runtimepath'/{dirnames}/{pat}.vim
5666 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5667 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5668 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5669 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005670 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 */
5672 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673ExpandRTDir(
5674 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005675 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676 int *num_file,
5677 char_u ***file,
5678 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 char_u *s;
5681 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005682 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005684 int i;
5685 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 *num_file = 0;
5688 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005689 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005690 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005692 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005694 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5695 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005697 ga_clear_strings(&ga);
5698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005700 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005701 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005702 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005704
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005705 if (flags & DIP_START) {
5706 for (i = 0; dirnames[i] != NULL; ++i)
5707 {
5708 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5709 if (s == NULL)
5710 {
5711 ga_clear_strings(&ga);
5712 return FAIL;
5713 }
5714 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5715 globpath(p_pp, s, &ga, 0);
5716 vim_free(s);
5717 }
5718 }
5719
5720 if (flags & DIP_OPT) {
5721 for (i = 0; dirnames[i] != NULL; ++i)
5722 {
5723 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5724 if (s == NULL)
5725 {
5726 ga_clear_strings(&ga);
5727 return FAIL;
5728 }
5729 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5730 globpath(p_pp, s, &ga, 0);
5731 vim_free(s);
5732 }
5733 }
5734
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005735 for (i = 0; i < ga.ga_len; ++i)
5736 {
5737 match = ((char_u **)ga.ga_data)[i];
5738 s = match;
5739 e = s + STRLEN(s);
5740 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5741 {
5742 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005743 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005744 if (s < match || vim_ispathsep(*s))
5745 break;
5746 ++s;
5747 *e = NUL;
5748 mch_memmove(match, s, e - s + 1);
5749 }
5750 }
5751
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005752 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005753 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005754
5755 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005756 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005757 remove_duplicates(&ga);
5758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 *file = ga.ga_data;
5760 *num_file = ga.ga_len;
5761 return OK;
5762}
5763
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005764/*
5765 * Expand loadplugin names:
5766 * 'packpath'/pack/ * /opt/{pat}
5767 */
5768 static int
5769ExpandPackAddDir(
5770 char_u *pat,
5771 int *num_file,
5772 char_u ***file)
5773{
5774 char_u *s;
5775 char_u *e;
5776 char_u *match;
5777 garray_T ga;
5778 int i;
5779 int pat_len;
5780
5781 *num_file = 0;
5782 *file = NULL;
5783 pat_len = (int)STRLEN(pat);
5784 ga_init2(&ga, (int)sizeof(char *), 10);
5785
5786 s = alloc((unsigned)(pat_len + 26));
5787 if (s == NULL)
5788 {
5789 ga_clear_strings(&ga);
5790 return FAIL;
5791 }
5792 sprintf((char *)s, "pack/*/opt/%s*", pat);
5793 globpath(p_pp, s, &ga, 0);
5794 vim_free(s);
5795
5796 for (i = 0; i < ga.ga_len; ++i)
5797 {
5798 match = ((char_u **)ga.ga_data)[i];
5799 s = gettail(match);
5800 e = s + STRLEN(s);
5801 mch_memmove(match, s, e - s + 1);
5802 }
5803
5804 if (ga.ga_len == 0)
5805 return FAIL;
5806
5807 /* Sort and remove duplicates which can happen when specifying multiple
5808 * directories in dirnames. */
5809 remove_duplicates(&ga);
5810
5811 *file = ga.ga_data;
5812 *num_file = ga.ga_len;
5813 return OK;
5814}
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816#endif
5817
5818#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5819/*
5820 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005821 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005823 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005824globpath(
5825 char_u *path,
5826 char_u *file,
5827 garray_T *ga,
5828 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 expand_T xpc;
5831 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 int num_p;
5834 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
5836 buf = alloc(MAXPATHL);
5837 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005838 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005840 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 /* Loop over all entries in {path}. */
5844 while (*path != NUL)
5845 {
5846 /* Copy one item of the path to buf[] and concatenate the file name. */
5847 copy_option_part(&path, buf, MAXPATHL, ",");
5848 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5849 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005850# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005851 /* Using the platform's path separator (\) makes vim incorrectly
5852 * treat it as an escape character, use '/' instead. */
5853 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5854 STRCAT(buf, "/");
5855# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005859 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5860 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005862 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005864 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 for (i = 0; i < num_p; ++i)
5867 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005868 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005869 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005870 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 FreeWild(num_p, p);
5875 }
5876 }
5877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880}
5881
5882#endif
5883
5884#if defined(FEAT_CMDHIST) || defined(PROTO)
5885
5886/*********************************
5887 * Command line history stuff *
5888 *********************************/
5889
5890/*
5891 * Translate a history character to the associated type number.
5892 */
5893 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005894hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 if (c == ':')
5897 return HIST_CMD;
5898 if (c == '=')
5899 return HIST_EXPR;
5900 if (c == '@')
5901 return HIST_INPUT;
5902 if (c == '>')
5903 return HIST_DEBUG;
5904 return HIST_SEARCH; /* must be '?' or '/' */
5905}
5906
5907/*
5908 * Table of history names.
5909 * These names are used in :history and various hist...() functions.
5910 * It is sufficient to give the significant prefix of a history name.
5911 */
5912
5913static char *(history_names[]) =
5914{
5915 "cmd",
5916 "search",
5917 "expr",
5918 "input",
5919 "debug",
5920 NULL
5921};
5922
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005923#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5924/*
5925 * Function given to ExpandGeneric() to obtain the possible first
5926 * arguments of the ":history command.
5927 */
5928 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005929get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005930{
5931 static char_u compl[2] = { NUL, NUL };
5932 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005933 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005934 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5935
5936 if (idx < short_names_count)
5937 {
5938 compl[0] = (char_u)short_names[idx];
5939 return compl;
5940 }
5941 if (idx < short_names_count + history_name_count)
5942 return (char_u *)history_names[idx - short_names_count];
5943 if (idx == short_names_count + history_name_count)
5944 return (char_u *)"all";
5945 return NULL;
5946}
5947#endif
5948
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949/*
5950 * init_history() - Initialize the command line history.
5951 * Also used to re-allocate the history when the size changes.
5952 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 int newlen; /* new length of history table */
5957 histentry_T *temp;
5958 int i;
5959 int j;
5960 int type;
5961
5962 /*
5963 * If size of history table changed, reallocate it
5964 */
5965 newlen = (int)p_hi;
5966 if (newlen != hislen) /* history length changed */
5967 {
5968 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5969 {
5970 if (newlen)
5971 {
5972 temp = (histentry_T *)lalloc(
5973 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5974 if (temp == NULL) /* out of memory! */
5975 {
5976 if (type == 0) /* first one: just keep the old length */
5977 {
5978 newlen = hislen;
5979 break;
5980 }
5981 /* Already changed one table, now we can only have zero
5982 * length for all tables. */
5983 newlen = 0;
5984 type = -1;
5985 continue;
5986 }
5987 }
5988 else
5989 temp = NULL;
5990 if (newlen == 0 || temp != NULL)
5991 {
5992 if (hisidx[type] < 0) /* there are no entries yet */
5993 {
5994 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005995 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 }
5997 else if (newlen > hislen) /* array becomes bigger */
5998 {
5999 for (i = 0; i <= hisidx[type]; ++i)
6000 temp[i] = history[type][i];
6001 j = i;
6002 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006003 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 for ( ; j < hislen; ++i, ++j)
6005 temp[i] = history[type][j];
6006 }
6007 else /* array becomes smaller or 0 */
6008 {
6009 j = hisidx[type];
6010 for (i = newlen - 1; ; --i)
6011 {
6012 if (i >= 0) /* copy newest entries */
6013 temp[i] = history[type][j];
6014 else /* remove older entries */
6015 vim_free(history[type][j].hisstr);
6016 if (--j < 0)
6017 j = hislen - 1;
6018 if (j == hisidx[type])
6019 break;
6020 }
6021 hisidx[type] = newlen - 1;
6022 }
6023 vim_free(history[type]);
6024 history[type] = temp;
6025 }
6026 }
6027 hislen = newlen;
6028 }
6029}
6030
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006032clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006033{
6034 hisptr->hisnum = 0;
6035 hisptr->viminfo = FALSE;
6036 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006037 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006038}
6039
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040/*
6041 * Check if command line 'str' is already in history.
6042 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6043 */
6044 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006045in_history(
6046 int type,
6047 char_u *str,
6048 int move_to_front, /* Move the entry to the front if it exists */
6049 int sep,
6050 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 int i;
6053 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006054 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056 if (hisidx[type] < 0)
6057 return FALSE;
6058 i = hisidx[type];
6059 do
6060 {
6061 if (history[type][i].hisstr == NULL)
6062 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006063
6064 /* For search history, check that the separator character matches as
6065 * well. */
6066 p = history[type][i].hisstr;
6067 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006068 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006069 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {
6071 if (!move_to_front)
6072 return TRUE;
6073 last_i = i;
6074 break;
6075 }
6076 if (--i < 0)
6077 i = hislen - 1;
6078 } while (i != hisidx[type]);
6079
6080 if (last_i >= 0)
6081 {
6082 str = history[type][i].hisstr;
6083 while (i != hisidx[type])
6084 {
6085 if (++i >= hislen)
6086 i = 0;
6087 history[type][last_i] = history[type][i];
6088 last_i = i;
6089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006091 history[type][i].viminfo = FALSE;
6092 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006093 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 return TRUE;
6095 }
6096 return FALSE;
6097}
6098
6099/*
6100 * Convert history name (from table above) to its HIST_ equivalent.
6101 * When "name" is empty, return "cmd" history.
6102 * Returns -1 for unknown history name.
6103 */
6104 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006105get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
6107 int i;
6108 int len = (int)STRLEN(name);
6109
6110 /* No argument: use current history. */
6111 if (len == 0)
6112 return hist_char2type(ccline.cmdfirstc);
6113
6114 for (i = 0; history_names[i] != NULL; ++i)
6115 if (STRNICMP(name, history_names[i], len) == 0)
6116 return i;
6117
6118 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6119 return hist_char2type(name[0]);
6120
6121 return -1;
6122}
6123
6124static int last_maptick = -1; /* last seen maptick */
6125
6126/*
6127 * Add the given string to the given history. If the string is already in the
6128 * history then it is moved to the front. "histype" may be one of he HIST_
6129 * values.
6130 */
6131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006132add_to_history(
6133 int histype,
6134 char_u *new_entry,
6135 int in_map, /* consider maptick when inside a mapping */
6136 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 histentry_T *hisptr;
6139 int len;
6140
6141 if (hislen == 0) /* no history */
6142 return;
6143
Bram Moolenaara939e432013-11-09 05:30:26 +01006144 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6145 return;
6146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 /*
6148 * Searches inside the same mapping overwrite each other, so that only
6149 * the last line is kept. Be careful not to remove a line that was moved
6150 * down, only lines that were added.
6151 */
6152 if (histype == HIST_SEARCH && in_map)
6153 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006154 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
6156 /* Current line is from the same mapping, remove it */
6157 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6158 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006159 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 --hisnum[histype];
6161 if (--hisidx[HIST_SEARCH] < 0)
6162 hisidx[HIST_SEARCH] = hislen - 1;
6163 }
6164 last_maptick = -1;
6165 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006166 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 {
6168 if (++hisidx[histype] == hislen)
6169 hisidx[histype] = 0;
6170 hisptr = &history[histype][hisidx[histype]];
6171 vim_free(hisptr->hisstr);
6172
6173 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006174 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6176 if (hisptr->hisstr != NULL)
6177 hisptr->hisstr[len + 1] = sep;
6178
6179 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006180 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006181 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (histype == HIST_SEARCH && in_map)
6183 last_maptick = maptick;
6184 }
6185}
6186
6187#if defined(FEAT_EVAL) || defined(PROTO)
6188
6189/*
6190 * Get identifier of newest history entry.
6191 * "histype" may be one of the HIST_ values.
6192 */
6193 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006194get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6197 || hisidx[histype] < 0)
6198 return -1;
6199
6200 return history[histype][hisidx[histype]].hisnum;
6201}
6202
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 * Calculate history index from a number:
6205 * num > 0: seen as identifying number of a history entry
6206 * num < 0: relative position in history wrt newest entry
6207 * "histype" may be one of the HIST_ values.
6208 */
6209 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006210calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 int i;
6213 histentry_T *hist;
6214 int wrapped = FALSE;
6215
6216 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6217 || (i = hisidx[histype]) < 0 || num == 0)
6218 return -1;
6219
6220 hist = history[histype];
6221 if (num > 0)
6222 {
6223 while (hist[i].hisnum > num)
6224 if (--i < 0)
6225 {
6226 if (wrapped)
6227 break;
6228 i += hislen;
6229 wrapped = TRUE;
6230 }
6231 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6232 return i;
6233 }
6234 else if (-num <= hislen)
6235 {
6236 i += num + 1;
6237 if (i < 0)
6238 i += hislen;
6239 if (hist[i].hisstr != NULL)
6240 return i;
6241 }
6242 return -1;
6243}
6244
6245/*
6246 * Get a history entry by its index.
6247 * "histype" may be one of the HIST_ values.
6248 */
6249 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 idx = calc_hist_idx(histype, idx);
6253 if (idx >= 0)
6254 return history[histype][idx].hisstr;
6255 else
6256 return (char_u *)"";
6257}
6258
6259/*
6260 * Clear all entries of a history.
6261 * "histype" may be one of the HIST_ values.
6262 */
6263 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006264clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 int i;
6267 histentry_T *hisptr;
6268
6269 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6270 {
6271 hisptr = history[histype];
6272 for (i = hislen; i--;)
6273 {
6274 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006275 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006276 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 }
6278 hisidx[histype] = -1; /* mark history as cleared */
6279 hisnum[histype] = 0; /* reset identifier counter */
6280 return OK;
6281 }
6282 return FAIL;
6283}
6284
6285/*
6286 * Remove all entries matching {str} from a history.
6287 * "histype" may be one of the HIST_ values.
6288 */
6289 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006290del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 regmatch_T regmatch;
6293 histentry_T *hisptr;
6294 int idx;
6295 int i;
6296 int last;
6297 int found = FALSE;
6298
6299 regmatch.regprog = NULL;
6300 regmatch.rm_ic = FALSE; /* always match case */
6301 if (hislen != 0
6302 && histype >= 0
6303 && histype < HIST_COUNT
6304 && *str != NUL
6305 && (idx = hisidx[histype]) >= 0
6306 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6307 != NULL)
6308 {
6309 i = last = idx;
6310 do
6311 {
6312 hisptr = &history[histype][i];
6313 if (hisptr->hisstr == NULL)
6314 break;
6315 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6316 {
6317 found = TRUE;
6318 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006319 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 }
6321 else
6322 {
6323 if (i != last)
6324 {
6325 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006326 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
6328 if (--last < 0)
6329 last += hislen;
6330 }
6331 if (--i < 0)
6332 i += hislen;
6333 } while (i != idx);
6334 if (history[histype][idx].hisstr == NULL)
6335 hisidx[histype] = -1;
6336 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006337 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 return found;
6339}
6340
6341/*
6342 * Remove an indexed entry from a history.
6343 * "histype" may be one of the HIST_ values.
6344 */
6345 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006346del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 int i, j;
6349
6350 i = calc_hist_idx(histype, idx);
6351 if (i < 0)
6352 return FALSE;
6353 idx = hisidx[histype];
6354 vim_free(history[histype][i].hisstr);
6355
6356 /* When deleting the last added search string in a mapping, reset
6357 * last_maptick, so that the last added search string isn't deleted again.
6358 */
6359 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6360 last_maptick = -1;
6361
6362 while (i != idx)
6363 {
6364 j = (i + 1) % hislen;
6365 history[histype][i] = history[histype][j];
6366 i = j;
6367 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006368 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 if (--i < 0)
6370 i += hislen;
6371 hisidx[histype] = i;
6372 return TRUE;
6373}
6374
6375#endif /* FEAT_EVAL */
6376
6377#if defined(FEAT_CRYPT) || defined(PROTO)
6378/*
6379 * Very specific function to remove the value in ":set key=val" from the
6380 * history.
6381 */
6382 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006383remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 char_u *p;
6386 int i;
6387
6388 i = hisidx[HIST_CMD];
6389 if (i < 0)
6390 return;
6391 p = history[HIST_CMD][i].hisstr;
6392 if (p != NULL)
6393 for ( ; *p; ++p)
6394 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6395 {
6396 p = vim_strchr(p + 3, '=');
6397 if (p == NULL)
6398 break;
6399 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006400 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (p[i] == '\\' && p[i + 1])
6402 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006403 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 --p;
6405 }
6406}
6407#endif
6408
6409#endif /* FEAT_CMDHIST */
6410
Bram Moolenaar064154c2016-03-19 22:50:43 +01006411#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006412/*
6413 * Get pointer to the command line info to use. cmdline_paste() may clear
6414 * ccline and put the previous value in prev_ccline.
6415 */
6416 static struct cmdline_info *
6417get_ccline_ptr(void)
6418{
6419 if ((State & CMDLINE) == 0)
6420 return NULL;
6421 if (ccline.cmdbuff != NULL)
6422 return &ccline;
6423 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6424 return &prev_ccline;
6425 return NULL;
6426}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006427#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006428
Bram Moolenaar064154c2016-03-19 22:50:43 +01006429#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006430/*
6431 * Get the current command line in allocated memory.
6432 * Only works when the command line is being edited.
6433 * Returns NULL when something is wrong.
6434 */
6435 char_u *
6436get_cmdline_str(void)
6437{
6438 struct cmdline_info *p = get_ccline_ptr();
6439
6440 if (p == NULL)
6441 return NULL;
6442 return vim_strnsave(p->cmdbuff, p->cmdlen);
6443}
6444
6445/*
6446 * Get the current command line position, counted in bytes.
6447 * Zero is the first position.
6448 * Only works when the command line is being edited.
6449 * Returns -1 when something is wrong.
6450 */
6451 int
6452get_cmdline_pos(void)
6453{
6454 struct cmdline_info *p = get_ccline_ptr();
6455
6456 if (p == NULL)
6457 return -1;
6458 return p->cmdpos;
6459}
6460
6461/*
6462 * Set the command line byte position to "pos". Zero is the first position.
6463 * Only works when the command line is being edited.
6464 * Returns 1 when failed, 0 when OK.
6465 */
6466 int
6467set_cmdline_pos(
6468 int pos)
6469{
6470 struct cmdline_info *p = get_ccline_ptr();
6471
6472 if (p == NULL)
6473 return 1;
6474
6475 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6476 * changed the command line. */
6477 if (pos < 0)
6478 new_cmdpos = 0;
6479 else
6480 new_cmdpos = pos;
6481 return 0;
6482}
6483#endif
6484
6485#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6486/*
6487 * Get the current command-line type.
6488 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6489 * Only works when the command line is being edited.
6490 * Returns NUL when something is wrong.
6491 */
6492 int
6493get_cmdline_type(void)
6494{
6495 struct cmdline_info *p = get_ccline_ptr();
6496
6497 if (p == NULL)
6498 return NUL;
6499 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006500 return
6501# ifdef FEAT_EVAL
6502 (p->input_fn) ? '@' :
6503# endif
6504 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006505 return p->cmdfirstc;
6506}
6507#endif
6508
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6510/*
6511 * Get indices "num1,num2" that specify a range within a list (not a range of
6512 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6513 * Returns OK if parsed successfully, otherwise FAIL.
6514 */
6515 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006516get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 int len;
6519 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006520 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
6522 *str = skipwhite(*str);
6523 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6524 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006525 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 *str += len;
6527 *num1 = (int)num;
6528 first = TRUE;
6529 }
6530 *str = skipwhite(*str);
6531 if (**str == ',') /* parse "to" part of range */
6532 {
6533 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006534 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (len > 0)
6536 {
6537 *num2 = (int)num;
6538 *str = skipwhite(*str + len);
6539 }
6540 else if (!first) /* no number given at all */
6541 return FAIL;
6542 }
6543 else if (first) /* only one number given */
6544 *num2 = *num1;
6545 return OK;
6546}
6547#endif
6548
6549#if defined(FEAT_CMDHIST) || defined(PROTO)
6550/*
6551 * :history command - print a history
6552 */
6553 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006554ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 histentry_T *hist;
6557 int histype1 = HIST_CMD;
6558 int histype2 = HIST_CMD;
6559 int hisidx1 = 1;
6560 int hisidx2 = -1;
6561 int idx;
6562 int i, j, k;
6563 char_u *end;
6564 char_u *arg = eap->arg;
6565
6566 if (hislen == 0)
6567 {
6568 MSG(_("'history' option is zero"));
6569 return;
6570 }
6571
6572 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6573 {
6574 end = arg;
6575 while (ASCII_ISALPHA(*end)
6576 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6577 end++;
6578 i = *end;
6579 *end = NUL;
6580 histype1 = get_histtype(arg);
6581 if (histype1 == -1)
6582 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006583 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 {
6585 histype1 = 0;
6586 histype2 = HIST_COUNT-1;
6587 }
6588 else
6589 {
6590 *end = i;
6591 EMSG(_(e_trailing));
6592 return;
6593 }
6594 }
6595 else
6596 histype2 = histype1;
6597 *end = i;
6598 }
6599 else
6600 end = arg;
6601 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6602 {
6603 EMSG(_(e_trailing));
6604 return;
6605 }
6606
6607 for (; !got_int && histype1 <= histype2; ++histype1)
6608 {
6609 STRCPY(IObuff, "\n # ");
6610 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6611 MSG_PUTS_TITLE(IObuff);
6612 idx = hisidx[histype1];
6613 hist = history[histype1];
6614 j = hisidx1;
6615 k = hisidx2;
6616 if (j < 0)
6617 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6618 if (k < 0)
6619 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6620 if (idx >= 0 && j <= k)
6621 for (i = idx + 1; !got_int; ++i)
6622 {
6623 if (i == hislen)
6624 i = 0;
6625 if (hist[i].hisstr != NULL
6626 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6627 {
6628 msg_putchar('\n');
6629 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6630 hist[i].hisnum);
6631 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6632 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006633 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 else
6635 STRCAT(IObuff, hist[i].hisstr);
6636 msg_outtrans(IObuff);
6637 out_flush();
6638 }
6639 if (i == idx)
6640 break;
6641 }
6642 }
6643}
6644#endif
6645
6646#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006647/*
6648 * Buffers for history read from a viminfo file. Only valid while reading.
6649 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006650static histentry_T *viminfo_history[HIST_COUNT] =
6651 {NULL, NULL, NULL, NULL, NULL};
6652static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6653static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654static int viminfo_add_at_front = FALSE;
6655
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006656static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
6658/*
6659 * Translate a history type number to the associated character.
6660 */
6661 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006662hist_type2char(
6663 int type,
6664 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 if (type == HIST_CMD)
6667 return ':';
6668 if (type == HIST_SEARCH)
6669 {
6670 if (use_question)
6671 return '?';
6672 else
6673 return '/';
6674 }
6675 if (type == HIST_EXPR)
6676 return '=';
6677 return '@';
6678}
6679
6680/*
6681 * Prepare for reading the history from the viminfo file.
6682 * This allocates history arrays to store the read history lines.
6683 */
6684 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006685prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 int i;
6688 int num;
6689 int type;
6690 int len;
6691
6692 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006693 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 if (asklen > hislen)
6695 asklen = hislen;
6696
6697 for (type = 0; type < HIST_COUNT; ++type)
6698 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006699 /* Count the number of empty spaces in the history list. Entries read
6700 * from viminfo previously are also considered empty. If there are
6701 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006703 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 num++;
6705 len = asklen;
6706 if (num > len)
6707 len = num;
6708 if (len <= 0)
6709 viminfo_history[type] = NULL;
6710 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006711 viminfo_history[type] = (histentry_T *)lalloc(
6712 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (viminfo_history[type] == NULL)
6714 len = 0;
6715 viminfo_hislen[type] = len;
6716 viminfo_hisidx[type] = 0;
6717 }
6718}
6719
6720/*
6721 * Accept a line from the viminfo, store it in the history array when it's
6722 * new.
6723 */
6724 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006725read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 int type;
6728 long_u len;
6729 char_u *val;
6730 char_u *p;
6731
6732 type = hist_char2type(virp->vir_line[0]);
6733 if (viminfo_hisidx[type] < viminfo_hislen[type])
6734 {
6735 val = viminfo_readstring(virp, 1, TRUE);
6736 if (val != NULL && *val != NUL)
6737 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006738 int sep = (*val == ' ' ? NUL : *val);
6739
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006741 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
6743 /* Need to re-allocate to append the separator byte. */
6744 len = STRLEN(val);
6745 p = lalloc(len + 2, TRUE);
6746 if (p != NULL)
6747 {
6748 if (type == HIST_SEARCH)
6749 {
6750 /* Search entry: Move the separator from the first
6751 * column to after the NUL. */
6752 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006753 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 }
6755 else
6756 {
6757 /* Not a search entry: No separator in the viminfo
6758 * file, add a NUL separator. */
6759 mch_memmove(p, val, (size_t)len + 1);
6760 p[len + 1] = NUL;
6761 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006762 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6763 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006764 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6765 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006766 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
6768 }
6769 }
6770 vim_free(val);
6771 }
6772 return viminfo_readline(virp);
6773}
6774
Bram Moolenaar07219f92013-04-14 23:19:36 +02006775/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006776 * Accept a new style history line from the viminfo, store it in the history
6777 * array when it's new.
6778 */
6779 void
6780handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006781 garray_T *values,
6782 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006783{
6784 int type;
6785 long_u len;
6786 char_u *val;
6787 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006788 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006789
6790 /* Check the format:
6791 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006792 if (values->ga_len < 4
6793 || vp[0].bv_type != BVAL_NR
6794 || vp[1].bv_type != BVAL_NR
6795 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6796 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006797 return;
6798
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006799 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006800 if (type >= HIST_COUNT)
6801 return;
6802 if (viminfo_hisidx[type] < viminfo_hislen[type])
6803 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006804 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006805 if (val != NULL && *val != NUL)
6806 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006807 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6808 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006809 int idx;
6810 int overwrite = FALSE;
6811
6812 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6813 {
6814 /* If lines were written by an older Vim we need to avoid
6815 * getting duplicates. See if the entry already exists. */
6816 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6817 {
6818 p = viminfo_history[type][idx].hisstr;
6819 if (STRCMP(val, p) == 0
6820 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6821 {
6822 overwrite = TRUE;
6823 break;
6824 }
6825 }
6826
6827 if (!overwrite)
6828 {
6829 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006830 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006831 p = lalloc(len + 2, TRUE);
6832 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006833 else
6834 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006835 if (p != NULL)
6836 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006837 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006838 if (!overwrite)
6839 {
6840 mch_memmove(p, val, (size_t)len + 1);
6841 /* Put the separator after the NUL. */
6842 p[len + 1] = sep;
6843 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006844 viminfo_history[type][idx].hisnum = 0;
6845 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006846 viminfo_hisidx[type]++;
6847 }
6848 }
6849 }
6850 }
6851 }
6852}
6853
6854/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006855 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006856 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006857 static void
6858concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 int idx;
6861 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006862
6863 idx = hisidx[type] + viminfo_hisidx[type];
6864 if (idx >= hislen)
6865 idx -= hislen;
6866 else if (idx < 0)
6867 idx = hislen - 1;
6868 if (viminfo_add_at_front)
6869 hisidx[type] = idx;
6870 else
6871 {
6872 if (hisidx[type] == -1)
6873 hisidx[type] = hislen - 1;
6874 do
6875 {
6876 if (history[type][idx].hisstr != NULL
6877 || history[type][idx].viminfo)
6878 break;
6879 if (++idx == hislen)
6880 idx = 0;
6881 } while (idx != hisidx[type]);
6882 if (idx != hisidx[type] && --idx < 0)
6883 idx = hislen - 1;
6884 }
6885 for (i = 0; i < viminfo_hisidx[type]; i++)
6886 {
6887 vim_free(history[type][idx].hisstr);
6888 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6889 history[type][idx].viminfo = TRUE;
6890 history[type][idx].time_set = viminfo_history[type][i].time_set;
6891 if (--idx < 0)
6892 idx = hislen - 1;
6893 }
6894 idx += 1;
6895 idx %= hislen;
6896 for (i = 0; i < viminfo_hisidx[type]; i++)
6897 {
6898 history[type][idx++].hisnum = ++hisnum[type];
6899 idx %= hislen;
6900 }
6901}
6902
6903#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6904 static int
6905#ifdef __BORLANDC__
6906_RTLENTRYF
6907#endif
6908sort_hist(const void *s1, const void *s2)
6909{
6910 histentry_T *p1 = *(histentry_T **)s1;
6911 histentry_T *p2 = *(histentry_T **)s2;
6912
6913 if (p1->time_set < p2->time_set) return -1;
6914 if (p1->time_set > p2->time_set) return 1;
6915 return 0;
6916}
6917#endif
6918
6919/*
6920 * Merge history lines from viminfo and lines typed in this Vim based on the
6921 * timestamp;
6922 */
6923 static void
6924merge_history(int type)
6925{
6926 int max_len;
6927 histentry_T **tot_hist;
6928 histentry_T *new_hist;
6929 int i;
6930 int len;
6931
6932 /* Make one long list with all entries. */
6933 max_len = hislen + viminfo_hisidx[type];
6934 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6935 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6936 if (tot_hist == NULL || new_hist == NULL)
6937 {
6938 vim_free(tot_hist);
6939 vim_free(new_hist);
6940 return;
6941 }
6942 for (i = 0; i < viminfo_hisidx[type]; i++)
6943 tot_hist[i] = &viminfo_history[type][i];
6944 len = i;
6945 for (i = 0; i < hislen; i++)
6946 if (history[type][i].hisstr != NULL)
6947 tot_hist[len++] = &history[type][i];
6948
6949 /* Sort the list on timestamp. */
6950 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6951
6952 /* Keep the newest ones. */
6953 for (i = 0; i < hislen; i++)
6954 {
6955 if (i < len)
6956 {
6957 new_hist[i] = *tot_hist[i];
6958 tot_hist[i]->hisstr = NULL;
6959 if (new_hist[i].hisnum == 0)
6960 new_hist[i].hisnum = ++hisnum[type];
6961 }
6962 else
6963 clear_hist_entry(&new_hist[i]);
6964 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006965 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006966
6967 /* Free what is not kept. */
6968 for (i = 0; i < viminfo_hisidx[type]; i++)
6969 vim_free(viminfo_history[type][i].hisstr);
6970 for (i = 0; i < hislen; i++)
6971 vim_free(history[type][i].hisstr);
6972 vim_free(history[type]);
6973 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006974 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006975}
6976
6977/*
6978 * Finish reading history lines from viminfo. Not used when writing viminfo.
6979 */
6980 void
6981finish_viminfo_history(vir_T *virp)
6982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006984 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 for (type = 0; type < HIST_COUNT; ++type)
6987 {
6988 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006989 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006990
6991 if (merge)
6992 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006994 concat_history(type);
6995
Bram Moolenaard23a8232018-02-10 18:45:26 +01006996 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006997 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 }
6999}
7000
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007001/*
7002 * Write history to viminfo file in "fp".
7003 * When "merge" is TRUE merge history lines with a previously read viminfo
7004 * file, data is in viminfo_history[].
7005 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007008write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 int i;
7011 int type;
7012 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007013 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
7015 init_history();
7016 if (hislen == 0)
7017 return;
7018 for (type = 0; type < HIST_COUNT; ++type)
7019 {
7020 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7021 if (num_saved == 0)
7022 continue;
7023 if (num_saved < 0) /* Use default */
7024 num_saved = hislen;
7025 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7026 type == HIST_CMD ? _("Command Line") :
7027 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007028 type == HIST_EXPR ? _("Expression") :
7029 type == HIST_INPUT ? _("Input Line") :
7030 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (num_saved > hislen)
7032 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007033
7034 /*
7035 * Merge typed and viminfo history:
7036 * round 1: history of typed commands.
7037 * round 2: history from recently read viminfo.
7038 */
7039 for (round = 1; round <= 2; ++round)
7040 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007041 if (round == 1)
7042 /* start at newest entry, somewhere in the list */
7043 i = hisidx[type];
7044 else if (viminfo_hisidx[type] > 0)
7045 /* start at newest entry, first in the list */
7046 i = 0;
7047 else
7048 /* empty list */
7049 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007050 if (i >= 0)
7051 while (num_saved > 0
7052 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007054 char_u *p;
7055 time_t timestamp;
7056 int c = NUL;
7057
7058 if (round == 1)
7059 {
7060 p = history[type][i].hisstr;
7061 timestamp = history[type][i].time_set;
7062 }
7063 else
7064 {
7065 p = viminfo_history[type] == NULL ? NULL
7066 : viminfo_history[type][i].hisstr;
7067 timestamp = viminfo_history[type] == NULL ? 0
7068 : viminfo_history[type][i].time_set;
7069 }
7070
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007071 if (p != NULL && (round == 2
7072 || !merge
7073 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007075 --num_saved;
7076 fputc(hist_type2char(type, TRUE), fp);
7077 /* For the search history: put the separator in the
7078 * second column; use a space if there isn't one. */
7079 if (type == HIST_SEARCH)
7080 {
7081 c = p[STRLEN(p) + 1];
7082 putc(c == NUL ? ' ' : c, fp);
7083 }
7084 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007085
7086 {
7087 char cbuf[NUMBUFLEN];
7088
7089 /* New style history with a bar line. Format:
7090 * |{bartype},{histtype},{timestamp},{separator},"text" */
7091 if (c == NUL)
7092 cbuf[0] = NUL;
7093 else
7094 sprintf(cbuf, "%d", c);
7095 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7096 type, (long)timestamp, cbuf);
7097 barline_writestring(fp, p, LSIZE - 20);
7098 putc('\n', fp);
7099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007101 if (round == 1)
7102 {
7103 /* Decrement index, loop around and stop when back at
7104 * the start. */
7105 if (--i < 0)
7106 i = hislen - 1;
7107 if (i == hisidx[type])
7108 break;
7109 }
7110 else
7111 {
7112 /* Increment index. Stop at the end in the while. */
7113 ++i;
7114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007116 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007117 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007118 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007119 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007120 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007121 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123}
7124#endif /* FEAT_VIMINFO */
7125
7126#if defined(FEAT_FKMAP) || defined(PROTO)
7127/*
7128 * Write a character at the current cursor+offset position.
7129 * It is directly written into the command buffer block.
7130 */
7131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007132cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7135 {
7136 EMSG(_("E198: cmd_pchar beyond the command length"));
7137 return;
7138 }
7139 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7140 ccline.cmdbuff[ccline.cmdlen] = NUL;
7141}
7142
7143 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007144cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
7146 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7147 {
7148 /* EMSG(_("cmd_gchar beyond the command length")); */
7149 return NUL;
7150 }
7151 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7152}
7153#endif
7154
7155#if defined(FEAT_CMDWIN) || defined(PROTO)
7156/*
7157 * Open a window on the current command line and history. Allow editing in
7158 * the window. Returns when the window is closed.
7159 * Returns:
7160 * CR if the command is to be executed
7161 * Ctrl_C if it is to be abandoned
7162 * K_IGNORE if editing continues
7163 */
7164 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007165open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007168 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007170 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 win_T *wp;
7172 int i;
7173 linenr_T lnum;
7174 int histtype;
7175 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 int save_restart_edit = restart_edit;
7177 int save_State = State;
7178 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007179#ifdef FEAT_RIGHTLEFT
7180 int save_cmdmsg_rl = cmdmsg_rl;
7181#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007182#ifdef FEAT_FOLDING
7183 int save_KeyTyped;
7184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
7186 /* Can't do this recursively. Can't do it when typing a password. */
7187 if (cmdwin_type != 0
7188# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7189 || cmdline_star > 0
7190# endif
7191 )
7192 {
7193 beep_flush();
7194 return K_IGNORE;
7195 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007196 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198 /* Save current window sizes. */
7199 win_size_save(&winsizes);
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007202 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007203
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007204 /* don't use a new tab page */
7205 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007206 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 /* Create a window for the command-line buffer. */
7209 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7210 {
7211 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007212 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 return K_IGNORE;
7214 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007215 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007218 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007219 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007222#ifdef FEAT_FOLDING
7223 curwin->w_p_fen = FALSE;
7224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007226 curwin->w_p_rl = cmdmsg_rl;
7227 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007229 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007232 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007233 /* But don't allow switching to another buffer. */
7234 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
Bram Moolenaar46152342005-09-07 21:18:43 +00007236 /* Showing the prompt may have set need_wait_return, reset it. */
7237 need_wait_return = FALSE;
7238
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007239 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7241 {
7242 if (p_wc == TAB)
7243 {
7244 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7245 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7246 }
7247 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7248 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007249 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007251 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7252 * sets 'textwidth' to 78). */
7253 curbuf->b_p_tw = 0;
7254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 /* Fill the buffer with the history. */
7256 init_history();
7257 if (hislen > 0)
7258 {
7259 i = hisidx[histtype];
7260 if (i >= 0)
7261 {
7262 lnum = 0;
7263 do
7264 {
7265 if (++i == hislen)
7266 i = 0;
7267 if (history[histtype][i].hisstr != NULL)
7268 ml_append(lnum++, history[histtype][i].hisstr,
7269 (colnr_T)0, FALSE);
7270 }
7271 while (i != hisidx[histtype]);
7272 }
7273 }
7274
7275 /* Replace the empty last line with the current command-line and put the
7276 * cursor there. */
7277 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7278 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7279 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007280 changed_line_abv_curs();
7281 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007282 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007285 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
7287 /* No Ex mode here! */
7288 exmode_active = 0;
7289
7290 State = NORMAL;
7291# ifdef FEAT_MOUSE
7292 setmouse();
7293# endif
7294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007296 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007297 if (restart_edit != 0) /* autocmd with ":startinsert" */
7298 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299
7300 i = RedrawingDisabled;
7301 RedrawingDisabled = 0;
7302
7303 /*
7304 * Call the main loop until <CR> or CTRL-C is typed.
7305 */
7306 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007307 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
7309 RedrawingDisabled = i;
7310
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007311# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007312 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007313# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007316 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007317
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007318# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007319 /* Restore KeyTyped in case it is modified by autocommands */
7320 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321# endif
7322
Bram Moolenaarccc18222007-05-10 18:25:20 +00007323 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007324 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 cmdwin_type = 0;
7326
7327 exmode_active = save_exmode;
7328
Bram Moolenaarf9821062008-06-20 16:31:07 +00007329 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007331 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
7333 cmdwin_result = Ctrl_C;
7334 EMSG(_("E199: Active window or buffer deleted"));
7335 }
7336 else
7337 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007338# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 /* autocmds may abort script processing */
7340 if (aborting() && cmdwin_result != K_IGNORE)
7341 cmdwin_result = Ctrl_C;
7342# endif
7343 /* Set the new command line from the cmdline buffer. */
7344 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007345 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007347 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7348
7349 if (histtype == HIST_CMD)
7350 {
7351 /* Execute the command directly. */
7352 ccline.cmdbuff = vim_strsave((char_u *)p);
7353 cmdwin_result = CAR;
7354 }
7355 else
7356 {
7357 /* First need to cancel what we were doing. */
7358 ccline.cmdbuff = NULL;
7359 stuffcharReadbuff(':');
7360 stuffReadbuff((char_u *)p);
7361 stuffcharReadbuff(CAR);
7362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 }
7364 else if (cmdwin_result == K_XF2) /* :qa typed */
7365 {
7366 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7367 cmdwin_result = CAR;
7368 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007369 else if (cmdwin_result == Ctrl_C)
7370 {
7371 /* :q or :close, don't execute any command
7372 * and don't modify the cmd window. */
7373 ccline.cmdbuff = NULL;
7374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 else
7376 ccline.cmdbuff = vim_strsave(ml_get_curline());
7377 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007378 {
7379 ccline.cmdbuff = vim_strsave((char_u *)"");
7380 ccline.cmdlen = 0;
7381 ccline.cmdbufflen = 1;
7382 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 else
7386 {
7387 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7388 ccline.cmdbufflen = ccline.cmdlen + 1;
7389 ccline.cmdpos = curwin->w_cursor.col;
7390 if (ccline.cmdpos > ccline.cmdlen)
7391 ccline.cmdpos = ccline.cmdlen;
7392 if (cmdwin_result == K_IGNORE)
7393 {
7394 set_cmdspos_cursor();
7395 redrawcmd();
7396 }
7397 }
7398
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007400 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007401# ifdef FEAT_CONCEAL
7402 /* Avoid command-line window first character being concealed. */
7403 curwin->w_p_cole = 0;
7404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007406 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 win_goto(old_curwin);
7408 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007409
7410 /* win_close() may have already wiped the buffer when 'bh' is
7411 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007412 if (bufref_valid(&bufref))
7413 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415 /* Restore window sizes. */
7416 win_size_restore(&winsizes);
7417
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007418 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 }
7420
7421 ga_clear(&winsizes);
7422 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007423# ifdef FEAT_RIGHTLEFT
7424 cmdmsg_rl = save_cmdmsg_rl;
7425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
7427 State = save_State;
7428# ifdef FEAT_MOUSE
7429 setmouse();
7430# endif
7431
7432 return cmdwin_result;
7433}
7434#endif /* FEAT_CMDWIN */
7435
7436/*
7437 * Used for commands that either take a simple command string argument, or:
7438 * cmd << endmarker
7439 * {script}
7440 * endmarker
7441 * Returns a pointer to allocated memory with {script} or NULL.
7442 */
7443 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007444script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 char_u *theline;
7447 char *end_pattern = NULL;
7448 char dot[] = ".";
7449 garray_T ga;
7450
7451 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7452 return NULL;
7453
7454 ga_init2(&ga, 1, 0x400);
7455
7456 if (cmd[2] != NUL)
7457 end_pattern = (char *)skipwhite(cmd + 2);
7458 else
7459 end_pattern = dot;
7460
7461 for (;;)
7462 {
7463 theline = eap->getline(
7464#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007465 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466#endif
7467 NUL, eap->cookie, 0);
7468
7469 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007470 {
7471 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475 ga_concat(&ga, theline);
7476 ga_append(&ga, '\n');
7477 vim_free(theline);
7478 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007479 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
7481 return (char_u *)ga.ga_data;
7482}