blob: bce4aac79049a1019eba02adad8dcd74b5250a65 [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 Moolenaar264cf5c2018-08-18 21:05:31 +0200310 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v' || *cmd == 'l')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200311 {
312 // Skip over "substitute" to find the pattern separator.
313 for (p = cmd; ASCII_ISALPHA(*p); ++p)
314 ;
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200315 if (*skipwhite(p) != NUL)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200316 {
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200317 if (STRNCMP(cmd, "substitute", p - cmd) == 0
318 || STRNCMP(cmd, "smagic", p - cmd) == 0
319 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
320 || STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0
321 || STRNCMP(cmd, "global", p - cmd) == 0
322 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200323 {
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200324 if (*cmd == 's' && cmd[1] == 'm')
325 p_magic = TRUE;
326 else if (*cmd == 's' && cmd[1] == 'n')
327 p_magic = FALSE;
328
329 // Check for "global!/".
330 if (*cmd == 'g' && *p == '!')
331 {
332 p++;
333 if (*skipwhite(p) == NUL)
334 return FALSE;
335 }
336
337 // For ":sort" skip over flags.
338 if (cmd[0] == 's' && cmd[1] == 'o')
339 {
340 while (ASCII_ISALPHA(*(p = skipwhite(p))))
341 ++p;
342 if (*p == NUL)
343 return FALSE;
344 }
345
346 p = skipwhite(p);
347 delim = *p++;
348 end = skip_regexp(p, delim, p_magic, NULL);
349 }
350 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
351 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
352 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
353 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0)
354 {
355 // Check for "!/".
356 if (*p == '!')
357 {
358 p++;
359 if (*skipwhite(p) == NUL)
360 return FALSE;
361 }
362 p = skipwhite(p);
363 delim = (vim_isIDc(*p)) ? ' ' : *p++;
364 end = skip_regexp(p, delim, p_magic, NULL);
365 }
366 else
367 {
368 end = p;
369 delim = -1;
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200370 }
Bram Moolenaar81f56532018-08-18 16:19:42 +0200371
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200372 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200373 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200374 pos_T save_cursor = curwin->w_cursor;
375
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200376 // found a non-empty pattern or //
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200377 *skiplen = (int)(p - ccline.cmdbuff);
378 *patlen = (int)(end - p);
379
380 // parse the address range
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200381 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200382 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200383 if (ea.addr_count > 0)
384 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200385 // Allow for reverse match.
386 if (ea.line2 < ea.line1)
387 {
388 search_first_line = ea.line2;
389 search_last_line = ea.line1;
390 }
391 else
392 {
393 search_first_line = ea.line1;
394 search_last_line = ea.line2;
395 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200396 }
Bram Moolenaar81f56532018-08-18 16:19:42 +0200397 else if (cmd[0] == 's' && cmd[1] != 'o')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200398 {
399 // :s defaults to the current line
400 search_first_line = curwin->w_cursor.lnum;
401 search_last_line = curwin->w_cursor.lnum;
402 }
403
404 curwin->w_cursor = save_cursor;
405 return TRUE;
406 }
407 }
408 }
409 }
410 }
411
412 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200413}
414
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200415 static void
416finish_incsearch_highlighting(
417 int gotesc,
418 incsearch_state_T *is_state,
419 int call_update_screen)
420{
421 if (is_state->did_incsearch)
422 {
423 is_state->did_incsearch = FALSE;
424 if (gotesc)
425 curwin->w_cursor = is_state->save_cursor;
426 else
427 {
428 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
429 {
430 // put the '" mark at the original position
431 curwin->w_cursor = is_state->save_cursor;
432 setpcmark();
433 }
434 curwin->w_cursor = is_state->search_start;
435 }
436 restore_viewstate(&is_state->old_viewstate);
437 highlight_match = FALSE;
438 validate_cursor(); /* needed for TAB */
439 if (call_update_screen)
440 update_screen(SOME_VALID);
441 else
442 redraw_all_later(SOME_VALID);
Bram Moolenaar167ae422018-08-14 21:32:21 +0200443 p_magic = is_state->magic_save;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200444 }
445}
446
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200447/*
448 * Do 'incsearch' highlighting if desired.
449 */
450 static void
451may_do_incsearch_highlighting(
452 int firstc,
453 long count,
454 incsearch_state_T *is_state)
455{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200456 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457 int i;
458 pos_T end_pos;
459 struct cmdline_info save_ccline;
460#ifdef FEAT_RELTIME
461 proftime_T tm;
462#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200463 int next_char;
464 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200465
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200466 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200467 {
468 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200470 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200471
472 // If there is a character waiting, search and redraw later.
473 if (char_avail())
474 {
475 is_state->incsearch_postponed = TRUE;
476 return;
477 }
478 is_state->incsearch_postponed = FALSE;
479
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200480 if (search_first_line == 0)
481 // start at the original cursor position
482 curwin->w_cursor = is_state->search_start;
483 else
484 {
485 // start at the first line in the range
486 curwin->w_cursor.lnum = search_first_line;
487 curwin->w_cursor.col = 0;
488 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200489 save_last_search_pattern();
490
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200491 // Use the previous pattern for ":s//".
492 next_char = ccline.cmdbuff[skiplen + patlen];
493 use_last_pat = patlen == 0 && skiplen > 0
494 && ccline.cmdbuff[skiplen - 1] == next_char;
495
496 // If there is no pattern, don't do anything.
497 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200498 {
499 i = 0;
500 set_no_hlsearch(TRUE); // turn off previous highlight
501 redraw_all_later(SOME_VALID);
502 }
503 else
504 {
505 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
506
507 cursor_off(); // so the user knows we're busy
508 out_flush();
509 ++emsg_off; // so it doesn't beep if bad expr
510#ifdef FEAT_RELTIME
511 // Set the time limit to half a second.
512 profile_setlimit(500L, &tm);
513#endif
514 if (!p_hls)
515 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200516 if (search_first_line != 0)
517 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200518 ccline.cmdbuff[skiplen + patlen] = NUL;
519 i = do_search(NULL, firstc == ':' ? '/' : firstc,
520 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200521#ifdef FEAT_RELTIME
522 &tm, NULL
523#else
524 NULL, NULL
525#endif
526 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200527 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200528 --emsg_off;
529
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200530 if (curwin->w_cursor.lnum < search_first_line
531 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200532 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200533 // match outside of address range
534 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200535 curwin->w_cursor = is_state->search_start;
536 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200537
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200538 // if interrupted while searching, behave like it failed
539 if (got_int)
540 {
541 (void)vpeekc(); // remove <C-C> from input stream
542 got_int = FALSE; // don't abandon the command line
543 i = 0;
544 }
545 else if (char_avail())
546 // cancelled searching because a char was typed
547 is_state->incsearch_postponed = TRUE;
548 }
549 if (i != 0)
550 highlight_match = TRUE; // highlight position
551 else
552 highlight_match = FALSE; // remove highlight
553
554 // First restore the old curwin values, so the screen is positioned in the
555 // same way as the actual search command.
556 restore_viewstate(&is_state->old_viewstate);
557 changed_cline_bef_curs();
558 update_topline();
559
560 if (i != 0)
561 {
562 pos_T save_pos = curwin->w_cursor;
563
564 is_state->match_start = curwin->w_cursor;
565 set_search_match(&curwin->w_cursor);
566 validate_cursor();
567 end_pos = curwin->w_cursor;
568 is_state->match_end = end_pos;
569 curwin->w_cursor = save_pos;
570 }
571 else
572 end_pos = curwin->w_cursor; // shutup gcc 4
573
574 // Disable 'hlsearch' highlighting if the pattern matches everything.
575 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200576 if (!use_last_pat)
577 {
578 next_char = ccline.cmdbuff[skiplen + patlen];
579 ccline.cmdbuff[skiplen + patlen] = NUL;
580 if (empty_pattern(ccline.cmdbuff))
581 set_no_hlsearch(TRUE);
582 ccline.cmdbuff[skiplen + patlen] = next_char;
583 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200584
585 validate_cursor();
586 // May redraw the status line to show the cursor position.
587 if (p_ru && curwin->w_status_height > 0)
588 curwin->w_redr_status = TRUE;
589
590 save_cmdline(&save_ccline);
591 update_screen(SOME_VALID);
592 restore_cmdline(&save_ccline);
593 restore_last_search_pattern();
594
595 // Leave it at the end to make CTRL-R CTRL-W work.
596 if (i != 0)
597 curwin->w_cursor = end_pos;
598
599 msg_starthere();
600 redrawcmdline();
601 is_state->did_incsearch = TRUE;
602}
603
604/*
605 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
606 * or previous match.
607 * Returns FAIL when jumping to cmdline_not_changed;
608 */
609 static int
610may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200611 int firstc,
612 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200613 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200614 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200615{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200616 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200617 pos_T t;
618 char_u *pat;
619 int search_flags = SEARCH_NOOF;
620 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200623 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200625 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200626 return FAIL;
627
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200629 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200630 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200631 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200632 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200633 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200634 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200635 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200636
637 save_last_search_pattern();
638 cursor_off();
639 out_flush();
640 if (c == Ctrl_G)
641 {
642 t = is_state->match_end;
643 if (LT_POS(is_state->match_start, is_state->match_end))
644 // Start searching at the end of the match not at the beginning of
645 // the next column.
646 (void)decl(&t);
647 search_flags += SEARCH_COL;
648 }
649 else
650 t = is_state->match_start;
651 if (!p_hls)
652 search_flags += SEARCH_KEEP;
653 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200654 save = pat[patlen];
655 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200656 i = searchit(curwin, curbuf, &t,
657 c == Ctrl_G ? FORWARD : BACKWARD,
658 pat, count, search_flags,
659 RE_SEARCH, 0, NULL, NULL);
660 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200661 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200662 if (i)
663 {
664 is_state->search_start = is_state->match_start;
665 is_state->match_end = t;
666 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200667 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200668 {
669 // Move just before the current match, so that when nv_search
670 // finishes the cursor will be put back on the match.
671 is_state->search_start = t;
672 (void)decl(&is_state->search_start);
673 }
674 else if (c == Ctrl_G && firstc == '?')
675 {
676 // Move just after the current match, so that when nv_search
677 // finishes the cursor will be put back on the match.
678 is_state->search_start = t;
679 (void)incl(&is_state->search_start);
680 }
681 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
682 {
683 // wrap around
684 is_state->search_start = t;
685 if (firstc == '?')
686 (void)incl(&is_state->search_start);
687 else
688 (void)decl(&is_state->search_start);
689 }
690
691 set_search_match(&is_state->match_end);
692 curwin->w_cursor = is_state->match_start;
693 changed_cline_bef_curs();
694 update_topline();
695 validate_cursor();
696 highlight_match = TRUE;
697 save_viewstate(&is_state->old_viewstate);
698 update_screen(NOT_VALID);
699 redrawcmdline();
700 }
701 else
702 vim_beep(BO_ERROR);
703 restore_last_search_pattern();
704 return FAIL;
705}
706
707/*
708 * When CTRL-L typed: add character from the match to the pattern.
709 * May set "*c" to the added character.
710 * Return OK when jumping to cmdline_not_changed.
711 */
712 static int
713may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
714{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200715 int skiplen, patlen;
716
717 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200718 return FAIL;
719
720 // Add a character from under the cursor for 'incsearch'.
721 if (is_state->did_incsearch)
722 {
723 curwin->w_cursor = is_state->match_end;
724 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
725 {
726 *c = gchar_cursor();
727
728 // If 'ignorecase' and 'smartcase' are set and the
729 // command line has no uppercase characters, convert
730 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200731 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200732 *c = MB_TOLOWER(*c);
733 if (*c != NUL)
734 {
735 if (*c == firstc || vim_strchr((char_u *)(
736 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
737 {
738 // put a backslash before special characters
739 stuffcharReadbuff(*c);
740 *c = '\\';
741 }
742 return FAIL;
743 }
744 }
745 }
746 return OK;
747}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200748#endif
749
Bram Moolenaar66216052017-12-16 16:33:44 +0100750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 * getcmdline() - accept a command line starting with firstc.
752 *
753 * firstc == ':' get ":" command line.
754 * firstc == '/' or '?' get search pattern
755 * firstc == '=' get expression
756 * firstc == '@' get text for input() function
757 * firstc == '>' get text for debug mode
758 * firstc == NUL get text for :insert command
759 * firstc == -1 like NUL, and break on CTRL-C
760 *
761 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
762 * command line.
763 *
764 * Careful: getcmdline() can be called recursively!
765 *
766 * Return pointer to allocated string if there is a commandline, NULL
767 * otherwise.
768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100770getcmdline(
771 int firstc,
772 long count UNUSED, /* only used for incremental search */
773 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 int c;
776 int i;
777 int j;
778 int gotesc = FALSE; /* TRUE when <ESC> just typed */
779 int do_abbr; /* when TRUE check for abbr. */
780#ifdef FEAT_CMDHIST
781 char_u *lookfor = NULL; /* string to match */
782 int hiscnt; /* current history line in use */
783 int histype; /* history type to be used */
784#endif
785#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200786 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#endif
788 int did_wild_list = FALSE; /* did wild_list() recently */
789 int wim_index = 0; /* index in wim_flags[] */
790 int res;
791 int save_msg_scroll = msg_scroll;
792 int save_State = State; /* remember State when called */
793 int some_key_typed = FALSE; /* one of the keys was typed */
794#ifdef FEAT_MOUSE
795 /* mouse drag and release events are ignored, unless they are
796 * preceded with a mouse down event */
797 int ignore_drag_release = TRUE;
798#endif
799#ifdef FEAT_EVAL
800 int break_ctrl_c = FALSE;
801#endif
802 expand_T xpc;
803 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200804#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000805 /* Everything that may work recursively should save and restore the
806 * current command line in save_ccline. That includes update_screen(), a
807 * custom status line may invoke ":normal". */
808 struct cmdline_info save_ccline;
809#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200810 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812#ifdef FEAT_EVAL
813 if (firstc == -1)
814 {
815 firstc = NUL;
816 break_ctrl_c = TRUE;
817 }
818#endif
819#ifdef FEAT_RIGHTLEFT
820 /* start without Hebrew mapping for a command line */
821 if (firstc == ':' || firstc == '=' || firstc == '>')
822 cmd_hkmap = 0;
823#endif
824
825 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200828 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829#endif
830
831 /*
832 * set some variables for redrawcmd()
833 */
834 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000835 ccline.cmdindent = (firstc > 0 ? indent : 0);
836
837 /* alloc initial ccline.cmdbuff */
838 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (ccline.cmdbuff == NULL)
840 return NULL; /* out of memory */
841 ccline.cmdlen = ccline.cmdpos = 0;
842 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100843 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000845 /* autoindent for :insert and :append */
846 if (firstc <= 0)
847 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200848 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000849 ccline.cmdbuff[indent] = NUL;
850 ccline.cmdpos = indent;
851 ccline.cmdspos = indent;
852 ccline.cmdlen = indent;
853 }
854
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000856 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858#ifdef FEAT_RIGHTLEFT
859 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
860 && (firstc == '/' || firstc == '?'))
861 cmdmsg_rl = TRUE;
862 else
863 cmdmsg_rl = FALSE;
864#endif
865
866 redir_off = TRUE; /* don't redirect the typed command */
867 if (!cmd_silent)
868 {
869 i = msg_scrolled;
870 msg_scrolled = 0; /* avoid wait_return message */
871 gotocmdline(TRUE);
872 msg_scrolled += i;
873 redrawcmdprompt(); /* draw prompt or indent */
874 set_cmdspos();
875 }
876 xpc.xp_context = EXPAND_NOTHING;
877 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000878#ifndef BACKSLASH_IN_FILENAME
879 xpc.xp_shell = FALSE;
880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000882#if defined(FEAT_EVAL)
883 if (ccline.input_fn)
884 {
885 xpc.xp_context = ccline.xp_context;
886 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000887# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000888 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000889# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000890 }
891#endif
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 /*
894 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
895 * doing ":@0" when register 0 doesn't contain a CR.
896 */
897 msg_scroll = FALSE;
898
899 State = CMDLINE;
900
901 if (firstc == '/' || firstc == '?' || firstc == '@')
902 {
903 /* Use ":lmap" mappings for search pattern and input(). */
904 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
905 b_im_ptr = &curbuf->b_p_iminsert;
906 else
907 b_im_ptr = &curbuf->b_p_imsearch;
908 if (*b_im_ptr == B_IMODE_LMAP)
909 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100910#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 im_set_active(*b_im_ptr == B_IMODE_IM);
912#endif
913 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100914#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 else if (p_imcmdline)
916 im_set_active(TRUE);
917#endif
918
919#ifdef FEAT_MOUSE
920 setmouse();
921#endif
922#ifdef CURSOR_SHAPE
923 ui_cursor_shape(); /* may show different cursor shape */
924#endif
925
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000926 /* When inside an autocommand for writing "exiting" may be set and
927 * terminal mode set to cooked. Need to set raw mode here then. */
928 settmode(TMODE_RAW);
929
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200930 /* Trigger CmdlineEnter autocommands. */
931 cmdline_type = firstc == NUL ? '-' : firstc;
932 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200933
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#ifdef FEAT_CMDHIST
935 init_history();
936 hiscnt = hislen; /* set hiscnt to impossible history value */
937 histype = hist_char2type(firstc);
938#endif
939
940#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000941 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942#endif
943
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200944 /* If something above caused an error, reset the flags, we do want to type
945 * and execute commands. Display may be messed up a bit. */
946 if (did_emsg)
947 redrawcmd();
948 did_emsg = FALSE;
949 got_int = FALSE;
950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 /*
952 * Collect the command string, handling editing keys.
953 */
954 for (;;)
955 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000956 redir_off = TRUE; /* Don't redirect the typed command.
957 Repeated, because a ":redir" inside
958 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959#ifdef USE_ON_FLY_SCROLL
960 dont_scroll = FALSE; /* allow scrolling here */
961#endif
962 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
963
Bram Moolenaar72532d32018-04-07 19:09:09 +0200964 did_emsg = FALSE; /* There can't really be a reason why an error
965 that occurs while typing a command should
966 cause the command not to be executed. */
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000969
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100970 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
971 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000972 do
973 {
974 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100975 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (KeyTyped)
978 {
979 some_key_typed = TRUE;
980#ifdef FEAT_RIGHTLEFT
981 if (cmd_hkmap)
982 c = hkmap(c);
983# ifdef FEAT_FKMAP
984 if (cmd_fkmap)
985 c = cmdl_fkmap(c);
986# endif
987 if (cmdmsg_rl && !KeyStuffed)
988 {
989 /* Invert horizontal movements and operations. Only when
990 * typed by the user directly, not when the result of a
991 * mapping. */
992 switch (c)
993 {
994 case K_RIGHT: c = K_LEFT; break;
995 case K_S_RIGHT: c = K_S_LEFT; break;
996 case K_C_RIGHT: c = K_C_LEFT; break;
997 case K_LEFT: c = K_RIGHT; break;
998 case K_S_LEFT: c = K_S_RIGHT; break;
999 case K_C_LEFT: c = K_C_RIGHT; break;
1000 }
1001 }
1002#endif
1003 }
1004
1005 /*
1006 * Ignore got_int when CTRL-C was typed here.
1007 * Don't ignore it in :global, we really need to break then, e.g., for
1008 * ":g/pat/normal /pat" (without the <CR>).
1009 * Don't ignore it for the input() function.
1010 */
1011 if ((c == Ctrl_C
1012#ifdef UNIX
1013 || c == intr_char
1014#endif
1015 )
1016#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1017 && firstc != '@'
1018#endif
1019#ifdef FEAT_EVAL
1020 && !break_ctrl_c
1021#endif
1022 && !global_busy)
1023 got_int = FALSE;
1024
1025#ifdef FEAT_CMDHIST
1026 /* free old command line when finished moving around in the history
1027 * list */
1028 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001029 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001030 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 && c != K_PAGEDOWN && c != K_PAGEUP
1032 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001033 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001035 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#endif
1037
1038 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001039 * When there are matching completions to select <S-Tab> works like
1040 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001042 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 c = Ctrl_P;
1044
1045#ifdef FEAT_WILDMENU
1046 /* Special translations for 'wildmenu' */
1047 if (did_wild_list && p_wmnu)
1048 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001049 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001051 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 c = Ctrl_N;
1053 }
1054 /* Hitting CR after "emenu Name.": complete submenu */
1055 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1056 && ccline.cmdpos > 1
1057 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1058 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1059 && (c == '\n' || c == '\r' || c == K_KENTER))
1060 c = K_DOWN;
1061#endif
1062
1063 /* free expanded names when finished walking through matches */
1064 if (xpc.xp_numfiles != -1
1065 && !(c == p_wc && KeyTyped) && c != p_wcm
1066 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1067 && c != Ctrl_L)
1068 {
1069 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1070 did_wild_list = FALSE;
1071#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001072 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#endif
1074 xpc.xp_context = EXPAND_NOTHING;
1075 wim_index = 0;
1076#ifdef FEAT_WILDMENU
1077 if (p_wmnu && wild_menu_showing != 0)
1078 {
1079 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001080 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001081
1082 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001083 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 if (wild_menu_showing == WM_SCROLLED)
1086 {
1087 /* Entered command line, move it up */
1088 cmdline_row--;
1089 redrawcmd();
1090 }
1091 else if (save_p_ls != -1)
1092 {
1093 /* restore 'laststatus' and 'winminheight' */
1094 p_ls = save_p_ls;
1095 p_wmh = save_p_wmh;
1096 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001097 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001099 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 redrawcmd();
1101 save_p_ls = -1;
1102 }
1103 else
1104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 redraw_statuslines();
1107 }
1108 KeyTyped = skt;
1109 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001110 if (ccline.input_fn)
1111 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113#endif
1114 }
1115
1116#ifdef FEAT_WILDMENU
1117 /* Special translations for 'wildmenu' */
1118 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1119 {
1120 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001121 if (c == K_DOWN && ccline.cmdpos > 0
1122 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001124 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
1126 /* Hitting <Up>: Remove one submenu name in front of the
1127 * cursor */
1128 int found = FALSE;
1129
1130 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1131 i = 0;
1132 while (--j > 0)
1133 {
1134 /* check for start of menu name */
1135 if (ccline.cmdbuff[j] == ' '
1136 && ccline.cmdbuff[j - 1] != '\\')
1137 {
1138 i = j + 1;
1139 break;
1140 }
1141 /* check for start of submenu name */
1142 if (ccline.cmdbuff[j] == '.'
1143 && ccline.cmdbuff[j - 1] != '\\')
1144 {
1145 if (found)
1146 {
1147 i = j + 1;
1148 break;
1149 }
1150 else
1151 found = TRUE;
1152 }
1153 }
1154 if (i > 0)
1155 cmdline_del(i);
1156 c = p_wc;
1157 xpc.xp_context = EXPAND_NOTHING;
1158 }
1159 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001160 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001161 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001162 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {
1164 char_u upseg[5];
1165
1166 upseg[0] = PATHSEP;
1167 upseg[1] = '.';
1168 upseg[2] = '.';
1169 upseg[3] = PATHSEP;
1170 upseg[4] = NUL;
1171
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001172 if (c == K_DOWN
1173 && ccline.cmdpos > 0
1174 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1175 && (ccline.cmdpos < 3
1176 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1178 {
1179 /* go down a directory */
1180 c = p_wc;
1181 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001182 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184 /* If in a direct ancestor, strip off one ../ to go down */
1185 int found = FALSE;
1186
1187 j = ccline.cmdpos;
1188 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1189 while (--j > i)
1190 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001191#ifdef FEAT_MBYTE
1192 if (has_mbyte)
1193 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (vim_ispathsep(ccline.cmdbuff[j]))
1196 {
1197 found = TRUE;
1198 break;
1199 }
1200 }
1201 if (found
1202 && ccline.cmdbuff[j - 1] == '.'
1203 && ccline.cmdbuff[j - 2] == '.'
1204 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1205 {
1206 cmdline_del(j - 2);
1207 c = p_wc;
1208 }
1209 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001210 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
1212 /* go up a directory */
1213 int found = FALSE;
1214
1215 j = ccline.cmdpos - 1;
1216 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1217 while (--j > i)
1218 {
1219#ifdef FEAT_MBYTE
1220 if (has_mbyte)
1221 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1222#endif
1223 if (vim_ispathsep(ccline.cmdbuff[j])
1224#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001225 && vim_strchr((char_u *)" *?[{`$%#",
1226 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227#endif
1228 )
1229 {
1230 if (found)
1231 {
1232 i = j + 1;
1233 break;
1234 }
1235 else
1236 found = TRUE;
1237 }
1238 }
1239
1240 if (!found)
1241 j = i;
1242 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1243 j += 4;
1244 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1245 && j == i)
1246 j += 3;
1247 else
1248 j = 0;
1249 if (j > 0)
1250 {
1251 /* TODO this is only for DOS/UNIX systems - need to put in
1252 * machine-specific stuff here and in upseg init */
1253 cmdline_del(j);
1254 put_on_cmdline(upseg + 1, 3, FALSE);
1255 }
1256 else if (ccline.cmdpos > i)
1257 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001258
1259 /* Now complete in the new directory. Set KeyTyped in case the
1260 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001262 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266#endif /* FEAT_WILDMENU */
1267
1268 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1269 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1270 if (c == Ctrl_BSL)
1271 {
1272 ++no_mapping;
1273 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001274 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 --no_mapping;
1276 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001277 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1278 * is in a mapping. */
1279 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1280 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {
1282 vungetc(c);
1283 c = Ctrl_BSL;
1284 }
1285#ifdef FEAT_EVAL
1286 else if (c == 'e')
1287 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001288 char_u *p = NULL;
1289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 /*
1292 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001293 * Need to save and restore the current command line, to be
1294 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 */
1296 if (ccline.cmdpos == ccline.cmdlen)
1297 new_cmdpos = 99999; /* keep it at the end */
1298 else
1299 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001300
1301 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001303 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (c == '=')
1305 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001306 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001307 * to avoid nasty things like going to another buffer when
1308 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001309 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001310 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001312 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001313 restore_cmdline(&save_ccline);
1314
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001315 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001317 len = (int)STRLEN(p);
1318 if (realloc_cmdbuff(len + 1) == OK)
1319 {
1320 ccline.cmdlen = len;
1321 STRCPY(ccline.cmdbuff, p);
1322 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001324 /* Restore the cursor or use the position set with
1325 * set_cmdline_pos(). */
1326 if (new_cmdpos > ccline.cmdlen)
1327 ccline.cmdpos = ccline.cmdlen;
1328 else
1329 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001331 KeyTyped = FALSE; /* Don't do p_wc completion. */
1332 redrawcmd();
1333 goto cmdline_changed;
1334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 }
1337 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001338 got_int = FALSE; /* don't abandon the command line */
1339 did_emsg = FALSE;
1340 emsg_on_display = FALSE;
1341 redrawcmd();
1342 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344#endif
1345 else
1346 {
1347 if (c == Ctrl_G && p_im && restart_edit == 0)
1348 restart_edit = 'a';
1349 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1350 in history */
1351 goto returncmd; /* back to Normal mode */
1352 }
1353 }
1354
1355#ifdef FEAT_CMDWIN
1356 if (c == cedit_key || c == K_CMDWIN)
1357 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001358 if (ex_normal_busy == 0 && got_int == FALSE)
1359 {
1360 /*
1361 * Open a window to edit the command line (and history).
1362 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001363 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001364 some_key_typed = TRUE;
1365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367# ifdef FEAT_DIGRAPHS
1368 else
1369# endif
1370#endif
1371#ifdef FEAT_DIGRAPHS
1372 c = do_digraph(c);
1373#endif
1374
1375 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1376 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1377 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001378 /* In Ex mode a backslash escapes a newline. */
1379 if (exmode_active
1380 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001381 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001382 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001383 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001385 if (c == K_KENTER)
1386 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001388 else
1389 {
1390 gotesc = FALSE; /* Might have typed ESC previously, don't
1391 truncate the cmdline now. */
1392 if (ccheck_abbr(c + ABBR_OFF))
1393 goto cmdline_changed;
1394 if (!cmd_silent)
1395 {
1396 windgoto(msg_row, 0);
1397 out_flush();
1398 }
1399 break;
1400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402
1403 /*
1404 * Completion for 'wildchar' or 'wildcharm' key.
1405 * - hitting <ESC> twice means: abandon command line.
1406 * - wildcard expansion is only done when the 'wildchar' key is really
1407 * typed, not when it comes from a macro
1408 */
1409 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1410 {
1411 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1412 {
1413 /* if 'wildmode' contains "list" may still need to list */
1414 if (xpc.xp_numfiles > 1
1415 && !did_wild_list
1416 && (wim_flags[wim_index] & WIM_LIST))
1417 {
1418 (void)showmatches(&xpc, FALSE);
1419 redrawcmd();
1420 did_wild_list = TRUE;
1421 }
1422 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001423 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1424 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001426 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1427 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 else
1429 res = OK; /* don't insert 'wildchar' now */
1430 }
1431 else /* typed p_wc first time */
1432 {
1433 wim_index = 0;
1434 j = ccline.cmdpos;
1435 /* if 'wildmode' first contains "longest", get longest
1436 * common part */
1437 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001438 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1439 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001441 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1442 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 /* if interrupted while completing, behave like it failed */
1445 if (got_int)
1446 {
1447 (void)vpeekc(); /* remove <C-C> from input stream */
1448 got_int = FALSE; /* don't abandon the command line */
1449 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1450#ifdef FEAT_WILDMENU
1451 xpc.xp_context = EXPAND_NOTHING;
1452#endif
1453 goto cmdline_changed;
1454 }
1455
1456 /* when more than one match, and 'wildmode' first contains
1457 * "list", or no change and 'wildmode' contains "longest,list",
1458 * list all matches */
1459 if (res == OK && xpc.xp_numfiles > 1)
1460 {
1461 /* a "longest" that didn't do anything is skipped (but not
1462 * "list:longest") */
1463 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1464 wim_index = 1;
1465 if ((wim_flags[wim_index] & WIM_LIST)
1466#ifdef FEAT_WILDMENU
1467 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1468#endif
1469 )
1470 {
1471 if (!(wim_flags[0] & WIM_LONGEST))
1472 {
1473#ifdef FEAT_WILDMENU
1474 int p_wmnu_save = p_wmnu;
1475 p_wmnu = 0;
1476#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001477 /* remove match */
1478 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479#ifdef FEAT_WILDMENU
1480 p_wmnu = p_wmnu_save;
1481#endif
1482 }
1483#ifdef FEAT_WILDMENU
1484 (void)showmatches(&xpc, p_wmnu
1485 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1486#else
1487 (void)showmatches(&xpc, FALSE);
1488#endif
1489 redrawcmd();
1490 did_wild_list = TRUE;
1491 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001492 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1493 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001495 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1496 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001499 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501#ifdef FEAT_WILDMENU
1502 else if (xpc.xp_numfiles == -1)
1503 xpc.xp_context = EXPAND_NOTHING;
1504#endif
1505 }
1506 if (wim_index < 3)
1507 ++wim_index;
1508 if (c == ESC)
1509 gotesc = TRUE;
1510 if (res == OK)
1511 goto cmdline_changed;
1512 }
1513
1514 gotesc = FALSE;
1515
1516 /* <S-Tab> goes to last match, in a clumsy way */
1517 if (c == K_S_TAB && KeyTyped)
1518 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001519 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1520 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1521 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 goto cmdline_changed;
1523 }
1524
1525 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1526 c = NL;
1527
1528 do_abbr = TRUE; /* default: check for abbreviation */
1529
1530 /*
1531 * Big switch for a typed command line character.
1532 */
1533 switch (c)
1534 {
1535 case K_BS:
1536 case Ctrl_H:
1537 case K_DEL:
1538 case K_KDEL:
1539 case Ctrl_W:
1540#ifdef FEAT_FKMAP
1541 if (cmd_fkmap && c == K_BS)
1542 c = K_DEL;
1543#endif
1544 if (c == K_KDEL)
1545 c = K_DEL;
1546
1547 /*
1548 * delete current character is the same as backspace on next
1549 * character, except at end of line
1550 */
1551 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1552 ++ccline.cmdpos;
1553#ifdef FEAT_MBYTE
1554 if (has_mbyte && c == K_DEL)
1555 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1556 ccline.cmdbuff + ccline.cmdpos);
1557#endif
1558 if (ccline.cmdpos > 0)
1559 {
1560 char_u *p;
1561
1562 j = ccline.cmdpos;
1563 p = ccline.cmdbuff + j;
1564#ifdef FEAT_MBYTE
1565 if (has_mbyte)
1566 {
1567 p = mb_prevptr(ccline.cmdbuff, p);
1568 if (c == Ctrl_W)
1569 {
1570 while (p > ccline.cmdbuff && vim_isspace(*p))
1571 p = mb_prevptr(ccline.cmdbuff, p);
1572 i = mb_get_class(p);
1573 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1574 p = mb_prevptr(ccline.cmdbuff, p);
1575 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001576 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 }
1579 else
1580#endif
1581 if (c == Ctrl_W)
1582 {
1583 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1584 --p;
1585 i = vim_iswordc(p[-1]);
1586 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1587 && vim_iswordc(p[-1]) == i)
1588 --p;
1589 }
1590 else
1591 --p;
1592 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1593 ccline.cmdlen -= j - ccline.cmdpos;
1594 i = ccline.cmdpos;
1595 while (i < ccline.cmdlen)
1596 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1597
1598 /* Truncate at the end, required for multi-byte chars. */
1599 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001600#ifdef FEAT_SEARCH_EXTRA
1601 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001602 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001603 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001604 /* save view settings, so that the screen
1605 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001606 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001607 }
1608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 redrawcmd();
1610 }
1611 else if (ccline.cmdlen == 0 && c != Ctrl_W
1612 && ccline.cmdprompt == NULL && indent == 0)
1613 {
1614 /* In ex and debug mode it doesn't make sense to return. */
1615 if (exmode_active
1616#ifdef FEAT_EVAL
1617 || ccline.cmdfirstc == '>'
1618#endif
1619 )
1620 goto cmdline_not_changed;
1621
Bram Moolenaard23a8232018-02-10 18:45:26 +01001622 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (!cmd_silent)
1624 {
1625#ifdef FEAT_RIGHTLEFT
1626 if (cmdmsg_rl)
1627 msg_col = Columns;
1628 else
1629#endif
1630 msg_col = 0;
1631 msg_putchar(' '); /* delete ':' */
1632 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001633#ifdef FEAT_SEARCH_EXTRA
1634 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001635 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 redraw_cmdline = TRUE;
1638 goto returncmd; /* back to cmd mode */
1639 }
1640 goto cmdline_changed;
1641
1642 case K_INS:
1643 case K_KINS:
1644#ifdef FEAT_FKMAP
1645 /* if Farsi mode set, we are in reverse insert mode -
1646 Do not change the mode */
1647 if (cmd_fkmap)
1648 beep_flush();
1649 else
1650#endif
1651 ccline.overstrike = !ccline.overstrike;
1652#ifdef CURSOR_SHAPE
1653 ui_cursor_shape(); /* may show different cursor shape */
1654#endif
1655 goto cmdline_not_changed;
1656
1657 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001658 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
1660 /* ":lmap" mappings exists, toggle use of mappings. */
1661 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001662#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 im_set_active(FALSE); /* Disable input method */
1664#endif
1665 if (b_im_ptr != NULL)
1666 {
1667 if (State & LANGMAP)
1668 *b_im_ptr = B_IMODE_LMAP;
1669 else
1670 *b_im_ptr = B_IMODE_NONE;
1671 }
1672 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001673#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 else
1675 {
1676 /* There are no ":lmap" mappings, toggle IM. When
1677 * 'imdisable' is set don't try getting the status, it's
1678 * always off. */
1679 if ((p_imdisable && b_im_ptr != NULL)
1680 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1681 {
1682 im_set_active(FALSE); /* Disable input method */
1683 if (b_im_ptr != NULL)
1684 *b_im_ptr = B_IMODE_NONE;
1685 }
1686 else
1687 {
1688 im_set_active(TRUE); /* Enable input method */
1689 if (b_im_ptr != NULL)
1690 *b_im_ptr = B_IMODE_IM;
1691 }
1692 }
1693#endif
1694 if (b_im_ptr != NULL)
1695 {
1696 if (b_im_ptr == &curbuf->b_p_iminsert)
1697 set_iminsert_global();
1698 else
1699 set_imsearch_global();
1700 }
1701#ifdef CURSOR_SHAPE
1702 ui_cursor_shape(); /* may show different cursor shape */
1703#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001704#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 /* Show/unshow value of 'keymap' in status lines later. */
1706 status_redraw_curbuf();
1707#endif
1708 goto cmdline_not_changed;
1709
1710/* case '@': only in very old vi */
1711 case Ctrl_U:
1712 /* delete all characters left of the cursor */
1713 j = ccline.cmdpos;
1714 ccline.cmdlen -= j;
1715 i = ccline.cmdpos = 0;
1716 while (i < ccline.cmdlen)
1717 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1718 /* Truncate at the end, required for multi-byte chars. */
1719 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001720#ifdef FEAT_SEARCH_EXTRA
1721 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001722 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 redrawcmd();
1725 goto cmdline_changed;
1726
1727#ifdef FEAT_CLIPBOARD
1728 case Ctrl_Y:
1729 /* Copy the modeless selection, if there is one. */
1730 if (clip_star.state != SELECT_CLEARED)
1731 {
1732 if (clip_star.state == SELECT_DONE)
1733 clip_copy_modeless_selection(TRUE);
1734 goto cmdline_not_changed;
1735 }
1736 break;
1737#endif
1738
1739 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1740 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001741 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001742 * ":normal" runs out of characters. */
1743 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001744 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 goto cmdline_not_changed;
1746
1747 gotesc = TRUE; /* will free ccline.cmdbuff after
1748 putting it in history */
1749 goto returncmd; /* back to cmd mode */
1750
1751 case Ctrl_R: /* insert register */
1752#ifdef USE_ON_FLY_SCROLL
1753 dont_scroll = TRUE; /* disallow scrolling here */
1754#endif
1755 putcmdline('"', TRUE);
1756 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001757 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if (i == Ctrl_O)
1759 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1760 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001761 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001762 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 --no_mapping;
1764#ifdef FEAT_EVAL
1765 /*
1766 * Insert the result of an expression.
1767 * Need to save the current command line, to be able to enter
1768 * a new one...
1769 */
1770 new_cmdpos = -1;
1771 if (c == '=')
1772 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1774 {
1775 beep_flush();
1776 c = ESC;
1777 }
1778 else
1779 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001780 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001782 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
1784 }
1785#endif
1786 if (c != ESC) /* use ESC to cancel inserting register */
1787 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001788 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001789
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001790#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001791 /* When there was a serious error abort getting the
1792 * command line. */
1793 if (aborting())
1794 {
1795 gotesc = TRUE; /* will free ccline.cmdbuff after
1796 putting it in history */
1797 goto returncmd; /* back to cmd mode */
1798 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 KeyTyped = FALSE; /* Don't do p_wc completion. */
1801#ifdef FEAT_EVAL
1802 if (new_cmdpos >= 0)
1803 {
1804 /* set_cmdline_pos() was used */
1805 if (new_cmdpos > ccline.cmdlen)
1806 ccline.cmdpos = ccline.cmdlen;
1807 else
1808 ccline.cmdpos = new_cmdpos;
1809 }
1810#endif
1811 }
1812 redrawcmd();
1813 goto cmdline_changed;
1814
1815 case Ctrl_D:
1816 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1817 break; /* Use ^D as normal char instead */
1818
1819 redrawcmd();
1820 continue; /* don't do incremental search now */
1821
1822 case K_RIGHT:
1823 case K_S_RIGHT:
1824 case K_C_RIGHT:
1825 do
1826 {
1827 if (ccline.cmdpos >= ccline.cmdlen)
1828 break;
1829 i = cmdline_charsize(ccline.cmdpos);
1830 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1831 break;
1832 ccline.cmdspos += i;
1833#ifdef FEAT_MBYTE
1834 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001835 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 + ccline.cmdpos);
1837 else
1838#endif
1839 ++ccline.cmdpos;
1840 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001841 while ((c == K_S_RIGHT || c == K_C_RIGHT
1842 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1844#ifdef FEAT_MBYTE
1845 if (has_mbyte)
1846 set_cmdspos_cursor();
1847#endif
1848 goto cmdline_not_changed;
1849
1850 case K_LEFT:
1851 case K_S_LEFT:
1852 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001853 if (ccline.cmdpos == 0)
1854 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 do
1856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 --ccline.cmdpos;
1858#ifdef FEAT_MBYTE
1859 if (has_mbyte) /* move to first byte of char */
1860 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1861 ccline.cmdbuff + ccline.cmdpos);
1862#endif
1863 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1864 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001865 while (ccline.cmdpos > 0
1866 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001867 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1869#ifdef FEAT_MBYTE
1870 if (has_mbyte)
1871 set_cmdspos_cursor();
1872#endif
1873 goto cmdline_not_changed;
1874
1875 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001876 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001877 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
Bram Moolenaar4770d092006-01-12 23:22:24 +00001879#ifdef FEAT_GUI_W32
1880 /* On Win32 ignore <M-F4>, we get it when closing the window was
1881 * cancelled. */
1882 case K_F4:
1883 if (mod_mask == MOD_MASK_ALT)
1884 {
1885 redrawcmd(); /* somehow the cmdline is cleared */
1886 goto cmdline_not_changed;
1887 }
1888 break;
1889#endif
1890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#ifdef FEAT_MOUSE
1892 case K_MIDDLEDRAG:
1893 case K_MIDDLERELEASE:
1894 goto cmdline_not_changed; /* Ignore mouse */
1895
1896 case K_MIDDLEMOUSE:
1897# ifdef FEAT_GUI
1898 /* When GUI is active, also paste when 'mouse' is empty */
1899 if (!gui.in_use)
1900# endif
1901 if (!mouse_has(MOUSE_COMMAND))
1902 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001903# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001905 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001907# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001908 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 redrawcmd();
1910 goto cmdline_changed;
1911
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001912# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001914 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 redrawcmd();
1916 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919 case K_LEFTDRAG:
1920 case K_LEFTRELEASE:
1921 case K_RIGHTDRAG:
1922 case K_RIGHTRELEASE:
1923 /* Ignore drag and release events when the button-down wasn't
1924 * seen before. */
1925 if (ignore_drag_release)
1926 goto cmdline_not_changed;
1927 /* FALLTHROUGH */
1928 case K_LEFTMOUSE:
1929 case K_RIGHTMOUSE:
1930 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1931 ignore_drag_release = TRUE;
1932 else
1933 ignore_drag_release = FALSE;
1934# ifdef FEAT_GUI
1935 /* When GUI is active, also move when 'mouse' is empty */
1936 if (!gui.in_use)
1937# endif
1938 if (!mouse_has(MOUSE_COMMAND))
1939 goto cmdline_not_changed; /* Ignore mouse */
1940# ifdef FEAT_CLIPBOARD
1941 if (mouse_row < cmdline_row && clip_star.available)
1942 {
1943 int button, is_click, is_drag;
1944
1945 /*
1946 * Handle modeless selection.
1947 */
1948 button = get_mouse_button(KEY2TERMCAP1(c),
1949 &is_click, &is_drag);
1950 if (mouse_model_popup() && button == MOUSE_LEFT
1951 && (mod_mask & MOD_MASK_SHIFT))
1952 {
1953 /* Translate shift-left to right button. */
1954 button = MOUSE_RIGHT;
1955 mod_mask &= ~MOD_MASK_SHIFT;
1956 }
1957 clip_modeless(button, is_click, is_drag);
1958 goto cmdline_not_changed;
1959 }
1960# endif
1961
1962 set_cmdspos();
1963 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1964 ++ccline.cmdpos)
1965 {
1966 i = cmdline_charsize(ccline.cmdpos);
1967 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1968 && mouse_col < ccline.cmdspos % Columns + i)
1969 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001970# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if (has_mbyte)
1972 {
1973 /* Count ">" for double-wide char that doesn't fit. */
1974 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001975 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 + ccline.cmdpos) - 1;
1977 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 ccline.cmdspos += i;
1980 }
1981 goto cmdline_not_changed;
1982
1983 /* Mouse scroll wheel: ignored here */
1984 case K_MOUSEDOWN:
1985 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001986 case K_MOUSELEFT:
1987 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 /* Alternate buttons ignored here */
1989 case K_X1MOUSE:
1990 case K_X1DRAG:
1991 case K_X1RELEASE:
1992 case K_X2MOUSE:
1993 case K_X2DRAG:
1994 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001995 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 goto cmdline_not_changed;
1997
1998#endif /* FEAT_MOUSE */
1999
2000#ifdef FEAT_GUI
2001 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2002 case K_LEFTRELEASE_NM:
2003 goto cmdline_not_changed;
2004
2005 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002006 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 gui_do_scroll();
2009 redrawcmd();
2010 }
2011 goto cmdline_not_changed;
2012
2013 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002014 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002016 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 redrawcmd();
2018 }
2019 goto cmdline_not_changed;
2020#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002021#ifdef FEAT_GUI_TABLINE
2022 case K_TABLINE:
2023 case K_TABMENU:
2024 /* Don't want to change any tabs here. Make sure the same tab
2025 * is still selected. */
2026 if (gui_use_tabline())
2027 gui_mch_set_curtab(tabpage_index(curtab));
2028 goto cmdline_not_changed;
2029#endif
2030
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 case K_SELECT: /* end of Select mode mapping - ignore */
2032 goto cmdline_not_changed;
2033
2034 case Ctrl_B: /* begin of command line */
2035 case K_HOME:
2036 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 case K_S_HOME:
2038 case K_C_HOME:
2039 ccline.cmdpos = 0;
2040 set_cmdspos();
2041 goto cmdline_not_changed;
2042
2043 case Ctrl_E: /* end of command line */
2044 case K_END:
2045 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 case K_S_END:
2047 case K_C_END:
2048 ccline.cmdpos = ccline.cmdlen;
2049 set_cmdspos_cursor();
2050 goto cmdline_not_changed;
2051
2052 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002053 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 break;
2055 goto cmdline_changed;
2056
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002057 case Ctrl_L:
2058#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002059 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002060 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002061#endif
2062
2063 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002064 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 break;
2066 goto cmdline_changed;
2067
2068 case Ctrl_N: /* next match */
2069 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002070 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002072 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2073 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002075 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002078 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 case K_UP:
2080 case K_DOWN:
2081 case K_S_UP:
2082 case K_S_DOWN:
2083 case K_PAGEUP:
2084 case K_KPAGEUP:
2085 case K_PAGEDOWN:
2086 case K_KPAGEDOWN:
2087 if (hislen == 0 || firstc == NUL) /* no history */
2088 goto cmdline_not_changed;
2089
2090 i = hiscnt;
2091
2092 /* save current command string so it can be restored later */
2093 if (lookfor == NULL)
2094 {
2095 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2096 goto cmdline_not_changed;
2097 lookfor[ccline.cmdpos] = NUL;
2098 }
2099
2100 j = (int)STRLEN(lookfor);
2101 for (;;)
2102 {
2103 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002104 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002105 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
2107 if (hiscnt == hislen) /* first time */
2108 hiscnt = hisidx[histype];
2109 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2110 hiscnt = hislen - 1;
2111 else if (hiscnt != hisidx[histype] + 1)
2112 --hiscnt;
2113 else /* at top of list */
2114 {
2115 hiscnt = i;
2116 break;
2117 }
2118 }
2119 else /* one step forwards */
2120 {
2121 /* on last entry, clear the line */
2122 if (hiscnt == hisidx[histype])
2123 {
2124 hiscnt = hislen;
2125 break;
2126 }
2127
2128 /* not on a history line, nothing to do */
2129 if (hiscnt == hislen)
2130 break;
2131 if (hiscnt == hislen - 1) /* wrap around */
2132 hiscnt = 0;
2133 else
2134 ++hiscnt;
2135 }
2136 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2137 {
2138 hiscnt = i;
2139 break;
2140 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002141 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002142 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 || STRNCMP(history[histype][hiscnt].hisstr,
2144 lookfor, (size_t)j) == 0)
2145 break;
2146 }
2147
2148 if (hiscnt != i) /* jumped to other entry */
2149 {
2150 char_u *p;
2151 int len;
2152 int old_firstc;
2153
2154 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002155 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (hiscnt == hislen)
2157 p = lookfor; /* back to the old one */
2158 else
2159 p = history[histype][hiscnt].hisstr;
2160
2161 if (histype == HIST_SEARCH
2162 && p != lookfor
2163 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2164 {
2165 /* Correct for the separator character used when
2166 * adding the history entry vs the one used now.
2167 * First loop: count length.
2168 * Second loop: copy the characters. */
2169 for (i = 0; i <= 1; ++i)
2170 {
2171 len = 0;
2172 for (j = 0; p[j] != NUL; ++j)
2173 {
2174 /* Replace old sep with new sep, unless it is
2175 * escaped. */
2176 if (p[j] == old_firstc
2177 && (j == 0 || p[j - 1] != '\\'))
2178 {
2179 if (i > 0)
2180 ccline.cmdbuff[len] = firstc;
2181 }
2182 else
2183 {
2184 /* Escape new sep, unless it is already
2185 * escaped. */
2186 if (p[j] == firstc
2187 && (j == 0 || p[j - 1] != '\\'))
2188 {
2189 if (i > 0)
2190 ccline.cmdbuff[len] = '\\';
2191 ++len;
2192 }
2193 if (i > 0)
2194 ccline.cmdbuff[len] = p[j];
2195 }
2196 ++len;
2197 }
2198 if (i == 0)
2199 {
2200 alloc_cmdbuff(len);
2201 if (ccline.cmdbuff == NULL)
2202 goto returncmd;
2203 }
2204 }
2205 ccline.cmdbuff[len] = NUL;
2206 }
2207 else
2208 {
2209 alloc_cmdbuff((int)STRLEN(p));
2210 if (ccline.cmdbuff == NULL)
2211 goto returncmd;
2212 STRCPY(ccline.cmdbuff, p);
2213 }
2214
2215 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2216 redrawcmd();
2217 goto cmdline_changed;
2218 }
2219 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002220#endif
2221 goto cmdline_not_changed;
2222
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002223#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002224 case Ctrl_G: /* next match */
2225 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002226 if (may_adjust_incsearch_highlighting(
2227 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002228 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230#endif
2231
2232 case Ctrl_V:
2233 case Ctrl_Q:
2234#ifdef FEAT_MOUSE
2235 ignore_drag_release = TRUE;
2236#endif
2237 putcmdline('^', TRUE);
2238 c = get_literal(); /* get next (two) character(s) */
2239 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002240 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#ifdef FEAT_MBYTE
2242 /* may need to remove ^ when composing char was typed */
2243 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2244 {
2245 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2246 msg_putchar(' ');
2247 cursorcmd();
2248 }
2249#endif
2250 break;
2251
2252#ifdef FEAT_DIGRAPHS
2253 case Ctrl_K:
2254#ifdef FEAT_MOUSE
2255 ignore_drag_release = TRUE;
2256#endif
2257 putcmdline('?', TRUE);
2258#ifdef USE_ON_FLY_SCROLL
2259 dont_scroll = TRUE; /* disallow scrolling here */
2260#endif
2261 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002262 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (c != NUL)
2264 break;
2265
2266 redrawcmd();
2267 goto cmdline_not_changed;
2268#endif /* FEAT_DIGRAPHS */
2269
2270#ifdef FEAT_RIGHTLEFT
2271 case Ctrl__: /* CTRL-_: switch language mode */
2272 if (!p_ari)
2273 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002274# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (p_altkeymap)
2276 {
2277 cmd_fkmap = !cmd_fkmap;
2278 if (cmd_fkmap) /* in Farsi always in Insert mode */
2279 ccline.overstrike = FALSE;
2280 }
2281 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 cmd_hkmap = !cmd_hkmap;
2284 goto cmdline_not_changed;
2285#endif
2286
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002287 case K_PS:
2288 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2289 goto cmdline_changed;
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 default:
2292#ifdef UNIX
2293 if (c == intr_char)
2294 {
2295 gotesc = TRUE; /* will free ccline.cmdbuff after
2296 putting it in history */
2297 goto returncmd; /* back to Normal mode */
2298 }
2299#endif
2300 /*
2301 * Normal character with no special meaning. Just set mod_mask
2302 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2303 * the string <S-Space>. This should only happen after ^V.
2304 */
2305 if (!IS_SPECIAL(c))
2306 mod_mask = 0x0;
2307 break;
2308 }
2309 /*
2310 * End of switch on command line character.
2311 * We come here if we have a normal character.
2312 */
2313
Bram Moolenaarede3e632013-06-23 16:16:19 +02002314 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#ifdef FEAT_MBYTE
2316 /* Add ABBR_OFF for characters above 0x100, this is
2317 * what check_abbr() expects. */
2318 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2319#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002320 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 goto cmdline_changed;
2322
2323 /*
2324 * put the character in the command line
2325 */
2326 if (IS_SPECIAL(c) || mod_mask != 0)
2327 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2328 else
2329 {
2330#ifdef FEAT_MBYTE
2331 if (has_mbyte)
2332 {
2333 j = (*mb_char2bytes)(c, IObuff);
2334 IObuff[j] = NUL; /* exclude composing chars */
2335 put_on_cmdline(IObuff, j, TRUE);
2336 }
2337 else
2338#endif
2339 {
2340 IObuff[0] = c;
2341 put_on_cmdline(IObuff, 1, TRUE);
2342 }
2343 }
2344 goto cmdline_changed;
2345
2346/*
2347 * This part implements incremental searches for "/" and "?"
2348 * Jump to cmdline_not_changed when a character has been read but the command
2349 * line did not change. Then we only search and redraw if something changed in
2350 * the past.
2351 * Jump to cmdline_changed when the command line did change.
2352 * (Sorry for the goto's, I know it is ugly).
2353 */
2354cmdline_not_changed:
2355#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002356 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 continue;
2358#endif
2359
2360cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002361 /* Trigger CmdlineChanged autocommands. */
2362 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002365 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366#endif
2367
2368#ifdef FEAT_RIGHTLEFT
2369 if (cmdmsg_rl
2370# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002371 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372# endif
2373 )
2374 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002375 * right-left typing. Not efficient, but it works.
2376 * Do it only when there are no characters left to read
2377 * to avoid useless intermediate redraws. */
2378 if (vpeekc() == NUL)
2379 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380#endif
2381 }
2382
2383returncmd:
2384
2385#ifdef FEAT_RIGHTLEFT
2386 cmdmsg_rl = FALSE;
2387#endif
2388
2389#ifdef FEAT_FKMAP
2390 cmd_fkmap = 0;
2391#endif
2392
2393 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002394 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002397 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#endif
2399
2400 if (ccline.cmdbuff != NULL)
2401 {
2402 /*
2403 * Put line in history buffer (":" and "=" only when it was typed).
2404 */
2405#ifdef FEAT_CMDHIST
2406 if (ccline.cmdlen && firstc != NUL
2407 && (some_key_typed || histype == HIST_SEARCH))
2408 {
2409 add_to_history(histype, ccline.cmdbuff, TRUE,
2410 histype == HIST_SEARCH ? firstc : NUL);
2411 if (firstc == ':')
2412 {
2413 vim_free(new_last_cmdline);
2414 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2415 }
2416 }
2417#endif
2418
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002419 if (gotesc)
2420 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422
2423 /*
2424 * If the screen was shifted up, redraw the whole screen (later).
2425 * If the line is too long, clear it, so ruler and shown command do
2426 * not get printed in the middle of it.
2427 */
2428 msg_check();
2429 msg_scroll = save_msg_scroll;
2430 redir_off = FALSE;
2431
2432 /* When the command line was typed, no need for a wait-return prompt. */
2433 if (some_key_typed)
2434 need_wait_return = FALSE;
2435
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002436 /* Trigger CmdlineLeave autocommands. */
2437 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002440#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2442 im_save_status(b_im_ptr);
2443 im_set_active(FALSE);
2444#endif
2445#ifdef FEAT_MOUSE
2446 setmouse();
2447#endif
2448#ifdef CURSOR_SHAPE
2449 ui_cursor_shape(); /* may show different cursor shape */
2450#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002451 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002453 {
2454 char_u *p = ccline.cmdbuff;
2455
2456 /* Make ccline empty, getcmdline() may try to use it. */
2457 ccline.cmdbuff = NULL;
2458 return p;
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460}
2461
2462#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2463/*
2464 * Get a command line with a prompt.
2465 * This is prepared to be called recursively from getcmdline() (e.g. by
2466 * f_input() when evaluating an expression from CTRL-R =).
2467 * Returns the command line in allocated memory, or NULL.
2468 */
2469 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002470getcmdline_prompt(
2471 int firstc,
2472 char_u *prompt, /* command line prompt */
2473 int attr, /* attributes for prompt */
2474 int xp_context, /* type of expansion */
2475 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 char_u *s;
2478 struct cmdline_info save_ccline;
2479 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002480 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002482 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 ccline.cmdprompt = prompt;
2484 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002485# ifdef FEAT_EVAL
2486 ccline.xp_context = xp_context;
2487 ccline.xp_arg = xp_arg;
2488 ccline.input_fn = (firstc == '@');
2489# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002490 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002492 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002493 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002494 /* Restore msg_col, the prompt from input() may have changed it.
2495 * But only if called recursively and the commandline is therefore being
2496 * restored to an old one; if not, the input() prompt stays on the screen,
2497 * so we need its modified msg_col left intact. */
2498 if (ccline.cmdbuff != NULL)
2499 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 return s;
2502}
2503#endif
2504
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002505/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002506 * Return TRUE when the text must not be changed and we can't switch to
2507 * another window or buffer. Used when editing the command line, evaluating
2508 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002509 */
2510 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002511text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002512{
2513#ifdef FEAT_CMDWIN
2514 if (cmdwin_type != 0)
2515 return TRUE;
2516#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002517 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002518}
2519
2520/*
2521 * Give an error message for a command that isn't allowed while the cmdline
2522 * window is open or editing the cmdline in another way.
2523 */
2524 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002525text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002526{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002527 EMSG(_(get_text_locked_msg()));
2528}
2529
2530 char_u *
2531get_text_locked_msg(void)
2532{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002533#ifdef FEAT_CMDWIN
2534 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002535 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002536#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002537 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002538}
2539
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002540/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002541 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2542 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002543 */
2544 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002545curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002546{
2547 if (curbuf_lock > 0)
2548 {
2549 EMSG(_("E788: Not allowed to edit another buffer now"));
2550 return TRUE;
2551 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002552 return allbuf_locked();
2553}
2554
2555/*
2556 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2557 * message.
2558 */
2559 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002560allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002561{
2562 if (allbuf_lock > 0)
2563 {
2564 EMSG(_("E811: Not allowed to change buffer information now"));
2565 return TRUE;
2566 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002567 return FALSE;
2568}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002571cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2574 if (cmdline_star > 0) /* showing '*', always 1 position */
2575 return 1;
2576#endif
2577 return ptr2cells(ccline.cmdbuff + idx);
2578}
2579
2580/*
2581 * Compute the offset of the cursor on the command line for the prompt and
2582 * indent.
2583 */
2584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002585set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002587 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 ccline.cmdspos = 1 + ccline.cmdindent;
2589 else
2590 ccline.cmdspos = 0 + ccline.cmdindent;
2591}
2592
2593/*
2594 * Compute the screen position for the cursor on the command line.
2595 */
2596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002597set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 int i, m, c;
2600
2601 set_cmdspos();
2602 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002605 if (m < 0) /* overflow, Columns or Rows at weird value */
2606 m = MAXCOL;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 else
2609 m = MAXCOL;
2610 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2611 {
2612 c = cmdline_charsize(i);
2613#ifdef FEAT_MBYTE
2614 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2615 if (has_mbyte)
2616 correct_cmdspos(i, c);
2617#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002618 /* If the cmdline doesn't fit, show cursor on last visible char.
2619 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if ((ccline.cmdspos += c) >= m)
2621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 ccline.cmdspos -= c;
2623 break;
2624 }
2625#ifdef FEAT_MBYTE
2626 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002627 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#endif
2629 }
2630}
2631
2632#ifdef FEAT_MBYTE
2633/*
2634 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2635 * character that doesn't fit, so that a ">" must be displayed.
2636 */
2637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002638correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002640 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2642 && ccline.cmdspos % Columns + cells > Columns)
2643 ccline.cmdspos++;
2644}
2645#endif
2646
2647/*
2648 * Get an Ex command line for the ":" command.
2649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002651getexline(
2652 int c, /* normally ':', NUL for ":append" */
2653 void *cookie UNUSED,
2654 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 /* When executing a register, remove ':' that's in front of each line. */
2657 if (exec_from_reg && vpeekc() == ':')
2658 (void)vgetc();
2659 return getcmdline(c, 1L, indent);
2660}
2661
2662/*
2663 * Get an Ex command line for Ex mode.
2664 * In Ex mode we only use the OS supplied line editing features and no
2665 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002666 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002669getexmodeline(
2670 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002671 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002672 void *cookie UNUSED,
2673 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002675 garray_T line_ga;
2676 char_u *pend;
2677 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002678 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002679 int escaped = FALSE; /* CTRL-V typed */
2680 int vcol = 0;
2681 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002682 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002683 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 /* Switch cursor on now. This avoids that it happens after the "\n", which
2686 * confuses the system function that computes tabstops. */
2687 cursor_on();
2688
2689 /* always start in column 0; write a newline if necessary */
2690 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002691 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002693 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002695 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 if (p_prompt)
2697 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 while (indent-- > 0)
2699 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702
2703 ga_init2(&line_ga, 1, 30);
2704
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002705 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002707 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002708 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002709 while (indent >= 8)
2710 {
2711 ga_append(&line_ga, TAB);
2712 msg_puts((char_u *)" ");
2713 indent -= 8;
2714 }
2715 while (indent-- > 0)
2716 {
2717 ga_append(&line_ga, ' ');
2718 msg_putchar(' ');
2719 }
2720 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002721 ++no_mapping;
2722 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 /*
2725 * Get the line, one character at a time.
2726 */
2727 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002730 long sw;
2731 char_u *s;
2732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (ga_grow(&line_ga, 40) == FAIL)
2734 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
Bram Moolenaarba748c82017-02-26 14:00:07 +01002736 /*
2737 * Get one character at a time.
2738 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002739 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002740
2741 /* Check for a ":normal" command and no more characters left. */
2742 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2743 c1 = '\n';
2744 else
2745 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748 * Handle line editing.
2749 * Previously this was left to the system, putting the terminal in
2750 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 msg_putchar('\n');
2755 break;
2756 }
2757
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002758 if (c1 == K_PS)
2759 {
2760 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2761 goto redraw;
2762 }
2763
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764 if (!escaped)
2765 {
2766 /* CR typed means "enter", which is NL */
2767 if (c1 == '\r')
2768 c1 = '\n';
2769
2770 if (c1 == BS || c1 == K_BS
2771 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002773 if (line_ga.ga_len > 0)
2774 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002775#ifdef FEAT_MBYTE
2776 if (has_mbyte)
2777 {
2778 p = (char_u *)line_ga.ga_data;
2779 p[line_ga.ga_len] = NUL;
2780 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2781 line_ga.ga_len -= len;
2782 }
2783 else
2784#endif
2785 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002786 goto redraw;
2787 }
2788 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 msg_col = startcol;
2794 msg_clr_eos();
2795 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002796 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002797 }
2798
2799 if (c1 == Ctrl_T)
2800 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002801 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 p = (char_u *)line_ga.ga_data;
2803 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002804 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002805 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002807 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002809 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002810 p = (char_u *)line_ga.ga_data;
2811 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002812 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2813 *s = ' ';
2814 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816redraw:
2817 /* redraw the line */
2818 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002820 p = (char_u *)line_ga.ga_data;
2821 p[line_ga.ga_len] = NUL;
2822 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828 msg_putchar(' ');
2829 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002830 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002834 len = MB_PTR2LEN(p);
2835 msg_outtrans_len(p, len);
2836 vcol += ptr2cells(p);
2837 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002840 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002841 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002842 continue;
2843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 if (c1 == Ctrl_D)
2846 {
2847 /* Delete one shiftwidth. */
2848 p = (char_u *)line_ga.ga_data;
2849 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 if (prev_char == '^')
2852 ex_keep_indent = TRUE;
2853 indent = 0;
2854 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
2856 else
2857 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002859 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002860 if (indent > 0)
2861 {
2862 --indent;
2863 indent -= indent % get_sw_value(curbuf);
2864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002866 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002867 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002868 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2870 --line_ga.ga_len;
2871 }
2872 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874
2875 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2876 {
2877 escaped = TRUE;
2878 continue;
2879 }
2880
2881 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2882 if (IS_SPECIAL(c1))
2883 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002885
2886 if (IS_SPECIAL(c1))
2887 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002888#ifdef FEAT_MBYTE
2889 if (has_mbyte)
2890 len = (*mb_char2bytes)(c1,
2891 (char_u *)line_ga.ga_data + line_ga.ga_len);
2892 else
2893#endif
2894 {
2895 len = 1;
2896 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2897 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898 if (c1 == '\n')
2899 msg_putchar('\n');
2900 else if (c1 == TAB)
2901 {
2902 /* Don't use chartabsize(), 'ts' can be different */
2903 do
2904 {
2905 msg_putchar(' ');
2906 } while (++vcol % 8);
2907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002911 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002912 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002914 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 escaped = FALSE;
2916
2917 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002918 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002919
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002920 /* We are done when a NL is entered, but not when it comes after an
2921 * odd number of backslashes, that results in a NUL. */
2922 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002924 int bcount = 0;
2925
2926 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2927 ++bcount;
2928
2929 if (bcount > 0)
2930 {
2931 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2932 * "\NL", etc. */
2933 line_ga.ga_len -= (bcount + 1) / 2;
2934 pend -= (bcount + 1) / 2;
2935 pend[-1] = '\n';
2936 }
2937
2938 if ((bcount & 1) == 0)
2939 {
2940 --line_ga.ga_len;
2941 --pend;
2942 *pend = NUL;
2943 break;
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946 }
2947
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002948 --no_mapping;
2949 --allow_keys;
2950
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 /* make following messages go to the next line */
2952 msg_didout = FALSE;
2953 msg_col = 0;
2954 if (msg_row < Rows - 1)
2955 ++msg_row;
2956 emsg_on_display = FALSE; /* don't want ui_delay() */
2957
2958 if (got_int)
2959 ga_clear(&line_ga);
2960
2961 return (char_u *)line_ga.ga_data;
2962}
2963
Bram Moolenaare344bea2005-09-01 20:46:49 +00002964# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2965 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966/*
2967 * Return TRUE if ccline.overstrike is on.
2968 */
2969 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002970cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 return ccline.overstrike;
2973}
2974
2975/*
2976 * Return TRUE if the cursor is at the end of the cmdline.
2977 */
2978 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002979cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 return (ccline.cmdpos >= ccline.cmdlen);
2982}
2983#endif
2984
Bram Moolenaar9372a112005-12-06 19:59:18 +00002985#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986/*
2987 * Return the virtual column number at the current cursor position.
2988 * This is used by the IM code to obtain the start of the preedit string.
2989 */
2990 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002991cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2994 return MAXCOL;
2995
2996# ifdef FEAT_MBYTE
2997 if (has_mbyte)
2998 {
2999 colnr_T col;
3000 int i = 0;
3001
3002 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003003 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 return col;
3006 }
3007 else
3008# endif
3009 return ccline.cmdpos;
3010}
3011#endif
3012
3013#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3014/*
3015 * If part of the command line is an IM preedit string, redraw it with
3016 * IM feedback attributes. The cursor position is restored after drawing.
3017 */
3018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003019redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
3021 if ((State & CMDLINE)
3022 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003023 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 && !p_imdisable
3025 && im_is_preediting())
3026 {
3027 int cmdpos = 0;
3028 int cmdspos;
3029 int old_row;
3030 int old_col;
3031 colnr_T col;
3032
3033 old_row = msg_row;
3034 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003035 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037# ifdef FEAT_MBYTE
3038 if (has_mbyte)
3039 {
3040 for (col = 0; col < preedit_start_col
3041 && cmdpos < ccline.cmdlen; ++col)
3042 {
3043 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003044 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046 }
3047 else
3048# endif
3049 {
3050 cmdspos += preedit_start_col;
3051 cmdpos += preedit_start_col;
3052 }
3053
3054 msg_row = cmdline_row + (cmdspos / (int)Columns);
3055 msg_col = cmdspos % (int)Columns;
3056 if (msg_row >= Rows)
3057 msg_row = Rows - 1;
3058
3059 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3060 {
3061 int char_len;
3062 int char_attr;
3063
3064 char_attr = im_get_feedback_attr(col);
3065 if (char_attr < 0)
3066 break; /* end of preedit string */
3067
3068# ifdef FEAT_MBYTE
3069 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003070 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 else
3072# endif
3073 char_len = 1;
3074
3075 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3076 cmdpos += char_len;
3077 }
3078
3079 msg_row = old_row;
3080 msg_col = old_col;
3081 }
3082}
3083#endif /* FEAT_XIM && FEAT_GUI_GTK */
3084
3085/*
3086 * Allocate a new command line buffer.
3087 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3088 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3089 */
3090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 /*
3094 * give some extra space to avoid having to allocate all the time
3095 */
3096 if (len < 80)
3097 len = 100;
3098 else
3099 len += 20;
3100
3101 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3102 ccline.cmdbufflen = len;
3103}
3104
3105/*
3106 * Re-allocate the command line to length len + something extra.
3107 * return FAIL for failure, OK otherwise
3108 */
3109 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 char_u *p;
3113
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003114 if (len < ccline.cmdbufflen)
3115 return OK; /* no need to resize */
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 p = ccline.cmdbuff;
3118 alloc_cmdbuff(len); /* will get some more */
3119 if (ccline.cmdbuff == NULL) /* out of memory */
3120 {
3121 ccline.cmdbuff = p; /* keep the old one */
3122 return FAIL;
3123 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003124 /* There isn't always a NUL after the command, but it may need to be
3125 * there, thus copy up to the NUL and add a NUL. */
3126 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3127 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003129
3130 if (ccline.xpc != NULL
3131 && ccline.xpc->xp_pattern != NULL
3132 && ccline.xpc->xp_context != EXPAND_NOTHING
3133 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3134 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003135 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003136
3137 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3138 * to point into the newly allocated memory. */
3139 if (i >= 0 && i <= ccline.cmdlen)
3140 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3141 }
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 return OK;
3144}
3145
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146#if defined(FEAT_ARABIC) || defined(PROTO)
3147static char_u *arshape_buf = NULL;
3148
3149# if defined(EXITFREE) || defined(PROTO)
3150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003152{
3153 vim_free(arshape_buf);
3154}
3155# endif
3156#endif
3157
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158/*
3159 * Draw part of the cmdline at the current cursor position. But draw stars
3160 * when cmdline_star is TRUE.
3161 */
3162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003163draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3166 int i;
3167
3168 if (cmdline_star > 0)
3169 for (i = 0; i < len; ++i)
3170 {
3171 msg_putchar('*');
3172# ifdef FEAT_MBYTE
3173 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003174 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175# endif
3176 }
3177 else
3178#endif
3179#ifdef FEAT_ARABIC
3180 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 static int buflen = 0;
3183 char_u *p;
3184 int j;
3185 int newlen = 0;
3186 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003187 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 int prev_c = 0;
3189 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003190 int u8c;
3191 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 /*
3195 * Do arabic shaping into a temporary buffer. This is very
3196 * inefficient!
3197 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003198 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 /* Re-allocate the buffer. We keep it around to avoid a lot of
3201 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003203 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204 arshape_buf = alloc(buflen);
3205 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 return; /* out of memory */
3207 }
3208
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003209 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3210 {
3211 /* Prepend a space to draw the leading composing char on. */
3212 arshape_buf[0] = ' ';
3213 newlen = 1;
3214 }
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 for (j = start; j < start + len; j += mb_l)
3217 {
3218 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003219 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003220 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (ARABIC_CHAR(u8c))
3222 {
3223 /* Do Arabic shaping. */
3224 if (cmdmsg_rl)
3225 {
3226 /* displaying from right to left */
3227 pc = prev_c;
3228 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003229 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (j + mb_l >= start + len)
3231 nc = NUL;
3232 else
3233 nc = utf_ptr2char(p + mb_l);
3234 }
3235 else
3236 {
3237 /* displaying from left to right */
3238 if (j + mb_l >= start + len)
3239 pc = NUL;
3240 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003241 {
3242 int pcc[MAX_MCO];
3243
3244 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003246 pc1 = pcc[0];
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 nc = prev_c;
3249 }
3250 prev_c = u8c;
3251
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003252 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003255 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003257 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3258 if (u8cc[1] != 0)
3259 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 }
3263 else
3264 {
3265 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 newlen += mb_l;
3268 }
3269 }
3270
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
3273 else
3274#endif
3275 msg_outtrans_len(ccline.cmdbuff + start, len);
3276}
3277
3278/*
3279 * Put a character on the command line. Shifts the following text to the
3280 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3281 * "c" must be printable (fit in one display cell)!
3282 */
3283 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003284putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286 if (cmd_silent)
3287 return;
3288 msg_no_more = TRUE;
3289 msg_putchar(c);
3290 if (shift)
3291 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3292 msg_no_more = FALSE;
3293 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003294 extra_char = c;
3295 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296}
3297
3298/*
3299 * Undo a putcmdline(c, FALSE).
3300 */
3301 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003302unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304 if (cmd_silent)
3305 return;
3306 msg_no_more = TRUE;
3307 if (ccline.cmdlen == ccline.cmdpos)
3308 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003309#ifdef FEAT_MBYTE
3310 else if (has_mbyte)
3311 draw_cmdline(ccline.cmdpos,
3312 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 else
3315 draw_cmdline(ccline.cmdpos, 1);
3316 msg_no_more = FALSE;
3317 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003318 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319}
3320
3321/*
3322 * Put the given string, of the given length, onto the command line.
3323 * If len is -1, then STRLEN() is used to calculate the length.
3324 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3325 * part will be redrawn, otherwise it will not. If this function is called
3326 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3327 * called afterwards.
3328 */
3329 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003330put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
3332 int retval;
3333 int i;
3334 int m;
3335 int c;
3336
3337 if (len < 0)
3338 len = (int)STRLEN(str);
3339
3340 /* Check if ccline.cmdbuff needs to be longer */
3341 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003342 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else
3344 retval = OK;
3345 if (retval == OK)
3346 {
3347 if (!ccline.overstrike)
3348 {
3349 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3350 ccline.cmdbuff + ccline.cmdpos,
3351 (size_t)(ccline.cmdlen - ccline.cmdpos));
3352 ccline.cmdlen += len;
3353 }
3354 else
3355 {
3356#ifdef FEAT_MBYTE
3357 if (has_mbyte)
3358 {
3359 /* Count nr of characters in the new string. */
3360 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003361 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 ++m;
3363 /* Count nr of bytes in cmdline that are overwritten by these
3364 * characters. */
3365 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003366 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 --m;
3368 if (i < ccline.cmdlen)
3369 {
3370 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3371 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3372 ccline.cmdlen += ccline.cmdpos + len - i;
3373 }
3374 else
3375 ccline.cmdlen = ccline.cmdpos + len;
3376 }
3377 else
3378#endif
3379 if (ccline.cmdpos + len > ccline.cmdlen)
3380 ccline.cmdlen = ccline.cmdpos + len;
3381 }
3382 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3383 ccline.cmdbuff[ccline.cmdlen] = NUL;
3384
3385#ifdef FEAT_MBYTE
3386 if (enc_utf8)
3387 {
3388 /* When the inserted text starts with a composing character,
3389 * backup to the character before it. There could be two of them.
3390 */
3391 i = 0;
3392 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3393 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3394 {
3395 i = (*mb_head_off)(ccline.cmdbuff,
3396 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3397 ccline.cmdpos -= i;
3398 len += i;
3399 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3400 }
3401# ifdef FEAT_ARABIC
3402 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3403 {
3404 /* Check the previous character for Arabic combining pair. */
3405 i = (*mb_head_off)(ccline.cmdbuff,
3406 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3407 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3408 + ccline.cmdpos - i), c))
3409 {
3410 ccline.cmdpos -= i;
3411 len += i;
3412 }
3413 else
3414 i = 0;
3415 }
3416# endif
3417 if (i != 0)
3418 {
3419 /* Also backup the cursor position. */
3420 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3421 ccline.cmdspos -= i;
3422 msg_col -= i;
3423 if (msg_col < 0)
3424 {
3425 msg_col += Columns;
3426 --msg_row;
3427 }
3428 }
3429 }
3430#endif
3431
3432 if (redraw && !cmd_silent)
3433 {
3434 msg_no_more = TRUE;
3435 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003436 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3438 /* Avoid clearing the rest of the line too often. */
3439 if (cmdline_row != i || ccline.overstrike)
3440 msg_clr_eos();
3441 msg_no_more = FALSE;
3442 }
3443#ifdef FEAT_FKMAP
3444 /*
3445 * If we are in Farsi command mode, the character input must be in
3446 * Insert mode. So do not advance the cmdpos.
3447 */
3448 if (!cmd_fkmap)
3449#endif
3450 {
3451 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003454 if (m < 0) /* overflow, Columns or Rows at weird value */
3455 m = MAXCOL;
3456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 else
3458 m = MAXCOL;
3459 for (i = 0; i < len; ++i)
3460 {
3461 c = cmdline_charsize(ccline.cmdpos);
3462#ifdef FEAT_MBYTE
3463 /* count ">" for a double-wide char that doesn't fit. */
3464 if (has_mbyte)
3465 correct_cmdspos(ccline.cmdpos, c);
3466#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003467 /* Stop cursor at the end of the screen, but do increment the
3468 * insert position, so that entering a very long command
3469 * works, even though you can't see it. */
3470 if (ccline.cmdspos + c < m)
3471 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#ifdef FEAT_MBYTE
3473 if (has_mbyte)
3474 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003475 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (c > len - i - 1)
3477 c = len - i - 1;
3478 ccline.cmdpos += c;
3479 i += c;
3480 }
3481#endif
3482 ++ccline.cmdpos;
3483 }
3484 }
3485 }
3486 if (redraw)
3487 msg_check();
3488 return retval;
3489}
3490
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003491static struct cmdline_info prev_ccline;
3492static int prev_ccline_used = FALSE;
3493
3494/*
3495 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3496 * and overwrite it. But get_cmdline_str() may need it, thus make it
3497 * available globally in prev_ccline.
3498 */
3499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003500save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003501{
3502 if (!prev_ccline_used)
3503 {
3504 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3505 prev_ccline_used = TRUE;
3506 }
3507 *ccp = prev_ccline;
3508 prev_ccline = ccline;
3509 ccline.cmdbuff = NULL;
3510 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003511 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003512}
3513
3514/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003515 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003516 */
3517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003518restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003519{
3520 ccline = prev_ccline;
3521 prev_ccline = *ccp;
3522}
3523
Bram Moolenaar5a305422006-04-28 22:38:25 +00003524#if defined(FEAT_EVAL) || defined(PROTO)
3525/*
3526 * Save the command line into allocated memory. Returns a pointer to be
3527 * passed to restore_cmdline_alloc() later.
3528 * Returns NULL when failed.
3529 */
3530 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003531save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003532{
3533 struct cmdline_info *p;
3534
3535 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3536 if (p != NULL)
3537 save_cmdline(p);
3538 return (char_u *)p;
3539}
3540
3541/*
3542 * Restore the command line from the return value of save_cmdline_alloc().
3543 */
3544 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003545restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003546{
3547 if (p != NULL)
3548 {
3549 restore_cmdline((struct cmdline_info *)p);
3550 vim_free(p);
3551 }
3552}
3553#endif
3554
Bram Moolenaar8299df92004-07-10 09:47:34 +00003555/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003556 * Paste a yank register into the command line.
3557 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003558 * insert_reg() can't be used here, because special characters from the
3559 * register contents will be interpreted as commands.
3560 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003561 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003562 */
3563 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003564cmdline_paste(
3565 int regname,
3566 int literally, /* Insert text literally instead of "as typed" */
3567 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003568{
3569 long i;
3570 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003571 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003572 int allocated;
3573 struct cmdline_info save_ccline;
3574
3575 /* check for valid regname; also accept special characters for CTRL-R in
3576 * the command line */
3577 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003578 && regname != Ctrl_A && regname != Ctrl_L
3579 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003580 return FAIL;
3581
3582 /* A register containing CTRL-R can cause an endless loop. Allow using
3583 * CTRL-C to break the loop. */
3584 line_breakcheck();
3585 if (got_int)
3586 return FAIL;
3587
3588#ifdef FEAT_CLIPBOARD
3589 regname = may_get_selection(regname);
3590#endif
3591
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003592 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003593 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003594 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003595 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003597 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003598 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003599
3600 if (i)
3601 {
3602 /* Got the value of a special register in "arg". */
3603 if (arg == NULL)
3604 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003605
3606 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3607 * part of the word. */
3608 p = arg;
3609 if (p_is && regname == Ctrl_W)
3610 {
3611 char_u *w;
3612 int len;
3613
3614 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003615 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003616 {
3617#ifdef FEAT_MBYTE
3618 if (has_mbyte)
3619 {
3620 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3621 if (!vim_iswordc(mb_ptr2char(w - len)))
3622 break;
3623 w -= len;
3624 }
3625 else
3626#endif
3627 {
3628 if (!vim_iswordc(w[-1]))
3629 break;
3630 --w;
3631 }
3632 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003633 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003634 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3635 p += len;
3636 }
3637
3638 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003639 if (allocated)
3640 vim_free(arg);
3641 return OK;
3642 }
3643
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003644 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003645}
3646
3647/*
3648 * Put a string on the command line.
3649 * When "literally" is TRUE, insert literally.
3650 * When "literally" is FALSE, insert as typed, but don't leave the command
3651 * line.
3652 */
3653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003654cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003655{
3656 int c, cv;
3657
3658 if (literally)
3659 put_on_cmdline(s, -1, TRUE);
3660 else
3661 while (*s != NUL)
3662 {
3663 cv = *s;
3664 if (cv == Ctrl_V && s[1])
3665 ++s;
3666#ifdef FEAT_MBYTE
3667 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003668 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003669 else
3670#endif
3671 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003672 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3673 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003674#ifdef UNIX
3675 || c == intr_char
3676#endif
3677 || (c == Ctrl_BSL && *s == Ctrl_N))
3678 stuffcharReadbuff(Ctrl_V);
3679 stuffcharReadbuff(c);
3680 }
3681}
3682
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683#ifdef FEAT_WILDMENU
3684/*
3685 * Delete characters on the command line, from "from" to the current
3686 * position.
3687 */
3688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003689cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3692 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3693 ccline.cmdlen -= ccline.cmdpos - from;
3694 ccline.cmdpos = from;
3695}
3696#endif
3697
3698/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003699 * This function is called when the screen size changes and with incremental
3700 * search and in other situations where the command line may have been
3701 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 */
3703 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003704redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003706 redrawcmdline_ex(TRUE);
3707}
3708
3709 void
3710redrawcmdline_ex(int do_compute_cmdrow)
3711{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 if (cmd_silent)
3713 return;
3714 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003715 if (do_compute_cmdrow)
3716 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 redrawcmd();
3718 cursorcmd();
3719}
3720
3721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003722redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724 int i;
3725
3726 if (cmd_silent)
3727 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003728 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 msg_putchar(ccline.cmdfirstc);
3730 if (ccline.cmdprompt != NULL)
3731 {
3732 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3733 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3734 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003735 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 --ccline.cmdindent;
3737 }
3738 else
3739 for (i = ccline.cmdindent; i > 0; --i)
3740 msg_putchar(' ');
3741}
3742
3743/*
3744 * Redraw what is currently on the command line.
3745 */
3746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003747redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 if (cmd_silent)
3750 return;
3751
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003752 /* when 'incsearch' is set there may be no command line while redrawing */
3753 if (ccline.cmdbuff == NULL)
3754 {
3755 windgoto(cmdline_row, 0);
3756 msg_clr_eos();
3757 return;
3758 }
3759
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 msg_start();
3761 redrawcmdprompt();
3762
3763 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3764 msg_no_more = TRUE;
3765 draw_cmdline(0, ccline.cmdlen);
3766 msg_clr_eos();
3767 msg_no_more = FALSE;
3768
3769 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003770 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003771 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * An emsg() before may have set msg_scroll. This is used in normal mode,
3775 * in cmdline mode we can reset them now.
3776 */
3777 msg_scroll = FALSE; /* next message overwrites cmdline */
3778
3779 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3780 * in cmdline mode */
3781 skip_redraw = FALSE;
3782}
3783
3784 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003785compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003787 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 cmdline_row = Rows - 1;
3789 else
3790 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003791 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792}
3793
3794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003795cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
3797 if (cmd_silent)
3798 return;
3799
3800#ifdef FEAT_RIGHTLEFT
3801 if (cmdmsg_rl)
3802 {
3803 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3804 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3805 if (msg_row <= 0)
3806 msg_row = Rows - 1;
3807 }
3808 else
3809#endif
3810 {
3811 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3812 msg_col = ccline.cmdspos % (int)Columns;
3813 if (msg_row >= Rows)
3814 msg_row = Rows - 1;
3815 }
3816
3817 windgoto(msg_row, msg_col);
3818#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003819 if (p_imst == IM_ON_THE_SPOT)
3820 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821#endif
3822#ifdef MCH_CURSOR_SHAPE
3823 mch_update_cursor();
3824#endif
3825}
3826
3827 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003828gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 msg_start();
3831#ifdef FEAT_RIGHTLEFT
3832 if (cmdmsg_rl)
3833 msg_col = Columns - 1;
3834 else
3835#endif
3836 msg_col = 0; /* always start in column 0 */
3837 if (clr) /* clear the bottom line(s) */
3838 msg_clr_eos(); /* will reset clear_cmdline */
3839 windgoto(cmdline_row, 0);
3840}
3841
3842/*
3843 * Check the word in front of the cursor for an abbreviation.
3844 * Called when the non-id character "c" has been entered.
3845 * When an abbreviation is recognized it is removed from the text with
3846 * backspaces and the replacement string is inserted, followed by "c".
3847 */
3848 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003849ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003851 int spos = 0;
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3854 return FALSE;
3855
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003856 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3857 * Actually accepts any mark. */
3858 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3859 spos++;
3860 if (ccline.cmdlen - spos > 5
3861 && ccline.cmdbuff[spos] == '\''
3862 && ccline.cmdbuff[spos + 2] == ','
3863 && ccline.cmdbuff[spos + 3] == '\'')
3864 spos += 5;
3865 else
3866 /* check abbreviation from the beginning of the commandline */
3867 spos = 0;
3868
3869 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870}
3871
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003872#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3873 static int
3874#ifdef __BORLANDC__
3875_RTLENTRYF
3876#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003877sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003878{
3879 char_u *p1 = *(char_u **)s1;
3880 char_u *p2 = *(char_u **)s2;
3881
3882 if (*p1 != '<' && *p2 == '<') return -1;
3883 if (*p1 == '<' && *p2 != '<') return 1;
3884 return STRCMP(p1, p2);
3885}
3886#endif
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
3889 * Return FAIL if this is not an appropriate context in which to do
3890 * completion of anything, return OK if it is (even if there are no matches).
3891 * For the caller, this means that the character is just passed through like a
3892 * normal character (instead of being expanded). This allows :s/^I^D etc.
3893 */
3894 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003895nextwild(
3896 expand_T *xp,
3897 int type,
3898 int options, /* extra options for ExpandOne() */
3899 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 int i, j;
3902 char_u *p1;
3903 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 int difflen;
3905 int v;
3906
3907 if (xp->xp_numfiles == -1)
3908 {
3909 set_expand_context(xp);
3910 cmd_showtail = expand_showtail(xp);
3911 }
3912
3913 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3914 {
3915 beep_flush();
3916 return OK; /* Something illegal on command line */
3917 }
3918 if (xp->xp_context == EXPAND_NOTHING)
3919 {
3920 /* Caller can use the character as a normal char instead */
3921 return FAIL;
3922 }
3923
3924 MSG_PUTS("..."); /* show that we are busy */
3925 out_flush();
3926
3927 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003928 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 if (type == WILD_NEXT || type == WILD_PREV)
3931 {
3932 /*
3933 * Get next/previous match for a previous expanded pattern.
3934 */
3935 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3936 }
3937 else
3938 {
3939 /*
3940 * Translate string into pattern and expand it.
3941 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003942 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3943 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 p2 = NULL;
3945 else
3946 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003947 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003948 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3949 if (escape)
3950 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003951
3952 if (p_wic)
3953 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003954 p2 = ExpandOne(xp, p1,
3955 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003956 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003958 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (p2 != NULL && type == WILD_LONGEST)
3960 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003961 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (ccline.cmdbuff[i + j] == '*'
3963 || ccline.cmdbuff[i + j] == '?')
3964 break;
3965 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003966 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 }
3969 }
3970
3971 if (p2 != NULL && !got_int)
3972 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003973 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003974 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003976 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 xp->xp_pattern = ccline.cmdbuff + i;
3978 }
3979 else
3980 v = OK;
3981 if (v == OK)
3982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003983 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3984 &ccline.cmdbuff[ccline.cmdpos],
3985 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3986 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 ccline.cmdlen += difflen;
3988 ccline.cmdpos += difflen;
3989 }
3990 }
3991 vim_free(p2);
3992
3993 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003994 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 /* When expanding a ":map" command and no matches are found, assume that
3997 * the key is supposed to be inserted literally */
3998 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3999 return FAIL;
4000
4001 if (xp->xp_numfiles <= 0 && p2 == NULL)
4002 beep_flush();
4003 else if (xp->xp_numfiles == 1)
4004 /* free expanded pattern */
4005 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4006
4007 return OK;
4008}
4009
4010/*
4011 * Do wildcard expansion on the string 'str'.
4012 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004013 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 * Return NULL for failure.
4015 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004016 * "orig" is the originally expanded string, copied to allocated memory. It
4017 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4018 * WILD_PREV "orig" should be NULL.
4019 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004020 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4021 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 *
4023 * mode = WILD_FREE: just free previously expanded matches
4024 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4025 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4026 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4027 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4028 * mode = WILD_ALL: return all matches concatenated
4029 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004030 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 *
4032 * options = WILD_LIST_NOTFOUND: list entries without a match
4033 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4034 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4035 * options = WILD_NO_BEEP: Don't beep for multiple matches
4036 * options = WILD_ADD_SLASH: add a slash after directory names
4037 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4038 * options = WILD_SILENT: don't print warning messages
4039 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004040 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 *
4042 * The variables xp->xp_context and xp->xp_backslash must have been set!
4043 */
4044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004045ExpandOne(
4046 expand_T *xp,
4047 char_u *str,
4048 char_u *orig, /* allocated copy of original of expanded string */
4049 int options,
4050 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051{
4052 char_u *ss = NULL;
4053 static int findex;
4054 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004055 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 int i;
4057 long_u len;
4058 int non_suf_match; /* number without matching suffix */
4059
4060 /*
4061 * first handle the case of using an old match
4062 */
4063 if (mode == WILD_NEXT || mode == WILD_PREV)
4064 {
4065 if (xp->xp_numfiles > 0)
4066 {
4067 if (mode == WILD_PREV)
4068 {
4069 if (findex == -1)
4070 findex = xp->xp_numfiles;
4071 --findex;
4072 }
4073 else /* mode == WILD_NEXT */
4074 ++findex;
4075
4076 /*
4077 * When wrapping around, return the original string, set findex to
4078 * -1.
4079 */
4080 if (findex < 0)
4081 {
4082 if (orig_save == NULL)
4083 findex = xp->xp_numfiles - 1;
4084 else
4085 findex = -1;
4086 }
4087 if (findex >= xp->xp_numfiles)
4088 {
4089 if (orig_save == NULL)
4090 findex = 0;
4091 else
4092 findex = -1;
4093 }
4094#ifdef FEAT_WILDMENU
4095 if (p_wmnu)
4096 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4097 findex, cmd_showtail);
4098#endif
4099 if (findex == -1)
4100 return vim_strsave(orig_save);
4101 return vim_strsave(xp->xp_files[findex]);
4102 }
4103 else
4104 return NULL;
4105 }
4106
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004107 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4109 {
4110 FreeWild(xp->xp_numfiles, xp->xp_files);
4111 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004112 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114 findex = 0;
4115
4116 if (mode == WILD_FREE) /* only release file name */
4117 return NULL;
4118
4119 if (xp->xp_numfiles == -1)
4120 {
4121 vim_free(orig_save);
4122 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004123 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125 /*
4126 * Do the expansion.
4127 */
4128 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4129 options) == FAIL)
4130 {
4131#ifdef FNAME_ILLEGAL
4132 /* Illegal file name has been silently skipped. But when there
4133 * are wildcards, the real problem is that there was no match,
4134 * causing the pattern to be added, which has illegal characters.
4135 */
4136 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4137 EMSG2(_(e_nomatch2), str);
4138#endif
4139 }
4140 else if (xp->xp_numfiles == 0)
4141 {
4142 if (!(options & WILD_SILENT))
4143 EMSG2(_(e_nomatch2), str);
4144 }
4145 else
4146 {
4147 /* Escape the matches for use on the command line. */
4148 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4149
4150 /*
4151 * Check for matching suffixes in file names.
4152 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004153 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4154 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
4156 if (xp->xp_numfiles)
4157 non_suf_match = xp->xp_numfiles;
4158 else
4159 non_suf_match = 1;
4160 if ((xp->xp_context == EXPAND_FILES
4161 || xp->xp_context == EXPAND_DIRECTORIES)
4162 && xp->xp_numfiles > 1)
4163 {
4164 /*
4165 * More than one match; check suffix.
4166 * The files will have been sorted on matching suffix in
4167 * expand_wildcards, only need to check the first two.
4168 */
4169 non_suf_match = 0;
4170 for (i = 0; i < 2; ++i)
4171 if (match_suffix(xp->xp_files[i]))
4172 ++non_suf_match;
4173 }
4174 if (non_suf_match != 1)
4175 {
4176 /* Can we ever get here unless it's while expanding
4177 * interactively? If not, we can get rid of this all
4178 * together. Don't really want to wait for this message
4179 * (and possibly have to hit return to continue!).
4180 */
4181 if (!(options & WILD_SILENT))
4182 EMSG(_(e_toomany));
4183 else if (!(options & WILD_NO_BEEP))
4184 beep_flush();
4185 }
4186 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4187 ss = vim_strsave(xp->xp_files[0]);
4188 }
4189 }
4190 }
4191
4192 /* Find longest common part */
4193 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4194 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004195 int mb_len = 1;
4196 int c0, ci;
4197
4198 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004200#ifdef FEAT_MBYTE
4201 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004203 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4204 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4205 }
4206 else
4207#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004208 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004209 for (i = 1; i < xp->xp_numfiles; ++i)
4210 {
4211#ifdef FEAT_MBYTE
4212 if (has_mbyte)
4213 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4214 else
4215#endif
4216 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004217 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004219 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004220 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004222 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 break;
4224 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004225 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 break;
4227 }
4228 if (i < xp->xp_numfiles)
4229 {
4230 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004231 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 break;
4233 }
4234 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 ss = alloc((unsigned)len + 1);
4237 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004238 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 findex = -1; /* next p_wc gets first one */
4240 }
4241
4242 /* Concatenate all matching names */
4243 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4244 {
4245 len = 0;
4246 for (i = 0; i < xp->xp_numfiles; ++i)
4247 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4248 ss = lalloc(len, TRUE);
4249 if (ss != NULL)
4250 {
4251 *ss = NUL;
4252 for (i = 0; i < xp->xp_numfiles; ++i)
4253 {
4254 STRCAT(ss, xp->xp_files[i]);
4255 if (i != xp->xp_numfiles - 1)
4256 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4257 }
4258 }
4259 }
4260
4261 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4262 ExpandCleanup(xp);
4263
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004264 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004265 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004266 vim_free(orig);
4267
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 return ss;
4269}
4270
4271/*
4272 * Prepare an expand structure for use.
4273 */
4274 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004275ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004277 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004278 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004280#ifndef BACKSLASH_IN_FILENAME
4281 xp->xp_shell = FALSE;
4282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 xp->xp_numfiles = -1;
4284 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004285#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4286 xp->xp_arg = NULL;
4287#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004288 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289}
4290
4291/*
4292 * Cleanup an expand structure after use.
4293 */
4294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004295ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296{
4297 if (xp->xp_numfiles >= 0)
4298 {
4299 FreeWild(xp->xp_numfiles, xp->xp_files);
4300 xp->xp_numfiles = -1;
4301 }
4302}
4303
4304 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004305ExpandEscape(
4306 expand_T *xp,
4307 char_u *str,
4308 int numfiles,
4309 char_u **files,
4310 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 int i;
4313 char_u *p;
4314
4315 /*
4316 * May change home directory back to "~"
4317 */
4318 if (options & WILD_HOME_REPLACE)
4319 tilde_replace(str, numfiles, files);
4320
4321 if (options & WILD_ESCAPE)
4322 {
4323 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004324 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004325 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 || xp->xp_context == EXPAND_BUFFERS
4327 || xp->xp_context == EXPAND_DIRECTORIES)
4328 {
4329 /*
4330 * Insert a backslash into a file name before a space, \, %, #
4331 * and wildmatch characters, except '~'.
4332 */
4333 for (i = 0; i < numfiles; ++i)
4334 {
4335 /* for ":set path=" we need to escape spaces twice */
4336 if (xp->xp_backslash == XP_BS_THREE)
4337 {
4338 p = vim_strsave_escaped(files[i], (char_u *)" ");
4339 if (p != NULL)
4340 {
4341 vim_free(files[i]);
4342 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004343#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 p = vim_strsave_escaped(files[i], (char_u *)" ");
4345 if (p != NULL)
4346 {
4347 vim_free(files[i]);
4348 files[i] = p;
4349 }
4350#endif
4351 }
4352 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004353#ifdef BACKSLASH_IN_FILENAME
4354 p = vim_strsave_fnameescape(files[i], FALSE);
4355#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004356 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (p != NULL)
4359 {
4360 vim_free(files[i]);
4361 files[i] = p;
4362 }
4363
4364 /* If 'str' starts with "\~", replace "~" at start of
4365 * files[i] with "\~". */
4366 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004367 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004370
4371 /* If the first file starts with a '+' escape it. Otherwise it
4372 * could be seen as "+cmd". */
4373 if (*files[0] == '+')
4374 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 else if (xp->xp_context == EXPAND_TAGS)
4377 {
4378 /*
4379 * Insert a backslash before characters in a tag name that
4380 * would terminate the ":tag" command.
4381 */
4382 for (i = 0; i < numfiles; ++i)
4383 {
4384 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4385 if (p != NULL)
4386 {
4387 vim_free(files[i]);
4388 files[i] = p;
4389 }
4390 }
4391 }
4392 }
4393}
4394
4395/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004396 * Escape special characters in "fname" for when used as a file name argument
4397 * after a Vim command, or, when "shell" is non-zero, a shell command.
4398 * Returns the result in allocated memory.
4399 */
4400 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004401vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004402{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004403 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004404#ifdef BACKSLASH_IN_FILENAME
4405 char_u buf[20];
4406 int j = 0;
4407
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004408 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004409 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004410 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004411 buf[j++] = *p;
4412 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004413 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004414#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004415 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4416 if (shell && csh_like_shell() && p != NULL)
4417 {
4418 char_u *s;
4419
4420 /* For csh and similar shells need to put two backslashes before '!'.
4421 * One is taken by Vim, one by the shell. */
4422 s = vim_strsave_escaped(p, (char_u *)"!");
4423 vim_free(p);
4424 p = s;
4425 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004426#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004427
4428 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4429 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004430 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004431 escape_fname(&p);
4432
4433 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004434}
4435
4436/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004437 * Put a backslash before the file name in "pp", which is in allocated memory.
4438 */
4439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004440escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004441{
4442 char_u *p;
4443
4444 p = alloc((unsigned)(STRLEN(*pp) + 2));
4445 if (p != NULL)
4446 {
4447 p[0] = '\\';
4448 STRCPY(p + 1, *pp);
4449 vim_free(*pp);
4450 *pp = p;
4451 }
4452}
4453
4454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 * For each file name in files[num_files]:
4456 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4457 */
4458 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004459tilde_replace(
4460 char_u *orig_pat,
4461 int num_files,
4462 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463{
4464 int i;
4465 char_u *p;
4466
4467 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4468 {
4469 for (i = 0; i < num_files; ++i)
4470 {
4471 p = home_replace_save(NULL, files[i]);
4472 if (p != NULL)
4473 {
4474 vim_free(files[i]);
4475 files[i] = p;
4476 }
4477 }
4478 }
4479}
4480
4481/*
4482 * Show all matches for completion on the command line.
4483 * Returns EXPAND_NOTHING when the character that triggered expansion should
4484 * be inserted like a normal character.
4485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004487showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4490 int num_files;
4491 char_u **files_found;
4492 int i, j, k;
4493 int maxlen;
4494 int lines;
4495 int columns;
4496 char_u *p;
4497 int lastlen;
4498 int attr;
4499 int showtail;
4500
4501 if (xp->xp_numfiles == -1)
4502 {
4503 set_expand_context(xp);
4504 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4505 &num_files, &files_found);
4506 showtail = expand_showtail(xp);
4507 if (i != EXPAND_OK)
4508 return i;
4509
4510 }
4511 else
4512 {
4513 num_files = xp->xp_numfiles;
4514 files_found = xp->xp_files;
4515 showtail = cmd_showtail;
4516 }
4517
4518#ifdef FEAT_WILDMENU
4519 if (!wildmenu)
4520 {
4521#endif
4522 msg_didany = FALSE; /* lines_left will be set */
4523 msg_start(); /* prepare for paging */
4524 msg_putchar('\n');
4525 out_flush();
4526 cmdline_row = msg_row;
4527 msg_didany = FALSE; /* lines_left will be set again */
4528 msg_start(); /* prepare for paging */
4529#ifdef FEAT_WILDMENU
4530 }
4531#endif
4532
4533 if (got_int)
4534 got_int = FALSE; /* only int. the completion, not the cmd line */
4535#ifdef FEAT_WILDMENU
4536 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004537 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#endif
4539 else
4540 {
4541 /* find the length of the longest file name */
4542 maxlen = 0;
4543 for (i = 0; i < num_files; ++i)
4544 {
4545 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004546 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 || xp->xp_context == EXPAND_BUFFERS))
4548 {
4549 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4550 j = vim_strsize(NameBuff);
4551 }
4552 else
4553 j = vim_strsize(L_SHOWFILE(i));
4554 if (j > maxlen)
4555 maxlen = j;
4556 }
4557
4558 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4559 lines = num_files;
4560 else
4561 {
4562 /* compute the number of columns and lines for the listing */
4563 maxlen += 2; /* two spaces between file names */
4564 columns = ((int)Columns + 2) / maxlen;
4565 if (columns < 1)
4566 columns = 1;
4567 lines = (num_files + columns - 1) / columns;
4568 }
4569
Bram Moolenaar8820b482017-03-16 17:23:31 +01004570 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
4572 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4573 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004574 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 msg_clr_eos();
4576 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004577 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579
4580 /* list the files line by line */
4581 for (i = 0; i < lines; ++i)
4582 {
4583 lastlen = 999;
4584 for (k = i; k < num_files; k += lines)
4585 {
4586 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4587 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004588 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 p = files_found[k] + STRLEN(files_found[k]) + 1;
4590 msg_advance(maxlen + 1);
4591 msg_puts(p);
4592 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004593 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 break;
4595 }
4596 for (j = maxlen - lastlen; --j >= 0; )
4597 msg_putchar(' ');
4598 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004599 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 || xp->xp_context == EXPAND_BUFFERS)
4601 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004602 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004603 if (xp->xp_numfiles != -1)
4604 {
4605 char_u *halved_slash;
4606 char_u *exp_path;
4607
4608 /* Expansion was done before and special characters
4609 * were escaped, need to halve backslashes. Also
4610 * $HOME has been replaced with ~/. */
4611 exp_path = expand_env_save_opt(files_found[k], TRUE);
4612 halved_slash = backslash_halve_save(
4613 exp_path != NULL ? exp_path : files_found[k]);
4614 j = mch_isdir(halved_slash != NULL ? halved_slash
4615 : files_found[k]);
4616 vim_free(exp_path);
4617 vim_free(halved_slash);
4618 }
4619 else
4620 /* Expansion was done here, file names are literal. */
4621 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (showtail)
4623 p = L_SHOWFILE(k);
4624 else
4625 {
4626 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4627 TRUE);
4628 p = NameBuff;
4629 }
4630 }
4631 else
4632 {
4633 j = FALSE;
4634 p = L_SHOWFILE(k);
4635 }
4636 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4637 }
4638 if (msg_col > 0) /* when not wrapped around */
4639 {
4640 msg_clr_eos();
4641 msg_putchar('\n');
4642 }
4643 out_flush(); /* show one line at a time */
4644 if (got_int)
4645 {
4646 got_int = FALSE;
4647 break;
4648 }
4649 }
4650
4651 /*
4652 * we redraw the command below the lines that we have just listed
4653 * This is a bit tricky, but it saves a lot of screen updating.
4654 */
4655 cmdline_row = msg_row; /* will put it back later */
4656 }
4657
4658 if (xp->xp_numfiles == -1)
4659 FreeWild(num_files, files_found);
4660
4661 return EXPAND_OK;
4662}
4663
4664/*
4665 * Private gettail for showmatches() (and win_redr_status_matches()):
4666 * Find tail of file name path, but ignore trailing "/".
4667 */
4668 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004669sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 char_u *p;
4672 char_u *t = s;
4673 int had_sep = FALSE;
4674
4675 for (p = s; *p != NUL; )
4676 {
4677 if (vim_ispathsep(*p)
4678#ifdef BACKSLASH_IN_FILENAME
4679 && !rem_backslash(p)
4680#endif
4681 )
4682 had_sep = TRUE;
4683 else if (had_sep)
4684 {
4685 t = p;
4686 had_sep = FALSE;
4687 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004688 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690 return t;
4691}
4692
4693/*
4694 * Return TRUE if we only need to show the tail of completion matches.
4695 * When not completing file names or there is a wildcard in the path FALSE is
4696 * returned.
4697 */
4698 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004699expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
4701 char_u *s;
4702 char_u *end;
4703
4704 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004705 if (xp->xp_context != EXPAND_FILES
4706 && xp->xp_context != EXPAND_SHELLCMD
4707 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return FALSE;
4709
4710 end = gettail(xp->xp_pattern);
4711 if (end == xp->xp_pattern) /* there is no path separator */
4712 return FALSE;
4713
4714 for (s = xp->xp_pattern; s < end; s++)
4715 {
4716 /* Skip escaped wildcards. Only when the backslash is not a path
4717 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4718 if (rem_backslash(s))
4719 ++s;
4720 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4721 return FALSE;
4722 }
4723 return TRUE;
4724}
4725
4726/*
4727 * Prepare a string for expansion.
4728 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004729 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 * When expanding other names: The string will be used with regcomp(). Copy
4731 * the name into allocated memory and prepend "^".
4732 */
4733 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004734addstar(
4735 char_u *fname,
4736 int len,
4737 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
4739 char_u *retval;
4740 int i, j;
4741 int new_len;
4742 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004743 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004745 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004746 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004747 && context != EXPAND_SHELLCMD
4748 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
4750 /*
4751 * Matching will be done internally (on something other than files).
4752 * So we convert the file-matching-type wildcards into our kind for
4753 * use with vim_regcomp(). First work out how long it will be:
4754 */
4755
4756 /* For help tags the translation is done in find_help_tags().
4757 * For a tag pattern starting with "/" no translation is needed. */
4758 if (context == EXPAND_HELP
4759 || context == EXPAND_COLORS
4760 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004761 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004762 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004763 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004764 || ((context == EXPAND_TAGS_LISTFILES
4765 || context == EXPAND_TAGS)
4766 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 retval = vim_strnsave(fname, len);
4768 else
4769 {
4770 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4771 for (i = 0; i < len; i++)
4772 {
4773 if (fname[i] == '*' || fname[i] == '~')
4774 new_len++; /* '*' needs to be replaced by ".*"
4775 '~' needs to be replaced by "\~" */
4776
4777 /* Buffer names are like file names. "." should be literal */
4778 if (context == EXPAND_BUFFERS && fname[i] == '.')
4779 new_len++; /* "." becomes "\." */
4780
4781 /* Custom expansion takes care of special things, match
4782 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004783 if ((context == EXPAND_USER_DEFINED
4784 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 new_len++; /* '\' becomes "\\" */
4786 }
4787 retval = alloc(new_len);
4788 if (retval != NULL)
4789 {
4790 retval[0] = '^';
4791 j = 1;
4792 for (i = 0; i < len; i++, j++)
4793 {
4794 /* Skip backslash. But why? At least keep it for custom
4795 * expansion. */
4796 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004797 && context != EXPAND_USER_LIST
4798 && fname[i] == '\\'
4799 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 break;
4801
4802 switch (fname[i])
4803 {
4804 case '*': retval[j++] = '.';
4805 break;
4806 case '~': retval[j++] = '\\';
4807 break;
4808 case '?': retval[j] = '.';
4809 continue;
4810 case '.': if (context == EXPAND_BUFFERS)
4811 retval[j++] = '\\';
4812 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004813 case '\\': if (context == EXPAND_USER_DEFINED
4814 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 retval[j++] = '\\';
4816 break;
4817 }
4818 retval[j] = fname[i];
4819 }
4820 retval[j] = NUL;
4821 }
4822 }
4823 }
4824 else
4825 {
4826 retval = alloc(len + 4);
4827 if (retval != NULL)
4828 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004829 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
4831 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004832 * Don't add a star to *, ~, ~user, $var or `cmd`.
4833 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 * ~ would be at the start of the file name, but not the tail.
4835 * $ could be anywhere in the tail.
4836 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004837 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 */
4839 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004840 ends_in_star = (len > 0 && retval[len - 1] == '*');
4841#ifndef BACKSLASH_IN_FILENAME
4842 for (i = len - 2; i >= 0; --i)
4843 {
4844 if (retval[i] != '\\')
4845 break;
4846 ends_in_star = !ends_in_star;
4847 }
4848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004850 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 && vim_strchr(tail, '$') == NULL
4852 && vim_strchr(retval, '`') == NULL)
4853 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004854 else if (len > 0 && retval[len - 1] == '$')
4855 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 retval[len] = NUL;
4857 }
4858 }
4859 return retval;
4860}
4861
4862/*
4863 * Must parse the command line so far to work out what context we are in.
4864 * Completion can then be done based on that context.
4865 * This routine sets the variables:
4866 * xp->xp_pattern The start of the pattern to be expanded within
4867 * the command line (ends at the cursor).
4868 * xp->xp_context The type of thing to expand. Will be one of:
4869 *
4870 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4871 * the command line, like an unknown command. Caller
4872 * should beep.
4873 * EXPAND_NOTHING Unrecognised context for completion, use char like
4874 * a normal char, rather than for completion. eg
4875 * :s/^I/
4876 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4877 * it.
4878 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4879 * EXPAND_FILES After command with XFILE set, or after setting
4880 * with P_EXPAND set. eg :e ^I, :w>>^I
4881 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4882 * when we know only directories are of interest. eg
4883 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004884 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4886 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4887 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4888 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4889 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4890 * EXPAND_EVENTS Complete event names
4891 * EXPAND_SYNTAX Complete :syntax command arguments
4892 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4893 * EXPAND_AUGROUP Complete autocommand group names
4894 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4895 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4896 * eg :unmap a^I , :cunab x^I
4897 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4898 * eg :call sub^I
4899 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4900 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4901 * names in expressions, eg :while s^I
4902 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004903 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 */
4905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004906set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004908 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (ccline.cmdfirstc != ':'
4910#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004911 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004912 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
4914 )
4915 {
4916 xp->xp_context = EXPAND_NOTHING;
4917 return;
4918 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004919 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920}
4921
4922 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004923set_cmd_context(
4924 expand_T *xp,
4925 char_u *str, /* start of command line */
4926 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004927 int col, /* position of cursor */
4928 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
4930 int old_char = NUL;
4931 char_u *nextcomm;
4932
4933 /*
4934 * Avoid a UMR warning from Purify, only save the character if it has been
4935 * written before.
4936 */
4937 if (col < len)
4938 old_char = str[col];
4939 str[col] = NUL;
4940 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004941
4942#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004943 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004944 {
4945# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004946 /* pass CMD_SIZE because there is no real command */
4947 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004948# endif
4949 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004950 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004951 {
4952 xp->xp_context = ccline.xp_context;
4953 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004954# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004955 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004956# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004957 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004958 else
4959#endif
4960 while (nextcomm != NULL)
4961 nextcomm = set_one_cmd_context(xp, nextcomm);
4962
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004963 /* Store the string here so that call_user_expand_func() can get to them
4964 * easily. */
4965 xp->xp_line = str;
4966 xp->xp_col = col;
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 str[col] = old_char;
4969}
4970
4971/*
4972 * Expand the command line "str" from context "xp".
4973 * "xp" must have been set by set_cmd_context().
4974 * xp->xp_pattern points into "str", to where the text that is to be expanded
4975 * starts.
4976 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4977 * cursor.
4978 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4979 * key that triggered expansion literally.
4980 * Returns EXPAND_OK otherwise.
4981 */
4982 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004983expand_cmdline(
4984 expand_T *xp,
4985 char_u *str, /* start of command line */
4986 int col, /* position of cursor */
4987 int *matchcount, /* return: nr of matches */
4988 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004991 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4994 {
4995 beep_flush();
4996 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4997 }
4998 if (xp->xp_context == EXPAND_NOTHING)
4999 {
5000 /* Caller can use the character as a normal char instead */
5001 return EXPAND_NOTHING;
5002 }
5003
5004 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005005 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5006 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (file_str == NULL)
5008 return EXPAND_UNSUCCESSFUL;
5009
Bram Moolenaar94950a92010-12-02 16:01:29 +01005010 if (p_wic)
5011 options += WILD_ICASE;
5012
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005014 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
5016 *matchcount = 0;
5017 *matches = NULL;
5018 }
5019 vim_free(file_str);
5020
5021 return EXPAND_OK;
5022}
5023
5024#ifdef FEAT_MULTI_LANG
5025/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005026 * Cleanup matches for help tags:
5027 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5028 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005030static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
5032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005033cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035 int i, j;
5036 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005037 char_u buf[4];
5038 char_u *p = buf;
5039
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005040 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005041 {
5042 *p++ = '@';
5043 *p++ = p_hlg[0];
5044 *p++ = p_hlg[1];
5045 }
5046 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 for (i = 0; i < num_file; ++i)
5049 {
5050 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005051 if (len <= 0)
5052 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005053 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 /* Sorting on priority means the same item in another language may
5056 * be anywhere. Search all items for a match up to the "@en". */
5057 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005058 if (j != i && (int)STRLEN(file[j]) == len + 3
5059 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005062 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 file[i][len] = NUL;
5064 }
5065 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005066
5067 if (*buf != NUL)
5068 for (i = 0; i < num_file; ++i)
5069 {
5070 len = (int)STRLEN(file[i]) - 3;
5071 if (len <= 0)
5072 continue;
5073 if (STRCMP(file[i] + len, buf) == 0)
5074 {
5075 /* remove the default language */
5076 file[i][len] = NUL;
5077 }
5078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079}
5080#endif
5081
5082/*
5083 * Do the expansion based on xp->xp_context and "pat".
5084 */
5085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005086ExpandFromContext(
5087 expand_T *xp,
5088 char_u *pat,
5089 int *num_file,
5090 char_u ***file,
5091 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093#ifdef FEAT_CMDL_COMPL
5094 regmatch_T regmatch;
5095#endif
5096 int ret;
5097 int flags;
5098
5099 flags = EW_DIR; /* include directories */
5100 if (options & WILD_LIST_NOTFOUND)
5101 flags |= EW_NOTFOUND;
5102 if (options & WILD_ADD_SLASH)
5103 flags |= EW_ADDSLASH;
5104 if (options & WILD_KEEP_ALL)
5105 flags |= EW_KEEPALL;
5106 if (options & WILD_SILENT)
5107 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005108 if (options & WILD_ALLLINKS)
5109 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005111 if (xp->xp_context == EXPAND_FILES
5112 || xp->xp_context == EXPAND_DIRECTORIES
5113 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
5115 /*
5116 * Expand file or directory names.
5117 */
5118 int free_pat = FALSE;
5119 int i;
5120
5121 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5122 * space */
5123 if (xp->xp_backslash != XP_BS_NONE)
5124 {
5125 free_pat = TRUE;
5126 pat = vim_strsave(pat);
5127 for (i = 0; pat[i]; ++i)
5128 if (pat[i] == '\\')
5129 {
5130 if (xp->xp_backslash == XP_BS_THREE
5131 && pat[i + 1] == '\\'
5132 && pat[i + 2] == '\\'
5133 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005134 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (xp->xp_backslash == XP_BS_ONE
5136 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005137 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 }
5140
5141 if (xp->xp_context == EXPAND_FILES)
5142 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005143 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5144 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 else
5146 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005147 if (options & WILD_ICASE)
5148 flags |= EW_ICASE;
5149
Bram Moolenaard7834d32009-12-02 16:14:36 +00005150 /* Expand wildcards, supporting %:h and the like. */
5151 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (free_pat)
5153 vim_free(pat);
5154 return ret;
5155 }
5156
5157 *file = (char_u **)"";
5158 *num_file = 0;
5159 if (xp->xp_context == EXPAND_HELP)
5160 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005161 /* With an empty argument we would get all the help tags, which is
5162 * very slow. Get matches for "help" instead. */
5163 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5164 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166#ifdef FEAT_MULTI_LANG
5167 cleanup_help_tags(*num_file, *file);
5168#endif
5169 return OK;
5170 }
5171 return FAIL;
5172 }
5173
5174#ifndef FEAT_CMDL_COMPL
5175 return FAIL;
5176#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005177 if (xp->xp_context == EXPAND_SHELLCMD)
5178 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (xp->xp_context == EXPAND_OLD_SETTING)
5180 return ExpandOldSetting(num_file, file);
5181 if (xp->xp_context == EXPAND_BUFFERS)
5182 return ExpandBufnames(pat, num_file, file, options);
5183 if (xp->xp_context == EXPAND_TAGS
5184 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5185 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5186 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005187 {
5188 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005189 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5190 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005193 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005194 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005195 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005196 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005197 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005198 {
5199 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005200 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005201 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005202 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005203 {
5204 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005205 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005206 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005207# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5208 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005209 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005210# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005211 if (xp->xp_context == EXPAND_PACKADD)
5212 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
5214 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5215 if (regmatch.regprog == NULL)
5216 return FAIL;
5217
5218 /* set ignore-case according to p_ic, p_scs and pat */
5219 regmatch.rm_ic = ignorecase(pat);
5220
5221 if (xp->xp_context == EXPAND_SETTINGS
5222 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5223 ret = ExpandSettings(xp, &regmatch, num_file, file);
5224 else if (xp->xp_context == EXPAND_MAPPINGS)
5225 ret = ExpandMappings(&regmatch, num_file, file);
5226# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5227 else if (xp->xp_context == EXPAND_USER_DEFINED)
5228 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5229# endif
5230 else
5231 {
5232 static struct expgen
5233 {
5234 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005235 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005237 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 } tab[] =
5239 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005240 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5241 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005242 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005243 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005244#ifdef FEAT_CMDHIST
5245 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005248 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005249 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005250 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5251 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5252 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#endif
5254#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005255 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5256 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5257 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5258 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259#endif
5260#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005261 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5262 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#endif
5264#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005265 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005267#ifdef FEAT_PROFILE
5268 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5269#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005270 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005271 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5272 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005273#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005274 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005275#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005276#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005277 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005278#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005279#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005280 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5283 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005284 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5285 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005287 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005288 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005289 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 };
5291 int i;
5292
5293 /*
5294 * Find a context in the table and call the ExpandGeneric() with the
5295 * right function to do the expansion.
5296 */
5297 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005298 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (xp->xp_context == tab[i].context)
5300 {
5301 if (tab[i].ic)
5302 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005303 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005304 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 break;
5306 }
5307 }
5308
Bram Moolenaar473de612013-06-08 18:19:48 +02005309 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
5311 return ret;
5312#endif /* FEAT_CMDL_COMPL */
5313}
5314
5315#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5316/*
5317 * Expand a list of names.
5318 *
5319 * Generic function for command line completion. It calls a function to
5320 * obtain strings, one by one. The strings are matched against a regexp
5321 * program. Matching strings are copied into an array, which is returned.
5322 *
5323 * Returns OK when no problems encountered, FAIL for error (out of memory).
5324 */
5325 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326ExpandGeneric(
5327 expand_T *xp,
5328 regmatch_T *regmatch,
5329 int *num_file,
5330 char_u ***file,
5331 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005333 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
5335 int i;
5336 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005337 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 char_u *str;
5339
5340 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005341 * round == 0: count the number of matching names
5342 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005344 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
5346 for (i = 0; ; ++i)
5347 {
5348 str = (*func)(xp, i);
5349 if (str == NULL) /* end of list */
5350 break;
5351 if (*str == NUL) /* skip empty strings */
5352 continue;
5353
5354 if (vim_regexec(regmatch, str, (colnr_T)0))
5355 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005356 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005358 if (escaped)
5359 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5360 else
5361 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 (*file)[count] = str;
5363#ifdef FEAT_MENU
5364 if (func == get_menu_names && str != NULL)
5365 {
5366 /* test for separator added by get_menu_names() */
5367 str += STRLEN(str) - 1;
5368 if (*str == '\001')
5369 *str = '.';
5370 }
5371#endif
5372 }
5373 ++count;
5374 }
5375 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005376 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
5378 if (count == 0)
5379 return OK;
5380 *num_file = count;
5381 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5382 if (*file == NULL)
5383 {
5384 *file = (char_u **)"";
5385 return FAIL;
5386 }
5387 count = 0;
5388 }
5389 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005390
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005391 /* Sort the results. Keep menu's in the specified order. */
5392 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005393 {
5394 if (xp->xp_context == EXPAND_EXPRESSION
5395 || xp->xp_context == EXPAND_FUNCTIONS
5396 || xp->xp_context == EXPAND_USER_FUNC)
5397 /* <SNR> functions should be sorted to the end. */
5398 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5399 sort_func_compare);
5400 else
5401 sort_strings(*file, *num_file);
5402 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005403
Bram Moolenaar4f688582007-07-24 12:34:30 +00005404#ifdef FEAT_CMDL_COMPL
5405 /* Reset the variables used for special highlight names expansion, so that
5406 * they don't show up when getting normal highlight names by ID. */
5407 reset_expand_highlight();
5408#endif
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 return OK;
5411}
5412
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005413/*
5414 * Complete a shell command.
5415 * Returns FAIL or OK;
5416 */
5417 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005418expand_shellcmd(
5419 char_u *filepat, /* pattern to match with command names */
5420 int *num_file, /* return: number of matches */
5421 char_u ***file, /* return: array with matches */
5422 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005423{
5424 char_u *pat;
5425 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005426 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005427 int mustfree = FALSE;
5428 garray_T ga;
5429 char_u *buf = alloc(MAXPATHL);
5430 size_t l;
5431 char_u *s, *e;
5432 int flags = flagsarg;
5433 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005434 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005435 hashtab_T found_ht;
5436 hashitem_T *hi;
5437 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005438
5439 if (buf == NULL)
5440 return FAIL;
5441
5442 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5443 * space */
5444 pat = vim_strsave(filepat);
5445 for (i = 0; pat[i]; ++i)
5446 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005447 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005448
Bram Moolenaarb5971142015-03-21 17:32:19 +01005449 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005450
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005451 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5452 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005453 path = (char_u *)".";
5454 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005455 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005456 /* For an absolute name we don't use $PATH. */
5457 if (!mch_isFullName(pat))
5458 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005459 if (path == NULL)
5460 path = (char_u *)"";
5461 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005462
5463 /*
5464 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005465 * collect them in "ga". When "." is not in $PATH also expand for the
5466 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005467 */
5468 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005469 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005470 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005471 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005472#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005473 e = vim_strchr(s, ';');
5474#else
5475 e = vim_strchr(s, ':');
5476#endif
5477 if (e == NULL)
5478 e = s + STRLEN(s);
5479
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005480 if (*s == NUL)
5481 {
5482 if (did_curdir)
5483 break;
5484 // Find directories in the current directory, path is empty.
5485 did_curdir = TRUE;
5486 flags |= EW_DIR;
5487 }
5488 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5489 {
5490 did_curdir = TRUE;
5491 flags |= EW_DIR;
5492 }
5493 else
5494 // Do not match directories inside a $PATH item.
5495 flags &= ~EW_DIR;
5496
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005497 l = e - s;
5498 if (l > MAXPATHL - 5)
5499 break;
5500 vim_strncpy(buf, s, l);
5501 add_pathsep(buf);
5502 l = STRLEN(buf);
5503 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5504
5505 /* Expand matches in one directory of $PATH. */
5506 ret = expand_wildcards(1, &buf, num_file, file, flags);
5507 if (ret == OK)
5508 {
5509 if (ga_grow(&ga, *num_file) == FAIL)
5510 FreeWild(*num_file, *file);
5511 else
5512 {
5513 for (i = 0; i < *num_file; ++i)
5514 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005515 char_u *name = (*file)[i];
5516
5517 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005518 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005519 // Check if this name was already found.
5520 hash = hash_hash(name + l);
5521 hi = hash_lookup(&found_ht, name + l, hash);
5522 if (HASHITEM_EMPTY(hi))
5523 {
5524 // Remove the path that was prepended.
5525 STRMOVE(name, name + l);
5526 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5527 hash_add_item(&found_ht, hi, name, hash);
5528 name = NULL;
5529 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005530 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005531 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005532 }
5533 vim_free(*file);
5534 }
5535 }
5536 if (*e != NUL)
5537 ++e;
5538 }
5539 *file = ga.ga_data;
5540 *num_file = ga.ga_len;
5541
5542 vim_free(buf);
5543 vim_free(pat);
5544 if (mustfree)
5545 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005546 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005547 return OK;
5548}
5549
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5552/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005553 * Call "user_expand_func()" to invoke a user defined Vim script function and
5554 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005556 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005558 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559 expand_T *xp,
5560 int *num_file,
5561 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005563 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005564 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005566 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005567 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005568 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
Bram Moolenaarb7515462013-06-29 12:58:33 +02005570 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005571 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 *num_file = 0;
5573 *file = NULL;
5574
Bram Moolenaarb7515462013-06-29 12:58:33 +02005575 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005576 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005577 keep = ccline.cmdbuff[ccline.cmdlen];
5578 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005579 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005580
Bram Moolenaarffa96842018-06-12 22:05:14 +02005581 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5582
5583 args[0].v_type = VAR_STRING;
5584 args[0].vval.v_string = pat;
5585 args[1].v_type = VAR_STRING;
5586 args[1].vval.v_string = xp->xp_line;
5587 args[2].v_type = VAR_NUMBER;
5588 args[2].vval.v_number = xp->xp_col;
5589 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005591 /* Save the cmdline, we don't know what the function may do. */
5592 save_ccline = ccline;
5593 ccline.cmdbuff = NULL;
5594 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005596
Bram Moolenaarded27a12018-08-01 19:06:03 +02005597 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005598
5599 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005601 if (ccline.cmdbuff != NULL)
5602 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005603
Bram Moolenaarffa96842018-06-12 22:05:14 +02005604 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005605 return ret;
5606}
5607
5608/*
5609 * Expand names with a function defined by the user.
5610 */
5611 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612ExpandUserDefined(
5613 expand_T *xp,
5614 regmatch_T *regmatch,
5615 int *num_file,
5616 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005617{
5618 char_u *retstr;
5619 char_u *s;
5620 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005621 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005622 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005623 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005624
5625 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5626 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 return FAIL;
5628
5629 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005630 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 e = vim_strchr(s, '\n');
5633 if (e == NULL)
5634 e = s + STRLEN(s);
5635 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005636 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005638 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5639 *e = keep;
5640
5641 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005643 if (ga_grow(&ga, 1) == FAIL)
5644 break;
5645 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5646 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (*e != NUL)
5650 ++e;
5651 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005652 vim_free(retstr);
5653 *file = ga.ga_data;
5654 *num_file = ga.ga_len;
5655 return OK;
5656}
5657
5658/*
5659 * Expand names with a list returned by a function defined by the user.
5660 */
5661 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662ExpandUserList(
5663 expand_T *xp,
5664 int *num_file,
5665 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005666{
5667 list_T *retlist;
5668 listitem_T *li;
5669 garray_T ga;
5670
5671 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5672 if (retlist == NULL)
5673 return FAIL;
5674
5675 ga_init2(&ga, (int)sizeof(char *), 3);
5676 /* Loop over the items in the list. */
5677 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5678 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005679 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5680 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005681
5682 if (ga_grow(&ga, 1) == FAIL)
5683 break;
5684
5685 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005686 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005687 ++ga.ga_len;
5688 }
5689 list_unref(retlist);
5690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 *file = ga.ga_data;
5692 *num_file = ga.ga_len;
5693 return OK;
5694}
5695#endif
5696
5697/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005698 * Expand color scheme, compiler or filetype names.
5699 * Search from 'runtimepath':
5700 * 'runtimepath'/{dirnames}/{pat}.vim
5701 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5702 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5703 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5704 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005705 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 */
5707 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005708ExpandRTDir(
5709 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005710 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005711 int *num_file,
5712 char_u ***file,
5713 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 char_u *s;
5716 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005717 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005719 int i;
5720 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
5722 *num_file = 0;
5723 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005724 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005725 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005727 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005729 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5730 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005732 ga_clear_strings(&ga);
5733 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005735 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005736 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005737 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005739
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005740 if (flags & DIP_START) {
5741 for (i = 0; dirnames[i] != NULL; ++i)
5742 {
5743 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5744 if (s == NULL)
5745 {
5746 ga_clear_strings(&ga);
5747 return FAIL;
5748 }
5749 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5750 globpath(p_pp, s, &ga, 0);
5751 vim_free(s);
5752 }
5753 }
5754
5755 if (flags & DIP_OPT) {
5756 for (i = 0; dirnames[i] != NULL; ++i)
5757 {
5758 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5759 if (s == NULL)
5760 {
5761 ga_clear_strings(&ga);
5762 return FAIL;
5763 }
5764 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5765 globpath(p_pp, s, &ga, 0);
5766 vim_free(s);
5767 }
5768 }
5769
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005770 for (i = 0; i < ga.ga_len; ++i)
5771 {
5772 match = ((char_u **)ga.ga_data)[i];
5773 s = match;
5774 e = s + STRLEN(s);
5775 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5776 {
5777 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005778 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005779 if (s < match || vim_ispathsep(*s))
5780 break;
5781 ++s;
5782 *e = NUL;
5783 mch_memmove(match, s, e - s + 1);
5784 }
5785 }
5786
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005787 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005788 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005789
5790 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005791 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005792 remove_duplicates(&ga);
5793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 *file = ga.ga_data;
5795 *num_file = ga.ga_len;
5796 return OK;
5797}
5798
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005799/*
5800 * Expand loadplugin names:
5801 * 'packpath'/pack/ * /opt/{pat}
5802 */
5803 static int
5804ExpandPackAddDir(
5805 char_u *pat,
5806 int *num_file,
5807 char_u ***file)
5808{
5809 char_u *s;
5810 char_u *e;
5811 char_u *match;
5812 garray_T ga;
5813 int i;
5814 int pat_len;
5815
5816 *num_file = 0;
5817 *file = NULL;
5818 pat_len = (int)STRLEN(pat);
5819 ga_init2(&ga, (int)sizeof(char *), 10);
5820
5821 s = alloc((unsigned)(pat_len + 26));
5822 if (s == NULL)
5823 {
5824 ga_clear_strings(&ga);
5825 return FAIL;
5826 }
5827 sprintf((char *)s, "pack/*/opt/%s*", pat);
5828 globpath(p_pp, s, &ga, 0);
5829 vim_free(s);
5830
5831 for (i = 0; i < ga.ga_len; ++i)
5832 {
5833 match = ((char_u **)ga.ga_data)[i];
5834 s = gettail(match);
5835 e = s + STRLEN(s);
5836 mch_memmove(match, s, e - s + 1);
5837 }
5838
5839 if (ga.ga_len == 0)
5840 return FAIL;
5841
5842 /* Sort and remove duplicates which can happen when specifying multiple
5843 * directories in dirnames. */
5844 remove_duplicates(&ga);
5845
5846 *file = ga.ga_data;
5847 *num_file = ga.ga_len;
5848 return OK;
5849}
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#endif
5852
5853#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5854/*
5855 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005856 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005858 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005859globpath(
5860 char_u *path,
5861 char_u *file,
5862 garray_T *ga,
5863 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 expand_T xpc;
5866 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 int num_p;
5869 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871 buf = alloc(MAXPATHL);
5872 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005873 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005875 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 /* Loop over all entries in {path}. */
5879 while (*path != NUL)
5880 {
5881 /* Copy one item of the path to buf[] and concatenate the file name. */
5882 copy_option_part(&path, buf, MAXPATHL, ",");
5883 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5884 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005885# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005886 /* Using the platform's path separator (\) makes vim incorrectly
5887 * treat it as an escape character, use '/' instead. */
5888 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5889 STRCAT(buf, "/");
5890# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005892# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005894 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5895 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005897 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005899 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 for (i = 0; i < num_p; ++i)
5902 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005903 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005904 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005905 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 FreeWild(num_p, p);
5910 }
5911 }
5912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
5914 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915}
5916
5917#endif
5918
5919#if defined(FEAT_CMDHIST) || defined(PROTO)
5920
5921/*********************************
5922 * Command line history stuff *
5923 *********************************/
5924
5925/*
5926 * Translate a history character to the associated type number.
5927 */
5928 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005929hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 if (c == ':')
5932 return HIST_CMD;
5933 if (c == '=')
5934 return HIST_EXPR;
5935 if (c == '@')
5936 return HIST_INPUT;
5937 if (c == '>')
5938 return HIST_DEBUG;
5939 return HIST_SEARCH; /* must be '?' or '/' */
5940}
5941
5942/*
5943 * Table of history names.
5944 * These names are used in :history and various hist...() functions.
5945 * It is sufficient to give the significant prefix of a history name.
5946 */
5947
5948static char *(history_names[]) =
5949{
5950 "cmd",
5951 "search",
5952 "expr",
5953 "input",
5954 "debug",
5955 NULL
5956};
5957
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005958#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5959/*
5960 * Function given to ExpandGeneric() to obtain the possible first
5961 * arguments of the ":history command.
5962 */
5963 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005964get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005965{
5966 static char_u compl[2] = { NUL, NUL };
5967 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005968 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005969 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5970
5971 if (idx < short_names_count)
5972 {
5973 compl[0] = (char_u)short_names[idx];
5974 return compl;
5975 }
5976 if (idx < short_names_count + history_name_count)
5977 return (char_u *)history_names[idx - short_names_count];
5978 if (idx == short_names_count + history_name_count)
5979 return (char_u *)"all";
5980 return NULL;
5981}
5982#endif
5983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984/*
5985 * init_history() - Initialize the command line history.
5986 * Also used to re-allocate the history when the size changes.
5987 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005988 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005989init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 int newlen; /* new length of history table */
5992 histentry_T *temp;
5993 int i;
5994 int j;
5995 int type;
5996
5997 /*
5998 * If size of history table changed, reallocate it
5999 */
6000 newlen = (int)p_hi;
6001 if (newlen != hislen) /* history length changed */
6002 {
6003 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6004 {
6005 if (newlen)
6006 {
6007 temp = (histentry_T *)lalloc(
6008 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6009 if (temp == NULL) /* out of memory! */
6010 {
6011 if (type == 0) /* first one: just keep the old length */
6012 {
6013 newlen = hislen;
6014 break;
6015 }
6016 /* Already changed one table, now we can only have zero
6017 * length for all tables. */
6018 newlen = 0;
6019 type = -1;
6020 continue;
6021 }
6022 }
6023 else
6024 temp = NULL;
6025 if (newlen == 0 || temp != NULL)
6026 {
6027 if (hisidx[type] < 0) /* there are no entries yet */
6028 {
6029 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006030 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032 else if (newlen > hislen) /* array becomes bigger */
6033 {
6034 for (i = 0; i <= hisidx[type]; ++i)
6035 temp[i] = history[type][i];
6036 j = i;
6037 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006038 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 for ( ; j < hislen; ++i, ++j)
6040 temp[i] = history[type][j];
6041 }
6042 else /* array becomes smaller or 0 */
6043 {
6044 j = hisidx[type];
6045 for (i = newlen - 1; ; --i)
6046 {
6047 if (i >= 0) /* copy newest entries */
6048 temp[i] = history[type][j];
6049 else /* remove older entries */
6050 vim_free(history[type][j].hisstr);
6051 if (--j < 0)
6052 j = hislen - 1;
6053 if (j == hisidx[type])
6054 break;
6055 }
6056 hisidx[type] = newlen - 1;
6057 }
6058 vim_free(history[type]);
6059 history[type] = temp;
6060 }
6061 }
6062 hislen = newlen;
6063 }
6064}
6065
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006067clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006068{
6069 hisptr->hisnum = 0;
6070 hisptr->viminfo = FALSE;
6071 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006072 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006073}
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075/*
6076 * Check if command line 'str' is already in history.
6077 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6078 */
6079 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006080in_history(
6081 int type,
6082 char_u *str,
6083 int move_to_front, /* Move the entry to the front if it exists */
6084 int sep,
6085 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 int i;
6088 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006089 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090
6091 if (hisidx[type] < 0)
6092 return FALSE;
6093 i = hisidx[type];
6094 do
6095 {
6096 if (history[type][i].hisstr == NULL)
6097 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006098
6099 /* For search history, check that the separator character matches as
6100 * well. */
6101 p = history[type][i].hisstr;
6102 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006103 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006104 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
6106 if (!move_to_front)
6107 return TRUE;
6108 last_i = i;
6109 break;
6110 }
6111 if (--i < 0)
6112 i = hislen - 1;
6113 } while (i != hisidx[type]);
6114
6115 if (last_i >= 0)
6116 {
6117 str = history[type][i].hisstr;
6118 while (i != hisidx[type])
6119 {
6120 if (++i >= hislen)
6121 i = 0;
6122 history[type][last_i] = history[type][i];
6123 last_i = i;
6124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006126 history[type][i].viminfo = FALSE;
6127 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006128 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 return TRUE;
6130 }
6131 return FALSE;
6132}
6133
6134/*
6135 * Convert history name (from table above) to its HIST_ equivalent.
6136 * When "name" is empty, return "cmd" history.
6137 * Returns -1 for unknown history name.
6138 */
6139 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006140get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
6142 int i;
6143 int len = (int)STRLEN(name);
6144
6145 /* No argument: use current history. */
6146 if (len == 0)
6147 return hist_char2type(ccline.cmdfirstc);
6148
6149 for (i = 0; history_names[i] != NULL; ++i)
6150 if (STRNICMP(name, history_names[i], len) == 0)
6151 return i;
6152
6153 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6154 return hist_char2type(name[0]);
6155
6156 return -1;
6157}
6158
6159static int last_maptick = -1; /* last seen maptick */
6160
6161/*
6162 * Add the given string to the given history. If the string is already in the
6163 * history then it is moved to the front. "histype" may be one of he HIST_
6164 * values.
6165 */
6166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006167add_to_history(
6168 int histype,
6169 char_u *new_entry,
6170 int in_map, /* consider maptick when inside a mapping */
6171 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 histentry_T *hisptr;
6174 int len;
6175
6176 if (hislen == 0) /* no history */
6177 return;
6178
Bram Moolenaara939e432013-11-09 05:30:26 +01006179 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6180 return;
6181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 /*
6183 * Searches inside the same mapping overwrite each other, so that only
6184 * the last line is kept. Be careful not to remove a line that was moved
6185 * down, only lines that were added.
6186 */
6187 if (histype == HIST_SEARCH && in_map)
6188 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006189 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
6191 /* Current line is from the same mapping, remove it */
6192 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6193 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006194 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 --hisnum[histype];
6196 if (--hisidx[HIST_SEARCH] < 0)
6197 hisidx[HIST_SEARCH] = hislen - 1;
6198 }
6199 last_maptick = -1;
6200 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006201 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
6203 if (++hisidx[histype] == hislen)
6204 hisidx[histype] = 0;
6205 hisptr = &history[histype][hisidx[histype]];
6206 vim_free(hisptr->hisstr);
6207
6208 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006209 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6211 if (hisptr->hisstr != NULL)
6212 hisptr->hisstr[len + 1] = sep;
6213
6214 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006215 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006216 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (histype == HIST_SEARCH && in_map)
6218 last_maptick = maptick;
6219 }
6220}
6221
6222#if defined(FEAT_EVAL) || defined(PROTO)
6223
6224/*
6225 * Get identifier of newest history entry.
6226 * "histype" may be one of the HIST_ values.
6227 */
6228 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006229get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6232 || hisidx[histype] < 0)
6233 return -1;
6234
6235 return history[histype][hisidx[histype]].hisnum;
6236}
6237
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 * Calculate history index from a number:
6240 * num > 0: seen as identifying number of a history entry
6241 * num < 0: relative position in history wrt newest entry
6242 * "histype" may be one of the HIST_ values.
6243 */
6244 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
6247 int i;
6248 histentry_T *hist;
6249 int wrapped = FALSE;
6250
6251 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6252 || (i = hisidx[histype]) < 0 || num == 0)
6253 return -1;
6254
6255 hist = history[histype];
6256 if (num > 0)
6257 {
6258 while (hist[i].hisnum > num)
6259 if (--i < 0)
6260 {
6261 if (wrapped)
6262 break;
6263 i += hislen;
6264 wrapped = TRUE;
6265 }
6266 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6267 return i;
6268 }
6269 else if (-num <= hislen)
6270 {
6271 i += num + 1;
6272 if (i < 0)
6273 i += hislen;
6274 if (hist[i].hisstr != NULL)
6275 return i;
6276 }
6277 return -1;
6278}
6279
6280/*
6281 * Get a history entry by its index.
6282 * "histype" may be one of the HIST_ values.
6283 */
6284 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006285get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287 idx = calc_hist_idx(histype, idx);
6288 if (idx >= 0)
6289 return history[histype][idx].hisstr;
6290 else
6291 return (char_u *)"";
6292}
6293
6294/*
6295 * Clear all entries of a history.
6296 * "histype" may be one of the HIST_ values.
6297 */
6298 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006299clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 int i;
6302 histentry_T *hisptr;
6303
6304 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6305 {
6306 hisptr = history[histype];
6307 for (i = hislen; i--;)
6308 {
6309 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006310 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006311 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 }
6313 hisidx[histype] = -1; /* mark history as cleared */
6314 hisnum[histype] = 0; /* reset identifier counter */
6315 return OK;
6316 }
6317 return FAIL;
6318}
6319
6320/*
6321 * Remove all entries matching {str} from a history.
6322 * "histype" may be one of the HIST_ values.
6323 */
6324 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 regmatch_T regmatch;
6328 histentry_T *hisptr;
6329 int idx;
6330 int i;
6331 int last;
6332 int found = FALSE;
6333
6334 regmatch.regprog = NULL;
6335 regmatch.rm_ic = FALSE; /* always match case */
6336 if (hislen != 0
6337 && histype >= 0
6338 && histype < HIST_COUNT
6339 && *str != NUL
6340 && (idx = hisidx[histype]) >= 0
6341 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6342 != NULL)
6343 {
6344 i = last = idx;
6345 do
6346 {
6347 hisptr = &history[histype][i];
6348 if (hisptr->hisstr == NULL)
6349 break;
6350 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6351 {
6352 found = TRUE;
6353 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006354 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 }
6356 else
6357 {
6358 if (i != last)
6359 {
6360 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006361 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363 if (--last < 0)
6364 last += hislen;
6365 }
6366 if (--i < 0)
6367 i += hislen;
6368 } while (i != idx);
6369 if (history[histype][idx].hisstr == NULL)
6370 hisidx[histype] = -1;
6371 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006372 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 return found;
6374}
6375
6376/*
6377 * Remove an indexed entry from a history.
6378 * "histype" may be one of the HIST_ values.
6379 */
6380 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006381del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 int i, j;
6384
6385 i = calc_hist_idx(histype, idx);
6386 if (i < 0)
6387 return FALSE;
6388 idx = hisidx[histype];
6389 vim_free(history[histype][i].hisstr);
6390
6391 /* When deleting the last added search string in a mapping, reset
6392 * last_maptick, so that the last added search string isn't deleted again.
6393 */
6394 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6395 last_maptick = -1;
6396
6397 while (i != idx)
6398 {
6399 j = (i + 1) % hislen;
6400 history[histype][i] = history[histype][j];
6401 i = j;
6402 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006403 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (--i < 0)
6405 i += hislen;
6406 hisidx[histype] = i;
6407 return TRUE;
6408}
6409
6410#endif /* FEAT_EVAL */
6411
6412#if defined(FEAT_CRYPT) || defined(PROTO)
6413/*
6414 * Very specific function to remove the value in ":set key=val" from the
6415 * history.
6416 */
6417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006418remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420 char_u *p;
6421 int i;
6422
6423 i = hisidx[HIST_CMD];
6424 if (i < 0)
6425 return;
6426 p = history[HIST_CMD][i].hisstr;
6427 if (p != NULL)
6428 for ( ; *p; ++p)
6429 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6430 {
6431 p = vim_strchr(p + 3, '=');
6432 if (p == NULL)
6433 break;
6434 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006435 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (p[i] == '\\' && p[i + 1])
6437 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006438 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 --p;
6440 }
6441}
6442#endif
6443
6444#endif /* FEAT_CMDHIST */
6445
Bram Moolenaar064154c2016-03-19 22:50:43 +01006446#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006447/*
6448 * Get pointer to the command line info to use. cmdline_paste() may clear
6449 * ccline and put the previous value in prev_ccline.
6450 */
6451 static struct cmdline_info *
6452get_ccline_ptr(void)
6453{
6454 if ((State & CMDLINE) == 0)
6455 return NULL;
6456 if (ccline.cmdbuff != NULL)
6457 return &ccline;
6458 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6459 return &prev_ccline;
6460 return NULL;
6461}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006462#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006463
Bram Moolenaar064154c2016-03-19 22:50:43 +01006464#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006465/*
6466 * Get the current command line in allocated memory.
6467 * Only works when the command line is being edited.
6468 * Returns NULL when something is wrong.
6469 */
6470 char_u *
6471get_cmdline_str(void)
6472{
6473 struct cmdline_info *p = get_ccline_ptr();
6474
6475 if (p == NULL)
6476 return NULL;
6477 return vim_strnsave(p->cmdbuff, p->cmdlen);
6478}
6479
6480/*
6481 * Get the current command line position, counted in bytes.
6482 * Zero is the first position.
6483 * Only works when the command line is being edited.
6484 * Returns -1 when something is wrong.
6485 */
6486 int
6487get_cmdline_pos(void)
6488{
6489 struct cmdline_info *p = get_ccline_ptr();
6490
6491 if (p == NULL)
6492 return -1;
6493 return p->cmdpos;
6494}
6495
6496/*
6497 * Set the command line byte position to "pos". Zero is the first position.
6498 * Only works when the command line is being edited.
6499 * Returns 1 when failed, 0 when OK.
6500 */
6501 int
6502set_cmdline_pos(
6503 int pos)
6504{
6505 struct cmdline_info *p = get_ccline_ptr();
6506
6507 if (p == NULL)
6508 return 1;
6509
6510 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6511 * changed the command line. */
6512 if (pos < 0)
6513 new_cmdpos = 0;
6514 else
6515 new_cmdpos = pos;
6516 return 0;
6517}
6518#endif
6519
6520#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6521/*
6522 * Get the current command-line type.
6523 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6524 * Only works when the command line is being edited.
6525 * Returns NUL when something is wrong.
6526 */
6527 int
6528get_cmdline_type(void)
6529{
6530 struct cmdline_info *p = get_ccline_ptr();
6531
6532 if (p == NULL)
6533 return NUL;
6534 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006535 return
6536# ifdef FEAT_EVAL
6537 (p->input_fn) ? '@' :
6538# endif
6539 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006540 return p->cmdfirstc;
6541}
6542#endif
6543
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6545/*
6546 * Get indices "num1,num2" that specify a range within a list (not a range of
6547 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6548 * Returns OK if parsed successfully, otherwise FAIL.
6549 */
6550 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006551get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
6553 int len;
6554 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006555 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 *str = skipwhite(*str);
6558 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6559 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006560 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 *str += len;
6562 *num1 = (int)num;
6563 first = TRUE;
6564 }
6565 *str = skipwhite(*str);
6566 if (**str == ',') /* parse "to" part of range */
6567 {
6568 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006569 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 if (len > 0)
6571 {
6572 *num2 = (int)num;
6573 *str = skipwhite(*str + len);
6574 }
6575 else if (!first) /* no number given at all */
6576 return FAIL;
6577 }
6578 else if (first) /* only one number given */
6579 *num2 = *num1;
6580 return OK;
6581}
6582#endif
6583
6584#if defined(FEAT_CMDHIST) || defined(PROTO)
6585/*
6586 * :history command - print a history
6587 */
6588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006589ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
6591 histentry_T *hist;
6592 int histype1 = HIST_CMD;
6593 int histype2 = HIST_CMD;
6594 int hisidx1 = 1;
6595 int hisidx2 = -1;
6596 int idx;
6597 int i, j, k;
6598 char_u *end;
6599 char_u *arg = eap->arg;
6600
6601 if (hislen == 0)
6602 {
6603 MSG(_("'history' option is zero"));
6604 return;
6605 }
6606
6607 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6608 {
6609 end = arg;
6610 while (ASCII_ISALPHA(*end)
6611 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6612 end++;
6613 i = *end;
6614 *end = NUL;
6615 histype1 = get_histtype(arg);
6616 if (histype1 == -1)
6617 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006618 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 {
6620 histype1 = 0;
6621 histype2 = HIST_COUNT-1;
6622 }
6623 else
6624 {
6625 *end = i;
6626 EMSG(_(e_trailing));
6627 return;
6628 }
6629 }
6630 else
6631 histype2 = histype1;
6632 *end = i;
6633 }
6634 else
6635 end = arg;
6636 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6637 {
6638 EMSG(_(e_trailing));
6639 return;
6640 }
6641
6642 for (; !got_int && histype1 <= histype2; ++histype1)
6643 {
6644 STRCPY(IObuff, "\n # ");
6645 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6646 MSG_PUTS_TITLE(IObuff);
6647 idx = hisidx[histype1];
6648 hist = history[histype1];
6649 j = hisidx1;
6650 k = hisidx2;
6651 if (j < 0)
6652 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6653 if (k < 0)
6654 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6655 if (idx >= 0 && j <= k)
6656 for (i = idx + 1; !got_int; ++i)
6657 {
6658 if (i == hislen)
6659 i = 0;
6660 if (hist[i].hisstr != NULL
6661 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6662 {
6663 msg_putchar('\n');
6664 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6665 hist[i].hisnum);
6666 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6667 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006668 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 else
6670 STRCAT(IObuff, hist[i].hisstr);
6671 msg_outtrans(IObuff);
6672 out_flush();
6673 }
6674 if (i == idx)
6675 break;
6676 }
6677 }
6678}
6679#endif
6680
6681#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006682/*
6683 * Buffers for history read from a viminfo file. Only valid while reading.
6684 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006685static histentry_T *viminfo_history[HIST_COUNT] =
6686 {NULL, NULL, NULL, NULL, NULL};
6687static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6688static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689static int viminfo_add_at_front = FALSE;
6690
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006691static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693/*
6694 * Translate a history type number to the associated character.
6695 */
6696 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006697hist_type2char(
6698 int type,
6699 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
6701 if (type == HIST_CMD)
6702 return ':';
6703 if (type == HIST_SEARCH)
6704 {
6705 if (use_question)
6706 return '?';
6707 else
6708 return '/';
6709 }
6710 if (type == HIST_EXPR)
6711 return '=';
6712 return '@';
6713}
6714
6715/*
6716 * Prepare for reading the history from the viminfo file.
6717 * This allocates history arrays to store the read history lines.
6718 */
6719 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006720prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 int i;
6723 int num;
6724 int type;
6725 int len;
6726
6727 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006728 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (asklen > hislen)
6730 asklen = hislen;
6731
6732 for (type = 0; type < HIST_COUNT; ++type)
6733 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006734 /* Count the number of empty spaces in the history list. Entries read
6735 * from viminfo previously are also considered empty. If there are
6736 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006738 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 num++;
6740 len = asklen;
6741 if (num > len)
6742 len = num;
6743 if (len <= 0)
6744 viminfo_history[type] = NULL;
6745 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006746 viminfo_history[type] = (histentry_T *)lalloc(
6747 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (viminfo_history[type] == NULL)
6749 len = 0;
6750 viminfo_hislen[type] = len;
6751 viminfo_hisidx[type] = 0;
6752 }
6753}
6754
6755/*
6756 * Accept a line from the viminfo, store it in the history array when it's
6757 * new.
6758 */
6759 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006760read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 int type;
6763 long_u len;
6764 char_u *val;
6765 char_u *p;
6766
6767 type = hist_char2type(virp->vir_line[0]);
6768 if (viminfo_hisidx[type] < viminfo_hislen[type])
6769 {
6770 val = viminfo_readstring(virp, 1, TRUE);
6771 if (val != NULL && *val != NUL)
6772 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006773 int sep = (*val == ' ' ? NUL : *val);
6774
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006776 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
6778 /* Need to re-allocate to append the separator byte. */
6779 len = STRLEN(val);
6780 p = lalloc(len + 2, TRUE);
6781 if (p != NULL)
6782 {
6783 if (type == HIST_SEARCH)
6784 {
6785 /* Search entry: Move the separator from the first
6786 * column to after the NUL. */
6787 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006788 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
6790 else
6791 {
6792 /* Not a search entry: No separator in the viminfo
6793 * file, add a NUL separator. */
6794 mch_memmove(p, val, (size_t)len + 1);
6795 p[len + 1] = NUL;
6796 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006797 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6798 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006799 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6800 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006801 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803 }
6804 }
6805 vim_free(val);
6806 }
6807 return viminfo_readline(virp);
6808}
6809
Bram Moolenaar07219f92013-04-14 23:19:36 +02006810/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006811 * Accept a new style history line from the viminfo, store it in the history
6812 * array when it's new.
6813 */
6814 void
6815handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006816 garray_T *values,
6817 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006818{
6819 int type;
6820 long_u len;
6821 char_u *val;
6822 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006823 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006824
6825 /* Check the format:
6826 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006827 if (values->ga_len < 4
6828 || vp[0].bv_type != BVAL_NR
6829 || vp[1].bv_type != BVAL_NR
6830 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6831 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006832 return;
6833
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006834 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006835 if (type >= HIST_COUNT)
6836 return;
6837 if (viminfo_hisidx[type] < viminfo_hislen[type])
6838 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006839 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006840 if (val != NULL && *val != NUL)
6841 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006842 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6843 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006844 int idx;
6845 int overwrite = FALSE;
6846
6847 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6848 {
6849 /* If lines were written by an older Vim we need to avoid
6850 * getting duplicates. See if the entry already exists. */
6851 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6852 {
6853 p = viminfo_history[type][idx].hisstr;
6854 if (STRCMP(val, p) == 0
6855 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6856 {
6857 overwrite = TRUE;
6858 break;
6859 }
6860 }
6861
6862 if (!overwrite)
6863 {
6864 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006865 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006866 p = lalloc(len + 2, TRUE);
6867 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006868 else
6869 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006870 if (p != NULL)
6871 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006872 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006873 if (!overwrite)
6874 {
6875 mch_memmove(p, val, (size_t)len + 1);
6876 /* Put the separator after the NUL. */
6877 p[len + 1] = sep;
6878 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006879 viminfo_history[type][idx].hisnum = 0;
6880 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006881 viminfo_hisidx[type]++;
6882 }
6883 }
6884 }
6885 }
6886 }
6887}
6888
6889/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006890 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006891 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006892 static void
6893concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 int idx;
6896 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006897
6898 idx = hisidx[type] + viminfo_hisidx[type];
6899 if (idx >= hislen)
6900 idx -= hislen;
6901 else if (idx < 0)
6902 idx = hislen - 1;
6903 if (viminfo_add_at_front)
6904 hisidx[type] = idx;
6905 else
6906 {
6907 if (hisidx[type] == -1)
6908 hisidx[type] = hislen - 1;
6909 do
6910 {
6911 if (history[type][idx].hisstr != NULL
6912 || history[type][idx].viminfo)
6913 break;
6914 if (++idx == hislen)
6915 idx = 0;
6916 } while (idx != hisidx[type]);
6917 if (idx != hisidx[type] && --idx < 0)
6918 idx = hislen - 1;
6919 }
6920 for (i = 0; i < viminfo_hisidx[type]; i++)
6921 {
6922 vim_free(history[type][idx].hisstr);
6923 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6924 history[type][idx].viminfo = TRUE;
6925 history[type][idx].time_set = viminfo_history[type][i].time_set;
6926 if (--idx < 0)
6927 idx = hislen - 1;
6928 }
6929 idx += 1;
6930 idx %= hislen;
6931 for (i = 0; i < viminfo_hisidx[type]; i++)
6932 {
6933 history[type][idx++].hisnum = ++hisnum[type];
6934 idx %= hislen;
6935 }
6936}
6937
6938#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6939 static int
6940#ifdef __BORLANDC__
6941_RTLENTRYF
6942#endif
6943sort_hist(const void *s1, const void *s2)
6944{
6945 histentry_T *p1 = *(histentry_T **)s1;
6946 histentry_T *p2 = *(histentry_T **)s2;
6947
6948 if (p1->time_set < p2->time_set) return -1;
6949 if (p1->time_set > p2->time_set) return 1;
6950 return 0;
6951}
6952#endif
6953
6954/*
6955 * Merge history lines from viminfo and lines typed in this Vim based on the
6956 * timestamp;
6957 */
6958 static void
6959merge_history(int type)
6960{
6961 int max_len;
6962 histentry_T **tot_hist;
6963 histentry_T *new_hist;
6964 int i;
6965 int len;
6966
6967 /* Make one long list with all entries. */
6968 max_len = hislen + viminfo_hisidx[type];
6969 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6970 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6971 if (tot_hist == NULL || new_hist == NULL)
6972 {
6973 vim_free(tot_hist);
6974 vim_free(new_hist);
6975 return;
6976 }
6977 for (i = 0; i < viminfo_hisidx[type]; i++)
6978 tot_hist[i] = &viminfo_history[type][i];
6979 len = i;
6980 for (i = 0; i < hislen; i++)
6981 if (history[type][i].hisstr != NULL)
6982 tot_hist[len++] = &history[type][i];
6983
6984 /* Sort the list on timestamp. */
6985 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6986
6987 /* Keep the newest ones. */
6988 for (i = 0; i < hislen; i++)
6989 {
6990 if (i < len)
6991 {
6992 new_hist[i] = *tot_hist[i];
6993 tot_hist[i]->hisstr = NULL;
6994 if (new_hist[i].hisnum == 0)
6995 new_hist[i].hisnum = ++hisnum[type];
6996 }
6997 else
6998 clear_hist_entry(&new_hist[i]);
6999 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007000 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007001
7002 /* Free what is not kept. */
7003 for (i = 0; i < viminfo_hisidx[type]; i++)
7004 vim_free(viminfo_history[type][i].hisstr);
7005 for (i = 0; i < hislen; i++)
7006 vim_free(history[type][i].hisstr);
7007 vim_free(history[type]);
7008 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007009 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007010}
7011
7012/*
7013 * Finish reading history lines from viminfo. Not used when writing viminfo.
7014 */
7015 void
7016finish_viminfo_history(vir_T *virp)
7017{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007019 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
7021 for (type = 0; type < HIST_COUNT; ++type)
7022 {
7023 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007024 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007025
7026 if (merge)
7027 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007029 concat_history(type);
7030
Bram Moolenaard23a8232018-02-10 18:45:26 +01007031 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007032 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
7034}
7035
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007036/*
7037 * Write history to viminfo file in "fp".
7038 * When "merge" is TRUE merge history lines with a previously read viminfo
7039 * file, data is in viminfo_history[].
7040 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007043write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 int i;
7046 int type;
7047 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007048 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
7050 init_history();
7051 if (hislen == 0)
7052 return;
7053 for (type = 0; type < HIST_COUNT; ++type)
7054 {
7055 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7056 if (num_saved == 0)
7057 continue;
7058 if (num_saved < 0) /* Use default */
7059 num_saved = hislen;
7060 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7061 type == HIST_CMD ? _("Command Line") :
7062 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007063 type == HIST_EXPR ? _("Expression") :
7064 type == HIST_INPUT ? _("Input Line") :
7065 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 if (num_saved > hislen)
7067 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007068
7069 /*
7070 * Merge typed and viminfo history:
7071 * round 1: history of typed commands.
7072 * round 2: history from recently read viminfo.
7073 */
7074 for (round = 1; round <= 2; ++round)
7075 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007076 if (round == 1)
7077 /* start at newest entry, somewhere in the list */
7078 i = hisidx[type];
7079 else if (viminfo_hisidx[type] > 0)
7080 /* start at newest entry, first in the list */
7081 i = 0;
7082 else
7083 /* empty list */
7084 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007085 if (i >= 0)
7086 while (num_saved > 0
7087 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007089 char_u *p;
7090 time_t timestamp;
7091 int c = NUL;
7092
7093 if (round == 1)
7094 {
7095 p = history[type][i].hisstr;
7096 timestamp = history[type][i].time_set;
7097 }
7098 else
7099 {
7100 p = viminfo_history[type] == NULL ? NULL
7101 : viminfo_history[type][i].hisstr;
7102 timestamp = viminfo_history[type] == NULL ? 0
7103 : viminfo_history[type][i].time_set;
7104 }
7105
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007106 if (p != NULL && (round == 2
7107 || !merge
7108 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007110 --num_saved;
7111 fputc(hist_type2char(type, TRUE), fp);
7112 /* For the search history: put the separator in the
7113 * second column; use a space if there isn't one. */
7114 if (type == HIST_SEARCH)
7115 {
7116 c = p[STRLEN(p) + 1];
7117 putc(c == NUL ? ' ' : c, fp);
7118 }
7119 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007120
7121 {
7122 char cbuf[NUMBUFLEN];
7123
7124 /* New style history with a bar line. Format:
7125 * |{bartype},{histtype},{timestamp},{separator},"text" */
7126 if (c == NUL)
7127 cbuf[0] = NUL;
7128 else
7129 sprintf(cbuf, "%d", c);
7130 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7131 type, (long)timestamp, cbuf);
7132 barline_writestring(fp, p, LSIZE - 20);
7133 putc('\n', fp);
7134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007136 if (round == 1)
7137 {
7138 /* Decrement index, loop around and stop when back at
7139 * the start. */
7140 if (--i < 0)
7141 i = hislen - 1;
7142 if (i == hisidx[type])
7143 break;
7144 }
7145 else
7146 {
7147 /* Increment index. Stop at the end in the while. */
7148 ++i;
7149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007151 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007152 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007153 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007154 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007155 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007156 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
7158}
7159#endif /* FEAT_VIMINFO */
7160
7161#if defined(FEAT_FKMAP) || defined(PROTO)
7162/*
7163 * Write a character at the current cursor+offset position.
7164 * It is directly written into the command buffer block.
7165 */
7166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007167cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7170 {
7171 EMSG(_("E198: cmd_pchar beyond the command length"));
7172 return;
7173 }
7174 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7175 ccline.cmdbuff[ccline.cmdlen] = NUL;
7176}
7177
7178 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007179cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7182 {
7183 /* EMSG(_("cmd_gchar beyond the command length")); */
7184 return NUL;
7185 }
7186 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7187}
7188#endif
7189
7190#if defined(FEAT_CMDWIN) || defined(PROTO)
7191/*
7192 * Open a window on the current command line and history. Allow editing in
7193 * the window. Returns when the window is closed.
7194 * Returns:
7195 * CR if the command is to be executed
7196 * Ctrl_C if it is to be abandoned
7197 * K_IGNORE if editing continues
7198 */
7199 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007200open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007203 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007205 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 win_T *wp;
7207 int i;
7208 linenr_T lnum;
7209 int histtype;
7210 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 int save_restart_edit = restart_edit;
7212 int save_State = State;
7213 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007214#ifdef FEAT_RIGHTLEFT
7215 int save_cmdmsg_rl = cmdmsg_rl;
7216#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007217#ifdef FEAT_FOLDING
7218 int save_KeyTyped;
7219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221 /* Can't do this recursively. Can't do it when typing a password. */
7222 if (cmdwin_type != 0
7223# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7224 || cmdline_star > 0
7225# endif
7226 )
7227 {
7228 beep_flush();
7229 return K_IGNORE;
7230 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007231 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233 /* Save current window sizes. */
7234 win_size_save(&winsizes);
7235
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007237 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007238
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007239 /* don't use a new tab page */
7240 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007241 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007242
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 /* Create a window for the command-line buffer. */
7244 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7245 {
7246 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007247 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 return K_IGNORE;
7249 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007250 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
7252 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007253 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007254 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007257#ifdef FEAT_FOLDING
7258 curwin->w_p_fen = FALSE;
7259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007261 curwin->w_p_rl = cmdmsg_rl;
7262 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007264 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007267 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007268 /* But don't allow switching to another buffer. */
7269 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
Bram Moolenaar46152342005-09-07 21:18:43 +00007271 /* Showing the prompt may have set need_wait_return, reset it. */
7272 need_wait_return = FALSE;
7273
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007274 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7276 {
7277 if (p_wc == TAB)
7278 {
7279 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7280 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7281 }
7282 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7283 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007284 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007286 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7287 * sets 'textwidth' to 78). */
7288 curbuf->b_p_tw = 0;
7289
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 /* Fill the buffer with the history. */
7291 init_history();
7292 if (hislen > 0)
7293 {
7294 i = hisidx[histtype];
7295 if (i >= 0)
7296 {
7297 lnum = 0;
7298 do
7299 {
7300 if (++i == hislen)
7301 i = 0;
7302 if (history[histtype][i].hisstr != NULL)
7303 ml_append(lnum++, history[histtype][i].hisstr,
7304 (colnr_T)0, FALSE);
7305 }
7306 while (i != hisidx[histtype]);
7307 }
7308 }
7309
7310 /* Replace the empty last line with the current command-line and put the
7311 * cursor there. */
7312 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7313 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7314 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007315 changed_line_abv_curs();
7316 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007317 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
7319 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007320 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
7322 /* No Ex mode here! */
7323 exmode_active = 0;
7324
7325 State = NORMAL;
7326# ifdef FEAT_MOUSE
7327 setmouse();
7328# endif
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007331 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007332 if (restart_edit != 0) /* autocmd with ":startinsert" */
7333 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
7335 i = RedrawingDisabled;
7336 RedrawingDisabled = 0;
7337
7338 /*
7339 * Call the main loop until <CR> or CTRL-C is typed.
7340 */
7341 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007342 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
7344 RedrawingDisabled = i;
7345
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007346# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007347 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007348# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007351 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007352
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007353# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007354 /* Restore KeyTyped in case it is modified by autocommands */
7355 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356# endif
7357
Bram Moolenaarccc18222007-05-10 18:25:20 +00007358 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007359 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 cmdwin_type = 0;
7361
7362 exmode_active = save_exmode;
7363
Bram Moolenaarf9821062008-06-20 16:31:07 +00007364 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007366 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
7368 cmdwin_result = Ctrl_C;
7369 EMSG(_("E199: Active window or buffer deleted"));
7370 }
7371 else
7372 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007373# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 /* autocmds may abort script processing */
7375 if (aborting() && cmdwin_result != K_IGNORE)
7376 cmdwin_result = Ctrl_C;
7377# endif
7378 /* Set the new command line from the cmdline buffer. */
7379 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007380 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007382 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7383
7384 if (histtype == HIST_CMD)
7385 {
7386 /* Execute the command directly. */
7387 ccline.cmdbuff = vim_strsave((char_u *)p);
7388 cmdwin_result = CAR;
7389 }
7390 else
7391 {
7392 /* First need to cancel what we were doing. */
7393 ccline.cmdbuff = NULL;
7394 stuffcharReadbuff(':');
7395 stuffReadbuff((char_u *)p);
7396 stuffcharReadbuff(CAR);
7397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
7399 else if (cmdwin_result == K_XF2) /* :qa typed */
7400 {
7401 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7402 cmdwin_result = CAR;
7403 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007404 else if (cmdwin_result == Ctrl_C)
7405 {
7406 /* :q or :close, don't execute any command
7407 * and don't modify the cmd window. */
7408 ccline.cmdbuff = NULL;
7409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 else
7411 ccline.cmdbuff = vim_strsave(ml_get_curline());
7412 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007413 {
7414 ccline.cmdbuff = vim_strsave((char_u *)"");
7415 ccline.cmdlen = 0;
7416 ccline.cmdbufflen = 1;
7417 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 else
7421 {
7422 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7423 ccline.cmdbufflen = ccline.cmdlen + 1;
7424 ccline.cmdpos = curwin->w_cursor.col;
7425 if (ccline.cmdpos > ccline.cmdlen)
7426 ccline.cmdpos = ccline.cmdlen;
7427 if (cmdwin_result == K_IGNORE)
7428 {
7429 set_cmdspos_cursor();
7430 redrawcmd();
7431 }
7432 }
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007435 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007436# ifdef FEAT_CONCEAL
7437 /* Avoid command-line window first character being concealed. */
7438 curwin->w_p_cole = 0;
7439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007441 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 win_goto(old_curwin);
7443 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007444
7445 /* win_close() may have already wiped the buffer when 'bh' is
7446 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007447 if (bufref_valid(&bufref))
7448 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
7450 /* Restore window sizes. */
7451 win_size_restore(&winsizes);
7452
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007453 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 }
7455
7456 ga_clear(&winsizes);
7457 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007458# ifdef FEAT_RIGHTLEFT
7459 cmdmsg_rl = save_cmdmsg_rl;
7460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 State = save_State;
7463# ifdef FEAT_MOUSE
7464 setmouse();
7465# endif
7466
7467 return cmdwin_result;
7468}
7469#endif /* FEAT_CMDWIN */
7470
7471/*
7472 * Used for commands that either take a simple command string argument, or:
7473 * cmd << endmarker
7474 * {script}
7475 * endmarker
7476 * Returns a pointer to allocated memory with {script} or NULL.
7477 */
7478 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481 char_u *theline;
7482 char *end_pattern = NULL;
7483 char dot[] = ".";
7484 garray_T ga;
7485
7486 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7487 return NULL;
7488
7489 ga_init2(&ga, 1, 0x400);
7490
7491 if (cmd[2] != NUL)
7492 end_pattern = (char *)skipwhite(cmd + 2);
7493 else
7494 end_pattern = dot;
7495
7496 for (;;)
7497 {
7498 theline = eap->getline(
7499#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007500 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501#endif
7502 NUL, eap->cookie, 0);
7503
7504 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007505 {
7506 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
7510 ga_concat(&ga, theline);
7511 ga_append(&ga, '\n');
7512 vim_free(theline);
7513 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007514 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
7516 return (char_u *)ga.ga_data;
7517}