blob: c316e192ad5f92a896f705cb4f610b21b701330d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020/*
21 * Variables shared between getcmdline(), redrawcmdline() and others.
22 * These need to be saved when using CTRL-R |, that's why they are in a
23 * structure.
24 */
25struct cmdline_info
26{
27 char_u *cmdbuff; /* pointer to command line buffer */
28 int cmdbufflen; /* length of cmdbuff */
29 int cmdlen; /* number of chars in command line */
30 int cmdpos; /* current cursor position */
31 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020032 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 int cmdindent; /* number of spaces before cmdline */
34 char_u *cmdprompt; /* message in front of cmdline */
35 int cmdattr; /* attributes for prompt */
36 int overstrike; /* Typing mode on the command line. Shared by
37 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000038 expand_T *xpc; /* struct being used for expansion, xp_pattern
39 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040 int xp_context; /* type of expansion */
41# ifdef FEAT_EVAL
42 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000043 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000047/* The current cmdline_info. It is initialized in getcmdline() and after that
48 * used by other functions. When invoking getcmdline() recursively it needs
49 * to be saved with save_cmdline() and restored with restore_cmdline().
50 * TODO: make it local to getcmdline() and pass it around. */
51static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53static int cmd_showtail; /* Only show path tail in lists ? */
54
55#ifdef FEAT_EVAL
56static int new_cmdpos; /* position set by set_cmdline_pos() */
57#endif
58
Bram Moolenaara92522f2017-07-15 15:21:38 +020059static int extra_char = NUL; /* extra character to display when redrawing
60 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020061static int extra_char_shift;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#ifdef FEAT_CMDHIST
64typedef struct hist_entry
65{
66 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020067 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020069 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} histentry_T;
71
72static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
73static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
74static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
75 /* identifying (unique) number of newest history entry */
76static int hislen = 0; /* actual length of history tables */
77
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaard25c16e2016-01-29 22:13:30 +010080static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010082static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083# endif
84#endif
85
86#ifdef FEAT_RIGHTLEFT
87static int cmd_hkmap = 0; /* Hebrew mapping during command line */
88#endif
89
90#ifdef FEAT_FKMAP
91static int cmd_fkmap = 0; /* Farsi mapping during command line */
92#endif
93
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static int cmdline_charsize(int idx);
95static void set_cmdspos(void);
96static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void alloc_cmdbuff(int len);
101static int realloc_cmdbuff(int len);
102static void draw_cmdline(int start, int len);
103static void save_cmdline(struct cmdline_info *ccp);
104static void restore_cmdline(struct cmdline_info *ccp);
105static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100107static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100110static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static void redrawcmdprompt(void);
113static void cursorcmd(void);
114static int ccheck_abbr(int);
115static int nextwild(expand_T *xp, int type, int options, int escape);
116static void escape_fname(char_u **pp);
117static int showmatches(expand_T *xp, int wildmenu);
118static void set_expand_context(expand_T *xp);
119static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
120static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100123static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100124static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200125# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100129static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
130static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# endif
132#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200133#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100134static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200138static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
142static int
143#ifdef __BORLANDC__
144_RTLENTRYF
145#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100146sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147#endif
148
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200149
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200161 * Abandon the command line.
162 */
163 static void
164abandon_cmdline(void)
165{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100166 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200167 if (msg_scrolled == 0)
168 compute_cmdrow();
169 MSG("");
170 redraw_cmdline = TRUE;
171}
172
Bram Moolenaaree219b02017-12-17 14:55:01 +0100173#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200174/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100175 * Guess that the pattern matches everything. Only finds specific cases, such
176 * as a trailing \|, which can happen while typing a pattern.
177 */
178 static int
179empty_pattern(char_u *p)
180{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100181 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100182
183 /* remove trailing \v and the like */
184 while (n >= 2 && p[n - 2] == '\\'
185 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
186 n -= 2;
187 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
188}
189
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200190// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200191typedef struct {
192 colnr_T vs_curswant;
193 colnr_T vs_leftcol;
194 linenr_T vs_topline;
195# ifdef FEAT_DIFF
196 int vs_topfill;
197# endif
198 linenr_T vs_botline;
199 linenr_T vs_empty_rows;
200} viewstate_T;
201
202 static void
203save_viewstate(viewstate_T *vs)
204{
205 vs->vs_curswant = curwin->w_curswant;
206 vs->vs_leftcol = curwin->w_leftcol;
207 vs->vs_topline = curwin->w_topline;
208# ifdef FEAT_DIFF
209 vs->vs_topfill = curwin->w_topfill;
210# endif
211 vs->vs_botline = curwin->w_botline;
212 vs->vs_empty_rows = curwin->w_empty_rows;
213}
214
215 static void
216restore_viewstate(viewstate_T *vs)
217{
218 curwin->w_curswant = vs->vs_curswant;
219 curwin->w_leftcol = vs->vs_leftcol;
220 curwin->w_topline = vs->vs_topline;
221# ifdef FEAT_DIFF
222 curwin->w_topfill = vs->vs_topfill;
223# endif
224 curwin->w_botline = vs->vs_botline;
225 curwin->w_empty_rows = vs->vs_empty_rows;
226}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200227
228// Struct to store the state of 'incsearch' highlighting.
229typedef struct {
230 pos_T search_start; // where 'incsearch' starts searching
231 pos_T save_cursor;
232 viewstate_T init_viewstate;
233 viewstate_T old_viewstate;
234 pos_T match_start;
235 pos_T match_end;
236 int did_incsearch;
237 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200238 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200239} incsearch_state_T;
240
241 static void
242init_incsearch_state(incsearch_state_T *is_state)
243{
244 is_state->match_start = curwin->w_cursor;
245 is_state->did_incsearch = FALSE;
246 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200247 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200248 CLEAR_POS(&is_state->match_end);
249 is_state->save_cursor = curwin->w_cursor; // may be restored later
250 is_state->search_start = curwin->w_cursor;
251 save_viewstate(&is_state->init_viewstate);
252 save_viewstate(&is_state->old_viewstate);
253}
254
255/*
256 * First move cursor to end of match, then to the start. This
257 * moves the whole match onto the screen when 'nowrap' is set.
258 */
259 static void
260set_search_match(pos_T *t)
261{
262 t->lnum += search_match_lines;
263 t->col = search_match_endcol;
264 if (t->lnum > curbuf->b_ml.ml_line_count)
265 {
266 t->lnum = curbuf->b_ml.ml_line_count;
267 coladvance((colnr_T)MAXCOL);
268 }
269}
270
271/*
272 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200274 */
275 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200276do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
277 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200278{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200279 char_u *cmd;
280 cmdmod_T save_cmdmod = cmdmod;
281 char_u *p;
282 int delim_optional = FALSE;
283 int delim;
284 char_u *end;
285 char_u *dummy;
286 exarg_T ea;
287 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200288 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200289
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200290 *skiplen = 0;
291 *patlen = ccline.cmdlen;
292
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200293 if (!p_is || cmd_silent)
294 return FALSE;
295
296 // by default search all lines
297 search_first_line = 0;
298 search_last_line = MAXLNUM;
299
300 if (firstc == '/' || firstc == '?')
301 return TRUE;
302 if (firstc != ':')
303 return FALSE;
304
305 vim_memset(&ea, 0, sizeof(ea));
306 ea.line1 = 1;
307 ea.line2 = 1;
308 ea.cmd = ccline.cmdbuff;
309 ea.addr_type = ADDR_LINES;
310
311 parse_command_modifiers(&ea, &dummy, TRUE);
312 cmdmod = save_cmdmod;
313
314 cmd = skip_range(ea.cmd, NULL);
315 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
316 return FALSE;
317
318 // Skip over "substitute" to find the pattern separator.
319 for (p = cmd; ASCII_ISALPHA(*p); ++p)
320 ;
321 if (*skipwhite(p) == NUL)
322 return FALSE;
323
324 if (STRNCMP(cmd, "substitute", p - cmd) == 0
325 || STRNCMP(cmd, "smagic", p - cmd) == 0
326 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
327 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200328 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200329 if (*cmd == 's' && cmd[1] == 'm')
330 p_magic = TRUE;
331 else if (*cmd == 's' && cmd[1] == 'n')
332 p_magic = FALSE;
333 }
334 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
335 {
336 // skip over flags
337 while (ASCII_ISALPHA(*(p = skipwhite(p))))
338 ++p;
339 if (*p == NUL)
340 return FALSE;
341 }
342 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
343 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
344 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
345 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
346 || STRNCMP(cmd, "global", p - cmd) == 0)
347 {
348 // skip over "!"
349 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200350 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200351 p++;
352 if (*skipwhite(p) == NUL)
353 return FALSE;
354 }
355 if (*cmd != 'g')
356 delim_optional = TRUE;
357 }
358 else
359 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200360
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200361 p = skipwhite(p);
362 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
363 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200364
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200365 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200366
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200367 if (end == p && !use_last_pat)
368 return FALSE;
369
370 // Don't do 'hlsearch' highlighting if the pattern matches everything.
371 if (!use_last_pat)
372 {
373 char c = *end;
374 int empty;
375
376 *end = NUL;
377 empty = empty_pattern(p);
378 *end = c;
379 if (empty)
380 return FALSE;
381 }
382
383 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200384 *skiplen = (int)(p - ccline.cmdbuff);
385 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200386
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200387 // parse the address range
388 save_cursor = curwin->w_cursor;
389 curwin->w_cursor = is_state->search_start;
390 parse_cmd_address(&ea, &dummy);
391 if (ea.addr_count > 0)
392 {
393 // Allow for reverse match.
394 if (ea.line2 < ea.line1)
395 {
396 search_first_line = ea.line2;
397 search_last_line = ea.line1;
398 }
399 else
400 {
401 search_first_line = ea.line1;
402 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200403 }
404 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200405 else if (cmd[0] == 's' && cmd[1] != 'o')
406 {
407 // :s defaults to the current line
408 search_first_line = curwin->w_cursor.lnum;
409 search_last_line = curwin->w_cursor.lnum;
410 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200411
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200412 curwin->w_cursor = save_cursor;
413 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200414}
415
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200416 static void
417finish_incsearch_highlighting(
418 int gotesc,
419 incsearch_state_T *is_state,
420 int call_update_screen)
421{
422 if (is_state->did_incsearch)
423 {
424 is_state->did_incsearch = FALSE;
425 if (gotesc)
426 curwin->w_cursor = is_state->save_cursor;
427 else
428 {
429 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
430 {
431 // put the '" mark at the original position
432 curwin->w_cursor = is_state->save_cursor;
433 setpcmark();
434 }
435 curwin->w_cursor = is_state->search_start;
436 }
437 restore_viewstate(&is_state->old_viewstate);
438 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200439
440 // by default search all lines
441 search_first_line = 0;
442 search_last_line = MAXLNUM;
443
444 p_magic = is_state->magic_save;
445
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200446 validate_cursor(); /* needed for TAB */
447 if (call_update_screen)
448 update_screen(SOME_VALID);
449 else
450 redraw_all_later(SOME_VALID);
451 }
452}
453
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200454/*
455 * Do 'incsearch' highlighting if desired.
456 */
457 static void
458may_do_incsearch_highlighting(
459 int firstc,
460 long count,
461 incsearch_state_T *is_state)
462{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200463 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200464 int i;
465 pos_T end_pos;
466 struct cmdline_info save_ccline;
467#ifdef FEAT_RELTIME
468 proftime_T tm;
469#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200470 int next_char;
471 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200473 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200474 {
475 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200476 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200477 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200478
479 // If there is a character waiting, search and redraw later.
480 if (char_avail())
481 {
482 is_state->incsearch_postponed = TRUE;
483 return;
484 }
485 is_state->incsearch_postponed = FALSE;
486
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200487 if (search_first_line == 0)
488 // start at the original cursor position
489 curwin->w_cursor = is_state->search_start;
490 else
491 {
492 // start at the first line in the range
493 curwin->w_cursor.lnum = search_first_line;
494 curwin->w_cursor.col = 0;
495 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200496 save_last_search_pattern();
497
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200498 // Use the previous pattern for ":s//".
499 next_char = ccline.cmdbuff[skiplen + patlen];
500 use_last_pat = patlen == 0 && skiplen > 0
501 && ccline.cmdbuff[skiplen - 1] == next_char;
502
503 // If there is no pattern, don't do anything.
504 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200505 {
506 i = 0;
507 set_no_hlsearch(TRUE); // turn off previous highlight
508 redraw_all_later(SOME_VALID);
509 }
510 else
511 {
512 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
513
514 cursor_off(); // so the user knows we're busy
515 out_flush();
516 ++emsg_off; // so it doesn't beep if bad expr
517#ifdef FEAT_RELTIME
518 // Set the time limit to half a second.
519 profile_setlimit(500L, &tm);
520#endif
521 if (!p_hls)
522 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200523 if (search_first_line != 0)
524 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200525 ccline.cmdbuff[skiplen + patlen] = NUL;
526 i = do_search(NULL, firstc == ':' ? '/' : firstc,
527 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200528#ifdef FEAT_RELTIME
529 &tm, NULL
530#else
531 NULL, NULL
532#endif
533 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200534 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200535 --emsg_off;
536
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200537 if (curwin->w_cursor.lnum < search_first_line
538 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200539 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200540 // match outside of address range
541 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200542 curwin->w_cursor = is_state->search_start;
543 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200544
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200545 // if interrupted while searching, behave like it failed
546 if (got_int)
547 {
548 (void)vpeekc(); // remove <C-C> from input stream
549 got_int = FALSE; // don't abandon the command line
550 i = 0;
551 }
552 else if (char_avail())
553 // cancelled searching because a char was typed
554 is_state->incsearch_postponed = TRUE;
555 }
556 if (i != 0)
557 highlight_match = TRUE; // highlight position
558 else
559 highlight_match = FALSE; // remove highlight
560
561 // First restore the old curwin values, so the screen is positioned in the
562 // same way as the actual search command.
563 restore_viewstate(&is_state->old_viewstate);
564 changed_cline_bef_curs();
565 update_topline();
566
567 if (i != 0)
568 {
569 pos_T save_pos = curwin->w_cursor;
570
571 is_state->match_start = curwin->w_cursor;
572 set_search_match(&curwin->w_cursor);
573 validate_cursor();
574 end_pos = curwin->w_cursor;
575 is_state->match_end = end_pos;
576 curwin->w_cursor = save_pos;
577 }
578 else
579 end_pos = curwin->w_cursor; // shutup gcc 4
580
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200581 // Disable 'hlsearch' highlighting if the pattern matches everything.
582 // Avoids a flash when typing "foo\|".
583 if (!use_last_pat)
584 {
585 next_char = ccline.cmdbuff[skiplen + patlen];
586 ccline.cmdbuff[skiplen + patlen] = NUL;
587 if (empty_pattern(ccline.cmdbuff))
588 set_no_hlsearch(TRUE);
589 ccline.cmdbuff[skiplen + patlen] = next_char;
590 }
591
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592 validate_cursor();
593 // May redraw the status line to show the cursor position.
594 if (p_ru && curwin->w_status_height > 0)
595 curwin->w_redr_status = TRUE;
596
597 save_cmdline(&save_ccline);
598 update_screen(SOME_VALID);
599 restore_cmdline(&save_ccline);
600 restore_last_search_pattern();
601
602 // Leave it at the end to make CTRL-R CTRL-W work.
603 if (i != 0)
604 curwin->w_cursor = end_pos;
605
606 msg_starthere();
607 redrawcmdline();
608 is_state->did_incsearch = TRUE;
609}
610
611/*
612 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
613 * or previous match.
614 * Returns FAIL when jumping to cmdline_not_changed;
615 */
616 static int
617may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200618 int firstc,
619 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200623 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 pos_T t;
625 char_u *pat;
626 int search_flags = SEARCH_NOOF;
627 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200630 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200631 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200632 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200633 return FAIL;
634
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200635 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200636 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200637 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200638 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200639 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200640 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200641 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200642 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200643
644 save_last_search_pattern();
645 cursor_off();
646 out_flush();
647 if (c == Ctrl_G)
648 {
649 t = is_state->match_end;
650 if (LT_POS(is_state->match_start, is_state->match_end))
651 // Start searching at the end of the match not at the beginning of
652 // the next column.
653 (void)decl(&t);
654 search_flags += SEARCH_COL;
655 }
656 else
657 t = is_state->match_start;
658 if (!p_hls)
659 search_flags += SEARCH_KEEP;
660 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200661 save = pat[patlen];
662 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200663 i = searchit(curwin, curbuf, &t,
664 c == Ctrl_G ? FORWARD : BACKWARD,
665 pat, count, search_flags,
666 RE_SEARCH, 0, NULL, NULL);
667 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200668 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200669 if (i)
670 {
671 is_state->search_start = is_state->match_start;
672 is_state->match_end = t;
673 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200674 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200675 {
676 // Move just before 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)decl(&is_state->search_start);
680 }
681 else if (c == Ctrl_G && firstc == '?')
682 {
683 // Move just after the current match, so that when nv_search
684 // finishes the cursor will be put back on the match.
685 is_state->search_start = t;
686 (void)incl(&is_state->search_start);
687 }
688 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
689 {
690 // wrap around
691 is_state->search_start = t;
692 if (firstc == '?')
693 (void)incl(&is_state->search_start);
694 else
695 (void)decl(&is_state->search_start);
696 }
697
698 set_search_match(&is_state->match_end);
699 curwin->w_cursor = is_state->match_start;
700 changed_cline_bef_curs();
701 update_topline();
702 validate_cursor();
703 highlight_match = TRUE;
704 save_viewstate(&is_state->old_viewstate);
705 update_screen(NOT_VALID);
706 redrawcmdline();
707 }
708 else
709 vim_beep(BO_ERROR);
710 restore_last_search_pattern();
711 return FAIL;
712}
713
714/*
715 * When CTRL-L typed: add character from the match to the pattern.
716 * May set "*c" to the added character.
717 * Return OK when jumping to cmdline_not_changed.
718 */
719 static int
720may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
721{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200722 int skiplen, patlen;
723
724 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200725 return FAIL;
726
727 // Add a character from under the cursor for 'incsearch'.
728 if (is_state->did_incsearch)
729 {
730 curwin->w_cursor = is_state->match_end;
731 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
732 {
733 *c = gchar_cursor();
734
735 // If 'ignorecase' and 'smartcase' are set and the
736 // command line has no uppercase characters, convert
737 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200738 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200739 *c = MB_TOLOWER(*c);
740 if (*c != NUL)
741 {
742 if (*c == firstc || vim_strchr((char_u *)(
743 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
744 {
745 // put a backslash before special characters
746 stuffcharReadbuff(*c);
747 *c = '\\';
748 }
749 return FAIL;
750 }
751 }
752 }
753 return OK;
754}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200755#endif
756
Bram Moolenaar66216052017-12-16 16:33:44 +0100757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * getcmdline() - accept a command line starting with firstc.
759 *
760 * firstc == ':' get ":" command line.
761 * firstc == '/' or '?' get search pattern
762 * firstc == '=' get expression
763 * firstc == '@' get text for input() function
764 * firstc == '>' get text for debug mode
765 * firstc == NUL get text for :insert command
766 * firstc == -1 like NUL, and break on CTRL-C
767 *
768 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
769 * command line.
770 *
771 * Careful: getcmdline() can be called recursively!
772 *
773 * Return pointer to allocated string if there is a commandline, NULL
774 * otherwise.
775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100777getcmdline(
778 int firstc,
779 long count UNUSED, /* only used for incremental search */
780 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 int c;
783 int i;
784 int j;
785 int gotesc = FALSE; /* TRUE when <ESC> just typed */
786 int do_abbr; /* when TRUE check for abbr. */
787#ifdef FEAT_CMDHIST
788 char_u *lookfor = NULL; /* string to match */
789 int hiscnt; /* current history line in use */
790 int histype; /* history type to be used */
791#endif
792#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200793 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794#endif
795 int did_wild_list = FALSE; /* did wild_list() recently */
796 int wim_index = 0; /* index in wim_flags[] */
797 int res;
798 int save_msg_scroll = msg_scroll;
799 int save_State = State; /* remember State when called */
800 int some_key_typed = FALSE; /* one of the keys was typed */
801#ifdef FEAT_MOUSE
802 /* mouse drag and release events are ignored, unless they are
803 * preceded with a mouse down event */
804 int ignore_drag_release = TRUE;
805#endif
806#ifdef FEAT_EVAL
807 int break_ctrl_c = FALSE;
808#endif
809 expand_T xpc;
810 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200811#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000812 /* Everything that may work recursively should save and restore the
813 * current command line in save_ccline. That includes update_screen(), a
814 * custom status line may invoke ":normal". */
815 struct cmdline_info save_ccline;
816#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200817 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#ifdef FEAT_EVAL
820 if (firstc == -1)
821 {
822 firstc = NUL;
823 break_ctrl_c = TRUE;
824 }
825#endif
826#ifdef FEAT_RIGHTLEFT
827 /* start without Hebrew mapping for a command line */
828 if (firstc == ':' || firstc == '=' || firstc == '>')
829 cmd_hkmap = 0;
830#endif
831
832 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200835 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#endif
837
838 /*
839 * set some variables for redrawcmd()
840 */
841 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000842 ccline.cmdindent = (firstc > 0 ? indent : 0);
843
844 /* alloc initial ccline.cmdbuff */
845 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (ccline.cmdbuff == NULL)
847 return NULL; /* out of memory */
848 ccline.cmdlen = ccline.cmdpos = 0;
849 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100850 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000852 /* autoindent for :insert and :append */
853 if (firstc <= 0)
854 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200855 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000856 ccline.cmdbuff[indent] = NUL;
857 ccline.cmdpos = indent;
858 ccline.cmdspos = indent;
859 ccline.cmdlen = indent;
860 }
861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000863 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865#ifdef FEAT_RIGHTLEFT
866 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
867 && (firstc == '/' || firstc == '?'))
868 cmdmsg_rl = TRUE;
869 else
870 cmdmsg_rl = FALSE;
871#endif
872
873 redir_off = TRUE; /* don't redirect the typed command */
874 if (!cmd_silent)
875 {
876 i = msg_scrolled;
877 msg_scrolled = 0; /* avoid wait_return message */
878 gotocmdline(TRUE);
879 msg_scrolled += i;
880 redrawcmdprompt(); /* draw prompt or indent */
881 set_cmdspos();
882 }
883 xpc.xp_context = EXPAND_NOTHING;
884 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000885#ifndef BACKSLASH_IN_FILENAME
886 xpc.xp_shell = FALSE;
887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000889#if defined(FEAT_EVAL)
890 if (ccline.input_fn)
891 {
892 xpc.xp_context = ccline.xp_context;
893 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000894# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000895 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000896# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000897 }
898#endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 /*
901 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
902 * doing ":@0" when register 0 doesn't contain a CR.
903 */
904 msg_scroll = FALSE;
905
906 State = CMDLINE;
907
908 if (firstc == '/' || firstc == '?' || firstc == '@')
909 {
910 /* Use ":lmap" mappings for search pattern and input(). */
911 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
912 b_im_ptr = &curbuf->b_p_iminsert;
913 else
914 b_im_ptr = &curbuf->b_p_imsearch;
915 if (*b_im_ptr == B_IMODE_LMAP)
916 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100917#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 im_set_active(*b_im_ptr == B_IMODE_IM);
919#endif
920 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100921#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else if (p_imcmdline)
923 im_set_active(TRUE);
924#endif
925
926#ifdef FEAT_MOUSE
927 setmouse();
928#endif
929#ifdef CURSOR_SHAPE
930 ui_cursor_shape(); /* may show different cursor shape */
931#endif
932
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000933 /* When inside an autocommand for writing "exiting" may be set and
934 * terminal mode set to cooked. Need to set raw mode here then. */
935 settmode(TMODE_RAW);
936
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200937 /* Trigger CmdlineEnter autocommands. */
938 cmdline_type = firstc == NUL ? '-' : firstc;
939 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#ifdef FEAT_CMDHIST
942 init_history();
943 hiscnt = hislen; /* set hiscnt to impossible history value */
944 histype = hist_char2type(firstc);
945#endif
946
947#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000948 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#endif
950
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200951 /* If something above caused an error, reset the flags, we do want to type
952 * and execute commands. Display may be messed up a bit. */
953 if (did_emsg)
954 redrawcmd();
955 did_emsg = FALSE;
956 got_int = FALSE;
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 /*
959 * Collect the command string, handling editing keys.
960 */
961 for (;;)
962 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000963 redir_off = TRUE; /* Don't redirect the typed command.
964 Repeated, because a ":redir" inside
965 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#ifdef USE_ON_FLY_SCROLL
967 dont_scroll = FALSE; /* allow scrolling here */
968#endif
969 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
970
Bram Moolenaar72532d32018-04-07 19:09:09 +0200971 did_emsg = FALSE; /* There can't really be a reason why an error
972 that occurs while typing a command should
973 cause the command not to be executed. */
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000976
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100977 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
978 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000979 do
980 {
981 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100982 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (KeyTyped)
985 {
986 some_key_typed = TRUE;
987#ifdef FEAT_RIGHTLEFT
988 if (cmd_hkmap)
989 c = hkmap(c);
990# ifdef FEAT_FKMAP
991 if (cmd_fkmap)
992 c = cmdl_fkmap(c);
993# endif
994 if (cmdmsg_rl && !KeyStuffed)
995 {
996 /* Invert horizontal movements and operations. Only when
997 * typed by the user directly, not when the result of a
998 * mapping. */
999 switch (c)
1000 {
1001 case K_RIGHT: c = K_LEFT; break;
1002 case K_S_RIGHT: c = K_S_LEFT; break;
1003 case K_C_RIGHT: c = K_C_LEFT; break;
1004 case K_LEFT: c = K_RIGHT; break;
1005 case K_S_LEFT: c = K_S_RIGHT; break;
1006 case K_C_LEFT: c = K_C_RIGHT; break;
1007 }
1008 }
1009#endif
1010 }
1011
1012 /*
1013 * Ignore got_int when CTRL-C was typed here.
1014 * Don't ignore it in :global, we really need to break then, e.g., for
1015 * ":g/pat/normal /pat" (without the <CR>).
1016 * Don't ignore it for the input() function.
1017 */
1018 if ((c == Ctrl_C
1019#ifdef UNIX
1020 || c == intr_char
1021#endif
1022 )
1023#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1024 && firstc != '@'
1025#endif
1026#ifdef FEAT_EVAL
1027 && !break_ctrl_c
1028#endif
1029 && !global_busy)
1030 got_int = FALSE;
1031
1032#ifdef FEAT_CMDHIST
1033 /* free old command line when finished moving around in the history
1034 * list */
1035 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001036 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001037 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 && c != K_PAGEDOWN && c != K_PAGEUP
1039 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001040 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001042 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043#endif
1044
1045 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001046 * When there are matching completions to select <S-Tab> works like
1047 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001049 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 c = Ctrl_P;
1051
1052#ifdef FEAT_WILDMENU
1053 /* Special translations for 'wildmenu' */
1054 if (did_wild_list && p_wmnu)
1055 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001056 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001058 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 c = Ctrl_N;
1060 }
1061 /* Hitting CR after "emenu Name.": complete submenu */
1062 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1063 && ccline.cmdpos > 1
1064 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1065 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1066 && (c == '\n' || c == '\r' || c == K_KENTER))
1067 c = K_DOWN;
1068#endif
1069
1070 /* free expanded names when finished walking through matches */
1071 if (xpc.xp_numfiles != -1
1072 && !(c == p_wc && KeyTyped) && c != p_wcm
1073 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1074 && c != Ctrl_L)
1075 {
1076 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1077 did_wild_list = FALSE;
1078#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001079 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080#endif
1081 xpc.xp_context = EXPAND_NOTHING;
1082 wim_index = 0;
1083#ifdef FEAT_WILDMENU
1084 if (p_wmnu && wild_menu_showing != 0)
1085 {
1086 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001087 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001088
1089 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001090 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 if (wild_menu_showing == WM_SCROLLED)
1093 {
1094 /* Entered command line, move it up */
1095 cmdline_row--;
1096 redrawcmd();
1097 }
1098 else if (save_p_ls != -1)
1099 {
1100 /* restore 'laststatus' and 'winminheight' */
1101 p_ls = save_p_ls;
1102 p_wmh = save_p_wmh;
1103 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001104 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001106 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 redrawcmd();
1108 save_p_ls = -1;
1109 }
1110 else
1111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 redraw_statuslines();
1114 }
1115 KeyTyped = skt;
1116 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001117 if (ccline.input_fn)
1118 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120#endif
1121 }
1122
1123#ifdef FEAT_WILDMENU
1124 /* Special translations for 'wildmenu' */
1125 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1126 {
1127 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001128 if (c == K_DOWN && ccline.cmdpos > 0
1129 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001131 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
1133 /* Hitting <Up>: Remove one submenu name in front of the
1134 * cursor */
1135 int found = FALSE;
1136
1137 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1138 i = 0;
1139 while (--j > 0)
1140 {
1141 /* check for start of menu name */
1142 if (ccline.cmdbuff[j] == ' '
1143 && ccline.cmdbuff[j - 1] != '\\')
1144 {
1145 i = j + 1;
1146 break;
1147 }
1148 /* check for start of submenu name */
1149 if (ccline.cmdbuff[j] == '.'
1150 && ccline.cmdbuff[j - 1] != '\\')
1151 {
1152 if (found)
1153 {
1154 i = j + 1;
1155 break;
1156 }
1157 else
1158 found = TRUE;
1159 }
1160 }
1161 if (i > 0)
1162 cmdline_del(i);
1163 c = p_wc;
1164 xpc.xp_context = EXPAND_NOTHING;
1165 }
1166 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001167 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001168 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001169 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
1171 char_u upseg[5];
1172
1173 upseg[0] = PATHSEP;
1174 upseg[1] = '.';
1175 upseg[2] = '.';
1176 upseg[3] = PATHSEP;
1177 upseg[4] = NUL;
1178
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001179 if (c == K_DOWN
1180 && ccline.cmdpos > 0
1181 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1182 && (ccline.cmdpos < 3
1183 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1185 {
1186 /* go down a directory */
1187 c = p_wc;
1188 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001189 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
1191 /* If in a direct ancestor, strip off one ../ to go down */
1192 int found = FALSE;
1193
1194 j = ccline.cmdpos;
1195 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1196 while (--j > i)
1197 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001198#ifdef FEAT_MBYTE
1199 if (has_mbyte)
1200 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (vim_ispathsep(ccline.cmdbuff[j]))
1203 {
1204 found = TRUE;
1205 break;
1206 }
1207 }
1208 if (found
1209 && ccline.cmdbuff[j - 1] == '.'
1210 && ccline.cmdbuff[j - 2] == '.'
1211 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1212 {
1213 cmdline_del(j - 2);
1214 c = p_wc;
1215 }
1216 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001217 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 /* go up a directory */
1220 int found = FALSE;
1221
1222 j = ccline.cmdpos - 1;
1223 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1224 while (--j > i)
1225 {
1226#ifdef FEAT_MBYTE
1227 if (has_mbyte)
1228 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1229#endif
1230 if (vim_ispathsep(ccline.cmdbuff[j])
1231#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001232 && vim_strchr((char_u *)" *?[{`$%#",
1233 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#endif
1235 )
1236 {
1237 if (found)
1238 {
1239 i = j + 1;
1240 break;
1241 }
1242 else
1243 found = TRUE;
1244 }
1245 }
1246
1247 if (!found)
1248 j = i;
1249 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1250 j += 4;
1251 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1252 && j == i)
1253 j += 3;
1254 else
1255 j = 0;
1256 if (j > 0)
1257 {
1258 /* TODO this is only for DOS/UNIX systems - need to put in
1259 * machine-specific stuff here and in upseg init */
1260 cmdline_del(j);
1261 put_on_cmdline(upseg + 1, 3, FALSE);
1262 }
1263 else if (ccline.cmdpos > i)
1264 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001265
1266 /* Now complete in the new directory. Set KeyTyped in case the
1267 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001269 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
1273#endif /* FEAT_WILDMENU */
1274
1275 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1276 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1277 if (c == Ctrl_BSL)
1278 {
1279 ++no_mapping;
1280 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001281 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 --no_mapping;
1283 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001284 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1285 * is in a mapping. */
1286 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1287 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
1289 vungetc(c);
1290 c = Ctrl_BSL;
1291 }
1292#ifdef FEAT_EVAL
1293 else if (c == 'e')
1294 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001295 char_u *p = NULL;
1296 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298 /*
1299 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001300 * Need to save and restore the current command line, to be
1301 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 */
1303 if (ccline.cmdpos == ccline.cmdlen)
1304 new_cmdpos = 99999; /* keep it at the end */
1305 else
1306 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001307
1308 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001310 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (c == '=')
1312 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001313 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001314 * to avoid nasty things like going to another buffer when
1315 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001316 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001317 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001319 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001320 restore_cmdline(&save_ccline);
1321
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001322 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001324 len = (int)STRLEN(p);
1325 if (realloc_cmdbuff(len + 1) == OK)
1326 {
1327 ccline.cmdlen = len;
1328 STRCPY(ccline.cmdbuff, p);
1329 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001331 /* Restore the cursor or use the position set with
1332 * set_cmdline_pos(). */
1333 if (new_cmdpos > ccline.cmdlen)
1334 ccline.cmdpos = ccline.cmdlen;
1335 else
1336 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001338 KeyTyped = FALSE; /* Don't do p_wc completion. */
1339 redrawcmd();
1340 goto cmdline_changed;
1341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 }
1344 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001345 got_int = FALSE; /* don't abandon the command line */
1346 did_emsg = FALSE;
1347 emsg_on_display = FALSE;
1348 redrawcmd();
1349 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351#endif
1352 else
1353 {
1354 if (c == Ctrl_G && p_im && restart_edit == 0)
1355 restart_edit = 'a';
1356 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1357 in history */
1358 goto returncmd; /* back to Normal mode */
1359 }
1360 }
1361
1362#ifdef FEAT_CMDWIN
1363 if (c == cedit_key || c == K_CMDWIN)
1364 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001365 if (ex_normal_busy == 0 && got_int == FALSE)
1366 {
1367 /*
1368 * Open a window to edit the command line (and history).
1369 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001370 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001371 some_key_typed = TRUE;
1372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374# ifdef FEAT_DIGRAPHS
1375 else
1376# endif
1377#endif
1378#ifdef FEAT_DIGRAPHS
1379 c = do_digraph(c);
1380#endif
1381
1382 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1383 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1384 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001385 /* In Ex mode a backslash escapes a newline. */
1386 if (exmode_active
1387 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001388 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001389 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001390 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001392 if (c == K_KENTER)
1393 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001395 else
1396 {
1397 gotesc = FALSE; /* Might have typed ESC previously, don't
1398 truncate the cmdline now. */
1399 if (ccheck_abbr(c + ABBR_OFF))
1400 goto cmdline_changed;
1401 if (!cmd_silent)
1402 {
1403 windgoto(msg_row, 0);
1404 out_flush();
1405 }
1406 break;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409
1410 /*
1411 * Completion for 'wildchar' or 'wildcharm' key.
1412 * - hitting <ESC> twice means: abandon command line.
1413 * - wildcard expansion is only done when the 'wildchar' key is really
1414 * typed, not when it comes from a macro
1415 */
1416 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1417 {
1418 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1419 {
1420 /* if 'wildmode' contains "list" may still need to list */
1421 if (xpc.xp_numfiles > 1
1422 && !did_wild_list
1423 && (wim_flags[wim_index] & WIM_LIST))
1424 {
1425 (void)showmatches(&xpc, FALSE);
1426 redrawcmd();
1427 did_wild_list = TRUE;
1428 }
1429 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001430 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1431 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001433 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1434 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 else
1436 res = OK; /* don't insert 'wildchar' now */
1437 }
1438 else /* typed p_wc first time */
1439 {
1440 wim_index = 0;
1441 j = ccline.cmdpos;
1442 /* if 'wildmode' first contains "longest", get longest
1443 * common part */
1444 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001445 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1446 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001448 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1449 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451 /* if interrupted while completing, behave like it failed */
1452 if (got_int)
1453 {
1454 (void)vpeekc(); /* remove <C-C> from input stream */
1455 got_int = FALSE; /* don't abandon the command line */
1456 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1457#ifdef FEAT_WILDMENU
1458 xpc.xp_context = EXPAND_NOTHING;
1459#endif
1460 goto cmdline_changed;
1461 }
1462
1463 /* when more than one match, and 'wildmode' first contains
1464 * "list", or no change and 'wildmode' contains "longest,list",
1465 * list all matches */
1466 if (res == OK && xpc.xp_numfiles > 1)
1467 {
1468 /* a "longest" that didn't do anything is skipped (but not
1469 * "list:longest") */
1470 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1471 wim_index = 1;
1472 if ((wim_flags[wim_index] & WIM_LIST)
1473#ifdef FEAT_WILDMENU
1474 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1475#endif
1476 )
1477 {
1478 if (!(wim_flags[0] & WIM_LONGEST))
1479 {
1480#ifdef FEAT_WILDMENU
1481 int p_wmnu_save = p_wmnu;
1482 p_wmnu = 0;
1483#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001484 /* remove match */
1485 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#ifdef FEAT_WILDMENU
1487 p_wmnu = p_wmnu_save;
1488#endif
1489 }
1490#ifdef FEAT_WILDMENU
1491 (void)showmatches(&xpc, p_wmnu
1492 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1493#else
1494 (void)showmatches(&xpc, FALSE);
1495#endif
1496 redrawcmd();
1497 did_wild_list = TRUE;
1498 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001499 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1500 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001502 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1503 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001506 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508#ifdef FEAT_WILDMENU
1509 else if (xpc.xp_numfiles == -1)
1510 xpc.xp_context = EXPAND_NOTHING;
1511#endif
1512 }
1513 if (wim_index < 3)
1514 ++wim_index;
1515 if (c == ESC)
1516 gotesc = TRUE;
1517 if (res == OK)
1518 goto cmdline_changed;
1519 }
1520
1521 gotesc = FALSE;
1522
1523 /* <S-Tab> goes to last match, in a clumsy way */
1524 if (c == K_S_TAB && KeyTyped)
1525 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001526 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1527 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1528 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 goto cmdline_changed;
1530 }
1531
1532 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1533 c = NL;
1534
1535 do_abbr = TRUE; /* default: check for abbreviation */
1536
1537 /*
1538 * Big switch for a typed command line character.
1539 */
1540 switch (c)
1541 {
1542 case K_BS:
1543 case Ctrl_H:
1544 case K_DEL:
1545 case K_KDEL:
1546 case Ctrl_W:
1547#ifdef FEAT_FKMAP
1548 if (cmd_fkmap && c == K_BS)
1549 c = K_DEL;
1550#endif
1551 if (c == K_KDEL)
1552 c = K_DEL;
1553
1554 /*
1555 * delete current character is the same as backspace on next
1556 * character, except at end of line
1557 */
1558 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1559 ++ccline.cmdpos;
1560#ifdef FEAT_MBYTE
1561 if (has_mbyte && c == K_DEL)
1562 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1563 ccline.cmdbuff + ccline.cmdpos);
1564#endif
1565 if (ccline.cmdpos > 0)
1566 {
1567 char_u *p;
1568
1569 j = ccline.cmdpos;
1570 p = ccline.cmdbuff + j;
1571#ifdef FEAT_MBYTE
1572 if (has_mbyte)
1573 {
1574 p = mb_prevptr(ccline.cmdbuff, p);
1575 if (c == Ctrl_W)
1576 {
1577 while (p > ccline.cmdbuff && vim_isspace(*p))
1578 p = mb_prevptr(ccline.cmdbuff, p);
1579 i = mb_get_class(p);
1580 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1581 p = mb_prevptr(ccline.cmdbuff, p);
1582 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001583 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585 }
1586 else
1587#endif
1588 if (c == Ctrl_W)
1589 {
1590 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1591 --p;
1592 i = vim_iswordc(p[-1]);
1593 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1594 && vim_iswordc(p[-1]) == i)
1595 --p;
1596 }
1597 else
1598 --p;
1599 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1600 ccline.cmdlen -= j - ccline.cmdpos;
1601 i = ccline.cmdpos;
1602 while (i < ccline.cmdlen)
1603 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1604
1605 /* Truncate at the end, required for multi-byte chars. */
1606 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001607#ifdef FEAT_SEARCH_EXTRA
1608 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001609 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001610 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001611 /* save view settings, so that the screen
1612 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001613 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001614 }
1615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 redrawcmd();
1617 }
1618 else if (ccline.cmdlen == 0 && c != Ctrl_W
1619 && ccline.cmdprompt == NULL && indent == 0)
1620 {
1621 /* In ex and debug mode it doesn't make sense to return. */
1622 if (exmode_active
1623#ifdef FEAT_EVAL
1624 || ccline.cmdfirstc == '>'
1625#endif
1626 )
1627 goto cmdline_not_changed;
1628
Bram Moolenaard23a8232018-02-10 18:45:26 +01001629 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (!cmd_silent)
1631 {
1632#ifdef FEAT_RIGHTLEFT
1633 if (cmdmsg_rl)
1634 msg_col = Columns;
1635 else
1636#endif
1637 msg_col = 0;
1638 msg_putchar(' '); /* delete ':' */
1639 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001640#ifdef FEAT_SEARCH_EXTRA
1641 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001642 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 redraw_cmdline = TRUE;
1645 goto returncmd; /* back to cmd mode */
1646 }
1647 goto cmdline_changed;
1648
1649 case K_INS:
1650 case K_KINS:
1651#ifdef FEAT_FKMAP
1652 /* if Farsi mode set, we are in reverse insert mode -
1653 Do not change the mode */
1654 if (cmd_fkmap)
1655 beep_flush();
1656 else
1657#endif
1658 ccline.overstrike = !ccline.overstrike;
1659#ifdef CURSOR_SHAPE
1660 ui_cursor_shape(); /* may show different cursor shape */
1661#endif
1662 goto cmdline_not_changed;
1663
1664 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001665 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
1667 /* ":lmap" mappings exists, toggle use of mappings. */
1668 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001669#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 im_set_active(FALSE); /* Disable input method */
1671#endif
1672 if (b_im_ptr != NULL)
1673 {
1674 if (State & LANGMAP)
1675 *b_im_ptr = B_IMODE_LMAP;
1676 else
1677 *b_im_ptr = B_IMODE_NONE;
1678 }
1679 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001680#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 else
1682 {
1683 /* There are no ":lmap" mappings, toggle IM. When
1684 * 'imdisable' is set don't try getting the status, it's
1685 * always off. */
1686 if ((p_imdisable && b_im_ptr != NULL)
1687 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1688 {
1689 im_set_active(FALSE); /* Disable input method */
1690 if (b_im_ptr != NULL)
1691 *b_im_ptr = B_IMODE_NONE;
1692 }
1693 else
1694 {
1695 im_set_active(TRUE); /* Enable input method */
1696 if (b_im_ptr != NULL)
1697 *b_im_ptr = B_IMODE_IM;
1698 }
1699 }
1700#endif
1701 if (b_im_ptr != NULL)
1702 {
1703 if (b_im_ptr == &curbuf->b_p_iminsert)
1704 set_iminsert_global();
1705 else
1706 set_imsearch_global();
1707 }
1708#ifdef CURSOR_SHAPE
1709 ui_cursor_shape(); /* may show different cursor shape */
1710#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001711#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 /* Show/unshow value of 'keymap' in status lines later. */
1713 status_redraw_curbuf();
1714#endif
1715 goto cmdline_not_changed;
1716
1717/* case '@': only in very old vi */
1718 case Ctrl_U:
1719 /* delete all characters left of the cursor */
1720 j = ccline.cmdpos;
1721 ccline.cmdlen -= j;
1722 i = ccline.cmdpos = 0;
1723 while (i < ccline.cmdlen)
1724 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1725 /* Truncate at the end, required for multi-byte chars. */
1726 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001727#ifdef FEAT_SEARCH_EXTRA
1728 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001729 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 redrawcmd();
1732 goto cmdline_changed;
1733
1734#ifdef FEAT_CLIPBOARD
1735 case Ctrl_Y:
1736 /* Copy the modeless selection, if there is one. */
1737 if (clip_star.state != SELECT_CLEARED)
1738 {
1739 if (clip_star.state == SELECT_DONE)
1740 clip_copy_modeless_selection(TRUE);
1741 goto cmdline_not_changed;
1742 }
1743 break;
1744#endif
1745
1746 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1747 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001748 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001749 * ":normal" runs out of characters. */
1750 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001751 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 goto cmdline_not_changed;
1753
1754 gotesc = TRUE; /* will free ccline.cmdbuff after
1755 putting it in history */
1756 goto returncmd; /* back to cmd mode */
1757
1758 case Ctrl_R: /* insert register */
1759#ifdef USE_ON_FLY_SCROLL
1760 dont_scroll = TRUE; /* disallow scrolling here */
1761#endif
1762 putcmdline('"', TRUE);
1763 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001764 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 if (i == Ctrl_O)
1766 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1767 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001768 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001769 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 --no_mapping;
1771#ifdef FEAT_EVAL
1772 /*
1773 * Insert the result of an expression.
1774 * Need to save the current command line, to be able to enter
1775 * a new one...
1776 */
1777 new_cmdpos = -1;
1778 if (c == '=')
1779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1781 {
1782 beep_flush();
1783 c = ESC;
1784 }
1785 else
1786 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001787 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001789 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 }
1791 }
1792#endif
1793 if (c != ESC) /* use ESC to cancel inserting register */
1794 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001795 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001796
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001797#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001798 /* When there was a serious error abort getting the
1799 * command line. */
1800 if (aborting())
1801 {
1802 gotesc = TRUE; /* will free ccline.cmdbuff after
1803 putting it in history */
1804 goto returncmd; /* back to cmd mode */
1805 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 KeyTyped = FALSE; /* Don't do p_wc completion. */
1808#ifdef FEAT_EVAL
1809 if (new_cmdpos >= 0)
1810 {
1811 /* set_cmdline_pos() was used */
1812 if (new_cmdpos > ccline.cmdlen)
1813 ccline.cmdpos = ccline.cmdlen;
1814 else
1815 ccline.cmdpos = new_cmdpos;
1816 }
1817#endif
1818 }
1819 redrawcmd();
1820 goto cmdline_changed;
1821
1822 case Ctrl_D:
1823 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1824 break; /* Use ^D as normal char instead */
1825
1826 redrawcmd();
1827 continue; /* don't do incremental search now */
1828
1829 case K_RIGHT:
1830 case K_S_RIGHT:
1831 case K_C_RIGHT:
1832 do
1833 {
1834 if (ccline.cmdpos >= ccline.cmdlen)
1835 break;
1836 i = cmdline_charsize(ccline.cmdpos);
1837 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1838 break;
1839 ccline.cmdspos += i;
1840#ifdef FEAT_MBYTE
1841 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001842 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 + ccline.cmdpos);
1844 else
1845#endif
1846 ++ccline.cmdpos;
1847 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001848 while ((c == K_S_RIGHT || c == K_C_RIGHT
1849 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1851#ifdef FEAT_MBYTE
1852 if (has_mbyte)
1853 set_cmdspos_cursor();
1854#endif
1855 goto cmdline_not_changed;
1856
1857 case K_LEFT:
1858 case K_S_LEFT:
1859 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001860 if (ccline.cmdpos == 0)
1861 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 do
1863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 --ccline.cmdpos;
1865#ifdef FEAT_MBYTE
1866 if (has_mbyte) /* move to first byte of char */
1867 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1868 ccline.cmdbuff + ccline.cmdpos);
1869#endif
1870 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1871 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001872 while (ccline.cmdpos > 0
1873 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001874 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1876#ifdef FEAT_MBYTE
1877 if (has_mbyte)
1878 set_cmdspos_cursor();
1879#endif
1880 goto cmdline_not_changed;
1881
1882 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001883 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001884 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
Bram Moolenaar4770d092006-01-12 23:22:24 +00001886#ifdef FEAT_GUI_W32
1887 /* On Win32 ignore <M-F4>, we get it when closing the window was
1888 * cancelled. */
1889 case K_F4:
1890 if (mod_mask == MOD_MASK_ALT)
1891 {
1892 redrawcmd(); /* somehow the cmdline is cleared */
1893 goto cmdline_not_changed;
1894 }
1895 break;
1896#endif
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#ifdef FEAT_MOUSE
1899 case K_MIDDLEDRAG:
1900 case K_MIDDLERELEASE:
1901 goto cmdline_not_changed; /* Ignore mouse */
1902
1903 case K_MIDDLEMOUSE:
1904# ifdef FEAT_GUI
1905 /* When GUI is active, also paste when 'mouse' is empty */
1906 if (!gui.in_use)
1907# endif
1908 if (!mouse_has(MOUSE_COMMAND))
1909 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001910# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001912 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001914# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001915 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 redrawcmd();
1917 goto cmdline_changed;
1918
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001919# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001921 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 redrawcmd();
1923 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926 case K_LEFTDRAG:
1927 case K_LEFTRELEASE:
1928 case K_RIGHTDRAG:
1929 case K_RIGHTRELEASE:
1930 /* Ignore drag and release events when the button-down wasn't
1931 * seen before. */
1932 if (ignore_drag_release)
1933 goto cmdline_not_changed;
1934 /* FALLTHROUGH */
1935 case K_LEFTMOUSE:
1936 case K_RIGHTMOUSE:
1937 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1938 ignore_drag_release = TRUE;
1939 else
1940 ignore_drag_release = FALSE;
1941# ifdef FEAT_GUI
1942 /* When GUI is active, also move when 'mouse' is empty */
1943 if (!gui.in_use)
1944# endif
1945 if (!mouse_has(MOUSE_COMMAND))
1946 goto cmdline_not_changed; /* Ignore mouse */
1947# ifdef FEAT_CLIPBOARD
1948 if (mouse_row < cmdline_row && clip_star.available)
1949 {
1950 int button, is_click, is_drag;
1951
1952 /*
1953 * Handle modeless selection.
1954 */
1955 button = get_mouse_button(KEY2TERMCAP1(c),
1956 &is_click, &is_drag);
1957 if (mouse_model_popup() && button == MOUSE_LEFT
1958 && (mod_mask & MOD_MASK_SHIFT))
1959 {
1960 /* Translate shift-left to right button. */
1961 button = MOUSE_RIGHT;
1962 mod_mask &= ~MOD_MASK_SHIFT;
1963 }
1964 clip_modeless(button, is_click, is_drag);
1965 goto cmdline_not_changed;
1966 }
1967# endif
1968
1969 set_cmdspos();
1970 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1971 ++ccline.cmdpos)
1972 {
1973 i = cmdline_charsize(ccline.cmdpos);
1974 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1975 && mouse_col < ccline.cmdspos % Columns + i)
1976 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001977# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (has_mbyte)
1979 {
1980 /* Count ">" for double-wide char that doesn't fit. */
1981 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001982 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 + ccline.cmdpos) - 1;
1984 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 ccline.cmdspos += i;
1987 }
1988 goto cmdline_not_changed;
1989
1990 /* Mouse scroll wheel: ignored here */
1991 case K_MOUSEDOWN:
1992 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001993 case K_MOUSELEFT:
1994 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 /* Alternate buttons ignored here */
1996 case K_X1MOUSE:
1997 case K_X1DRAG:
1998 case K_X1RELEASE:
1999 case K_X2MOUSE:
2000 case K_X2DRAG:
2001 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002002 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 goto cmdline_not_changed;
2004
2005#endif /* FEAT_MOUSE */
2006
2007#ifdef FEAT_GUI
2008 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2009 case K_LEFTRELEASE_NM:
2010 goto cmdline_not_changed;
2011
2012 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002013 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 gui_do_scroll();
2016 redrawcmd();
2017 }
2018 goto cmdline_not_changed;
2019
2020 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002021 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002023 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 redrawcmd();
2025 }
2026 goto cmdline_not_changed;
2027#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002028#ifdef FEAT_GUI_TABLINE
2029 case K_TABLINE:
2030 case K_TABMENU:
2031 /* Don't want to change any tabs here. Make sure the same tab
2032 * is still selected. */
2033 if (gui_use_tabline())
2034 gui_mch_set_curtab(tabpage_index(curtab));
2035 goto cmdline_not_changed;
2036#endif
2037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 case K_SELECT: /* end of Select mode mapping - ignore */
2039 goto cmdline_not_changed;
2040
2041 case Ctrl_B: /* begin of command line */
2042 case K_HOME:
2043 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 case K_S_HOME:
2045 case K_C_HOME:
2046 ccline.cmdpos = 0;
2047 set_cmdspos();
2048 goto cmdline_not_changed;
2049
2050 case Ctrl_E: /* end of command line */
2051 case K_END:
2052 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 case K_S_END:
2054 case K_C_END:
2055 ccline.cmdpos = ccline.cmdlen;
2056 set_cmdspos_cursor();
2057 goto cmdline_not_changed;
2058
2059 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002060 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 break;
2062 goto cmdline_changed;
2063
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002064 case Ctrl_L:
2065#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002066 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002067 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002068#endif
2069
2070 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002071 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 break;
2073 goto cmdline_changed;
2074
2075 case Ctrl_N: /* next match */
2076 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002077 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002079 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2080 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002082 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002085 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 case K_UP:
2087 case K_DOWN:
2088 case K_S_UP:
2089 case K_S_DOWN:
2090 case K_PAGEUP:
2091 case K_KPAGEUP:
2092 case K_PAGEDOWN:
2093 case K_KPAGEDOWN:
2094 if (hislen == 0 || firstc == NUL) /* no history */
2095 goto cmdline_not_changed;
2096
2097 i = hiscnt;
2098
2099 /* save current command string so it can be restored later */
2100 if (lookfor == NULL)
2101 {
2102 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2103 goto cmdline_not_changed;
2104 lookfor[ccline.cmdpos] = NUL;
2105 }
2106
2107 j = (int)STRLEN(lookfor);
2108 for (;;)
2109 {
2110 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002111 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002112 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
2114 if (hiscnt == hislen) /* first time */
2115 hiscnt = hisidx[histype];
2116 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2117 hiscnt = hislen - 1;
2118 else if (hiscnt != hisidx[histype] + 1)
2119 --hiscnt;
2120 else /* at top of list */
2121 {
2122 hiscnt = i;
2123 break;
2124 }
2125 }
2126 else /* one step forwards */
2127 {
2128 /* on last entry, clear the line */
2129 if (hiscnt == hisidx[histype])
2130 {
2131 hiscnt = hislen;
2132 break;
2133 }
2134
2135 /* not on a history line, nothing to do */
2136 if (hiscnt == hislen)
2137 break;
2138 if (hiscnt == hislen - 1) /* wrap around */
2139 hiscnt = 0;
2140 else
2141 ++hiscnt;
2142 }
2143 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2144 {
2145 hiscnt = i;
2146 break;
2147 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002148 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002149 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 || STRNCMP(history[histype][hiscnt].hisstr,
2151 lookfor, (size_t)j) == 0)
2152 break;
2153 }
2154
2155 if (hiscnt != i) /* jumped to other entry */
2156 {
2157 char_u *p;
2158 int len;
2159 int old_firstc;
2160
2161 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002162 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (hiscnt == hislen)
2164 p = lookfor; /* back to the old one */
2165 else
2166 p = history[histype][hiscnt].hisstr;
2167
2168 if (histype == HIST_SEARCH
2169 && p != lookfor
2170 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2171 {
2172 /* Correct for the separator character used when
2173 * adding the history entry vs the one used now.
2174 * First loop: count length.
2175 * Second loop: copy the characters. */
2176 for (i = 0; i <= 1; ++i)
2177 {
2178 len = 0;
2179 for (j = 0; p[j] != NUL; ++j)
2180 {
2181 /* Replace old sep with new sep, unless it is
2182 * escaped. */
2183 if (p[j] == old_firstc
2184 && (j == 0 || p[j - 1] != '\\'))
2185 {
2186 if (i > 0)
2187 ccline.cmdbuff[len] = firstc;
2188 }
2189 else
2190 {
2191 /* Escape new sep, unless it is already
2192 * escaped. */
2193 if (p[j] == firstc
2194 && (j == 0 || p[j - 1] != '\\'))
2195 {
2196 if (i > 0)
2197 ccline.cmdbuff[len] = '\\';
2198 ++len;
2199 }
2200 if (i > 0)
2201 ccline.cmdbuff[len] = p[j];
2202 }
2203 ++len;
2204 }
2205 if (i == 0)
2206 {
2207 alloc_cmdbuff(len);
2208 if (ccline.cmdbuff == NULL)
2209 goto returncmd;
2210 }
2211 }
2212 ccline.cmdbuff[len] = NUL;
2213 }
2214 else
2215 {
2216 alloc_cmdbuff((int)STRLEN(p));
2217 if (ccline.cmdbuff == NULL)
2218 goto returncmd;
2219 STRCPY(ccline.cmdbuff, p);
2220 }
2221
2222 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2223 redrawcmd();
2224 goto cmdline_changed;
2225 }
2226 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002227#endif
2228 goto cmdline_not_changed;
2229
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002230#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002231 case Ctrl_G: /* next match */
2232 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002233 if (may_adjust_incsearch_highlighting(
2234 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002235 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
2238
2239 case Ctrl_V:
2240 case Ctrl_Q:
2241#ifdef FEAT_MOUSE
2242 ignore_drag_release = TRUE;
2243#endif
2244 putcmdline('^', TRUE);
2245 c = get_literal(); /* get next (two) character(s) */
2246 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002247 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248#ifdef FEAT_MBYTE
2249 /* may need to remove ^ when composing char was typed */
2250 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2251 {
2252 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2253 msg_putchar(' ');
2254 cursorcmd();
2255 }
2256#endif
2257 break;
2258
2259#ifdef FEAT_DIGRAPHS
2260 case Ctrl_K:
2261#ifdef FEAT_MOUSE
2262 ignore_drag_release = TRUE;
2263#endif
2264 putcmdline('?', TRUE);
2265#ifdef USE_ON_FLY_SCROLL
2266 dont_scroll = TRUE; /* disallow scrolling here */
2267#endif
2268 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002269 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (c != NUL)
2271 break;
2272
2273 redrawcmd();
2274 goto cmdline_not_changed;
2275#endif /* FEAT_DIGRAPHS */
2276
2277#ifdef FEAT_RIGHTLEFT
2278 case Ctrl__: /* CTRL-_: switch language mode */
2279 if (!p_ari)
2280 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002281# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (p_altkeymap)
2283 {
2284 cmd_fkmap = !cmd_fkmap;
2285 if (cmd_fkmap) /* in Farsi always in Insert mode */
2286 ccline.overstrike = FALSE;
2287 }
2288 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 cmd_hkmap = !cmd_hkmap;
2291 goto cmdline_not_changed;
2292#endif
2293
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002294 case K_PS:
2295 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2296 goto cmdline_changed;
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 default:
2299#ifdef UNIX
2300 if (c == intr_char)
2301 {
2302 gotesc = TRUE; /* will free ccline.cmdbuff after
2303 putting it in history */
2304 goto returncmd; /* back to Normal mode */
2305 }
2306#endif
2307 /*
2308 * Normal character with no special meaning. Just set mod_mask
2309 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2310 * the string <S-Space>. This should only happen after ^V.
2311 */
2312 if (!IS_SPECIAL(c))
2313 mod_mask = 0x0;
2314 break;
2315 }
2316 /*
2317 * End of switch on command line character.
2318 * We come here if we have a normal character.
2319 */
2320
Bram Moolenaarede3e632013-06-23 16:16:19 +02002321 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#ifdef FEAT_MBYTE
2323 /* Add ABBR_OFF for characters above 0x100, this is
2324 * what check_abbr() expects. */
2325 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2326#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002327 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 goto cmdline_changed;
2329
2330 /*
2331 * put the character in the command line
2332 */
2333 if (IS_SPECIAL(c) || mod_mask != 0)
2334 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2335 else
2336 {
2337#ifdef FEAT_MBYTE
2338 if (has_mbyte)
2339 {
2340 j = (*mb_char2bytes)(c, IObuff);
2341 IObuff[j] = NUL; /* exclude composing chars */
2342 put_on_cmdline(IObuff, j, TRUE);
2343 }
2344 else
2345#endif
2346 {
2347 IObuff[0] = c;
2348 put_on_cmdline(IObuff, 1, TRUE);
2349 }
2350 }
2351 goto cmdline_changed;
2352
2353/*
2354 * This part implements incremental searches for "/" and "?"
2355 * Jump to cmdline_not_changed when a character has been read but the command
2356 * line did not change. Then we only search and redraw if something changed in
2357 * the past.
2358 * Jump to cmdline_changed when the command line did change.
2359 * (Sorry for the goto's, I know it is ugly).
2360 */
2361cmdline_not_changed:
2362#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002363 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 continue;
2365#endif
2366
2367cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002368 /* Trigger CmdlineChanged autocommands. */
2369 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002372 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#endif
2374
2375#ifdef FEAT_RIGHTLEFT
2376 if (cmdmsg_rl
2377# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002378 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379# endif
2380 )
2381 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002382 * right-left typing. Not efficient, but it works.
2383 * Do it only when there are no characters left to read
2384 * to avoid useless intermediate redraws. */
2385 if (vpeekc() == NUL)
2386 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#endif
2388 }
2389
2390returncmd:
2391
2392#ifdef FEAT_RIGHTLEFT
2393 cmdmsg_rl = FALSE;
2394#endif
2395
2396#ifdef FEAT_FKMAP
2397 cmd_fkmap = 0;
2398#endif
2399
2400 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002401 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002404 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405#endif
2406
2407 if (ccline.cmdbuff != NULL)
2408 {
2409 /*
2410 * Put line in history buffer (":" and "=" only when it was typed).
2411 */
2412#ifdef FEAT_CMDHIST
2413 if (ccline.cmdlen && firstc != NUL
2414 && (some_key_typed || histype == HIST_SEARCH))
2415 {
2416 add_to_history(histype, ccline.cmdbuff, TRUE,
2417 histype == HIST_SEARCH ? firstc : NUL);
2418 if (firstc == ':')
2419 {
2420 vim_free(new_last_cmdline);
2421 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2422 }
2423 }
2424#endif
2425
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002426 if (gotesc)
2427 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429
2430 /*
2431 * If the screen was shifted up, redraw the whole screen (later).
2432 * If the line is too long, clear it, so ruler and shown command do
2433 * not get printed in the middle of it.
2434 */
2435 msg_check();
2436 msg_scroll = save_msg_scroll;
2437 redir_off = FALSE;
2438
2439 /* When the command line was typed, no need for a wait-return prompt. */
2440 if (some_key_typed)
2441 need_wait_return = FALSE;
2442
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002443 /* Trigger CmdlineLeave autocommands. */
2444 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002445
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002447#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2449 im_save_status(b_im_ptr);
2450 im_set_active(FALSE);
2451#endif
2452#ifdef FEAT_MOUSE
2453 setmouse();
2454#endif
2455#ifdef CURSOR_SHAPE
2456 ui_cursor_shape(); /* may show different cursor shape */
2457#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002458 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002460 {
2461 char_u *p = ccline.cmdbuff;
2462
2463 /* Make ccline empty, getcmdline() may try to use it. */
2464 ccline.cmdbuff = NULL;
2465 return p;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467}
2468
2469#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2470/*
2471 * Get a command line with a prompt.
2472 * This is prepared to be called recursively from getcmdline() (e.g. by
2473 * f_input() when evaluating an expression from CTRL-R =).
2474 * Returns the command line in allocated memory, or NULL.
2475 */
2476 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002477getcmdline_prompt(
2478 int firstc,
2479 char_u *prompt, /* command line prompt */
2480 int attr, /* attributes for prompt */
2481 int xp_context, /* type of expansion */
2482 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 char_u *s;
2485 struct cmdline_info save_ccline;
2486 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002487 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002489 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 ccline.cmdprompt = prompt;
2491 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002492# ifdef FEAT_EVAL
2493 ccline.xp_context = xp_context;
2494 ccline.xp_arg = xp_arg;
2495 ccline.input_fn = (firstc == '@');
2496# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002497 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002499 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002500 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002501 /* Restore msg_col, the prompt from input() may have changed it.
2502 * But only if called recursively and the commandline is therefore being
2503 * restored to an old one; if not, the input() prompt stays on the screen,
2504 * so we need its modified msg_col left intact. */
2505 if (ccline.cmdbuff != NULL)
2506 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508 return s;
2509}
2510#endif
2511
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002512/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002513 * Return TRUE when the text must not be changed and we can't switch to
2514 * another window or buffer. Used when editing the command line, evaluating
2515 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002516 */
2517 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002518text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002519{
2520#ifdef FEAT_CMDWIN
2521 if (cmdwin_type != 0)
2522 return TRUE;
2523#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002524 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002525}
2526
2527/*
2528 * Give an error message for a command that isn't allowed while the cmdline
2529 * window is open or editing the cmdline in another way.
2530 */
2531 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002532text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002533{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002534 EMSG(_(get_text_locked_msg()));
2535}
2536
2537 char_u *
2538get_text_locked_msg(void)
2539{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002540#ifdef FEAT_CMDWIN
2541 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002542 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002543#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002544 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002545}
2546
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002547/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002548 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2549 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002550 */
2551 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002552curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002553{
2554 if (curbuf_lock > 0)
2555 {
2556 EMSG(_("E788: Not allowed to edit another buffer now"));
2557 return TRUE;
2558 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002559 return allbuf_locked();
2560}
2561
2562/*
2563 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2564 * message.
2565 */
2566 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002567allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002568{
2569 if (allbuf_lock > 0)
2570 {
2571 EMSG(_("E811: Not allowed to change buffer information now"));
2572 return TRUE;
2573 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002574 return FALSE;
2575}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002578cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2581 if (cmdline_star > 0) /* showing '*', always 1 position */
2582 return 1;
2583#endif
2584 return ptr2cells(ccline.cmdbuff + idx);
2585}
2586
2587/*
2588 * Compute the offset of the cursor on the command line for the prompt and
2589 * indent.
2590 */
2591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002592set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002594 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 ccline.cmdspos = 1 + ccline.cmdindent;
2596 else
2597 ccline.cmdspos = 0 + ccline.cmdindent;
2598}
2599
2600/*
2601 * Compute the screen position for the cursor on the command line.
2602 */
2603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002604set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 int i, m, c;
2607
2608 set_cmdspos();
2609 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002612 if (m < 0) /* overflow, Columns or Rows at weird value */
2613 m = MAXCOL;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 else
2616 m = MAXCOL;
2617 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2618 {
2619 c = cmdline_charsize(i);
2620#ifdef FEAT_MBYTE
2621 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2622 if (has_mbyte)
2623 correct_cmdspos(i, c);
2624#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002625 /* If the cmdline doesn't fit, show cursor on last visible char.
2626 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if ((ccline.cmdspos += c) >= m)
2628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 ccline.cmdspos -= c;
2630 break;
2631 }
2632#ifdef FEAT_MBYTE
2633 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002634 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635#endif
2636 }
2637}
2638
2639#ifdef FEAT_MBYTE
2640/*
2641 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2642 * character that doesn't fit, so that a ">" must be displayed.
2643 */
2644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002645correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002647 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2649 && ccline.cmdspos % Columns + cells > Columns)
2650 ccline.cmdspos++;
2651}
2652#endif
2653
2654/*
2655 * Get an Ex command line for the ":" command.
2656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002658getexline(
2659 int c, /* normally ':', NUL for ":append" */
2660 void *cookie UNUSED,
2661 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663 /* When executing a register, remove ':' that's in front of each line. */
2664 if (exec_from_reg && vpeekc() == ':')
2665 (void)vgetc();
2666 return getcmdline(c, 1L, indent);
2667}
2668
2669/*
2670 * Get an Ex command line for Ex mode.
2671 * In Ex mode we only use the OS supplied line editing features and no
2672 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002673 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002676getexmodeline(
2677 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002678 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002679 void *cookie UNUSED,
2680 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002682 garray_T line_ga;
2683 char_u *pend;
2684 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002685 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 int escaped = FALSE; /* CTRL-V typed */
2687 int vcol = 0;
2688 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002689 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002690 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 /* Switch cursor on now. This avoids that it happens after the "\n", which
2693 * confuses the system function that computes tabstops. */
2694 cursor_on();
2695
2696 /* always start in column 0; write a newline if necessary */
2697 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002702 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002703 if (p_prompt)
2704 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 while (indent-- > 0)
2706 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709
2710 ga_init2(&line_ga, 1, 30);
2711
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002712 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002714 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002715 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002716 while (indent >= 8)
2717 {
2718 ga_append(&line_ga, TAB);
2719 msg_puts((char_u *)" ");
2720 indent -= 8;
2721 }
2722 while (indent-- > 0)
2723 {
2724 ga_append(&line_ga, ' ');
2725 msg_putchar(' ');
2726 }
2727 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 ++no_mapping;
2729 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 /*
2732 * Get the line, one character at a time.
2733 */
2734 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002737 long sw;
2738 char_u *s;
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (ga_grow(&line_ga, 40) == FAIL)
2741 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
Bram Moolenaarba748c82017-02-26 14:00:07 +01002743 /*
2744 * Get one character at a time.
2745 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002746 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002747
2748 /* Check for a ":normal" command and no more characters left. */
2749 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2750 c1 = '\n';
2751 else
2752 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
2754 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002755 * Handle line editing.
2756 * Previously this was left to the system, putting the terminal in
2757 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002761 msg_putchar('\n');
2762 break;
2763 }
2764
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002765 if (c1 == K_PS)
2766 {
2767 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2768 goto redraw;
2769 }
2770
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002771 if (!escaped)
2772 {
2773 /* CR typed means "enter", which is NL */
2774 if (c1 == '\r')
2775 c1 = '\n';
2776
2777 if (c1 == BS || c1 == K_BS
2778 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 if (line_ga.ga_len > 0)
2781 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002782#ifdef FEAT_MBYTE
2783 if (has_mbyte)
2784 {
2785 p = (char_u *)line_ga.ga_data;
2786 p[line_ga.ga_len] = NUL;
2787 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2788 line_ga.ga_len -= len;
2789 }
2790 else
2791#endif
2792 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 goto redraw;
2794 }
2795 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002798 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 msg_col = startcol;
2801 msg_clr_eos();
2802 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002803 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 }
2805
2806 if (c1 == Ctrl_T)
2807 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002808 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 p = (char_u *)line_ga.ga_data;
2810 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002811 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002812 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002814 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002816 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002817 p = (char_u *)line_ga.ga_data;
2818 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2820 *s = ' ';
2821 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823redraw:
2824 /* redraw the line */
2825 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002827 p = (char_u *)line_ga.ga_data;
2828 p[line_ga.ga_len] = NUL;
2829 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 msg_putchar(' ');
2836 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002837 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002841 len = MB_PTR2LEN(p);
2842 msg_outtrans_len(p, len);
2843 vcol += ptr2cells(p);
2844 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002847 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002848 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 continue;
2850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 if (c1 == Ctrl_D)
2853 {
2854 /* Delete one shiftwidth. */
2855 p = (char_u *)line_ga.ga_data;
2856 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 if (prev_char == '^')
2859 ex_keep_indent = TRUE;
2860 indent = 0;
2861 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 else
2864 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002865 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002866 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002867 if (indent > 0)
2868 {
2869 --indent;
2870 indent -= indent % get_sw_value(curbuf);
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002873 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002875 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2877 --line_ga.ga_len;
2878 }
2879 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002881
2882 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2883 {
2884 escaped = TRUE;
2885 continue;
2886 }
2887
2888 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2889 if (IS_SPECIAL(c1))
2890 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002892
2893 if (IS_SPECIAL(c1))
2894 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002895#ifdef FEAT_MBYTE
2896 if (has_mbyte)
2897 len = (*mb_char2bytes)(c1,
2898 (char_u *)line_ga.ga_data + line_ga.ga_len);
2899 else
2900#endif
2901 {
2902 len = 1;
2903 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2904 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002905 if (c1 == '\n')
2906 msg_putchar('\n');
2907 else if (c1 == TAB)
2908 {
2909 /* Don't use chartabsize(), 'ts' can be different */
2910 do
2911 {
2912 msg_putchar(' ');
2913 } while (++vcol % 8);
2914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002917 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002918 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002919 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002921 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002922 escaped = FALSE;
2923
2924 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002925 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002926
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002927 /* We are done when a NL is entered, but not when it comes after an
2928 * odd number of backslashes, that results in a NUL. */
2929 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002931 int bcount = 0;
2932
2933 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2934 ++bcount;
2935
2936 if (bcount > 0)
2937 {
2938 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2939 * "\NL", etc. */
2940 line_ga.ga_len -= (bcount + 1) / 2;
2941 pend -= (bcount + 1) / 2;
2942 pend[-1] = '\n';
2943 }
2944
2945 if ((bcount & 1) == 0)
2946 {
2947 --line_ga.ga_len;
2948 --pend;
2949 *pend = NUL;
2950 break;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 }
2954
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002955 --no_mapping;
2956 --allow_keys;
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 /* make following messages go to the next line */
2959 msg_didout = FALSE;
2960 msg_col = 0;
2961 if (msg_row < Rows - 1)
2962 ++msg_row;
2963 emsg_on_display = FALSE; /* don't want ui_delay() */
2964
2965 if (got_int)
2966 ga_clear(&line_ga);
2967
2968 return (char_u *)line_ga.ga_data;
2969}
2970
Bram Moolenaare344bea2005-09-01 20:46:49 +00002971# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2972 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973/*
2974 * Return TRUE if ccline.overstrike is on.
2975 */
2976 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002977cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
2979 return ccline.overstrike;
2980}
2981
2982/*
2983 * Return TRUE if the cursor is at the end of the cmdline.
2984 */
2985 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002986cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 return (ccline.cmdpos >= ccline.cmdlen);
2989}
2990#endif
2991
Bram Moolenaar9372a112005-12-06 19:59:18 +00002992#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993/*
2994 * Return the virtual column number at the current cursor position.
2995 * This is used by the IM code to obtain the start of the preedit string.
2996 */
2997 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002998cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3001 return MAXCOL;
3002
3003# ifdef FEAT_MBYTE
3004 if (has_mbyte)
3005 {
3006 colnr_T col;
3007 int i = 0;
3008
3009 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003010 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 return col;
3013 }
3014 else
3015# endif
3016 return ccline.cmdpos;
3017}
3018#endif
3019
3020#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3021/*
3022 * If part of the command line is an IM preedit string, redraw it with
3023 * IM feedback attributes. The cursor position is restored after drawing.
3024 */
3025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003026redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 if ((State & CMDLINE)
3029 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003030 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 && !p_imdisable
3032 && im_is_preediting())
3033 {
3034 int cmdpos = 0;
3035 int cmdspos;
3036 int old_row;
3037 int old_col;
3038 colnr_T col;
3039
3040 old_row = msg_row;
3041 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003042 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044# ifdef FEAT_MBYTE
3045 if (has_mbyte)
3046 {
3047 for (col = 0; col < preedit_start_col
3048 && cmdpos < ccline.cmdlen; ++col)
3049 {
3050 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003051 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053 }
3054 else
3055# endif
3056 {
3057 cmdspos += preedit_start_col;
3058 cmdpos += preedit_start_col;
3059 }
3060
3061 msg_row = cmdline_row + (cmdspos / (int)Columns);
3062 msg_col = cmdspos % (int)Columns;
3063 if (msg_row >= Rows)
3064 msg_row = Rows - 1;
3065
3066 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3067 {
3068 int char_len;
3069 int char_attr;
3070
3071 char_attr = im_get_feedback_attr(col);
3072 if (char_attr < 0)
3073 break; /* end of preedit string */
3074
3075# ifdef FEAT_MBYTE
3076 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003077 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 else
3079# endif
3080 char_len = 1;
3081
3082 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3083 cmdpos += char_len;
3084 }
3085
3086 msg_row = old_row;
3087 msg_col = old_col;
3088 }
3089}
3090#endif /* FEAT_XIM && FEAT_GUI_GTK */
3091
3092/*
3093 * Allocate a new command line buffer.
3094 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3095 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3096 */
3097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003098alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 /*
3101 * give some extra space to avoid having to allocate all the time
3102 */
3103 if (len < 80)
3104 len = 100;
3105 else
3106 len += 20;
3107
3108 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3109 ccline.cmdbufflen = len;
3110}
3111
3112/*
3113 * Re-allocate the command line to length len + something extra.
3114 * return FAIL for failure, OK otherwise
3115 */
3116 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003117realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 char_u *p;
3120
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003121 if (len < ccline.cmdbufflen)
3122 return OK; /* no need to resize */
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 p = ccline.cmdbuff;
3125 alloc_cmdbuff(len); /* will get some more */
3126 if (ccline.cmdbuff == NULL) /* out of memory */
3127 {
3128 ccline.cmdbuff = p; /* keep the old one */
3129 return FAIL;
3130 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003131 /* There isn't always a NUL after the command, but it may need to be
3132 * there, thus copy up to the NUL and add a NUL. */
3133 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3134 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003136
3137 if (ccline.xpc != NULL
3138 && ccline.xpc->xp_pattern != NULL
3139 && ccline.xpc->xp_context != EXPAND_NOTHING
3140 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3141 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003142 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003143
3144 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3145 * to point into the newly allocated memory. */
3146 if (i >= 0 && i <= ccline.cmdlen)
3147 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3148 }
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return OK;
3151}
3152
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003153#if defined(FEAT_ARABIC) || defined(PROTO)
3154static char_u *arshape_buf = NULL;
3155
3156# if defined(EXITFREE) || defined(PROTO)
3157 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003158free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003159{
3160 vim_free(arshape_buf);
3161}
3162# endif
3163#endif
3164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165/*
3166 * Draw part of the cmdline at the current cursor position. But draw stars
3167 * when cmdline_star is TRUE.
3168 */
3169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003170draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3173 int i;
3174
3175 if (cmdline_star > 0)
3176 for (i = 0; i < len; ++i)
3177 {
3178 msg_putchar('*');
3179# ifdef FEAT_MBYTE
3180 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003181 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182# endif
3183 }
3184 else
3185#endif
3186#ifdef FEAT_ARABIC
3187 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 static int buflen = 0;
3190 char_u *p;
3191 int j;
3192 int newlen = 0;
3193 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003194 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 int prev_c = 0;
3196 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003197 int u8c;
3198 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 /*
3202 * Do arabic shaping into a temporary buffer. This is very
3203 * inefficient!
3204 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003205 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
3207 /* Re-allocate the buffer. We keep it around to avoid a lot of
3208 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003209 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003210 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003211 arshape_buf = alloc(buflen);
3212 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 return; /* out of memory */
3214 }
3215
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003216 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3217 {
3218 /* Prepend a space to draw the leading composing char on. */
3219 arshape_buf[0] = ' ';
3220 newlen = 1;
3221 }
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 for (j = start; j < start + len; j += mb_l)
3224 {
3225 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003226 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003227 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (ARABIC_CHAR(u8c))
3229 {
3230 /* Do Arabic shaping. */
3231 if (cmdmsg_rl)
3232 {
3233 /* displaying from right to left */
3234 pc = prev_c;
3235 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003236 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (j + mb_l >= start + len)
3238 nc = NUL;
3239 else
3240 nc = utf_ptr2char(p + mb_l);
3241 }
3242 else
3243 {
3244 /* displaying from left to right */
3245 if (j + mb_l >= start + len)
3246 pc = NUL;
3247 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003248 {
3249 int pcc[MAX_MCO];
3250
3251 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003253 pc1 = pcc[0];
3254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 nc = prev_c;
3256 }
3257 prev_c = u8c;
3258
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003259 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003262 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003264 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3265 if (u8cc[1] != 0)
3266 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003267 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269 }
3270 else
3271 {
3272 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003273 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 newlen += mb_l;
3275 }
3276 }
3277
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 else
3281#endif
3282 msg_outtrans_len(ccline.cmdbuff + start, len);
3283}
3284
3285/*
3286 * Put a character on the command line. Shifts the following text to the
3287 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3288 * "c" must be printable (fit in one display cell)!
3289 */
3290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003291putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 if (cmd_silent)
3294 return;
3295 msg_no_more = TRUE;
3296 msg_putchar(c);
3297 if (shift)
3298 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3299 msg_no_more = FALSE;
3300 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003301 extra_char = c;
3302 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303}
3304
3305/*
3306 * Undo a putcmdline(c, FALSE).
3307 */
3308 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003309unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 if (cmd_silent)
3312 return;
3313 msg_no_more = TRUE;
3314 if (ccline.cmdlen == ccline.cmdpos)
3315 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003316#ifdef FEAT_MBYTE
3317 else if (has_mbyte)
3318 draw_cmdline(ccline.cmdpos,
3319 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 else
3322 draw_cmdline(ccline.cmdpos, 1);
3323 msg_no_more = FALSE;
3324 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003325 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
3329 * Put the given string, of the given length, onto the command line.
3330 * If len is -1, then STRLEN() is used to calculate the length.
3331 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3332 * part will be redrawn, otherwise it will not. If this function is called
3333 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3334 * called afterwards.
3335 */
3336 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003337put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 int retval;
3340 int i;
3341 int m;
3342 int c;
3343
3344 if (len < 0)
3345 len = (int)STRLEN(str);
3346
3347 /* Check if ccline.cmdbuff needs to be longer */
3348 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003349 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 else
3351 retval = OK;
3352 if (retval == OK)
3353 {
3354 if (!ccline.overstrike)
3355 {
3356 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3357 ccline.cmdbuff + ccline.cmdpos,
3358 (size_t)(ccline.cmdlen - ccline.cmdpos));
3359 ccline.cmdlen += len;
3360 }
3361 else
3362 {
3363#ifdef FEAT_MBYTE
3364 if (has_mbyte)
3365 {
3366 /* Count nr of characters in the new string. */
3367 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003368 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 ++m;
3370 /* Count nr of bytes in cmdline that are overwritten by these
3371 * characters. */
3372 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003373 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 --m;
3375 if (i < ccline.cmdlen)
3376 {
3377 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3378 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3379 ccline.cmdlen += ccline.cmdpos + len - i;
3380 }
3381 else
3382 ccline.cmdlen = ccline.cmdpos + len;
3383 }
3384 else
3385#endif
3386 if (ccline.cmdpos + len > ccline.cmdlen)
3387 ccline.cmdlen = ccline.cmdpos + len;
3388 }
3389 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3390 ccline.cmdbuff[ccline.cmdlen] = NUL;
3391
3392#ifdef FEAT_MBYTE
3393 if (enc_utf8)
3394 {
3395 /* When the inserted text starts with a composing character,
3396 * backup to the character before it. There could be two of them.
3397 */
3398 i = 0;
3399 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3400 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3401 {
3402 i = (*mb_head_off)(ccline.cmdbuff,
3403 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3404 ccline.cmdpos -= i;
3405 len += i;
3406 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3407 }
3408# ifdef FEAT_ARABIC
3409 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3410 {
3411 /* Check the previous character for Arabic combining pair. */
3412 i = (*mb_head_off)(ccline.cmdbuff,
3413 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3414 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3415 + ccline.cmdpos - i), c))
3416 {
3417 ccline.cmdpos -= i;
3418 len += i;
3419 }
3420 else
3421 i = 0;
3422 }
3423# endif
3424 if (i != 0)
3425 {
3426 /* Also backup the cursor position. */
3427 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3428 ccline.cmdspos -= i;
3429 msg_col -= i;
3430 if (msg_col < 0)
3431 {
3432 msg_col += Columns;
3433 --msg_row;
3434 }
3435 }
3436 }
3437#endif
3438
3439 if (redraw && !cmd_silent)
3440 {
3441 msg_no_more = TRUE;
3442 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003443 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3445 /* Avoid clearing the rest of the line too often. */
3446 if (cmdline_row != i || ccline.overstrike)
3447 msg_clr_eos();
3448 msg_no_more = FALSE;
3449 }
3450#ifdef FEAT_FKMAP
3451 /*
3452 * If we are in Farsi command mode, the character input must be in
3453 * Insert mode. So do not advance the cmdpos.
3454 */
3455 if (!cmd_fkmap)
3456#endif
3457 {
3458 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003461 if (m < 0) /* overflow, Columns or Rows at weird value */
3462 m = MAXCOL;
3463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 else
3465 m = MAXCOL;
3466 for (i = 0; i < len; ++i)
3467 {
3468 c = cmdline_charsize(ccline.cmdpos);
3469#ifdef FEAT_MBYTE
3470 /* count ">" for a double-wide char that doesn't fit. */
3471 if (has_mbyte)
3472 correct_cmdspos(ccline.cmdpos, c);
3473#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003474 /* Stop cursor at the end of the screen, but do increment the
3475 * insert position, so that entering a very long command
3476 * works, even though you can't see it. */
3477 if (ccline.cmdspos + c < m)
3478 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003482 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (c > len - i - 1)
3484 c = len - i - 1;
3485 ccline.cmdpos += c;
3486 i += c;
3487 }
3488#endif
3489 ++ccline.cmdpos;
3490 }
3491 }
3492 }
3493 if (redraw)
3494 msg_check();
3495 return retval;
3496}
3497
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003498static struct cmdline_info prev_ccline;
3499static int prev_ccline_used = FALSE;
3500
3501/*
3502 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3503 * and overwrite it. But get_cmdline_str() may need it, thus make it
3504 * available globally in prev_ccline.
3505 */
3506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003508{
3509 if (!prev_ccline_used)
3510 {
3511 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3512 prev_ccline_used = TRUE;
3513 }
3514 *ccp = prev_ccline;
3515 prev_ccline = ccline;
3516 ccline.cmdbuff = NULL;
3517 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003518 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003519}
3520
3521/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003522 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003523 */
3524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003526{
3527 ccline = prev_ccline;
3528 prev_ccline = *ccp;
3529}
3530
Bram Moolenaar5a305422006-04-28 22:38:25 +00003531#if defined(FEAT_EVAL) || defined(PROTO)
3532/*
3533 * Save the command line into allocated memory. Returns a pointer to be
3534 * passed to restore_cmdline_alloc() later.
3535 * Returns NULL when failed.
3536 */
3537 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003538save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003539{
3540 struct cmdline_info *p;
3541
3542 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3543 if (p != NULL)
3544 save_cmdline(p);
3545 return (char_u *)p;
3546}
3547
3548/*
3549 * Restore the command line from the return value of save_cmdline_alloc().
3550 */
3551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003552restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003553{
3554 if (p != NULL)
3555 {
3556 restore_cmdline((struct cmdline_info *)p);
3557 vim_free(p);
3558 }
3559}
3560#endif
3561
Bram Moolenaar8299df92004-07-10 09:47:34 +00003562/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003563 * Paste a yank register into the command line.
3564 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003565 * insert_reg() can't be used here, because special characters from the
3566 * register contents will be interpreted as commands.
3567 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003568 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003569 */
3570 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003571cmdline_paste(
3572 int regname,
3573 int literally, /* Insert text literally instead of "as typed" */
3574 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003575{
3576 long i;
3577 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003578 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003579 int allocated;
3580 struct cmdline_info save_ccline;
3581
3582 /* check for valid regname; also accept special characters for CTRL-R in
3583 * the command line */
3584 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003585 && regname != Ctrl_A && regname != Ctrl_L
3586 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003587 return FAIL;
3588
3589 /* A register containing CTRL-R can cause an endless loop. Allow using
3590 * CTRL-C to break the loop. */
3591 line_breakcheck();
3592 if (got_int)
3593 return FAIL;
3594
3595#ifdef FEAT_CLIPBOARD
3596 regname = may_get_selection(regname);
3597#endif
3598
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003599 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003600 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003601 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003602 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003604 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003605 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003606
3607 if (i)
3608 {
3609 /* Got the value of a special register in "arg". */
3610 if (arg == NULL)
3611 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003612
3613 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3614 * part of the word. */
3615 p = arg;
3616 if (p_is && regname == Ctrl_W)
3617 {
3618 char_u *w;
3619 int len;
3620
3621 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003622 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003623 {
3624#ifdef FEAT_MBYTE
3625 if (has_mbyte)
3626 {
3627 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3628 if (!vim_iswordc(mb_ptr2char(w - len)))
3629 break;
3630 w -= len;
3631 }
3632 else
3633#endif
3634 {
3635 if (!vim_iswordc(w[-1]))
3636 break;
3637 --w;
3638 }
3639 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003640 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003641 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3642 p += len;
3643 }
3644
3645 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003646 if (allocated)
3647 vim_free(arg);
3648 return OK;
3649 }
3650
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003651 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003652}
3653
3654/*
3655 * Put a string on the command line.
3656 * When "literally" is TRUE, insert literally.
3657 * When "literally" is FALSE, insert as typed, but don't leave the command
3658 * line.
3659 */
3660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003661cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003662{
3663 int c, cv;
3664
3665 if (literally)
3666 put_on_cmdline(s, -1, TRUE);
3667 else
3668 while (*s != NUL)
3669 {
3670 cv = *s;
3671 if (cv == Ctrl_V && s[1])
3672 ++s;
3673#ifdef FEAT_MBYTE
3674 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003675 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003676 else
3677#endif
3678 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003679 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3680 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003681#ifdef UNIX
3682 || c == intr_char
3683#endif
3684 || (c == Ctrl_BSL && *s == Ctrl_N))
3685 stuffcharReadbuff(Ctrl_V);
3686 stuffcharReadbuff(c);
3687 }
3688}
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#ifdef FEAT_WILDMENU
3691/*
3692 * Delete characters on the command line, from "from" to the current
3693 * position.
3694 */
3695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003696cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3699 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3700 ccline.cmdlen -= ccline.cmdpos - from;
3701 ccline.cmdpos = from;
3702}
3703#endif
3704
3705/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003706 * This function is called when the screen size changes and with incremental
3707 * search and in other situations where the command line may have been
3708 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 */
3710 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003711redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003713 redrawcmdline_ex(TRUE);
3714}
3715
3716 void
3717redrawcmdline_ex(int do_compute_cmdrow)
3718{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (cmd_silent)
3720 return;
3721 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003722 if (do_compute_cmdrow)
3723 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 redrawcmd();
3725 cursorcmd();
3726}
3727
3728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003729redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
3731 int i;
3732
3733 if (cmd_silent)
3734 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003735 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 msg_putchar(ccline.cmdfirstc);
3737 if (ccline.cmdprompt != NULL)
3738 {
3739 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3740 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3741 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003742 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 --ccline.cmdindent;
3744 }
3745 else
3746 for (i = ccline.cmdindent; i > 0; --i)
3747 msg_putchar(' ');
3748}
3749
3750/*
3751 * Redraw what is currently on the command line.
3752 */
3753 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003754redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755{
3756 if (cmd_silent)
3757 return;
3758
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003759 /* when 'incsearch' is set there may be no command line while redrawing */
3760 if (ccline.cmdbuff == NULL)
3761 {
3762 windgoto(cmdline_row, 0);
3763 msg_clr_eos();
3764 return;
3765 }
3766
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 msg_start();
3768 redrawcmdprompt();
3769
3770 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3771 msg_no_more = TRUE;
3772 draw_cmdline(0, ccline.cmdlen);
3773 msg_clr_eos();
3774 msg_no_more = FALSE;
3775
3776 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003777 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003778 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 /*
3781 * An emsg() before may have set msg_scroll. This is used in normal mode,
3782 * in cmdline mode we can reset them now.
3783 */
3784 msg_scroll = FALSE; /* next message overwrites cmdline */
3785
3786 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3787 * in cmdline mode */
3788 skip_redraw = FALSE;
3789}
3790
3791 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003792compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003794 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 cmdline_row = Rows - 1;
3796 else
3797 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003798 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799}
3800
3801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003802cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
3804 if (cmd_silent)
3805 return;
3806
3807#ifdef FEAT_RIGHTLEFT
3808 if (cmdmsg_rl)
3809 {
3810 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3811 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3812 if (msg_row <= 0)
3813 msg_row = Rows - 1;
3814 }
3815 else
3816#endif
3817 {
3818 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3819 msg_col = ccline.cmdspos % (int)Columns;
3820 if (msg_row >= Rows)
3821 msg_row = Rows - 1;
3822 }
3823
3824 windgoto(msg_row, msg_col);
3825#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003826 if (p_imst == IM_ON_THE_SPOT)
3827 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#endif
3829#ifdef MCH_CURSOR_SHAPE
3830 mch_update_cursor();
3831#endif
3832}
3833
3834 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003835gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
3837 msg_start();
3838#ifdef FEAT_RIGHTLEFT
3839 if (cmdmsg_rl)
3840 msg_col = Columns - 1;
3841 else
3842#endif
3843 msg_col = 0; /* always start in column 0 */
3844 if (clr) /* clear the bottom line(s) */
3845 msg_clr_eos(); /* will reset clear_cmdline */
3846 windgoto(cmdline_row, 0);
3847}
3848
3849/*
3850 * Check the word in front of the cursor for an abbreviation.
3851 * Called when the non-id character "c" has been entered.
3852 * When an abbreviation is recognized it is removed from the text with
3853 * backspaces and the replacement string is inserted, followed by "c".
3854 */
3855 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003856ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003858 int spos = 0;
3859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3861 return FALSE;
3862
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003863 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3864 * Actually accepts any mark. */
3865 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3866 spos++;
3867 if (ccline.cmdlen - spos > 5
3868 && ccline.cmdbuff[spos] == '\''
3869 && ccline.cmdbuff[spos + 2] == ','
3870 && ccline.cmdbuff[spos + 3] == '\'')
3871 spos += 5;
3872 else
3873 /* check abbreviation from the beginning of the commandline */
3874 spos = 0;
3875
3876 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877}
3878
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003879#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3880 static int
3881#ifdef __BORLANDC__
3882_RTLENTRYF
3883#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003884sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003885{
3886 char_u *p1 = *(char_u **)s1;
3887 char_u *p2 = *(char_u **)s2;
3888
3889 if (*p1 != '<' && *p2 == '<') return -1;
3890 if (*p1 == '<' && *p2 != '<') return 1;
3891 return STRCMP(p1, p2);
3892}
3893#endif
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895/*
3896 * Return FAIL if this is not an appropriate context in which to do
3897 * completion of anything, return OK if it is (even if there are no matches).
3898 * For the caller, this means that the character is just passed through like a
3899 * normal character (instead of being expanded). This allows :s/^I^D etc.
3900 */
3901 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003902nextwild(
3903 expand_T *xp,
3904 int type,
3905 int options, /* extra options for ExpandOne() */
3906 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
3908 int i, j;
3909 char_u *p1;
3910 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 int difflen;
3912 int v;
3913
3914 if (xp->xp_numfiles == -1)
3915 {
3916 set_expand_context(xp);
3917 cmd_showtail = expand_showtail(xp);
3918 }
3919
3920 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3921 {
3922 beep_flush();
3923 return OK; /* Something illegal on command line */
3924 }
3925 if (xp->xp_context == EXPAND_NOTHING)
3926 {
3927 /* Caller can use the character as a normal char instead */
3928 return FAIL;
3929 }
3930
3931 MSG_PUTS("..."); /* show that we are busy */
3932 out_flush();
3933
3934 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003935 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937 if (type == WILD_NEXT || type == WILD_PREV)
3938 {
3939 /*
3940 * Get next/previous match for a previous expanded pattern.
3941 */
3942 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3943 }
3944 else
3945 {
3946 /*
3947 * Translate string into pattern and expand it.
3948 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003949 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3950 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 p2 = NULL;
3952 else
3953 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003954 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003955 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3956 if (escape)
3957 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003958
3959 if (p_wic)
3960 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003961 p2 = ExpandOne(xp, p1,
3962 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003963 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003965 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 if (p2 != NULL && type == WILD_LONGEST)
3967 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003968 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (ccline.cmdbuff[i + j] == '*'
3970 || ccline.cmdbuff[i + j] == '?')
3971 break;
3972 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003973 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976 }
3977
3978 if (p2 != NULL && !got_int)
3979 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003980 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003981 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003983 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 xp->xp_pattern = ccline.cmdbuff + i;
3985 }
3986 else
3987 v = OK;
3988 if (v == OK)
3989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3991 &ccline.cmdbuff[ccline.cmdpos],
3992 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3993 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 ccline.cmdlen += difflen;
3995 ccline.cmdpos += difflen;
3996 }
3997 }
3998 vim_free(p2);
3999
4000 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004001 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 /* When expanding a ":map" command and no matches are found, assume that
4004 * the key is supposed to be inserted literally */
4005 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4006 return FAIL;
4007
4008 if (xp->xp_numfiles <= 0 && p2 == NULL)
4009 beep_flush();
4010 else if (xp->xp_numfiles == 1)
4011 /* free expanded pattern */
4012 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4013
4014 return OK;
4015}
4016
4017/*
4018 * Do wildcard expansion on the string 'str'.
4019 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004020 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * Return NULL for failure.
4022 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004023 * "orig" is the originally expanded string, copied to allocated memory. It
4024 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4025 * WILD_PREV "orig" should be NULL.
4026 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004027 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4028 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 *
4030 * mode = WILD_FREE: just free previously expanded matches
4031 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4032 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4033 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4034 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4035 * mode = WILD_ALL: return all matches concatenated
4036 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004037 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 *
4039 * options = WILD_LIST_NOTFOUND: list entries without a match
4040 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4041 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4042 * options = WILD_NO_BEEP: Don't beep for multiple matches
4043 * options = WILD_ADD_SLASH: add a slash after directory names
4044 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4045 * options = WILD_SILENT: don't print warning messages
4046 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004047 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 *
4049 * The variables xp->xp_context and xp->xp_backslash must have been set!
4050 */
4051 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004052ExpandOne(
4053 expand_T *xp,
4054 char_u *str,
4055 char_u *orig, /* allocated copy of original of expanded string */
4056 int options,
4057 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
4059 char_u *ss = NULL;
4060 static int findex;
4061 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004062 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 int i;
4064 long_u len;
4065 int non_suf_match; /* number without matching suffix */
4066
4067 /*
4068 * first handle the case of using an old match
4069 */
4070 if (mode == WILD_NEXT || mode == WILD_PREV)
4071 {
4072 if (xp->xp_numfiles > 0)
4073 {
4074 if (mode == WILD_PREV)
4075 {
4076 if (findex == -1)
4077 findex = xp->xp_numfiles;
4078 --findex;
4079 }
4080 else /* mode == WILD_NEXT */
4081 ++findex;
4082
4083 /*
4084 * When wrapping around, return the original string, set findex to
4085 * -1.
4086 */
4087 if (findex < 0)
4088 {
4089 if (orig_save == NULL)
4090 findex = xp->xp_numfiles - 1;
4091 else
4092 findex = -1;
4093 }
4094 if (findex >= xp->xp_numfiles)
4095 {
4096 if (orig_save == NULL)
4097 findex = 0;
4098 else
4099 findex = -1;
4100 }
4101#ifdef FEAT_WILDMENU
4102 if (p_wmnu)
4103 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4104 findex, cmd_showtail);
4105#endif
4106 if (findex == -1)
4107 return vim_strsave(orig_save);
4108 return vim_strsave(xp->xp_files[findex]);
4109 }
4110 else
4111 return NULL;
4112 }
4113
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004114 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4116 {
4117 FreeWild(xp->xp_numfiles, xp->xp_files);
4118 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004119 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121 findex = 0;
4122
4123 if (mode == WILD_FREE) /* only release file name */
4124 return NULL;
4125
4126 if (xp->xp_numfiles == -1)
4127 {
4128 vim_free(orig_save);
4129 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004130 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 /*
4133 * Do the expansion.
4134 */
4135 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4136 options) == FAIL)
4137 {
4138#ifdef FNAME_ILLEGAL
4139 /* Illegal file name has been silently skipped. But when there
4140 * are wildcards, the real problem is that there was no match,
4141 * causing the pattern to be added, which has illegal characters.
4142 */
4143 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4144 EMSG2(_(e_nomatch2), str);
4145#endif
4146 }
4147 else if (xp->xp_numfiles == 0)
4148 {
4149 if (!(options & WILD_SILENT))
4150 EMSG2(_(e_nomatch2), str);
4151 }
4152 else
4153 {
4154 /* Escape the matches for use on the command line. */
4155 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4156
4157 /*
4158 * Check for matching suffixes in file names.
4159 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004160 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4161 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
4163 if (xp->xp_numfiles)
4164 non_suf_match = xp->xp_numfiles;
4165 else
4166 non_suf_match = 1;
4167 if ((xp->xp_context == EXPAND_FILES
4168 || xp->xp_context == EXPAND_DIRECTORIES)
4169 && xp->xp_numfiles > 1)
4170 {
4171 /*
4172 * More than one match; check suffix.
4173 * The files will have been sorted on matching suffix in
4174 * expand_wildcards, only need to check the first two.
4175 */
4176 non_suf_match = 0;
4177 for (i = 0; i < 2; ++i)
4178 if (match_suffix(xp->xp_files[i]))
4179 ++non_suf_match;
4180 }
4181 if (non_suf_match != 1)
4182 {
4183 /* Can we ever get here unless it's while expanding
4184 * interactively? If not, we can get rid of this all
4185 * together. Don't really want to wait for this message
4186 * (and possibly have to hit return to continue!).
4187 */
4188 if (!(options & WILD_SILENT))
4189 EMSG(_(e_toomany));
4190 else if (!(options & WILD_NO_BEEP))
4191 beep_flush();
4192 }
4193 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4194 ss = vim_strsave(xp->xp_files[0]);
4195 }
4196 }
4197 }
4198
4199 /* Find longest common part */
4200 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4201 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004202 int mb_len = 1;
4203 int c0, ci;
4204
4205 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004207#ifdef FEAT_MBYTE
4208 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004210 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4211 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4212 }
4213 else
4214#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004215 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004216 for (i = 1; i < xp->xp_numfiles; ++i)
4217 {
4218#ifdef FEAT_MBYTE
4219 if (has_mbyte)
4220 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4221 else
4222#endif
4223 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004224 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004226 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004227 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004229 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 break;
4231 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004232 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 break;
4234 }
4235 if (i < xp->xp_numfiles)
4236 {
4237 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004238 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 break;
4240 }
4241 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004242
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 ss = alloc((unsigned)len + 1);
4244 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004245 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 findex = -1; /* next p_wc gets first one */
4247 }
4248
4249 /* Concatenate all matching names */
4250 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4251 {
4252 len = 0;
4253 for (i = 0; i < xp->xp_numfiles; ++i)
4254 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4255 ss = lalloc(len, TRUE);
4256 if (ss != NULL)
4257 {
4258 *ss = NUL;
4259 for (i = 0; i < xp->xp_numfiles; ++i)
4260 {
4261 STRCAT(ss, xp->xp_files[i]);
4262 if (i != xp->xp_numfiles - 1)
4263 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4264 }
4265 }
4266 }
4267
4268 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4269 ExpandCleanup(xp);
4270
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004271 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004272 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004273 vim_free(orig);
4274
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 return ss;
4276}
4277
4278/*
4279 * Prepare an expand structure for use.
4280 */
4281 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004282ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004284 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004285 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004287#ifndef BACKSLASH_IN_FILENAME
4288 xp->xp_shell = FALSE;
4289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 xp->xp_numfiles = -1;
4291 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004292#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4293 xp->xp_arg = NULL;
4294#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004295 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296}
4297
4298/*
4299 * Cleanup an expand structure after use.
4300 */
4301 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004302ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303{
4304 if (xp->xp_numfiles >= 0)
4305 {
4306 FreeWild(xp->xp_numfiles, xp->xp_files);
4307 xp->xp_numfiles = -1;
4308 }
4309}
4310
4311 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004312ExpandEscape(
4313 expand_T *xp,
4314 char_u *str,
4315 int numfiles,
4316 char_u **files,
4317 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
4319 int i;
4320 char_u *p;
4321
4322 /*
4323 * May change home directory back to "~"
4324 */
4325 if (options & WILD_HOME_REPLACE)
4326 tilde_replace(str, numfiles, files);
4327
4328 if (options & WILD_ESCAPE)
4329 {
4330 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004331 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004332 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 || xp->xp_context == EXPAND_BUFFERS
4334 || xp->xp_context == EXPAND_DIRECTORIES)
4335 {
4336 /*
4337 * Insert a backslash into a file name before a space, \, %, #
4338 * and wildmatch characters, except '~'.
4339 */
4340 for (i = 0; i < numfiles; ++i)
4341 {
4342 /* for ":set path=" we need to escape spaces twice */
4343 if (xp->xp_backslash == XP_BS_THREE)
4344 {
4345 p = vim_strsave_escaped(files[i], (char_u *)" ");
4346 if (p != NULL)
4347 {
4348 vim_free(files[i]);
4349 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004350#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 p = vim_strsave_escaped(files[i], (char_u *)" ");
4352 if (p != NULL)
4353 {
4354 vim_free(files[i]);
4355 files[i] = p;
4356 }
4357#endif
4358 }
4359 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004360#ifdef BACKSLASH_IN_FILENAME
4361 p = vim_strsave_fnameescape(files[i], FALSE);
4362#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004363 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 if (p != NULL)
4366 {
4367 vim_free(files[i]);
4368 files[i] = p;
4369 }
4370
4371 /* If 'str' starts with "\~", replace "~" at start of
4372 * files[i] with "\~". */
4373 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004374 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004377
4378 /* If the first file starts with a '+' escape it. Otherwise it
4379 * could be seen as "+cmd". */
4380 if (*files[0] == '+')
4381 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 else if (xp->xp_context == EXPAND_TAGS)
4384 {
4385 /*
4386 * Insert a backslash before characters in a tag name that
4387 * would terminate the ":tag" command.
4388 */
4389 for (i = 0; i < numfiles; ++i)
4390 {
4391 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4392 if (p != NULL)
4393 {
4394 vim_free(files[i]);
4395 files[i] = p;
4396 }
4397 }
4398 }
4399 }
4400}
4401
4402/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004403 * Escape special characters in "fname" for when used as a file name argument
4404 * after a Vim command, or, when "shell" is non-zero, a shell command.
4405 * Returns the result in allocated memory.
4406 */
4407 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004409{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004410 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004411#ifdef BACKSLASH_IN_FILENAME
4412 char_u buf[20];
4413 int j = 0;
4414
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004415 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004416 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004417 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004418 buf[j++] = *p;
4419 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004420 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004421#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004422 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4423 if (shell && csh_like_shell() && p != NULL)
4424 {
4425 char_u *s;
4426
4427 /* For csh and similar shells need to put two backslashes before '!'.
4428 * One is taken by Vim, one by the shell. */
4429 s = vim_strsave_escaped(p, (char_u *)"!");
4430 vim_free(p);
4431 p = s;
4432 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004433#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004434
4435 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4436 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004437 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004438 escape_fname(&p);
4439
4440 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004441}
4442
4443/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004444 * Put a backslash before the file name in "pp", which is in allocated memory.
4445 */
4446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004447escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004448{
4449 char_u *p;
4450
4451 p = alloc((unsigned)(STRLEN(*pp) + 2));
4452 if (p != NULL)
4453 {
4454 p[0] = '\\';
4455 STRCPY(p + 1, *pp);
4456 vim_free(*pp);
4457 *pp = p;
4458 }
4459}
4460
4461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 * For each file name in files[num_files]:
4463 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4464 */
4465 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004466tilde_replace(
4467 char_u *orig_pat,
4468 int num_files,
4469 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
4471 int i;
4472 char_u *p;
4473
4474 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4475 {
4476 for (i = 0; i < num_files; ++i)
4477 {
4478 p = home_replace_save(NULL, files[i]);
4479 if (p != NULL)
4480 {
4481 vim_free(files[i]);
4482 files[i] = p;
4483 }
4484 }
4485 }
4486}
4487
4488/*
4489 * Show all matches for completion on the command line.
4490 * Returns EXPAND_NOTHING when the character that triggered expansion should
4491 * be inserted like a normal character.
4492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004494showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495{
4496#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4497 int num_files;
4498 char_u **files_found;
4499 int i, j, k;
4500 int maxlen;
4501 int lines;
4502 int columns;
4503 char_u *p;
4504 int lastlen;
4505 int attr;
4506 int showtail;
4507
4508 if (xp->xp_numfiles == -1)
4509 {
4510 set_expand_context(xp);
4511 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4512 &num_files, &files_found);
4513 showtail = expand_showtail(xp);
4514 if (i != EXPAND_OK)
4515 return i;
4516
4517 }
4518 else
4519 {
4520 num_files = xp->xp_numfiles;
4521 files_found = xp->xp_files;
4522 showtail = cmd_showtail;
4523 }
4524
4525#ifdef FEAT_WILDMENU
4526 if (!wildmenu)
4527 {
4528#endif
4529 msg_didany = FALSE; /* lines_left will be set */
4530 msg_start(); /* prepare for paging */
4531 msg_putchar('\n');
4532 out_flush();
4533 cmdline_row = msg_row;
4534 msg_didany = FALSE; /* lines_left will be set again */
4535 msg_start(); /* prepare for paging */
4536#ifdef FEAT_WILDMENU
4537 }
4538#endif
4539
4540 if (got_int)
4541 got_int = FALSE; /* only int. the completion, not the cmd line */
4542#ifdef FEAT_WILDMENU
4543 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004544 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#endif
4546 else
4547 {
4548 /* find the length of the longest file name */
4549 maxlen = 0;
4550 for (i = 0; i < num_files; ++i)
4551 {
4552 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004553 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 || xp->xp_context == EXPAND_BUFFERS))
4555 {
4556 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4557 j = vim_strsize(NameBuff);
4558 }
4559 else
4560 j = vim_strsize(L_SHOWFILE(i));
4561 if (j > maxlen)
4562 maxlen = j;
4563 }
4564
4565 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4566 lines = num_files;
4567 else
4568 {
4569 /* compute the number of columns and lines for the listing */
4570 maxlen += 2; /* two spaces between file names */
4571 columns = ((int)Columns + 2) / maxlen;
4572 if (columns < 1)
4573 columns = 1;
4574 lines = (num_files + columns - 1) / columns;
4575 }
4576
Bram Moolenaar8820b482017-03-16 17:23:31 +01004577 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4580 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004581 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 msg_clr_eos();
4583 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004584 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586
4587 /* list the files line by line */
4588 for (i = 0; i < lines; ++i)
4589 {
4590 lastlen = 999;
4591 for (k = i; k < num_files; k += lines)
4592 {
4593 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4594 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004595 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 p = files_found[k] + STRLEN(files_found[k]) + 1;
4597 msg_advance(maxlen + 1);
4598 msg_puts(p);
4599 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004600 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 break;
4602 }
4603 for (j = maxlen - lastlen; --j >= 0; )
4604 msg_putchar(' ');
4605 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004606 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 || xp->xp_context == EXPAND_BUFFERS)
4608 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004609 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004610 if (xp->xp_numfiles != -1)
4611 {
4612 char_u *halved_slash;
4613 char_u *exp_path;
4614
4615 /* Expansion was done before and special characters
4616 * were escaped, need to halve backslashes. Also
4617 * $HOME has been replaced with ~/. */
4618 exp_path = expand_env_save_opt(files_found[k], TRUE);
4619 halved_slash = backslash_halve_save(
4620 exp_path != NULL ? exp_path : files_found[k]);
4621 j = mch_isdir(halved_slash != NULL ? halved_slash
4622 : files_found[k]);
4623 vim_free(exp_path);
4624 vim_free(halved_slash);
4625 }
4626 else
4627 /* Expansion was done here, file names are literal. */
4628 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (showtail)
4630 p = L_SHOWFILE(k);
4631 else
4632 {
4633 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4634 TRUE);
4635 p = NameBuff;
4636 }
4637 }
4638 else
4639 {
4640 j = FALSE;
4641 p = L_SHOWFILE(k);
4642 }
4643 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4644 }
4645 if (msg_col > 0) /* when not wrapped around */
4646 {
4647 msg_clr_eos();
4648 msg_putchar('\n');
4649 }
4650 out_flush(); /* show one line at a time */
4651 if (got_int)
4652 {
4653 got_int = FALSE;
4654 break;
4655 }
4656 }
4657
4658 /*
4659 * we redraw the command below the lines that we have just listed
4660 * This is a bit tricky, but it saves a lot of screen updating.
4661 */
4662 cmdline_row = msg_row; /* will put it back later */
4663 }
4664
4665 if (xp->xp_numfiles == -1)
4666 FreeWild(num_files, files_found);
4667
4668 return EXPAND_OK;
4669}
4670
4671/*
4672 * Private gettail for showmatches() (and win_redr_status_matches()):
4673 * Find tail of file name path, but ignore trailing "/".
4674 */
4675 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004676sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677{
4678 char_u *p;
4679 char_u *t = s;
4680 int had_sep = FALSE;
4681
4682 for (p = s; *p != NUL; )
4683 {
4684 if (vim_ispathsep(*p)
4685#ifdef BACKSLASH_IN_FILENAME
4686 && !rem_backslash(p)
4687#endif
4688 )
4689 had_sep = TRUE;
4690 else if (had_sep)
4691 {
4692 t = p;
4693 had_sep = FALSE;
4694 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004695 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
4697 return t;
4698}
4699
4700/*
4701 * Return TRUE if we only need to show the tail of completion matches.
4702 * When not completing file names or there is a wildcard in the path FALSE is
4703 * returned.
4704 */
4705 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004706expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
4708 char_u *s;
4709 char_u *end;
4710
4711 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004712 if (xp->xp_context != EXPAND_FILES
4713 && xp->xp_context != EXPAND_SHELLCMD
4714 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 return FALSE;
4716
4717 end = gettail(xp->xp_pattern);
4718 if (end == xp->xp_pattern) /* there is no path separator */
4719 return FALSE;
4720
4721 for (s = xp->xp_pattern; s < end; s++)
4722 {
4723 /* Skip escaped wildcards. Only when the backslash is not a path
4724 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4725 if (rem_backslash(s))
4726 ++s;
4727 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4728 return FALSE;
4729 }
4730 return TRUE;
4731}
4732
4733/*
4734 * Prepare a string for expansion.
4735 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004736 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 * When expanding other names: The string will be used with regcomp(). Copy
4738 * the name into allocated memory and prepend "^".
4739 */
4740 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004741addstar(
4742 char_u *fname,
4743 int len,
4744 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 char_u *retval;
4747 int i, j;
4748 int new_len;
4749 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004750 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004752 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004753 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004754 && context != EXPAND_SHELLCMD
4755 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 /*
4758 * Matching will be done internally (on something other than files).
4759 * So we convert the file-matching-type wildcards into our kind for
4760 * use with vim_regcomp(). First work out how long it will be:
4761 */
4762
4763 /* For help tags the translation is done in find_help_tags().
4764 * For a tag pattern starting with "/" no translation is needed. */
4765 if (context == EXPAND_HELP
4766 || context == EXPAND_COLORS
4767 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004768 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004769 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004770 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004771 || ((context == EXPAND_TAGS_LISTFILES
4772 || context == EXPAND_TAGS)
4773 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 retval = vim_strnsave(fname, len);
4775 else
4776 {
4777 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4778 for (i = 0; i < len; i++)
4779 {
4780 if (fname[i] == '*' || fname[i] == '~')
4781 new_len++; /* '*' needs to be replaced by ".*"
4782 '~' needs to be replaced by "\~" */
4783
4784 /* Buffer names are like file names. "." should be literal */
4785 if (context == EXPAND_BUFFERS && fname[i] == '.')
4786 new_len++; /* "." becomes "\." */
4787
4788 /* Custom expansion takes care of special things, match
4789 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004790 if ((context == EXPAND_USER_DEFINED
4791 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 new_len++; /* '\' becomes "\\" */
4793 }
4794 retval = alloc(new_len);
4795 if (retval != NULL)
4796 {
4797 retval[0] = '^';
4798 j = 1;
4799 for (i = 0; i < len; i++, j++)
4800 {
4801 /* Skip backslash. But why? At least keep it for custom
4802 * expansion. */
4803 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004804 && context != EXPAND_USER_LIST
4805 && fname[i] == '\\'
4806 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 break;
4808
4809 switch (fname[i])
4810 {
4811 case '*': retval[j++] = '.';
4812 break;
4813 case '~': retval[j++] = '\\';
4814 break;
4815 case '?': retval[j] = '.';
4816 continue;
4817 case '.': if (context == EXPAND_BUFFERS)
4818 retval[j++] = '\\';
4819 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004820 case '\\': if (context == EXPAND_USER_DEFINED
4821 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 retval[j++] = '\\';
4823 break;
4824 }
4825 retval[j] = fname[i];
4826 }
4827 retval[j] = NUL;
4828 }
4829 }
4830 }
4831 else
4832 {
4833 retval = alloc(len + 4);
4834 if (retval != NULL)
4835 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004836 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
4838 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004839 * Don't add a star to *, ~, ~user, $var or `cmd`.
4840 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 * ~ would be at the start of the file name, but not the tail.
4842 * $ could be anywhere in the tail.
4843 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004844 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 */
4846 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004847 ends_in_star = (len > 0 && retval[len - 1] == '*');
4848#ifndef BACKSLASH_IN_FILENAME
4849 for (i = len - 2; i >= 0; --i)
4850 {
4851 if (retval[i] != '\\')
4852 break;
4853 ends_in_star = !ends_in_star;
4854 }
4855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004857 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 && vim_strchr(tail, '$') == NULL
4859 && vim_strchr(retval, '`') == NULL)
4860 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004861 else if (len > 0 && retval[len - 1] == '$')
4862 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 retval[len] = NUL;
4864 }
4865 }
4866 return retval;
4867}
4868
4869/*
4870 * Must parse the command line so far to work out what context we are in.
4871 * Completion can then be done based on that context.
4872 * This routine sets the variables:
4873 * xp->xp_pattern The start of the pattern to be expanded within
4874 * the command line (ends at the cursor).
4875 * xp->xp_context The type of thing to expand. Will be one of:
4876 *
4877 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4878 * the command line, like an unknown command. Caller
4879 * should beep.
4880 * EXPAND_NOTHING Unrecognised context for completion, use char like
4881 * a normal char, rather than for completion. eg
4882 * :s/^I/
4883 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4884 * it.
4885 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4886 * EXPAND_FILES After command with XFILE set, or after setting
4887 * with P_EXPAND set. eg :e ^I, :w>>^I
4888 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4889 * when we know only directories are of interest. eg
4890 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004891 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4893 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4894 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4895 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4896 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4897 * EXPAND_EVENTS Complete event names
4898 * EXPAND_SYNTAX Complete :syntax command arguments
4899 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4900 * EXPAND_AUGROUP Complete autocommand group names
4901 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4902 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4903 * eg :unmap a^I , :cunab x^I
4904 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4905 * eg :call sub^I
4906 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4907 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4908 * names in expressions, eg :while s^I
4909 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004910 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 */
4912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004913set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004915 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (ccline.cmdfirstc != ':'
4917#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004918 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004919 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#endif
4921 )
4922 {
4923 xp->xp_context = EXPAND_NOTHING;
4924 return;
4925 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004926 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927}
4928
4929 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930set_cmd_context(
4931 expand_T *xp,
4932 char_u *str, /* start of command line */
4933 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004934 int col, /* position of cursor */
4935 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 int old_char = NUL;
4938 char_u *nextcomm;
4939
4940 /*
4941 * Avoid a UMR warning from Purify, only save the character if it has been
4942 * written before.
4943 */
4944 if (col < len)
4945 old_char = str[col];
4946 str[col] = NUL;
4947 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004948
4949#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004950 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004951 {
4952# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004953 /* pass CMD_SIZE because there is no real command */
4954 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004955# endif
4956 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004957 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004958 {
4959 xp->xp_context = ccline.xp_context;
4960 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004961# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004962 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004963# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004964 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004965 else
4966#endif
4967 while (nextcomm != NULL)
4968 nextcomm = set_one_cmd_context(xp, nextcomm);
4969
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004970 /* Store the string here so that call_user_expand_func() can get to them
4971 * easily. */
4972 xp->xp_line = str;
4973 xp->xp_col = col;
4974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 str[col] = old_char;
4976}
4977
4978/*
4979 * Expand the command line "str" from context "xp".
4980 * "xp" must have been set by set_cmd_context().
4981 * xp->xp_pattern points into "str", to where the text that is to be expanded
4982 * starts.
4983 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4984 * cursor.
4985 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4986 * key that triggered expansion literally.
4987 * Returns EXPAND_OK otherwise.
4988 */
4989 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004990expand_cmdline(
4991 expand_T *xp,
4992 char_u *str, /* start of command line */
4993 int col, /* position of cursor */
4994 int *matchcount, /* return: nr of matches */
4995 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
4997 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004998 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5001 {
5002 beep_flush();
5003 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5004 }
5005 if (xp->xp_context == EXPAND_NOTHING)
5006 {
5007 /* Caller can use the character as a normal char instead */
5008 return EXPAND_NOTHING;
5009 }
5010
5011 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005012 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5013 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (file_str == NULL)
5015 return EXPAND_UNSUCCESSFUL;
5016
Bram Moolenaar94950a92010-12-02 16:01:29 +01005017 if (p_wic)
5018 options += WILD_ICASE;
5019
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005021 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
5023 *matchcount = 0;
5024 *matches = NULL;
5025 }
5026 vim_free(file_str);
5027
5028 return EXPAND_OK;
5029}
5030
5031#ifdef FEAT_MULTI_LANG
5032/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005033 * Cleanup matches for help tags:
5034 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5035 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005037static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005040cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 int i, j;
5043 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005044 char_u buf[4];
5045 char_u *p = buf;
5046
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005047 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005048 {
5049 *p++ = '@';
5050 *p++ = p_hlg[0];
5051 *p++ = p_hlg[1];
5052 }
5053 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 for (i = 0; i < num_file; ++i)
5056 {
5057 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005058 if (len <= 0)
5059 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005060 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 /* Sorting on priority means the same item in another language may
5063 * be anywhere. Search all items for a match up to the "@en". */
5064 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005065 if (j != i && (int)STRLEN(file[j]) == len + 3
5066 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005069 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 file[i][len] = NUL;
5071 }
5072 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005073
5074 if (*buf != NUL)
5075 for (i = 0; i < num_file; ++i)
5076 {
5077 len = (int)STRLEN(file[i]) - 3;
5078 if (len <= 0)
5079 continue;
5080 if (STRCMP(file[i] + len, buf) == 0)
5081 {
5082 /* remove the default language */
5083 file[i][len] = NUL;
5084 }
5085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086}
5087#endif
5088
5089/*
5090 * Do the expansion based on xp->xp_context and "pat".
5091 */
5092 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005093ExpandFromContext(
5094 expand_T *xp,
5095 char_u *pat,
5096 int *num_file,
5097 char_u ***file,
5098 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100#ifdef FEAT_CMDL_COMPL
5101 regmatch_T regmatch;
5102#endif
5103 int ret;
5104 int flags;
5105
5106 flags = EW_DIR; /* include directories */
5107 if (options & WILD_LIST_NOTFOUND)
5108 flags |= EW_NOTFOUND;
5109 if (options & WILD_ADD_SLASH)
5110 flags |= EW_ADDSLASH;
5111 if (options & WILD_KEEP_ALL)
5112 flags |= EW_KEEPALL;
5113 if (options & WILD_SILENT)
5114 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005115 if (options & WILD_ALLLINKS)
5116 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005118 if (xp->xp_context == EXPAND_FILES
5119 || xp->xp_context == EXPAND_DIRECTORIES
5120 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 /*
5123 * Expand file or directory names.
5124 */
5125 int free_pat = FALSE;
5126 int i;
5127
5128 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5129 * space */
5130 if (xp->xp_backslash != XP_BS_NONE)
5131 {
5132 free_pat = TRUE;
5133 pat = vim_strsave(pat);
5134 for (i = 0; pat[i]; ++i)
5135 if (pat[i] == '\\')
5136 {
5137 if (xp->xp_backslash == XP_BS_THREE
5138 && pat[i + 1] == '\\'
5139 && pat[i + 2] == '\\'
5140 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005141 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (xp->xp_backslash == XP_BS_ONE
5143 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005144 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 }
5147
5148 if (xp->xp_context == EXPAND_FILES)
5149 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005150 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5151 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 else
5153 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005154 if (options & WILD_ICASE)
5155 flags |= EW_ICASE;
5156
Bram Moolenaard7834d32009-12-02 16:14:36 +00005157 /* Expand wildcards, supporting %:h and the like. */
5158 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (free_pat)
5160 vim_free(pat);
5161 return ret;
5162 }
5163
5164 *file = (char_u **)"";
5165 *num_file = 0;
5166 if (xp->xp_context == EXPAND_HELP)
5167 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005168 /* With an empty argument we would get all the help tags, which is
5169 * very slow. Get matches for "help" instead. */
5170 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5171 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
5173#ifdef FEAT_MULTI_LANG
5174 cleanup_help_tags(*num_file, *file);
5175#endif
5176 return OK;
5177 }
5178 return FAIL;
5179 }
5180
5181#ifndef FEAT_CMDL_COMPL
5182 return FAIL;
5183#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005184 if (xp->xp_context == EXPAND_SHELLCMD)
5185 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (xp->xp_context == EXPAND_OLD_SETTING)
5187 return ExpandOldSetting(num_file, file);
5188 if (xp->xp_context == EXPAND_BUFFERS)
5189 return ExpandBufnames(pat, num_file, file, options);
5190 if (xp->xp_context == EXPAND_TAGS
5191 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5192 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5193 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005194 {
5195 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005196 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5197 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005200 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005201 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005202 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005203 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005204 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005205 {
5206 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005207 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005208 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005209 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005210 {
5211 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005212 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005213 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005214# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5215 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005216 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005217# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005218 if (xp->xp_context == EXPAND_PACKADD)
5219 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
5221 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5222 if (regmatch.regprog == NULL)
5223 return FAIL;
5224
5225 /* set ignore-case according to p_ic, p_scs and pat */
5226 regmatch.rm_ic = ignorecase(pat);
5227
5228 if (xp->xp_context == EXPAND_SETTINGS
5229 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5230 ret = ExpandSettings(xp, &regmatch, num_file, file);
5231 else if (xp->xp_context == EXPAND_MAPPINGS)
5232 ret = ExpandMappings(&regmatch, num_file, file);
5233# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5234 else if (xp->xp_context == EXPAND_USER_DEFINED)
5235 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5236# endif
5237 else
5238 {
5239 static struct expgen
5240 {
5241 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005242 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005244 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 } tab[] =
5246 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005247 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5248 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005249 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005250 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005251#ifdef FEAT_CMDHIST
5252 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005255 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005256 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005257 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5258 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5259 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#endif
5261#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005262 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5263 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5264 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5265 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#endif
5267#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005268 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5269 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#endif
5271#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005272 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005274#ifdef FEAT_PROFILE
5275 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5276#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005277 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005278 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5279 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005280#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005281 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005282#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005283#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005284 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005285#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005286#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005287 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5290 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005291 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5292 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005294 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005295 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005296 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 };
5298 int i;
5299
5300 /*
5301 * Find a context in the table and call the ExpandGeneric() with the
5302 * right function to do the expansion.
5303 */
5304 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005305 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (xp->xp_context == tab[i].context)
5307 {
5308 if (tab[i].ic)
5309 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005310 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005311 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 break;
5313 }
5314 }
5315
Bram Moolenaar473de612013-06-08 18:19:48 +02005316 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
5318 return ret;
5319#endif /* FEAT_CMDL_COMPL */
5320}
5321
5322#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5323/*
5324 * Expand a list of names.
5325 *
5326 * Generic function for command line completion. It calls a function to
5327 * obtain strings, one by one. The strings are matched against a regexp
5328 * program. Matching strings are copied into an array, which is returned.
5329 *
5330 * Returns OK when no problems encountered, FAIL for error (out of memory).
5331 */
5332 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005333ExpandGeneric(
5334 expand_T *xp,
5335 regmatch_T *regmatch,
5336 int *num_file,
5337 char_u ***file,
5338 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 int i;
5343 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005344 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 char_u *str;
5346
5347 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005348 * round == 0: count the number of matching names
5349 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005351 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
5353 for (i = 0; ; ++i)
5354 {
5355 str = (*func)(xp, i);
5356 if (str == NULL) /* end of list */
5357 break;
5358 if (*str == NUL) /* skip empty strings */
5359 continue;
5360
5361 if (vim_regexec(regmatch, str, (colnr_T)0))
5362 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005363 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005365 if (escaped)
5366 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5367 else
5368 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 (*file)[count] = str;
5370#ifdef FEAT_MENU
5371 if (func == get_menu_names && str != NULL)
5372 {
5373 /* test for separator added by get_menu_names() */
5374 str += STRLEN(str) - 1;
5375 if (*str == '\001')
5376 *str = '.';
5377 }
5378#endif
5379 }
5380 ++count;
5381 }
5382 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005383 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 if (count == 0)
5386 return OK;
5387 *num_file = count;
5388 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5389 if (*file == NULL)
5390 {
5391 *file = (char_u **)"";
5392 return FAIL;
5393 }
5394 count = 0;
5395 }
5396 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005397
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005398 /* Sort the results. Keep menu's in the specified order. */
5399 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005400 {
5401 if (xp->xp_context == EXPAND_EXPRESSION
5402 || xp->xp_context == EXPAND_FUNCTIONS
5403 || xp->xp_context == EXPAND_USER_FUNC)
5404 /* <SNR> functions should be sorted to the end. */
5405 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5406 sort_func_compare);
5407 else
5408 sort_strings(*file, *num_file);
5409 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005410
Bram Moolenaar4f688582007-07-24 12:34:30 +00005411#ifdef FEAT_CMDL_COMPL
5412 /* Reset the variables used for special highlight names expansion, so that
5413 * they don't show up when getting normal highlight names by ID. */
5414 reset_expand_highlight();
5415#endif
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 return OK;
5418}
5419
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005420/*
5421 * Complete a shell command.
5422 * Returns FAIL or OK;
5423 */
5424 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005425expand_shellcmd(
5426 char_u *filepat, /* pattern to match with command names */
5427 int *num_file, /* return: number of matches */
5428 char_u ***file, /* return: array with matches */
5429 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005430{
5431 char_u *pat;
5432 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005433 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005434 int mustfree = FALSE;
5435 garray_T ga;
5436 char_u *buf = alloc(MAXPATHL);
5437 size_t l;
5438 char_u *s, *e;
5439 int flags = flagsarg;
5440 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005441 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005442 hashtab_T found_ht;
5443 hashitem_T *hi;
5444 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005445
5446 if (buf == NULL)
5447 return FAIL;
5448
5449 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5450 * space */
5451 pat = vim_strsave(filepat);
5452 for (i = 0; pat[i]; ++i)
5453 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005454 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005455
Bram Moolenaarb5971142015-03-21 17:32:19 +01005456 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005457
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005458 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5459 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005460 path = (char_u *)".";
5461 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005462 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005463 /* For an absolute name we don't use $PATH. */
5464 if (!mch_isFullName(pat))
5465 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005466 if (path == NULL)
5467 path = (char_u *)"";
5468 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005469
5470 /*
5471 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005472 * collect them in "ga". When "." is not in $PATH also expand for the
5473 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005474 */
5475 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005476 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005477 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005478 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005479#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005480 e = vim_strchr(s, ';');
5481#else
5482 e = vim_strchr(s, ':');
5483#endif
5484 if (e == NULL)
5485 e = s + STRLEN(s);
5486
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005487 if (*s == NUL)
5488 {
5489 if (did_curdir)
5490 break;
5491 // Find directories in the current directory, path is empty.
5492 did_curdir = TRUE;
5493 flags |= EW_DIR;
5494 }
5495 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5496 {
5497 did_curdir = TRUE;
5498 flags |= EW_DIR;
5499 }
5500 else
5501 // Do not match directories inside a $PATH item.
5502 flags &= ~EW_DIR;
5503
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005504 l = e - s;
5505 if (l > MAXPATHL - 5)
5506 break;
5507 vim_strncpy(buf, s, l);
5508 add_pathsep(buf);
5509 l = STRLEN(buf);
5510 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5511
5512 /* Expand matches in one directory of $PATH. */
5513 ret = expand_wildcards(1, &buf, num_file, file, flags);
5514 if (ret == OK)
5515 {
5516 if (ga_grow(&ga, *num_file) == FAIL)
5517 FreeWild(*num_file, *file);
5518 else
5519 {
5520 for (i = 0; i < *num_file; ++i)
5521 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005522 char_u *name = (*file)[i];
5523
5524 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005525 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005526 // Check if this name was already found.
5527 hash = hash_hash(name + l);
5528 hi = hash_lookup(&found_ht, name + l, hash);
5529 if (HASHITEM_EMPTY(hi))
5530 {
5531 // Remove the path that was prepended.
5532 STRMOVE(name, name + l);
5533 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5534 hash_add_item(&found_ht, hi, name, hash);
5535 name = NULL;
5536 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005537 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005538 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005539 }
5540 vim_free(*file);
5541 }
5542 }
5543 if (*e != NUL)
5544 ++e;
5545 }
5546 *file = ga.ga_data;
5547 *num_file = ga.ga_len;
5548
5549 vim_free(buf);
5550 vim_free(pat);
5551 if (mustfree)
5552 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005553 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005554 return OK;
5555}
5556
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5559/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005560 * Call "user_expand_func()" to invoke a user defined Vim script function and
5561 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005563 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005565 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005566 expand_T *xp,
5567 int *num_file,
5568 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005570 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005571 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005573 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005574 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005575 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
Bram Moolenaarb7515462013-06-29 12:58:33 +02005577 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005578 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 *num_file = 0;
5580 *file = NULL;
5581
Bram Moolenaarb7515462013-06-29 12:58:33 +02005582 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005583 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005584 keep = ccline.cmdbuff[ccline.cmdlen];
5585 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005586 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005587
Bram Moolenaarffa96842018-06-12 22:05:14 +02005588 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5589
5590 args[0].v_type = VAR_STRING;
5591 args[0].vval.v_string = pat;
5592 args[1].v_type = VAR_STRING;
5593 args[1].vval.v_string = xp->xp_line;
5594 args[2].v_type = VAR_NUMBER;
5595 args[2].vval.v_number = xp->xp_col;
5596 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005598 /* Save the cmdline, we don't know what the function may do. */
5599 save_ccline = ccline;
5600 ccline.cmdbuff = NULL;
5601 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005603
Bram Moolenaarded27a12018-08-01 19:06:03 +02005604 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005605
5606 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005608 if (ccline.cmdbuff != NULL)
5609 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005610
Bram Moolenaarffa96842018-06-12 22:05:14 +02005611 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005612 return ret;
5613}
5614
5615/*
5616 * Expand names with a function defined by the user.
5617 */
5618 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005619ExpandUserDefined(
5620 expand_T *xp,
5621 regmatch_T *regmatch,
5622 int *num_file,
5623 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005624{
5625 char_u *retstr;
5626 char_u *s;
5627 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005628 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005629 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005630 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005631
5632 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5633 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 return FAIL;
5635
5636 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005637 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 e = vim_strchr(s, '\n');
5640 if (e == NULL)
5641 e = s + STRLEN(s);
5642 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005643 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005645 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5646 *e = keep;
5647
5648 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005650 if (ga_grow(&ga, 1) == FAIL)
5651 break;
5652 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5653 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (*e != NUL)
5657 ++e;
5658 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005659 vim_free(retstr);
5660 *file = ga.ga_data;
5661 *num_file = ga.ga_len;
5662 return OK;
5663}
5664
5665/*
5666 * Expand names with a list returned by a function defined by the user.
5667 */
5668 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005669ExpandUserList(
5670 expand_T *xp,
5671 int *num_file,
5672 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005673{
5674 list_T *retlist;
5675 listitem_T *li;
5676 garray_T ga;
5677
5678 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5679 if (retlist == NULL)
5680 return FAIL;
5681
5682 ga_init2(&ga, (int)sizeof(char *), 3);
5683 /* Loop over the items in the list. */
5684 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5685 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005686 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5687 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005688
5689 if (ga_grow(&ga, 1) == FAIL)
5690 break;
5691
5692 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005693 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005694 ++ga.ga_len;
5695 }
5696 list_unref(retlist);
5697
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 *file = ga.ga_data;
5699 *num_file = ga.ga_len;
5700 return OK;
5701}
5702#endif
5703
5704/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005705 * Expand color scheme, compiler or filetype names.
5706 * Search from 'runtimepath':
5707 * 'runtimepath'/{dirnames}/{pat}.vim
5708 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5709 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5710 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5711 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005712 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 */
5714 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715ExpandRTDir(
5716 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005717 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718 int *num_file,
5719 char_u ***file,
5720 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 char_u *s;
5723 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005724 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005726 int i;
5727 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 *num_file = 0;
5730 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005731 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005732 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005734 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005736 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5737 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005739 ga_clear_strings(&ga);
5740 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005742 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005743 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005744 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005746
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005747 if (flags & DIP_START) {
5748 for (i = 0; dirnames[i] != NULL; ++i)
5749 {
5750 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5751 if (s == NULL)
5752 {
5753 ga_clear_strings(&ga);
5754 return FAIL;
5755 }
5756 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5757 globpath(p_pp, s, &ga, 0);
5758 vim_free(s);
5759 }
5760 }
5761
5762 if (flags & DIP_OPT) {
5763 for (i = 0; dirnames[i] != NULL; ++i)
5764 {
5765 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5766 if (s == NULL)
5767 {
5768 ga_clear_strings(&ga);
5769 return FAIL;
5770 }
5771 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5772 globpath(p_pp, s, &ga, 0);
5773 vim_free(s);
5774 }
5775 }
5776
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005777 for (i = 0; i < ga.ga_len; ++i)
5778 {
5779 match = ((char_u **)ga.ga_data)[i];
5780 s = match;
5781 e = s + STRLEN(s);
5782 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5783 {
5784 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005785 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005786 if (s < match || vim_ispathsep(*s))
5787 break;
5788 ++s;
5789 *e = NUL;
5790 mch_memmove(match, s, e - s + 1);
5791 }
5792 }
5793
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005794 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005795 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005796
5797 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005798 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005799 remove_duplicates(&ga);
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 *file = ga.ga_data;
5802 *num_file = ga.ga_len;
5803 return OK;
5804}
5805
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005806/*
5807 * Expand loadplugin names:
5808 * 'packpath'/pack/ * /opt/{pat}
5809 */
5810 static int
5811ExpandPackAddDir(
5812 char_u *pat,
5813 int *num_file,
5814 char_u ***file)
5815{
5816 char_u *s;
5817 char_u *e;
5818 char_u *match;
5819 garray_T ga;
5820 int i;
5821 int pat_len;
5822
5823 *num_file = 0;
5824 *file = NULL;
5825 pat_len = (int)STRLEN(pat);
5826 ga_init2(&ga, (int)sizeof(char *), 10);
5827
5828 s = alloc((unsigned)(pat_len + 26));
5829 if (s == NULL)
5830 {
5831 ga_clear_strings(&ga);
5832 return FAIL;
5833 }
5834 sprintf((char *)s, "pack/*/opt/%s*", pat);
5835 globpath(p_pp, s, &ga, 0);
5836 vim_free(s);
5837
5838 for (i = 0; i < ga.ga_len; ++i)
5839 {
5840 match = ((char_u **)ga.ga_data)[i];
5841 s = gettail(match);
5842 e = s + STRLEN(s);
5843 mch_memmove(match, s, e - s + 1);
5844 }
5845
5846 if (ga.ga_len == 0)
5847 return FAIL;
5848
5849 /* Sort and remove duplicates which can happen when specifying multiple
5850 * directories in dirnames. */
5851 remove_duplicates(&ga);
5852
5853 *file = ga.ga_data;
5854 *num_file = ga.ga_len;
5855 return OK;
5856}
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858#endif
5859
5860#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5861/*
5862 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005863 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005865 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005866globpath(
5867 char_u *path,
5868 char_u *file,
5869 garray_T *ga,
5870 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 expand_T xpc;
5873 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 int num_p;
5876 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878 buf = alloc(MAXPATHL);
5879 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005882 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 /* Loop over all entries in {path}. */
5886 while (*path != NUL)
5887 {
5888 /* Copy one item of the path to buf[] and concatenate the file name. */
5889 copy_option_part(&path, buf, MAXPATHL, ",");
5890 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5891 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005892# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005893 /* Using the platform's path separator (\) makes vim incorrectly
5894 * treat it as an escape character, use '/' instead. */
5895 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5896 STRCAT(buf, "/");
5897# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005901 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5902 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005904 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005906 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 for (i = 0; i < num_p; ++i)
5909 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005910 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005911 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005912 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005915
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 FreeWild(num_p, p);
5917 }
5918 }
5919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922}
5923
5924#endif
5925
5926#if defined(FEAT_CMDHIST) || defined(PROTO)
5927
5928/*********************************
5929 * Command line history stuff *
5930 *********************************/
5931
5932/*
5933 * Translate a history character to the associated type number.
5934 */
5935 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005936hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 if (c == ':')
5939 return HIST_CMD;
5940 if (c == '=')
5941 return HIST_EXPR;
5942 if (c == '@')
5943 return HIST_INPUT;
5944 if (c == '>')
5945 return HIST_DEBUG;
5946 return HIST_SEARCH; /* must be '?' or '/' */
5947}
5948
5949/*
5950 * Table of history names.
5951 * These names are used in :history and various hist...() functions.
5952 * It is sufficient to give the significant prefix of a history name.
5953 */
5954
5955static char *(history_names[]) =
5956{
5957 "cmd",
5958 "search",
5959 "expr",
5960 "input",
5961 "debug",
5962 NULL
5963};
5964
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005965#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5966/*
5967 * Function given to ExpandGeneric() to obtain the possible first
5968 * arguments of the ":history command.
5969 */
5970 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005971get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005972{
5973 static char_u compl[2] = { NUL, NUL };
5974 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005975 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005976 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5977
5978 if (idx < short_names_count)
5979 {
5980 compl[0] = (char_u)short_names[idx];
5981 return compl;
5982 }
5983 if (idx < short_names_count + history_name_count)
5984 return (char_u *)history_names[idx - short_names_count];
5985 if (idx == short_names_count + history_name_count)
5986 return (char_u *)"all";
5987 return NULL;
5988}
5989#endif
5990
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991/*
5992 * init_history() - Initialize the command line history.
5993 * Also used to re-allocate the history when the size changes.
5994 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005996init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 int newlen; /* new length of history table */
5999 histentry_T *temp;
6000 int i;
6001 int j;
6002 int type;
6003
6004 /*
6005 * If size of history table changed, reallocate it
6006 */
6007 newlen = (int)p_hi;
6008 if (newlen != hislen) /* history length changed */
6009 {
6010 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6011 {
6012 if (newlen)
6013 {
6014 temp = (histentry_T *)lalloc(
6015 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6016 if (temp == NULL) /* out of memory! */
6017 {
6018 if (type == 0) /* first one: just keep the old length */
6019 {
6020 newlen = hislen;
6021 break;
6022 }
6023 /* Already changed one table, now we can only have zero
6024 * length for all tables. */
6025 newlen = 0;
6026 type = -1;
6027 continue;
6028 }
6029 }
6030 else
6031 temp = NULL;
6032 if (newlen == 0 || temp != NULL)
6033 {
6034 if (hisidx[type] < 0) /* there are no entries yet */
6035 {
6036 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006037 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 }
6039 else if (newlen > hislen) /* array becomes bigger */
6040 {
6041 for (i = 0; i <= hisidx[type]; ++i)
6042 temp[i] = history[type][i];
6043 j = i;
6044 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006045 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 for ( ; j < hislen; ++i, ++j)
6047 temp[i] = history[type][j];
6048 }
6049 else /* array becomes smaller or 0 */
6050 {
6051 j = hisidx[type];
6052 for (i = newlen - 1; ; --i)
6053 {
6054 if (i >= 0) /* copy newest entries */
6055 temp[i] = history[type][j];
6056 else /* remove older entries */
6057 vim_free(history[type][j].hisstr);
6058 if (--j < 0)
6059 j = hislen - 1;
6060 if (j == hisidx[type])
6061 break;
6062 }
6063 hisidx[type] = newlen - 1;
6064 }
6065 vim_free(history[type]);
6066 history[type] = temp;
6067 }
6068 }
6069 hislen = newlen;
6070 }
6071}
6072
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006074clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006075{
6076 hisptr->hisnum = 0;
6077 hisptr->viminfo = FALSE;
6078 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006079 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006080}
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082/*
6083 * Check if command line 'str' is already in history.
6084 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6085 */
6086 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006087in_history(
6088 int type,
6089 char_u *str,
6090 int move_to_front, /* Move the entry to the front if it exists */
6091 int sep,
6092 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
6094 int i;
6095 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006096 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
6098 if (hisidx[type] < 0)
6099 return FALSE;
6100 i = hisidx[type];
6101 do
6102 {
6103 if (history[type][i].hisstr == NULL)
6104 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006105
6106 /* For search history, check that the separator character matches as
6107 * well. */
6108 p = history[type][i].hisstr;
6109 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006110 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006111 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
6113 if (!move_to_front)
6114 return TRUE;
6115 last_i = i;
6116 break;
6117 }
6118 if (--i < 0)
6119 i = hislen - 1;
6120 } while (i != hisidx[type]);
6121
6122 if (last_i >= 0)
6123 {
6124 str = history[type][i].hisstr;
6125 while (i != hisidx[type])
6126 {
6127 if (++i >= hislen)
6128 i = 0;
6129 history[type][last_i] = history[type][i];
6130 last_i = i;
6131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006133 history[type][i].viminfo = FALSE;
6134 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006135 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 return TRUE;
6137 }
6138 return FALSE;
6139}
6140
6141/*
6142 * Convert history name (from table above) to its HIST_ equivalent.
6143 * When "name" is empty, return "cmd" history.
6144 * Returns -1 for unknown history name.
6145 */
6146 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006147get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 int i;
6150 int len = (int)STRLEN(name);
6151
6152 /* No argument: use current history. */
6153 if (len == 0)
6154 return hist_char2type(ccline.cmdfirstc);
6155
6156 for (i = 0; history_names[i] != NULL; ++i)
6157 if (STRNICMP(name, history_names[i], len) == 0)
6158 return i;
6159
6160 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6161 return hist_char2type(name[0]);
6162
6163 return -1;
6164}
6165
6166static int last_maptick = -1; /* last seen maptick */
6167
6168/*
6169 * Add the given string to the given history. If the string is already in the
6170 * history then it is moved to the front. "histype" may be one of he HIST_
6171 * values.
6172 */
6173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006174add_to_history(
6175 int histype,
6176 char_u *new_entry,
6177 int in_map, /* consider maptick when inside a mapping */
6178 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 histentry_T *hisptr;
6181 int len;
6182
6183 if (hislen == 0) /* no history */
6184 return;
6185
Bram Moolenaara939e432013-11-09 05:30:26 +01006186 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6187 return;
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 /*
6190 * Searches inside the same mapping overwrite each other, so that only
6191 * the last line is kept. Be careful not to remove a line that was moved
6192 * down, only lines that were added.
6193 */
6194 if (histype == HIST_SEARCH && in_map)
6195 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006196 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
6198 /* Current line is from the same mapping, remove it */
6199 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6200 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006201 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 --hisnum[histype];
6203 if (--hisidx[HIST_SEARCH] < 0)
6204 hisidx[HIST_SEARCH] = hislen - 1;
6205 }
6206 last_maptick = -1;
6207 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006208 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
6210 if (++hisidx[histype] == hislen)
6211 hisidx[histype] = 0;
6212 hisptr = &history[histype][hisidx[histype]];
6213 vim_free(hisptr->hisstr);
6214
6215 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006216 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6218 if (hisptr->hisstr != NULL)
6219 hisptr->hisstr[len + 1] = sep;
6220
6221 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006222 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006223 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (histype == HIST_SEARCH && in_map)
6225 last_maptick = maptick;
6226 }
6227}
6228
6229#if defined(FEAT_EVAL) || defined(PROTO)
6230
6231/*
6232 * Get identifier of newest history entry.
6233 * "histype" may be one of the HIST_ values.
6234 */
6235 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006236get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6239 || hisidx[histype] < 0)
6240 return -1;
6241
6242 return history[histype][hisidx[histype]].hisnum;
6243}
6244
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 * Calculate history index from a number:
6247 * num > 0: seen as identifying number of a history entry
6248 * num < 0: relative position in history wrt newest entry
6249 * "histype" may be one of the HIST_ values.
6250 */
6251 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006252calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 int i;
6255 histentry_T *hist;
6256 int wrapped = FALSE;
6257
6258 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6259 || (i = hisidx[histype]) < 0 || num == 0)
6260 return -1;
6261
6262 hist = history[histype];
6263 if (num > 0)
6264 {
6265 while (hist[i].hisnum > num)
6266 if (--i < 0)
6267 {
6268 if (wrapped)
6269 break;
6270 i += hislen;
6271 wrapped = TRUE;
6272 }
6273 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6274 return i;
6275 }
6276 else if (-num <= hislen)
6277 {
6278 i += num + 1;
6279 if (i < 0)
6280 i += hislen;
6281 if (hist[i].hisstr != NULL)
6282 return i;
6283 }
6284 return -1;
6285}
6286
6287/*
6288 * Get a history entry by its index.
6289 * "histype" may be one of the HIST_ values.
6290 */
6291 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006292get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
6294 idx = calc_hist_idx(histype, idx);
6295 if (idx >= 0)
6296 return history[histype][idx].hisstr;
6297 else
6298 return (char_u *)"";
6299}
6300
6301/*
6302 * Clear all entries of a history.
6303 * "histype" may be one of the HIST_ values.
6304 */
6305 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006306clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 int i;
6309 histentry_T *hisptr;
6310
6311 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6312 {
6313 hisptr = history[histype];
6314 for (i = hislen; i--;)
6315 {
6316 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006317 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006318 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
6320 hisidx[histype] = -1; /* mark history as cleared */
6321 hisnum[histype] = 0; /* reset identifier counter */
6322 return OK;
6323 }
6324 return FAIL;
6325}
6326
6327/*
6328 * Remove all entries matching {str} from a history.
6329 * "histype" may be one of the HIST_ values.
6330 */
6331 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 regmatch_T regmatch;
6335 histentry_T *hisptr;
6336 int idx;
6337 int i;
6338 int last;
6339 int found = FALSE;
6340
6341 regmatch.regprog = NULL;
6342 regmatch.rm_ic = FALSE; /* always match case */
6343 if (hislen != 0
6344 && histype >= 0
6345 && histype < HIST_COUNT
6346 && *str != NUL
6347 && (idx = hisidx[histype]) >= 0
6348 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6349 != NULL)
6350 {
6351 i = last = idx;
6352 do
6353 {
6354 hisptr = &history[histype][i];
6355 if (hisptr->hisstr == NULL)
6356 break;
6357 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6358 {
6359 found = TRUE;
6360 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006361 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363 else
6364 {
6365 if (i != last)
6366 {
6367 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006368 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370 if (--last < 0)
6371 last += hislen;
6372 }
6373 if (--i < 0)
6374 i += hislen;
6375 } while (i != idx);
6376 if (history[histype][idx].hisstr == NULL)
6377 hisidx[histype] = -1;
6378 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006379 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 return found;
6381}
6382
6383/*
6384 * Remove an indexed entry from a history.
6385 * "histype" may be one of the HIST_ values.
6386 */
6387 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006388del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
6390 int i, j;
6391
6392 i = calc_hist_idx(histype, idx);
6393 if (i < 0)
6394 return FALSE;
6395 idx = hisidx[histype];
6396 vim_free(history[histype][i].hisstr);
6397
6398 /* When deleting the last added search string in a mapping, reset
6399 * last_maptick, so that the last added search string isn't deleted again.
6400 */
6401 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6402 last_maptick = -1;
6403
6404 while (i != idx)
6405 {
6406 j = (i + 1) % hislen;
6407 history[histype][i] = history[histype][j];
6408 i = j;
6409 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006410 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 if (--i < 0)
6412 i += hislen;
6413 hisidx[histype] = i;
6414 return TRUE;
6415}
6416
6417#endif /* FEAT_EVAL */
6418
6419#if defined(FEAT_CRYPT) || defined(PROTO)
6420/*
6421 * Very specific function to remove the value in ":set key=val" from the
6422 * history.
6423 */
6424 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006425remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 char_u *p;
6428 int i;
6429
6430 i = hisidx[HIST_CMD];
6431 if (i < 0)
6432 return;
6433 p = history[HIST_CMD][i].hisstr;
6434 if (p != NULL)
6435 for ( ; *p; ++p)
6436 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6437 {
6438 p = vim_strchr(p + 3, '=');
6439 if (p == NULL)
6440 break;
6441 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006442 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (p[i] == '\\' && p[i + 1])
6444 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006445 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 --p;
6447 }
6448}
6449#endif
6450
6451#endif /* FEAT_CMDHIST */
6452
Bram Moolenaar064154c2016-03-19 22:50:43 +01006453#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006454/*
6455 * Get pointer to the command line info to use. cmdline_paste() may clear
6456 * ccline and put the previous value in prev_ccline.
6457 */
6458 static struct cmdline_info *
6459get_ccline_ptr(void)
6460{
6461 if ((State & CMDLINE) == 0)
6462 return NULL;
6463 if (ccline.cmdbuff != NULL)
6464 return &ccline;
6465 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6466 return &prev_ccline;
6467 return NULL;
6468}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006469#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006470
Bram Moolenaar064154c2016-03-19 22:50:43 +01006471#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006472/*
6473 * Get the current command line in allocated memory.
6474 * Only works when the command line is being edited.
6475 * Returns NULL when something is wrong.
6476 */
6477 char_u *
6478get_cmdline_str(void)
6479{
6480 struct cmdline_info *p = get_ccline_ptr();
6481
6482 if (p == NULL)
6483 return NULL;
6484 return vim_strnsave(p->cmdbuff, p->cmdlen);
6485}
6486
6487/*
6488 * Get the current command line position, counted in bytes.
6489 * Zero is the first position.
6490 * Only works when the command line is being edited.
6491 * Returns -1 when something is wrong.
6492 */
6493 int
6494get_cmdline_pos(void)
6495{
6496 struct cmdline_info *p = get_ccline_ptr();
6497
6498 if (p == NULL)
6499 return -1;
6500 return p->cmdpos;
6501}
6502
6503/*
6504 * Set the command line byte position to "pos". Zero is the first position.
6505 * Only works when the command line is being edited.
6506 * Returns 1 when failed, 0 when OK.
6507 */
6508 int
6509set_cmdline_pos(
6510 int pos)
6511{
6512 struct cmdline_info *p = get_ccline_ptr();
6513
6514 if (p == NULL)
6515 return 1;
6516
6517 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6518 * changed the command line. */
6519 if (pos < 0)
6520 new_cmdpos = 0;
6521 else
6522 new_cmdpos = pos;
6523 return 0;
6524}
6525#endif
6526
6527#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6528/*
6529 * Get the current command-line type.
6530 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6531 * Only works when the command line is being edited.
6532 * Returns NUL when something is wrong.
6533 */
6534 int
6535get_cmdline_type(void)
6536{
6537 struct cmdline_info *p = get_ccline_ptr();
6538
6539 if (p == NULL)
6540 return NUL;
6541 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006542 return
6543# ifdef FEAT_EVAL
6544 (p->input_fn) ? '@' :
6545# endif
6546 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006547 return p->cmdfirstc;
6548}
6549#endif
6550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6552/*
6553 * Get indices "num1,num2" that specify a range within a list (not a range of
6554 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6555 * Returns OK if parsed successfully, otherwise FAIL.
6556 */
6557 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006558get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 int len;
6561 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006562 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564 *str = skipwhite(*str);
6565 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6566 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006567 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 *str += len;
6569 *num1 = (int)num;
6570 first = TRUE;
6571 }
6572 *str = skipwhite(*str);
6573 if (**str == ',') /* parse "to" part of range */
6574 {
6575 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006576 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 if (len > 0)
6578 {
6579 *num2 = (int)num;
6580 *str = skipwhite(*str + len);
6581 }
6582 else if (!first) /* no number given at all */
6583 return FAIL;
6584 }
6585 else if (first) /* only one number given */
6586 *num2 = *num1;
6587 return OK;
6588}
6589#endif
6590
6591#if defined(FEAT_CMDHIST) || defined(PROTO)
6592/*
6593 * :history command - print a history
6594 */
6595 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006596ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597{
6598 histentry_T *hist;
6599 int histype1 = HIST_CMD;
6600 int histype2 = HIST_CMD;
6601 int hisidx1 = 1;
6602 int hisidx2 = -1;
6603 int idx;
6604 int i, j, k;
6605 char_u *end;
6606 char_u *arg = eap->arg;
6607
6608 if (hislen == 0)
6609 {
6610 MSG(_("'history' option is zero"));
6611 return;
6612 }
6613
6614 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6615 {
6616 end = arg;
6617 while (ASCII_ISALPHA(*end)
6618 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6619 end++;
6620 i = *end;
6621 *end = NUL;
6622 histype1 = get_histtype(arg);
6623 if (histype1 == -1)
6624 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006625 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
6627 histype1 = 0;
6628 histype2 = HIST_COUNT-1;
6629 }
6630 else
6631 {
6632 *end = i;
6633 EMSG(_(e_trailing));
6634 return;
6635 }
6636 }
6637 else
6638 histype2 = histype1;
6639 *end = i;
6640 }
6641 else
6642 end = arg;
6643 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6644 {
6645 EMSG(_(e_trailing));
6646 return;
6647 }
6648
6649 for (; !got_int && histype1 <= histype2; ++histype1)
6650 {
6651 STRCPY(IObuff, "\n # ");
6652 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6653 MSG_PUTS_TITLE(IObuff);
6654 idx = hisidx[histype1];
6655 hist = history[histype1];
6656 j = hisidx1;
6657 k = hisidx2;
6658 if (j < 0)
6659 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6660 if (k < 0)
6661 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6662 if (idx >= 0 && j <= k)
6663 for (i = idx + 1; !got_int; ++i)
6664 {
6665 if (i == hislen)
6666 i = 0;
6667 if (hist[i].hisstr != NULL
6668 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6669 {
6670 msg_putchar('\n');
6671 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6672 hist[i].hisnum);
6673 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6674 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006675 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 else
6677 STRCAT(IObuff, hist[i].hisstr);
6678 msg_outtrans(IObuff);
6679 out_flush();
6680 }
6681 if (i == idx)
6682 break;
6683 }
6684 }
6685}
6686#endif
6687
6688#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006689/*
6690 * Buffers for history read from a viminfo file. Only valid while reading.
6691 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006692static histentry_T *viminfo_history[HIST_COUNT] =
6693 {NULL, NULL, NULL, NULL, NULL};
6694static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6695static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696static int viminfo_add_at_front = FALSE;
6697
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006698static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
6700/*
6701 * Translate a history type number to the associated character.
6702 */
6703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006704hist_type2char(
6705 int type,
6706 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 if (type == HIST_CMD)
6709 return ':';
6710 if (type == HIST_SEARCH)
6711 {
6712 if (use_question)
6713 return '?';
6714 else
6715 return '/';
6716 }
6717 if (type == HIST_EXPR)
6718 return '=';
6719 return '@';
6720}
6721
6722/*
6723 * Prepare for reading the history from the viminfo file.
6724 * This allocates history arrays to store the read history lines.
6725 */
6726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006727prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 int i;
6730 int num;
6731 int type;
6732 int len;
6733
6734 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006735 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (asklen > hislen)
6737 asklen = hislen;
6738
6739 for (type = 0; type < HIST_COUNT; ++type)
6740 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006741 /* Count the number of empty spaces in the history list. Entries read
6742 * from viminfo previously are also considered empty. If there are
6743 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006745 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 num++;
6747 len = asklen;
6748 if (num > len)
6749 len = num;
6750 if (len <= 0)
6751 viminfo_history[type] = NULL;
6752 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006753 viminfo_history[type] = (histentry_T *)lalloc(
6754 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (viminfo_history[type] == NULL)
6756 len = 0;
6757 viminfo_hislen[type] = len;
6758 viminfo_hisidx[type] = 0;
6759 }
6760}
6761
6762/*
6763 * Accept a line from the viminfo, store it in the history array when it's
6764 * new.
6765 */
6766 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 int type;
6770 long_u len;
6771 char_u *val;
6772 char_u *p;
6773
6774 type = hist_char2type(virp->vir_line[0]);
6775 if (viminfo_hisidx[type] < viminfo_hislen[type])
6776 {
6777 val = viminfo_readstring(virp, 1, TRUE);
6778 if (val != NULL && *val != NUL)
6779 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006780 int sep = (*val == ' ' ? NUL : *val);
6781
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006783 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
6785 /* Need to re-allocate to append the separator byte. */
6786 len = STRLEN(val);
6787 p = lalloc(len + 2, TRUE);
6788 if (p != NULL)
6789 {
6790 if (type == HIST_SEARCH)
6791 {
6792 /* Search entry: Move the separator from the first
6793 * column to after the NUL. */
6794 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006795 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
6797 else
6798 {
6799 /* Not a search entry: No separator in the viminfo
6800 * file, add a NUL separator. */
6801 mch_memmove(p, val, (size_t)len + 1);
6802 p[len + 1] = NUL;
6803 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006804 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6805 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006806 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6807 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 }
6811 }
6812 vim_free(val);
6813 }
6814 return viminfo_readline(virp);
6815}
6816
Bram Moolenaar07219f92013-04-14 23:19:36 +02006817/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006818 * Accept a new style history line from the viminfo, store it in the history
6819 * array when it's new.
6820 */
6821 void
6822handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006823 garray_T *values,
6824 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006825{
6826 int type;
6827 long_u len;
6828 char_u *val;
6829 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006830 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006831
6832 /* Check the format:
6833 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006834 if (values->ga_len < 4
6835 || vp[0].bv_type != BVAL_NR
6836 || vp[1].bv_type != BVAL_NR
6837 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6838 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006839 return;
6840
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006841 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006842 if (type >= HIST_COUNT)
6843 return;
6844 if (viminfo_hisidx[type] < viminfo_hislen[type])
6845 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006846 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006847 if (val != NULL && *val != NUL)
6848 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006849 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6850 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006851 int idx;
6852 int overwrite = FALSE;
6853
6854 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6855 {
6856 /* If lines were written by an older Vim we need to avoid
6857 * getting duplicates. See if the entry already exists. */
6858 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6859 {
6860 p = viminfo_history[type][idx].hisstr;
6861 if (STRCMP(val, p) == 0
6862 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6863 {
6864 overwrite = TRUE;
6865 break;
6866 }
6867 }
6868
6869 if (!overwrite)
6870 {
6871 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006872 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006873 p = lalloc(len + 2, TRUE);
6874 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006875 else
6876 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006877 if (p != NULL)
6878 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006879 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006880 if (!overwrite)
6881 {
6882 mch_memmove(p, val, (size_t)len + 1);
6883 /* Put the separator after the NUL. */
6884 p[len + 1] = sep;
6885 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006886 viminfo_history[type][idx].hisnum = 0;
6887 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006888 viminfo_hisidx[type]++;
6889 }
6890 }
6891 }
6892 }
6893 }
6894}
6895
6896/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006897 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006898 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006899 static void
6900concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
6902 int idx;
6903 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006904
6905 idx = hisidx[type] + viminfo_hisidx[type];
6906 if (idx >= hislen)
6907 idx -= hislen;
6908 else if (idx < 0)
6909 idx = hislen - 1;
6910 if (viminfo_add_at_front)
6911 hisidx[type] = idx;
6912 else
6913 {
6914 if (hisidx[type] == -1)
6915 hisidx[type] = hislen - 1;
6916 do
6917 {
6918 if (history[type][idx].hisstr != NULL
6919 || history[type][idx].viminfo)
6920 break;
6921 if (++idx == hislen)
6922 idx = 0;
6923 } while (idx != hisidx[type]);
6924 if (idx != hisidx[type] && --idx < 0)
6925 idx = hislen - 1;
6926 }
6927 for (i = 0; i < viminfo_hisidx[type]; i++)
6928 {
6929 vim_free(history[type][idx].hisstr);
6930 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6931 history[type][idx].viminfo = TRUE;
6932 history[type][idx].time_set = viminfo_history[type][i].time_set;
6933 if (--idx < 0)
6934 idx = hislen - 1;
6935 }
6936 idx += 1;
6937 idx %= hislen;
6938 for (i = 0; i < viminfo_hisidx[type]; i++)
6939 {
6940 history[type][idx++].hisnum = ++hisnum[type];
6941 idx %= hislen;
6942 }
6943}
6944
6945#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6946 static int
6947#ifdef __BORLANDC__
6948_RTLENTRYF
6949#endif
6950sort_hist(const void *s1, const void *s2)
6951{
6952 histentry_T *p1 = *(histentry_T **)s1;
6953 histentry_T *p2 = *(histentry_T **)s2;
6954
6955 if (p1->time_set < p2->time_set) return -1;
6956 if (p1->time_set > p2->time_set) return 1;
6957 return 0;
6958}
6959#endif
6960
6961/*
6962 * Merge history lines from viminfo and lines typed in this Vim based on the
6963 * timestamp;
6964 */
6965 static void
6966merge_history(int type)
6967{
6968 int max_len;
6969 histentry_T **tot_hist;
6970 histentry_T *new_hist;
6971 int i;
6972 int len;
6973
6974 /* Make one long list with all entries. */
6975 max_len = hislen + viminfo_hisidx[type];
6976 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6977 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6978 if (tot_hist == NULL || new_hist == NULL)
6979 {
6980 vim_free(tot_hist);
6981 vim_free(new_hist);
6982 return;
6983 }
6984 for (i = 0; i < viminfo_hisidx[type]; i++)
6985 tot_hist[i] = &viminfo_history[type][i];
6986 len = i;
6987 for (i = 0; i < hislen; i++)
6988 if (history[type][i].hisstr != NULL)
6989 tot_hist[len++] = &history[type][i];
6990
6991 /* Sort the list on timestamp. */
6992 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6993
6994 /* Keep the newest ones. */
6995 for (i = 0; i < hislen; i++)
6996 {
6997 if (i < len)
6998 {
6999 new_hist[i] = *tot_hist[i];
7000 tot_hist[i]->hisstr = NULL;
7001 if (new_hist[i].hisnum == 0)
7002 new_hist[i].hisnum = ++hisnum[type];
7003 }
7004 else
7005 clear_hist_entry(&new_hist[i]);
7006 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007007 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007008
7009 /* Free what is not kept. */
7010 for (i = 0; i < viminfo_hisidx[type]; i++)
7011 vim_free(viminfo_history[type][i].hisstr);
7012 for (i = 0; i < hislen; i++)
7013 vim_free(history[type][i].hisstr);
7014 vim_free(history[type]);
7015 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007016 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007017}
7018
7019/*
7020 * Finish reading history lines from viminfo. Not used when writing viminfo.
7021 */
7022 void
7023finish_viminfo_history(vir_T *virp)
7024{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007026 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
7028 for (type = 0; type < HIST_COUNT; ++type)
7029 {
7030 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007031 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007032
7033 if (merge)
7034 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007036 concat_history(type);
7037
Bram Moolenaard23a8232018-02-10 18:45:26 +01007038 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007039 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041}
7042
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007043/*
7044 * Write history to viminfo file in "fp".
7045 * When "merge" is TRUE merge history lines with a previously read viminfo
7046 * file, data is in viminfo_history[].
7047 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007050write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int i;
7053 int type;
7054 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007055 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057 init_history();
7058 if (hislen == 0)
7059 return;
7060 for (type = 0; type < HIST_COUNT; ++type)
7061 {
7062 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7063 if (num_saved == 0)
7064 continue;
7065 if (num_saved < 0) /* Use default */
7066 num_saved = hislen;
7067 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7068 type == HIST_CMD ? _("Command Line") :
7069 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007070 type == HIST_EXPR ? _("Expression") :
7071 type == HIST_INPUT ? _("Input Line") :
7072 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (num_saved > hislen)
7074 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007075
7076 /*
7077 * Merge typed and viminfo history:
7078 * round 1: history of typed commands.
7079 * round 2: history from recently read viminfo.
7080 */
7081 for (round = 1; round <= 2; ++round)
7082 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007083 if (round == 1)
7084 /* start at newest entry, somewhere in the list */
7085 i = hisidx[type];
7086 else if (viminfo_hisidx[type] > 0)
7087 /* start at newest entry, first in the list */
7088 i = 0;
7089 else
7090 /* empty list */
7091 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007092 if (i >= 0)
7093 while (num_saved > 0
7094 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007096 char_u *p;
7097 time_t timestamp;
7098 int c = NUL;
7099
7100 if (round == 1)
7101 {
7102 p = history[type][i].hisstr;
7103 timestamp = history[type][i].time_set;
7104 }
7105 else
7106 {
7107 p = viminfo_history[type] == NULL ? NULL
7108 : viminfo_history[type][i].hisstr;
7109 timestamp = viminfo_history[type] == NULL ? 0
7110 : viminfo_history[type][i].time_set;
7111 }
7112
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007113 if (p != NULL && (round == 2
7114 || !merge
7115 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007117 --num_saved;
7118 fputc(hist_type2char(type, TRUE), fp);
7119 /* For the search history: put the separator in the
7120 * second column; use a space if there isn't one. */
7121 if (type == HIST_SEARCH)
7122 {
7123 c = p[STRLEN(p) + 1];
7124 putc(c == NUL ? ' ' : c, fp);
7125 }
7126 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007127
7128 {
7129 char cbuf[NUMBUFLEN];
7130
7131 /* New style history with a bar line. Format:
7132 * |{bartype},{histtype},{timestamp},{separator},"text" */
7133 if (c == NUL)
7134 cbuf[0] = NUL;
7135 else
7136 sprintf(cbuf, "%d", c);
7137 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7138 type, (long)timestamp, cbuf);
7139 barline_writestring(fp, p, LSIZE - 20);
7140 putc('\n', fp);
7141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007143 if (round == 1)
7144 {
7145 /* Decrement index, loop around and stop when back at
7146 * the start. */
7147 if (--i < 0)
7148 i = hislen - 1;
7149 if (i == hisidx[type])
7150 break;
7151 }
7152 else
7153 {
7154 /* Increment index. Stop at the end in the while. */
7155 ++i;
7156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007158 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007159 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007160 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007161 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007162 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007163 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
7165}
7166#endif /* FEAT_VIMINFO */
7167
7168#if defined(FEAT_FKMAP) || defined(PROTO)
7169/*
7170 * Write a character at the current cursor+offset position.
7171 * It is directly written into the command buffer block.
7172 */
7173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7177 {
7178 EMSG(_("E198: cmd_pchar beyond the command length"));
7179 return;
7180 }
7181 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7182 ccline.cmdbuff[ccline.cmdlen] = NUL;
7183}
7184
7185 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7189 {
7190 /* EMSG(_("cmd_gchar beyond the command length")); */
7191 return NUL;
7192 }
7193 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7194}
7195#endif
7196
7197#if defined(FEAT_CMDWIN) || defined(PROTO)
7198/*
7199 * Open a window on the current command line and history. Allow editing in
7200 * the window. Returns when the window is closed.
7201 * Returns:
7202 * CR if the command is to be executed
7203 * Ctrl_C if it is to be abandoned
7204 * K_IGNORE if editing continues
7205 */
7206 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007207open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
7209 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007210 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007212 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 win_T *wp;
7214 int i;
7215 linenr_T lnum;
7216 int histtype;
7217 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 int save_restart_edit = restart_edit;
7219 int save_State = State;
7220 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007221#ifdef FEAT_RIGHTLEFT
7222 int save_cmdmsg_rl = cmdmsg_rl;
7223#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007224#ifdef FEAT_FOLDING
7225 int save_KeyTyped;
7226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
7228 /* Can't do this recursively. Can't do it when typing a password. */
7229 if (cmdwin_type != 0
7230# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7231 || cmdline_star > 0
7232# endif
7233 )
7234 {
7235 beep_flush();
7236 return K_IGNORE;
7237 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007238 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 /* Save current window sizes. */
7241 win_size_save(&winsizes);
7242
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007244 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007245
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007246 /* don't use a new tab page */
7247 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007248 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007249
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 /* Create a window for the command-line buffer. */
7251 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7252 {
7253 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007254 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 return K_IGNORE;
7256 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007257 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007260 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007261 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007264#ifdef FEAT_FOLDING
7265 curwin->w_p_fen = FALSE;
7266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007268 curwin->w_p_rl = cmdmsg_rl;
7269 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007271 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007274 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007275 /* But don't allow switching to another buffer. */
7276 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
Bram Moolenaar46152342005-09-07 21:18:43 +00007278 /* Showing the prompt may have set need_wait_return, reset it. */
7279 need_wait_return = FALSE;
7280
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007281 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7283 {
7284 if (p_wc == TAB)
7285 {
7286 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7287 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7288 }
7289 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7290 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007291 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007293 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7294 * sets 'textwidth' to 78). */
7295 curbuf->b_p_tw = 0;
7296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 /* Fill the buffer with the history. */
7298 init_history();
7299 if (hislen > 0)
7300 {
7301 i = hisidx[histtype];
7302 if (i >= 0)
7303 {
7304 lnum = 0;
7305 do
7306 {
7307 if (++i == hislen)
7308 i = 0;
7309 if (history[histtype][i].hisstr != NULL)
7310 ml_append(lnum++, history[histtype][i].hisstr,
7311 (colnr_T)0, FALSE);
7312 }
7313 while (i != hisidx[histtype]);
7314 }
7315 }
7316
7317 /* Replace the empty last line with the current command-line and put the
7318 * cursor there. */
7319 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7320 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7321 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007322 changed_line_abv_curs();
7323 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007324 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325
7326 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007327 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329 /* No Ex mode here! */
7330 exmode_active = 0;
7331
7332 State = NORMAL;
7333# ifdef FEAT_MOUSE
7334 setmouse();
7335# endif
7336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007338 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007339 if (restart_edit != 0) /* autocmd with ":startinsert" */
7340 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
7342 i = RedrawingDisabled;
7343 RedrawingDisabled = 0;
7344
7345 /*
7346 * Call the main loop until <CR> or CTRL-C is typed.
7347 */
7348 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007349 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
7351 RedrawingDisabled = i;
7352
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007353# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007354 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007355# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007356
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007358 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007359
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007360# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007361 /* Restore KeyTyped in case it is modified by autocommands */
7362 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363# endif
7364
Bram Moolenaarccc18222007-05-10 18:25:20 +00007365 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007366 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 cmdwin_type = 0;
7368
7369 exmode_active = save_exmode;
7370
Bram Moolenaarf9821062008-06-20 16:31:07 +00007371 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007373 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 {
7375 cmdwin_result = Ctrl_C;
7376 EMSG(_("E199: Active window or buffer deleted"));
7377 }
7378 else
7379 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007380# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 /* autocmds may abort script processing */
7382 if (aborting() && cmdwin_result != K_IGNORE)
7383 cmdwin_result = Ctrl_C;
7384# endif
7385 /* Set the new command line from the cmdline buffer. */
7386 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007387 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007389 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7390
7391 if (histtype == HIST_CMD)
7392 {
7393 /* Execute the command directly. */
7394 ccline.cmdbuff = vim_strsave((char_u *)p);
7395 cmdwin_result = CAR;
7396 }
7397 else
7398 {
7399 /* First need to cancel what we were doing. */
7400 ccline.cmdbuff = NULL;
7401 stuffcharReadbuff(':');
7402 stuffReadbuff((char_u *)p);
7403 stuffcharReadbuff(CAR);
7404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 else if (cmdwin_result == K_XF2) /* :qa typed */
7407 {
7408 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7409 cmdwin_result = CAR;
7410 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007411 else if (cmdwin_result == Ctrl_C)
7412 {
7413 /* :q or :close, don't execute any command
7414 * and don't modify the cmd window. */
7415 ccline.cmdbuff = NULL;
7416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 else
7418 ccline.cmdbuff = vim_strsave(ml_get_curline());
7419 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007420 {
7421 ccline.cmdbuff = vim_strsave((char_u *)"");
7422 ccline.cmdlen = 0;
7423 ccline.cmdbufflen = 1;
7424 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
7428 {
7429 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7430 ccline.cmdbufflen = ccline.cmdlen + 1;
7431 ccline.cmdpos = curwin->w_cursor.col;
7432 if (ccline.cmdpos > ccline.cmdlen)
7433 ccline.cmdpos = ccline.cmdlen;
7434 if (cmdwin_result == K_IGNORE)
7435 {
7436 set_cmdspos_cursor();
7437 redrawcmd();
7438 }
7439 }
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007442 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007443# ifdef FEAT_CONCEAL
7444 /* Avoid command-line window first character being concealed. */
7445 curwin->w_p_cole = 0;
7446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007448 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 win_goto(old_curwin);
7450 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007451
7452 /* win_close() may have already wiped the buffer when 'bh' is
7453 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007454 if (bufref_valid(&bufref))
7455 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457 /* Restore window sizes. */
7458 win_size_restore(&winsizes);
7459
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007460 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462
7463 ga_clear(&winsizes);
7464 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007465# ifdef FEAT_RIGHTLEFT
7466 cmdmsg_rl = save_cmdmsg_rl;
7467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
7469 State = save_State;
7470# ifdef FEAT_MOUSE
7471 setmouse();
7472# endif
7473
7474 return cmdwin_result;
7475}
7476#endif /* FEAT_CMDWIN */
7477
7478/*
7479 * Used for commands that either take a simple command string argument, or:
7480 * cmd << endmarker
7481 * {script}
7482 * endmarker
7483 * Returns a pointer to allocated memory with {script} or NULL.
7484 */
7485 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488 char_u *theline;
7489 char *end_pattern = NULL;
7490 char dot[] = ".";
7491 garray_T ga;
7492
7493 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7494 return NULL;
7495
7496 ga_init2(&ga, 1, 0x400);
7497
7498 if (cmd[2] != NUL)
7499 end_pattern = (char *)skipwhite(cmd + 2);
7500 else
7501 end_pattern = dot;
7502
7503 for (;;)
7504 {
7505 theline = eap->getline(
7506#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007507 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#endif
7509 NUL, eap->cookie, 0);
7510
7511 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007512 {
7513 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
7517 ga_concat(&ga, theline);
7518 ga_append(&ga, '\n');
7519 vim_free(theline);
7520 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007521 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
7523 return (char_u *)ga.ga_data;
7524}