blob: 80f210a5b080e5d82814e8031f18a9d7973ad949 [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 Moolenaar198cb662018-09-06 21:44:17 +0200274 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200275 */
276 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200277do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
278 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200279{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200280 char_u *cmd;
281 cmdmod_T save_cmdmod = cmdmod;
282 char_u *p;
283 int delim_optional = FALSE;
284 int delim;
285 char_u *end;
286 char_u *dummy;
287 exarg_T ea;
288 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200289 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200290
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200291 *skiplen = 0;
292 *patlen = ccline.cmdlen;
293
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200294 if (!p_is || cmd_silent)
295 return FALSE;
296
297 // by default search all lines
298 search_first_line = 0;
299 search_last_line = MAXLNUM;
300
301 if (firstc == '/' || firstc == '?')
302 return TRUE;
303 if (firstc != ':')
304 return FALSE;
305
306 vim_memset(&ea, 0, sizeof(ea));
307 ea.line1 = 1;
308 ea.line2 = 1;
309 ea.cmd = ccline.cmdbuff;
310 ea.addr_type = ADDR_LINES;
311
312 parse_command_modifiers(&ea, &dummy, TRUE);
313 cmdmod = save_cmdmod;
314
315 cmd = skip_range(ea.cmd, NULL);
316 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
317 return FALSE;
318
319 // Skip over "substitute" to find the pattern separator.
320 for (p = cmd; ASCII_ISALPHA(*p); ++p)
321 ;
322 if (*skipwhite(p) == NUL)
323 return FALSE;
324
325 if (STRNCMP(cmd, "substitute", p - cmd) == 0
326 || STRNCMP(cmd, "smagic", p - cmd) == 0
327 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
328 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200329 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200330 if (*cmd == 's' && cmd[1] == 'm')
331 p_magic = TRUE;
332 else if (*cmd == 's' && cmd[1] == 'n')
333 p_magic = FALSE;
334 }
335 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
336 {
337 // skip over flags
338 while (ASCII_ISALPHA(*(p = skipwhite(p))))
339 ++p;
340 if (*p == NUL)
341 return FALSE;
342 }
343 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
344 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
345 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
346 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
347 || STRNCMP(cmd, "global", p - cmd) == 0)
348 {
349 // skip over "!"
350 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200351 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200352 p++;
353 if (*skipwhite(p) == NUL)
354 return FALSE;
355 }
356 if (*cmd != 'g')
357 delim_optional = TRUE;
358 }
359 else
360 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200361
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200362 p = skipwhite(p);
363 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
364 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200365
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200366 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200367
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200368 if (end == p && !use_last_pat)
369 return FALSE;
370
371 // Don't do 'hlsearch' highlighting if the pattern matches everything.
372 if (!use_last_pat)
373 {
374 char c = *end;
375 int empty;
376
377 *end = NUL;
378 empty = empty_pattern(p);
379 *end = c;
380 if (empty)
381 return FALSE;
382 }
383
384 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200385 *skiplen = (int)(p - ccline.cmdbuff);
386 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200387
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200388 // parse the address range
389 save_cursor = curwin->w_cursor;
390 curwin->w_cursor = is_state->search_start;
391 parse_cmd_address(&ea, &dummy);
392 if (ea.addr_count > 0)
393 {
394 // Allow for reverse match.
395 if (ea.line2 < ea.line1)
396 {
397 search_first_line = ea.line2;
398 search_last_line = ea.line1;
399 }
400 else
401 {
402 search_first_line = ea.line1;
403 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404 }
405 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200406 else if (cmd[0] == 's' && cmd[1] != 'o')
407 {
408 // :s defaults to the current line
409 search_first_line = curwin->w_cursor.lnum;
410 search_last_line = curwin->w_cursor.lnum;
411 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200412
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200413 curwin->w_cursor = save_cursor;
414 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200415}
416
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200417 static void
418finish_incsearch_highlighting(
419 int gotesc,
420 incsearch_state_T *is_state,
421 int call_update_screen)
422{
423 if (is_state->did_incsearch)
424 {
425 is_state->did_incsearch = FALSE;
426 if (gotesc)
427 curwin->w_cursor = is_state->save_cursor;
428 else
429 {
430 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
431 {
432 // put the '" mark at the original position
433 curwin->w_cursor = is_state->save_cursor;
434 setpcmark();
435 }
436 curwin->w_cursor = is_state->search_start;
437 }
438 restore_viewstate(&is_state->old_viewstate);
439 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200440
441 // by default search all lines
442 search_first_line = 0;
443 search_last_line = MAXLNUM;
444
445 p_magic = is_state->magic_save;
446
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200447 validate_cursor(); /* needed for TAB */
448 if (call_update_screen)
449 update_screen(SOME_VALID);
450 else
451 redraw_all_later(SOME_VALID);
452 }
453}
454
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200455/*
456 * Do 'incsearch' highlighting if desired.
457 */
458 static void
459may_do_incsearch_highlighting(
460 int firstc,
461 long count,
462 incsearch_state_T *is_state)
463{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200464 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200465 int i;
466 pos_T end_pos;
467 struct cmdline_info save_ccline;
468#ifdef FEAT_RELTIME
469 proftime_T tm;
470#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200471 int next_char;
472 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473
Bram Moolenaar198cb662018-09-06 21:44:17 +0200474 // Parsing range may already set the last search pattern.
475 save_last_search_pattern();
476
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200477 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200478 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200479 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200480 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200481 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200482 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200483
484 // If there is a character waiting, search and redraw later.
485 if (char_avail())
486 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200487 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200488 is_state->incsearch_postponed = TRUE;
489 return;
490 }
491 is_state->incsearch_postponed = FALSE;
492
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200493 if (search_first_line == 0)
494 // start at the original cursor position
495 curwin->w_cursor = is_state->search_start;
496 else
497 {
498 // start at the first line in the range
499 curwin->w_cursor.lnum = search_first_line;
500 curwin->w_cursor.col = 0;
501 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200503 // Use the previous pattern for ":s//".
504 next_char = ccline.cmdbuff[skiplen + patlen];
505 use_last_pat = patlen == 0 && skiplen > 0
506 && ccline.cmdbuff[skiplen - 1] == next_char;
507
508 // If there is no pattern, don't do anything.
509 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200510 {
511 i = 0;
512 set_no_hlsearch(TRUE); // turn off previous highlight
513 redraw_all_later(SOME_VALID);
514 }
515 else
516 {
517 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
518
519 cursor_off(); // so the user knows we're busy
520 out_flush();
521 ++emsg_off; // so it doesn't beep if bad expr
522#ifdef FEAT_RELTIME
523 // Set the time limit to half a second.
524 profile_setlimit(500L, &tm);
525#endif
526 if (!p_hls)
527 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200528 if (search_first_line != 0)
529 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200530 ccline.cmdbuff[skiplen + patlen] = NUL;
531 i = do_search(NULL, firstc == ':' ? '/' : firstc,
532 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200533#ifdef FEAT_RELTIME
534 &tm, NULL
535#else
536 NULL, NULL
537#endif
538 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200539 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200540 --emsg_off;
541
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200542 if (curwin->w_cursor.lnum < search_first_line
543 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200544 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200545 // match outside of address range
546 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200547 curwin->w_cursor = is_state->search_start;
548 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200549
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200550 // if interrupted while searching, behave like it failed
551 if (got_int)
552 {
553 (void)vpeekc(); // remove <C-C> from input stream
554 got_int = FALSE; // don't abandon the command line
555 i = 0;
556 }
557 else if (char_avail())
558 // cancelled searching because a char was typed
559 is_state->incsearch_postponed = TRUE;
560 }
561 if (i != 0)
562 highlight_match = TRUE; // highlight position
563 else
564 highlight_match = FALSE; // remove highlight
565
566 // First restore the old curwin values, so the screen is positioned in the
567 // same way as the actual search command.
568 restore_viewstate(&is_state->old_viewstate);
569 changed_cline_bef_curs();
570 update_topline();
571
572 if (i != 0)
573 {
574 pos_T save_pos = curwin->w_cursor;
575
576 is_state->match_start = curwin->w_cursor;
577 set_search_match(&curwin->w_cursor);
578 validate_cursor();
579 end_pos = curwin->w_cursor;
580 is_state->match_end = end_pos;
581 curwin->w_cursor = save_pos;
582 }
583 else
584 end_pos = curwin->w_cursor; // shutup gcc 4
585
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200586 // Disable 'hlsearch' highlighting if the pattern matches everything.
587 // Avoids a flash when typing "foo\|".
588 if (!use_last_pat)
589 {
590 next_char = ccline.cmdbuff[skiplen + patlen];
591 ccline.cmdbuff[skiplen + patlen] = NUL;
592 if (empty_pattern(ccline.cmdbuff))
593 set_no_hlsearch(TRUE);
594 ccline.cmdbuff[skiplen + patlen] = next_char;
595 }
596
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 validate_cursor();
598 // May redraw the status line to show the cursor position.
599 if (p_ru && curwin->w_status_height > 0)
600 curwin->w_redr_status = TRUE;
601
602 save_cmdline(&save_ccline);
603 update_screen(SOME_VALID);
604 restore_cmdline(&save_ccline);
605 restore_last_search_pattern();
606
607 // Leave it at the end to make CTRL-R CTRL-W work.
608 if (i != 0)
609 curwin->w_cursor = end_pos;
610
611 msg_starthere();
612 redrawcmdline();
613 is_state->did_incsearch = TRUE;
614}
615
616/*
617 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
618 * or previous match.
619 * Returns FAIL when jumping to cmdline_not_changed;
620 */
621 static int
622may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200623 int firstc,
624 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200625 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200626 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200627{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629 pos_T t;
630 char_u *pat;
631 int search_flags = SEARCH_NOOF;
632 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200633 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200634
Bram Moolenaar198cb662018-09-06 21:44:17 +0200635 // Parsing range may already set the last search pattern.
636 save_last_search_pattern();
637
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200638 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200639 {
640 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200641 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200642 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200643 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200644 {
645 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200646 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200647 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200648
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200649 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200650 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200652 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200653 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200654 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200655 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200656 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200657
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200658 cursor_off();
659 out_flush();
660 if (c == Ctrl_G)
661 {
662 t = is_state->match_end;
663 if (LT_POS(is_state->match_start, is_state->match_end))
664 // Start searching at the end of the match not at the beginning of
665 // the next column.
666 (void)decl(&t);
667 search_flags += SEARCH_COL;
668 }
669 else
670 t = is_state->match_start;
671 if (!p_hls)
672 search_flags += SEARCH_KEEP;
673 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200674 save = pat[patlen];
675 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200676 i = searchit(curwin, curbuf, &t,
677 c == Ctrl_G ? FORWARD : BACKWARD,
678 pat, count, search_flags,
679 RE_SEARCH, 0, NULL, NULL);
680 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200681 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200682 if (i)
683 {
684 is_state->search_start = is_state->match_start;
685 is_state->match_end = t;
686 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200687 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200688 {
689 // Move just before the current match, so that when nv_search
690 // finishes the cursor will be put back on the match.
691 is_state->search_start = t;
692 (void)decl(&is_state->search_start);
693 }
694 else if (c == Ctrl_G && firstc == '?')
695 {
696 // Move just after the current match, so that when nv_search
697 // finishes the cursor will be put back on the match.
698 is_state->search_start = t;
699 (void)incl(&is_state->search_start);
700 }
701 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
702 {
703 // wrap around
704 is_state->search_start = t;
705 if (firstc == '?')
706 (void)incl(&is_state->search_start);
707 else
708 (void)decl(&is_state->search_start);
709 }
710
711 set_search_match(&is_state->match_end);
712 curwin->w_cursor = is_state->match_start;
713 changed_cline_bef_curs();
714 update_topline();
715 validate_cursor();
716 highlight_match = TRUE;
717 save_viewstate(&is_state->old_viewstate);
718 update_screen(NOT_VALID);
719 redrawcmdline();
720 }
721 else
722 vim_beep(BO_ERROR);
723 restore_last_search_pattern();
724 return FAIL;
725}
726
727/*
728 * When CTRL-L typed: add character from the match to the pattern.
729 * May set "*c" to the added character.
730 * Return OK when jumping to cmdline_not_changed.
731 */
732 static int
733may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
734{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200735 int skiplen, patlen;
736
Bram Moolenaar198cb662018-09-06 21:44:17 +0200737 // Parsing range may already set the last search pattern.
738 save_last_search_pattern();
739
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200740 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200741 {
742 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200743 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200744 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200745
746 // Add a character from under the cursor for 'incsearch'.
747 if (is_state->did_incsearch)
748 {
749 curwin->w_cursor = is_state->match_end;
750 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
751 {
752 *c = gchar_cursor();
753
754 // If 'ignorecase' and 'smartcase' are set and the
755 // command line has no uppercase characters, convert
756 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200757 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200758 *c = MB_TOLOWER(*c);
759 if (*c != NUL)
760 {
761 if (*c == firstc || vim_strchr((char_u *)(
762 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
763 {
764 // put a backslash before special characters
765 stuffcharReadbuff(*c);
766 *c = '\\';
767 }
768 return FAIL;
769 }
770 }
771 }
772 return OK;
773}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200774#endif
775
Bram Moolenaar66216052017-12-16 16:33:44 +0100776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 * getcmdline() - accept a command line starting with firstc.
778 *
779 * firstc == ':' get ":" command line.
780 * firstc == '/' or '?' get search pattern
781 * firstc == '=' get expression
782 * firstc == '@' get text for input() function
783 * firstc == '>' get text for debug mode
784 * firstc == NUL get text for :insert command
785 * firstc == -1 like NUL, and break on CTRL-C
786 *
787 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
788 * command line.
789 *
790 * Careful: getcmdline() can be called recursively!
791 *
792 * Return pointer to allocated string if there is a commandline, NULL
793 * otherwise.
794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100796getcmdline(
797 int firstc,
798 long count UNUSED, /* only used for incremental search */
799 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 int c;
802 int i;
803 int j;
804 int gotesc = FALSE; /* TRUE when <ESC> just typed */
805 int do_abbr; /* when TRUE check for abbr. */
806#ifdef FEAT_CMDHIST
807 char_u *lookfor = NULL; /* string to match */
808 int hiscnt; /* current history line in use */
809 int histype; /* history type to be used */
810#endif
811#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200812 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#endif
814 int did_wild_list = FALSE; /* did wild_list() recently */
815 int wim_index = 0; /* index in wim_flags[] */
816 int res;
817 int save_msg_scroll = msg_scroll;
818 int save_State = State; /* remember State when called */
819 int some_key_typed = FALSE; /* one of the keys was typed */
820#ifdef FEAT_MOUSE
821 /* mouse drag and release events are ignored, unless they are
822 * preceded with a mouse down event */
823 int ignore_drag_release = TRUE;
824#endif
825#ifdef FEAT_EVAL
826 int break_ctrl_c = FALSE;
827#endif
828 expand_T xpc;
829 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200830#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000831 /* Everything that may work recursively should save and restore the
832 * current command line in save_ccline. That includes update_screen(), a
833 * custom status line may invoke ":normal". */
834 struct cmdline_info save_ccline;
835#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200836 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838#ifdef FEAT_EVAL
839 if (firstc == -1)
840 {
841 firstc = NUL;
842 break_ctrl_c = TRUE;
843 }
844#endif
845#ifdef FEAT_RIGHTLEFT
846 /* start without Hebrew mapping for a command line */
847 if (firstc == ':' || firstc == '=' || firstc == '>')
848 cmd_hkmap = 0;
849#endif
850
851 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200852
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200854 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
856
857 /*
858 * set some variables for redrawcmd()
859 */
860 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000861 ccline.cmdindent = (firstc > 0 ? indent : 0);
862
863 /* alloc initial ccline.cmdbuff */
864 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (ccline.cmdbuff == NULL)
866 return NULL; /* out of memory */
867 ccline.cmdlen = ccline.cmdpos = 0;
868 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100869 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000871 /* autoindent for :insert and :append */
872 if (firstc <= 0)
873 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200874 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000875 ccline.cmdbuff[indent] = NUL;
876 ccline.cmdpos = indent;
877 ccline.cmdspos = indent;
878 ccline.cmdlen = indent;
879 }
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000882 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884#ifdef FEAT_RIGHTLEFT
885 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
886 && (firstc == '/' || firstc == '?'))
887 cmdmsg_rl = TRUE;
888 else
889 cmdmsg_rl = FALSE;
890#endif
891
892 redir_off = TRUE; /* don't redirect the typed command */
893 if (!cmd_silent)
894 {
895 i = msg_scrolled;
896 msg_scrolled = 0; /* avoid wait_return message */
897 gotocmdline(TRUE);
898 msg_scrolled += i;
899 redrawcmdprompt(); /* draw prompt or indent */
900 set_cmdspos();
901 }
902 xpc.xp_context = EXPAND_NOTHING;
903 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000904#ifndef BACKSLASH_IN_FILENAME
905 xpc.xp_shell = FALSE;
906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000908#if defined(FEAT_EVAL)
909 if (ccline.input_fn)
910 {
911 xpc.xp_context = ccline.xp_context;
912 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000913# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000914 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000915# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000916 }
917#endif
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 /*
920 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
921 * doing ":@0" when register 0 doesn't contain a CR.
922 */
923 msg_scroll = FALSE;
924
925 State = CMDLINE;
926
927 if (firstc == '/' || firstc == '?' || firstc == '@')
928 {
929 /* Use ":lmap" mappings for search pattern and input(). */
930 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
931 b_im_ptr = &curbuf->b_p_iminsert;
932 else
933 b_im_ptr = &curbuf->b_p_imsearch;
934 if (*b_im_ptr == B_IMODE_LMAP)
935 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100936#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 im_set_active(*b_im_ptr == B_IMODE_IM);
938#endif
939 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100940#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 else if (p_imcmdline)
942 im_set_active(TRUE);
943#endif
944
945#ifdef FEAT_MOUSE
946 setmouse();
947#endif
948#ifdef CURSOR_SHAPE
949 ui_cursor_shape(); /* may show different cursor shape */
950#endif
951
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000952 /* When inside an autocommand for writing "exiting" may be set and
953 * terminal mode set to cooked. Need to set raw mode here then. */
954 settmode(TMODE_RAW);
955
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200956 /* Trigger CmdlineEnter autocommands. */
957 cmdline_type = firstc == NUL ? '-' : firstc;
958 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_CMDHIST
961 init_history();
962 hiscnt = hislen; /* set hiscnt to impossible history value */
963 histype = hist_char2type(firstc);
964#endif
965
966#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000967 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#endif
969
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200970 /* If something above caused an error, reset the flags, we do want to type
971 * and execute commands. Display may be messed up a bit. */
972 if (did_emsg)
973 redrawcmd();
974 did_emsg = FALSE;
975 got_int = FALSE;
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 /*
978 * Collect the command string, handling editing keys.
979 */
980 for (;;)
981 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000982 redir_off = TRUE; /* Don't redirect the typed command.
983 Repeated, because a ":redir" inside
984 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef USE_ON_FLY_SCROLL
986 dont_scroll = FALSE; /* allow scrolling here */
987#endif
988 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
989
Bram Moolenaar72532d32018-04-07 19:09:09 +0200990 did_emsg = FALSE; /* There can't really be a reason why an error
991 that occurs while typing a command should
992 cause the command not to be executed. */
993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000995
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100996 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
997 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000998 do
999 {
1000 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001001 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (KeyTyped)
1004 {
1005 some_key_typed = TRUE;
1006#ifdef FEAT_RIGHTLEFT
1007 if (cmd_hkmap)
1008 c = hkmap(c);
1009# ifdef FEAT_FKMAP
1010 if (cmd_fkmap)
1011 c = cmdl_fkmap(c);
1012# endif
1013 if (cmdmsg_rl && !KeyStuffed)
1014 {
1015 /* Invert horizontal movements and operations. Only when
1016 * typed by the user directly, not when the result of a
1017 * mapping. */
1018 switch (c)
1019 {
1020 case K_RIGHT: c = K_LEFT; break;
1021 case K_S_RIGHT: c = K_S_LEFT; break;
1022 case K_C_RIGHT: c = K_C_LEFT; break;
1023 case K_LEFT: c = K_RIGHT; break;
1024 case K_S_LEFT: c = K_S_RIGHT; break;
1025 case K_C_LEFT: c = K_C_RIGHT; break;
1026 }
1027 }
1028#endif
1029 }
1030
1031 /*
1032 * Ignore got_int when CTRL-C was typed here.
1033 * Don't ignore it in :global, we really need to break then, e.g., for
1034 * ":g/pat/normal /pat" (without the <CR>).
1035 * Don't ignore it for the input() function.
1036 */
1037 if ((c == Ctrl_C
1038#ifdef UNIX
1039 || c == intr_char
1040#endif
1041 )
1042#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1043 && firstc != '@'
1044#endif
1045#ifdef FEAT_EVAL
1046 && !break_ctrl_c
1047#endif
1048 && !global_busy)
1049 got_int = FALSE;
1050
1051#ifdef FEAT_CMDHIST
1052 /* free old command line when finished moving around in the history
1053 * list */
1054 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001055 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001056 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 && c != K_PAGEDOWN && c != K_PAGEUP
1058 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001059 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001061 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#endif
1063
1064 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001065 * When there are matching completions to select <S-Tab> works like
1066 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001068 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 c = Ctrl_P;
1070
1071#ifdef FEAT_WILDMENU
1072 /* Special translations for 'wildmenu' */
1073 if (did_wild_list && p_wmnu)
1074 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001075 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001077 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 c = Ctrl_N;
1079 }
1080 /* Hitting CR after "emenu Name.": complete submenu */
1081 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1082 && ccline.cmdpos > 1
1083 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1084 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1085 && (c == '\n' || c == '\r' || c == K_KENTER))
1086 c = K_DOWN;
1087#endif
1088
1089 /* free expanded names when finished walking through matches */
1090 if (xpc.xp_numfiles != -1
1091 && !(c == p_wc && KeyTyped) && c != p_wcm
1092 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1093 && c != Ctrl_L)
1094 {
1095 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1096 did_wild_list = FALSE;
1097#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001098 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#endif
1100 xpc.xp_context = EXPAND_NOTHING;
1101 wim_index = 0;
1102#ifdef FEAT_WILDMENU
1103 if (p_wmnu && wild_menu_showing != 0)
1104 {
1105 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001106 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001107
1108 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001109 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
1111 if (wild_menu_showing == WM_SCROLLED)
1112 {
1113 /* Entered command line, move it up */
1114 cmdline_row--;
1115 redrawcmd();
1116 }
1117 else if (save_p_ls != -1)
1118 {
1119 /* restore 'laststatus' and 'winminheight' */
1120 p_ls = save_p_ls;
1121 p_wmh = save_p_wmh;
1122 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001123 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001125 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 redrawcmd();
1127 save_p_ls = -1;
1128 }
1129 else
1130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 redraw_statuslines();
1133 }
1134 KeyTyped = skt;
1135 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001136 if (ccline.input_fn)
1137 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139#endif
1140 }
1141
1142#ifdef FEAT_WILDMENU
1143 /* Special translations for 'wildmenu' */
1144 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1145 {
1146 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001147 if (c == K_DOWN && ccline.cmdpos > 0
1148 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001150 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 /* Hitting <Up>: Remove one submenu name in front of the
1153 * cursor */
1154 int found = FALSE;
1155
1156 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1157 i = 0;
1158 while (--j > 0)
1159 {
1160 /* check for start of menu name */
1161 if (ccline.cmdbuff[j] == ' '
1162 && ccline.cmdbuff[j - 1] != '\\')
1163 {
1164 i = j + 1;
1165 break;
1166 }
1167 /* check for start of submenu name */
1168 if (ccline.cmdbuff[j] == '.'
1169 && ccline.cmdbuff[j - 1] != '\\')
1170 {
1171 if (found)
1172 {
1173 i = j + 1;
1174 break;
1175 }
1176 else
1177 found = TRUE;
1178 }
1179 }
1180 if (i > 0)
1181 cmdline_del(i);
1182 c = p_wc;
1183 xpc.xp_context = EXPAND_NOTHING;
1184 }
1185 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001186 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001187 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001188 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
1190 char_u upseg[5];
1191
1192 upseg[0] = PATHSEP;
1193 upseg[1] = '.';
1194 upseg[2] = '.';
1195 upseg[3] = PATHSEP;
1196 upseg[4] = NUL;
1197
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001198 if (c == K_DOWN
1199 && ccline.cmdpos > 0
1200 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1201 && (ccline.cmdpos < 3
1202 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1204 {
1205 /* go down a directory */
1206 c = p_wc;
1207 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001208 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
1210 /* If in a direct ancestor, strip off one ../ to go down */
1211 int found = FALSE;
1212
1213 j = ccline.cmdpos;
1214 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1215 while (--j > i)
1216 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001217#ifdef FEAT_MBYTE
1218 if (has_mbyte)
1219 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (vim_ispathsep(ccline.cmdbuff[j]))
1222 {
1223 found = TRUE;
1224 break;
1225 }
1226 }
1227 if (found
1228 && ccline.cmdbuff[j - 1] == '.'
1229 && ccline.cmdbuff[j - 2] == '.'
1230 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1231 {
1232 cmdline_del(j - 2);
1233 c = p_wc;
1234 }
1235 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001236 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
1238 /* go up a directory */
1239 int found = FALSE;
1240
1241 j = ccline.cmdpos - 1;
1242 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1243 while (--j > i)
1244 {
1245#ifdef FEAT_MBYTE
1246 if (has_mbyte)
1247 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1248#endif
1249 if (vim_ispathsep(ccline.cmdbuff[j])
1250#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001251 && vim_strchr((char_u *)" *?[{`$%#",
1252 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#endif
1254 )
1255 {
1256 if (found)
1257 {
1258 i = j + 1;
1259 break;
1260 }
1261 else
1262 found = TRUE;
1263 }
1264 }
1265
1266 if (!found)
1267 j = i;
1268 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1269 j += 4;
1270 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1271 && j == i)
1272 j += 3;
1273 else
1274 j = 0;
1275 if (j > 0)
1276 {
1277 /* TODO this is only for DOS/UNIX systems - need to put in
1278 * machine-specific stuff here and in upseg init */
1279 cmdline_del(j);
1280 put_on_cmdline(upseg + 1, 3, FALSE);
1281 }
1282 else if (ccline.cmdpos > i)
1283 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001284
1285 /* Now complete in the new directory. Set KeyTyped in case the
1286 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001288 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
1292#endif /* FEAT_WILDMENU */
1293
1294 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1295 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1296 if (c == Ctrl_BSL)
1297 {
1298 ++no_mapping;
1299 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001300 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 --no_mapping;
1302 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001303 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1304 * is in a mapping. */
1305 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1306 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
1308 vungetc(c);
1309 c = Ctrl_BSL;
1310 }
1311#ifdef FEAT_EVAL
1312 else if (c == 'e')
1313 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001314 char_u *p = NULL;
1315 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 /*
1318 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001319 * Need to save and restore the current command line, to be
1320 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 */
1322 if (ccline.cmdpos == ccline.cmdlen)
1323 new_cmdpos = 99999; /* keep it at the end */
1324 else
1325 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001326
1327 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001329 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (c == '=')
1331 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001332 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001333 * to avoid nasty things like going to another buffer when
1334 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001335 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001336 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001338 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001339 restore_cmdline(&save_ccline);
1340
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001341 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001343 len = (int)STRLEN(p);
1344 if (realloc_cmdbuff(len + 1) == OK)
1345 {
1346 ccline.cmdlen = len;
1347 STRCPY(ccline.cmdbuff, p);
1348 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001350 /* Restore the cursor or use the position set with
1351 * set_cmdline_pos(). */
1352 if (new_cmdpos > ccline.cmdlen)
1353 ccline.cmdpos = ccline.cmdlen;
1354 else
1355 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001357 KeyTyped = FALSE; /* Don't do p_wc completion. */
1358 redrawcmd();
1359 goto cmdline_changed;
1360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362 }
1363 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001364 got_int = FALSE; /* don't abandon the command line */
1365 did_emsg = FALSE;
1366 emsg_on_display = FALSE;
1367 redrawcmd();
1368 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370#endif
1371 else
1372 {
1373 if (c == Ctrl_G && p_im && restart_edit == 0)
1374 restart_edit = 'a';
1375 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1376 in history */
1377 goto returncmd; /* back to Normal mode */
1378 }
1379 }
1380
1381#ifdef FEAT_CMDWIN
1382 if (c == cedit_key || c == K_CMDWIN)
1383 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001384 if (ex_normal_busy == 0 && got_int == FALSE)
1385 {
1386 /*
1387 * Open a window to edit the command line (and history).
1388 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001389 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001390 some_key_typed = TRUE;
1391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393# ifdef FEAT_DIGRAPHS
1394 else
1395# endif
1396#endif
1397#ifdef FEAT_DIGRAPHS
1398 c = do_digraph(c);
1399#endif
1400
1401 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1402 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1403 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001404 /* In Ex mode a backslash escapes a newline. */
1405 if (exmode_active
1406 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001407 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001408 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001409 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001411 if (c == K_KENTER)
1412 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001414 else
1415 {
1416 gotesc = FALSE; /* Might have typed ESC previously, don't
1417 truncate the cmdline now. */
1418 if (ccheck_abbr(c + ABBR_OFF))
1419 goto cmdline_changed;
1420 if (!cmd_silent)
1421 {
1422 windgoto(msg_row, 0);
1423 out_flush();
1424 }
1425 break;
1426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428
1429 /*
1430 * Completion for 'wildchar' or 'wildcharm' key.
1431 * - hitting <ESC> twice means: abandon command line.
1432 * - wildcard expansion is only done when the 'wildchar' key is really
1433 * typed, not when it comes from a macro
1434 */
1435 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1436 {
1437 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1438 {
1439 /* if 'wildmode' contains "list" may still need to list */
1440 if (xpc.xp_numfiles > 1
1441 && !did_wild_list
1442 && (wim_flags[wim_index] & WIM_LIST))
1443 {
1444 (void)showmatches(&xpc, FALSE);
1445 redrawcmd();
1446 did_wild_list = TRUE;
1447 }
1448 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001449 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1450 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001452 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1453 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 else
1455 res = OK; /* don't insert 'wildchar' now */
1456 }
1457 else /* typed p_wc first time */
1458 {
1459 wim_index = 0;
1460 j = ccline.cmdpos;
1461 /* if 'wildmode' first contains "longest", get longest
1462 * common part */
1463 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001464 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1465 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001467 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1468 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470 /* if interrupted while completing, behave like it failed */
1471 if (got_int)
1472 {
1473 (void)vpeekc(); /* remove <C-C> from input stream */
1474 got_int = FALSE; /* don't abandon the command line */
1475 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1476#ifdef FEAT_WILDMENU
1477 xpc.xp_context = EXPAND_NOTHING;
1478#endif
1479 goto cmdline_changed;
1480 }
1481
1482 /* when more than one match, and 'wildmode' first contains
1483 * "list", or no change and 'wildmode' contains "longest,list",
1484 * list all matches */
1485 if (res == OK && xpc.xp_numfiles > 1)
1486 {
1487 /* a "longest" that didn't do anything is skipped (but not
1488 * "list:longest") */
1489 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1490 wim_index = 1;
1491 if ((wim_flags[wim_index] & WIM_LIST)
1492#ifdef FEAT_WILDMENU
1493 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1494#endif
1495 )
1496 {
1497 if (!(wim_flags[0] & WIM_LONGEST))
1498 {
1499#ifdef FEAT_WILDMENU
1500 int p_wmnu_save = p_wmnu;
1501 p_wmnu = 0;
1502#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001503 /* remove match */
1504 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505#ifdef FEAT_WILDMENU
1506 p_wmnu = p_wmnu_save;
1507#endif
1508 }
1509#ifdef FEAT_WILDMENU
1510 (void)showmatches(&xpc, p_wmnu
1511 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1512#else
1513 (void)showmatches(&xpc, FALSE);
1514#endif
1515 redrawcmd();
1516 did_wild_list = TRUE;
1517 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001518 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1519 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001521 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1522 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001525 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
1527#ifdef FEAT_WILDMENU
1528 else if (xpc.xp_numfiles == -1)
1529 xpc.xp_context = EXPAND_NOTHING;
1530#endif
1531 }
1532 if (wim_index < 3)
1533 ++wim_index;
1534 if (c == ESC)
1535 gotesc = TRUE;
1536 if (res == OK)
1537 goto cmdline_changed;
1538 }
1539
1540 gotesc = FALSE;
1541
1542 /* <S-Tab> goes to last match, in a clumsy way */
1543 if (c == K_S_TAB && KeyTyped)
1544 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001545 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1546 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1547 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 goto cmdline_changed;
1549 }
1550
1551 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1552 c = NL;
1553
1554 do_abbr = TRUE; /* default: check for abbreviation */
1555
1556 /*
1557 * Big switch for a typed command line character.
1558 */
1559 switch (c)
1560 {
1561 case K_BS:
1562 case Ctrl_H:
1563 case K_DEL:
1564 case K_KDEL:
1565 case Ctrl_W:
1566#ifdef FEAT_FKMAP
1567 if (cmd_fkmap && c == K_BS)
1568 c = K_DEL;
1569#endif
1570 if (c == K_KDEL)
1571 c = K_DEL;
1572
1573 /*
1574 * delete current character is the same as backspace on next
1575 * character, except at end of line
1576 */
1577 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1578 ++ccline.cmdpos;
1579#ifdef FEAT_MBYTE
1580 if (has_mbyte && c == K_DEL)
1581 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1582 ccline.cmdbuff + ccline.cmdpos);
1583#endif
1584 if (ccline.cmdpos > 0)
1585 {
1586 char_u *p;
1587
1588 j = ccline.cmdpos;
1589 p = ccline.cmdbuff + j;
1590#ifdef FEAT_MBYTE
1591 if (has_mbyte)
1592 {
1593 p = mb_prevptr(ccline.cmdbuff, p);
1594 if (c == Ctrl_W)
1595 {
1596 while (p > ccline.cmdbuff && vim_isspace(*p))
1597 p = mb_prevptr(ccline.cmdbuff, p);
1598 i = mb_get_class(p);
1599 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1600 p = mb_prevptr(ccline.cmdbuff, p);
1601 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001602 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 }
1605 else
1606#endif
1607 if (c == Ctrl_W)
1608 {
1609 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1610 --p;
1611 i = vim_iswordc(p[-1]);
1612 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1613 && vim_iswordc(p[-1]) == i)
1614 --p;
1615 }
1616 else
1617 --p;
1618 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1619 ccline.cmdlen -= j - ccline.cmdpos;
1620 i = ccline.cmdpos;
1621 while (i < ccline.cmdlen)
1622 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1623
1624 /* Truncate at the end, required for multi-byte chars. */
1625 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001626#ifdef FEAT_SEARCH_EXTRA
1627 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001628 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001629 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001630 /* save view settings, so that the screen
1631 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001632 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001633 }
1634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 redrawcmd();
1636 }
1637 else if (ccline.cmdlen == 0 && c != Ctrl_W
1638 && ccline.cmdprompt == NULL && indent == 0)
1639 {
1640 /* In ex and debug mode it doesn't make sense to return. */
1641 if (exmode_active
1642#ifdef FEAT_EVAL
1643 || ccline.cmdfirstc == '>'
1644#endif
1645 )
1646 goto cmdline_not_changed;
1647
Bram Moolenaard23a8232018-02-10 18:45:26 +01001648 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (!cmd_silent)
1650 {
1651#ifdef FEAT_RIGHTLEFT
1652 if (cmdmsg_rl)
1653 msg_col = Columns;
1654 else
1655#endif
1656 msg_col = 0;
1657 msg_putchar(' '); /* delete ':' */
1658 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001659#ifdef FEAT_SEARCH_EXTRA
1660 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001661 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 redraw_cmdline = TRUE;
1664 goto returncmd; /* back to cmd mode */
1665 }
1666 goto cmdline_changed;
1667
1668 case K_INS:
1669 case K_KINS:
1670#ifdef FEAT_FKMAP
1671 /* if Farsi mode set, we are in reverse insert mode -
1672 Do not change the mode */
1673 if (cmd_fkmap)
1674 beep_flush();
1675 else
1676#endif
1677 ccline.overstrike = !ccline.overstrike;
1678#ifdef CURSOR_SHAPE
1679 ui_cursor_shape(); /* may show different cursor shape */
1680#endif
1681 goto cmdline_not_changed;
1682
1683 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001684 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
1686 /* ":lmap" mappings exists, toggle use of mappings. */
1687 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001688#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 im_set_active(FALSE); /* Disable input method */
1690#endif
1691 if (b_im_ptr != NULL)
1692 {
1693 if (State & LANGMAP)
1694 *b_im_ptr = B_IMODE_LMAP;
1695 else
1696 *b_im_ptr = B_IMODE_NONE;
1697 }
1698 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001699#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 else
1701 {
1702 /* There are no ":lmap" mappings, toggle IM. When
1703 * 'imdisable' is set don't try getting the status, it's
1704 * always off. */
1705 if ((p_imdisable && b_im_ptr != NULL)
1706 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1707 {
1708 im_set_active(FALSE); /* Disable input method */
1709 if (b_im_ptr != NULL)
1710 *b_im_ptr = B_IMODE_NONE;
1711 }
1712 else
1713 {
1714 im_set_active(TRUE); /* Enable input method */
1715 if (b_im_ptr != NULL)
1716 *b_im_ptr = B_IMODE_IM;
1717 }
1718 }
1719#endif
1720 if (b_im_ptr != NULL)
1721 {
1722 if (b_im_ptr == &curbuf->b_p_iminsert)
1723 set_iminsert_global();
1724 else
1725 set_imsearch_global();
1726 }
1727#ifdef CURSOR_SHAPE
1728 ui_cursor_shape(); /* may show different cursor shape */
1729#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001730#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 /* Show/unshow value of 'keymap' in status lines later. */
1732 status_redraw_curbuf();
1733#endif
1734 goto cmdline_not_changed;
1735
1736/* case '@': only in very old vi */
1737 case Ctrl_U:
1738 /* delete all characters left of the cursor */
1739 j = ccline.cmdpos;
1740 ccline.cmdlen -= j;
1741 i = ccline.cmdpos = 0;
1742 while (i < ccline.cmdlen)
1743 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1744 /* Truncate at the end, required for multi-byte chars. */
1745 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001746#ifdef FEAT_SEARCH_EXTRA
1747 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001748 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 redrawcmd();
1751 goto cmdline_changed;
1752
1753#ifdef FEAT_CLIPBOARD
1754 case Ctrl_Y:
1755 /* Copy the modeless selection, if there is one. */
1756 if (clip_star.state != SELECT_CLEARED)
1757 {
1758 if (clip_star.state == SELECT_DONE)
1759 clip_copy_modeless_selection(TRUE);
1760 goto cmdline_not_changed;
1761 }
1762 break;
1763#endif
1764
1765 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1766 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001767 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001768 * ":normal" runs out of characters. */
1769 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001770 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 goto cmdline_not_changed;
1772
1773 gotesc = TRUE; /* will free ccline.cmdbuff after
1774 putting it in history */
1775 goto returncmd; /* back to cmd mode */
1776
1777 case Ctrl_R: /* insert register */
1778#ifdef USE_ON_FLY_SCROLL
1779 dont_scroll = TRUE; /* disallow scrolling here */
1780#endif
1781 putcmdline('"', TRUE);
1782 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001783 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (i == Ctrl_O)
1785 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1786 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001787 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001788 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 --no_mapping;
1790#ifdef FEAT_EVAL
1791 /*
1792 * Insert the result of an expression.
1793 * Need to save the current command line, to be able to enter
1794 * a new one...
1795 */
1796 new_cmdpos = -1;
1797 if (c == '=')
1798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1800 {
1801 beep_flush();
1802 c = ESC;
1803 }
1804 else
1805 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001806 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001808 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810 }
1811#endif
1812 if (c != ESC) /* use ESC to cancel inserting register */
1813 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001814 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001815
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001816#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001817 /* When there was a serious error abort getting the
1818 * command line. */
1819 if (aborting())
1820 {
1821 gotesc = TRUE; /* will free ccline.cmdbuff after
1822 putting it in history */
1823 goto returncmd; /* back to cmd mode */
1824 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 KeyTyped = FALSE; /* Don't do p_wc completion. */
1827#ifdef FEAT_EVAL
1828 if (new_cmdpos >= 0)
1829 {
1830 /* set_cmdline_pos() was used */
1831 if (new_cmdpos > ccline.cmdlen)
1832 ccline.cmdpos = ccline.cmdlen;
1833 else
1834 ccline.cmdpos = new_cmdpos;
1835 }
1836#endif
1837 }
1838 redrawcmd();
1839 goto cmdline_changed;
1840
1841 case Ctrl_D:
1842 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1843 break; /* Use ^D as normal char instead */
1844
1845 redrawcmd();
1846 continue; /* don't do incremental search now */
1847
1848 case K_RIGHT:
1849 case K_S_RIGHT:
1850 case K_C_RIGHT:
1851 do
1852 {
1853 if (ccline.cmdpos >= ccline.cmdlen)
1854 break;
1855 i = cmdline_charsize(ccline.cmdpos);
1856 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1857 break;
1858 ccline.cmdspos += i;
1859#ifdef FEAT_MBYTE
1860 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001861 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 + ccline.cmdpos);
1863 else
1864#endif
1865 ++ccline.cmdpos;
1866 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001867 while ((c == K_S_RIGHT || c == K_C_RIGHT
1868 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1870#ifdef FEAT_MBYTE
1871 if (has_mbyte)
1872 set_cmdspos_cursor();
1873#endif
1874 goto cmdline_not_changed;
1875
1876 case K_LEFT:
1877 case K_S_LEFT:
1878 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001879 if (ccline.cmdpos == 0)
1880 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 do
1882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 --ccline.cmdpos;
1884#ifdef FEAT_MBYTE
1885 if (has_mbyte) /* move to first byte of char */
1886 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1887 ccline.cmdbuff + ccline.cmdpos);
1888#endif
1889 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1890 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001891 while (ccline.cmdpos > 0
1892 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001893 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1895#ifdef FEAT_MBYTE
1896 if (has_mbyte)
1897 set_cmdspos_cursor();
1898#endif
1899 goto cmdline_not_changed;
1900
1901 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001902 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001903 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaar4770d092006-01-12 23:22:24 +00001905#ifdef FEAT_GUI_W32
1906 /* On Win32 ignore <M-F4>, we get it when closing the window was
1907 * cancelled. */
1908 case K_F4:
1909 if (mod_mask == MOD_MASK_ALT)
1910 {
1911 redrawcmd(); /* somehow the cmdline is cleared */
1912 goto cmdline_not_changed;
1913 }
1914 break;
1915#endif
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#ifdef FEAT_MOUSE
1918 case K_MIDDLEDRAG:
1919 case K_MIDDLERELEASE:
1920 goto cmdline_not_changed; /* Ignore mouse */
1921
1922 case K_MIDDLEMOUSE:
1923# ifdef FEAT_GUI
1924 /* When GUI is active, also paste when 'mouse' is empty */
1925 if (!gui.in_use)
1926# endif
1927 if (!mouse_has(MOUSE_COMMAND))
1928 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001929# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001931 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001933# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001934 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 redrawcmd();
1936 goto cmdline_changed;
1937
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001938# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001940 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 redrawcmd();
1942 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 case K_LEFTDRAG:
1946 case K_LEFTRELEASE:
1947 case K_RIGHTDRAG:
1948 case K_RIGHTRELEASE:
1949 /* Ignore drag and release events when the button-down wasn't
1950 * seen before. */
1951 if (ignore_drag_release)
1952 goto cmdline_not_changed;
1953 /* FALLTHROUGH */
1954 case K_LEFTMOUSE:
1955 case K_RIGHTMOUSE:
1956 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1957 ignore_drag_release = TRUE;
1958 else
1959 ignore_drag_release = FALSE;
1960# ifdef FEAT_GUI
1961 /* When GUI is active, also move when 'mouse' is empty */
1962 if (!gui.in_use)
1963# endif
1964 if (!mouse_has(MOUSE_COMMAND))
1965 goto cmdline_not_changed; /* Ignore mouse */
1966# ifdef FEAT_CLIPBOARD
1967 if (mouse_row < cmdline_row && clip_star.available)
1968 {
1969 int button, is_click, is_drag;
1970
1971 /*
1972 * Handle modeless selection.
1973 */
1974 button = get_mouse_button(KEY2TERMCAP1(c),
1975 &is_click, &is_drag);
1976 if (mouse_model_popup() && button == MOUSE_LEFT
1977 && (mod_mask & MOD_MASK_SHIFT))
1978 {
1979 /* Translate shift-left to right button. */
1980 button = MOUSE_RIGHT;
1981 mod_mask &= ~MOD_MASK_SHIFT;
1982 }
1983 clip_modeless(button, is_click, is_drag);
1984 goto cmdline_not_changed;
1985 }
1986# endif
1987
1988 set_cmdspos();
1989 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1990 ++ccline.cmdpos)
1991 {
1992 i = cmdline_charsize(ccline.cmdpos);
1993 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1994 && mouse_col < ccline.cmdspos % Columns + i)
1995 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001996# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (has_mbyte)
1998 {
1999 /* Count ">" for double-wide char that doesn't fit. */
2000 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002001 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 + ccline.cmdpos) - 1;
2003 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 ccline.cmdspos += i;
2006 }
2007 goto cmdline_not_changed;
2008
2009 /* Mouse scroll wheel: ignored here */
2010 case K_MOUSEDOWN:
2011 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002012 case K_MOUSELEFT:
2013 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 /* Alternate buttons ignored here */
2015 case K_X1MOUSE:
2016 case K_X1DRAG:
2017 case K_X1RELEASE:
2018 case K_X2MOUSE:
2019 case K_X2DRAG:
2020 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002021 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 goto cmdline_not_changed;
2023
2024#endif /* FEAT_MOUSE */
2025
2026#ifdef FEAT_GUI
2027 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2028 case K_LEFTRELEASE_NM:
2029 goto cmdline_not_changed;
2030
2031 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002032 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 gui_do_scroll();
2035 redrawcmd();
2036 }
2037 goto cmdline_not_changed;
2038
2039 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002040 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002042 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 redrawcmd();
2044 }
2045 goto cmdline_not_changed;
2046#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002047#ifdef FEAT_GUI_TABLINE
2048 case K_TABLINE:
2049 case K_TABMENU:
2050 /* Don't want to change any tabs here. Make sure the same tab
2051 * is still selected. */
2052 if (gui_use_tabline())
2053 gui_mch_set_curtab(tabpage_index(curtab));
2054 goto cmdline_not_changed;
2055#endif
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 case K_SELECT: /* end of Select mode mapping - ignore */
2058 goto cmdline_not_changed;
2059
2060 case Ctrl_B: /* begin of command line */
2061 case K_HOME:
2062 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 case K_S_HOME:
2064 case K_C_HOME:
2065 ccline.cmdpos = 0;
2066 set_cmdspos();
2067 goto cmdline_not_changed;
2068
2069 case Ctrl_E: /* end of command line */
2070 case K_END:
2071 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 case K_S_END:
2073 case K_C_END:
2074 ccline.cmdpos = ccline.cmdlen;
2075 set_cmdspos_cursor();
2076 goto cmdline_not_changed;
2077
2078 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002079 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 break;
2081 goto cmdline_changed;
2082
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002083 case Ctrl_L:
2084#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002085 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002086 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002087#endif
2088
2089 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002090 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 break;
2092 goto cmdline_changed;
2093
2094 case Ctrl_N: /* next match */
2095 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002096 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002098 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2099 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002101 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002104 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 case K_UP:
2106 case K_DOWN:
2107 case K_S_UP:
2108 case K_S_DOWN:
2109 case K_PAGEUP:
2110 case K_KPAGEUP:
2111 case K_PAGEDOWN:
2112 case K_KPAGEDOWN:
2113 if (hislen == 0 || firstc == NUL) /* no history */
2114 goto cmdline_not_changed;
2115
2116 i = hiscnt;
2117
2118 /* save current command string so it can be restored later */
2119 if (lookfor == NULL)
2120 {
2121 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2122 goto cmdline_not_changed;
2123 lookfor[ccline.cmdpos] = NUL;
2124 }
2125
2126 j = (int)STRLEN(lookfor);
2127 for (;;)
2128 {
2129 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002130 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002131 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
2133 if (hiscnt == hislen) /* first time */
2134 hiscnt = hisidx[histype];
2135 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2136 hiscnt = hislen - 1;
2137 else if (hiscnt != hisidx[histype] + 1)
2138 --hiscnt;
2139 else /* at top of list */
2140 {
2141 hiscnt = i;
2142 break;
2143 }
2144 }
2145 else /* one step forwards */
2146 {
2147 /* on last entry, clear the line */
2148 if (hiscnt == hisidx[histype])
2149 {
2150 hiscnt = hislen;
2151 break;
2152 }
2153
2154 /* not on a history line, nothing to do */
2155 if (hiscnt == hislen)
2156 break;
2157 if (hiscnt == hislen - 1) /* wrap around */
2158 hiscnt = 0;
2159 else
2160 ++hiscnt;
2161 }
2162 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2163 {
2164 hiscnt = i;
2165 break;
2166 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002167 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002168 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 || STRNCMP(history[histype][hiscnt].hisstr,
2170 lookfor, (size_t)j) == 0)
2171 break;
2172 }
2173
2174 if (hiscnt != i) /* jumped to other entry */
2175 {
2176 char_u *p;
2177 int len;
2178 int old_firstc;
2179
2180 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002181 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (hiscnt == hislen)
2183 p = lookfor; /* back to the old one */
2184 else
2185 p = history[histype][hiscnt].hisstr;
2186
2187 if (histype == HIST_SEARCH
2188 && p != lookfor
2189 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2190 {
2191 /* Correct for the separator character used when
2192 * adding the history entry vs the one used now.
2193 * First loop: count length.
2194 * Second loop: copy the characters. */
2195 for (i = 0; i <= 1; ++i)
2196 {
2197 len = 0;
2198 for (j = 0; p[j] != NUL; ++j)
2199 {
2200 /* Replace old sep with new sep, unless it is
2201 * escaped. */
2202 if (p[j] == old_firstc
2203 && (j == 0 || p[j - 1] != '\\'))
2204 {
2205 if (i > 0)
2206 ccline.cmdbuff[len] = firstc;
2207 }
2208 else
2209 {
2210 /* Escape new sep, unless it is already
2211 * escaped. */
2212 if (p[j] == firstc
2213 && (j == 0 || p[j - 1] != '\\'))
2214 {
2215 if (i > 0)
2216 ccline.cmdbuff[len] = '\\';
2217 ++len;
2218 }
2219 if (i > 0)
2220 ccline.cmdbuff[len] = p[j];
2221 }
2222 ++len;
2223 }
2224 if (i == 0)
2225 {
2226 alloc_cmdbuff(len);
2227 if (ccline.cmdbuff == NULL)
2228 goto returncmd;
2229 }
2230 }
2231 ccline.cmdbuff[len] = NUL;
2232 }
2233 else
2234 {
2235 alloc_cmdbuff((int)STRLEN(p));
2236 if (ccline.cmdbuff == NULL)
2237 goto returncmd;
2238 STRCPY(ccline.cmdbuff, p);
2239 }
2240
2241 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2242 redrawcmd();
2243 goto cmdline_changed;
2244 }
2245 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002246#endif
2247 goto cmdline_not_changed;
2248
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002249#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002250 case Ctrl_G: /* next match */
2251 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002252 if (may_adjust_incsearch_highlighting(
2253 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002254 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002255 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256#endif
2257
2258 case Ctrl_V:
2259 case Ctrl_Q:
2260#ifdef FEAT_MOUSE
2261 ignore_drag_release = TRUE;
2262#endif
2263 putcmdline('^', TRUE);
2264 c = get_literal(); /* get next (two) character(s) */
2265 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002266 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#ifdef FEAT_MBYTE
2268 /* may need to remove ^ when composing char was typed */
2269 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2270 {
2271 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2272 msg_putchar(' ');
2273 cursorcmd();
2274 }
2275#endif
2276 break;
2277
2278#ifdef FEAT_DIGRAPHS
2279 case Ctrl_K:
2280#ifdef FEAT_MOUSE
2281 ignore_drag_release = TRUE;
2282#endif
2283 putcmdline('?', TRUE);
2284#ifdef USE_ON_FLY_SCROLL
2285 dont_scroll = TRUE; /* disallow scrolling here */
2286#endif
2287 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002288 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (c != NUL)
2290 break;
2291
2292 redrawcmd();
2293 goto cmdline_not_changed;
2294#endif /* FEAT_DIGRAPHS */
2295
2296#ifdef FEAT_RIGHTLEFT
2297 case Ctrl__: /* CTRL-_: switch language mode */
2298 if (!p_ari)
2299 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002300# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (p_altkeymap)
2302 {
2303 cmd_fkmap = !cmd_fkmap;
2304 if (cmd_fkmap) /* in Farsi always in Insert mode */
2305 ccline.overstrike = FALSE;
2306 }
2307 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 cmd_hkmap = !cmd_hkmap;
2310 goto cmdline_not_changed;
2311#endif
2312
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002313 case K_PS:
2314 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2315 goto cmdline_changed;
2316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 default:
2318#ifdef UNIX
2319 if (c == intr_char)
2320 {
2321 gotesc = TRUE; /* will free ccline.cmdbuff after
2322 putting it in history */
2323 goto returncmd; /* back to Normal mode */
2324 }
2325#endif
2326 /*
2327 * Normal character with no special meaning. Just set mod_mask
2328 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2329 * the string <S-Space>. This should only happen after ^V.
2330 */
2331 if (!IS_SPECIAL(c))
2332 mod_mask = 0x0;
2333 break;
2334 }
2335 /*
2336 * End of switch on command line character.
2337 * We come here if we have a normal character.
2338 */
2339
Bram Moolenaarede3e632013-06-23 16:16:19 +02002340 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#ifdef FEAT_MBYTE
2342 /* Add ABBR_OFF for characters above 0x100, this is
2343 * what check_abbr() expects. */
2344 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2345#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002346 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 goto cmdline_changed;
2348
2349 /*
2350 * put the character in the command line
2351 */
2352 if (IS_SPECIAL(c) || mod_mask != 0)
2353 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2354 else
2355 {
2356#ifdef FEAT_MBYTE
2357 if (has_mbyte)
2358 {
2359 j = (*mb_char2bytes)(c, IObuff);
2360 IObuff[j] = NUL; /* exclude composing chars */
2361 put_on_cmdline(IObuff, j, TRUE);
2362 }
2363 else
2364#endif
2365 {
2366 IObuff[0] = c;
2367 put_on_cmdline(IObuff, 1, TRUE);
2368 }
2369 }
2370 goto cmdline_changed;
2371
2372/*
2373 * This part implements incremental searches for "/" and "?"
2374 * Jump to cmdline_not_changed when a character has been read but the command
2375 * line did not change. Then we only search and redraw if something changed in
2376 * the past.
2377 * Jump to cmdline_changed when the command line did change.
2378 * (Sorry for the goto's, I know it is ugly).
2379 */
2380cmdline_not_changed:
2381#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002382 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 continue;
2384#endif
2385
2386cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002387 /* Trigger CmdlineChanged autocommands. */
2388 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002391 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#endif
2393
2394#ifdef FEAT_RIGHTLEFT
2395 if (cmdmsg_rl
2396# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002397 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398# endif
2399 )
2400 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002401 * right-left typing. Not efficient, but it works.
2402 * Do it only when there are no characters left to read
2403 * to avoid useless intermediate redraws. */
2404 if (vpeekc() == NUL)
2405 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#endif
2407 }
2408
2409returncmd:
2410
2411#ifdef FEAT_RIGHTLEFT
2412 cmdmsg_rl = FALSE;
2413#endif
2414
2415#ifdef FEAT_FKMAP
2416 cmd_fkmap = 0;
2417#endif
2418
2419 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002420 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002423 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#endif
2425
2426 if (ccline.cmdbuff != NULL)
2427 {
2428 /*
2429 * Put line in history buffer (":" and "=" only when it was typed).
2430 */
2431#ifdef FEAT_CMDHIST
2432 if (ccline.cmdlen && firstc != NUL
2433 && (some_key_typed || histype == HIST_SEARCH))
2434 {
2435 add_to_history(histype, ccline.cmdbuff, TRUE,
2436 histype == HIST_SEARCH ? firstc : NUL);
2437 if (firstc == ':')
2438 {
2439 vim_free(new_last_cmdline);
2440 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2441 }
2442 }
2443#endif
2444
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002445 if (gotesc)
2446 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448
2449 /*
2450 * If the screen was shifted up, redraw the whole screen (later).
2451 * If the line is too long, clear it, so ruler and shown command do
2452 * not get printed in the middle of it.
2453 */
2454 msg_check();
2455 msg_scroll = save_msg_scroll;
2456 redir_off = FALSE;
2457
2458 /* When the command line was typed, no need for a wait-return prompt. */
2459 if (some_key_typed)
2460 need_wait_return = FALSE;
2461
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002462 /* Trigger CmdlineLeave autocommands. */
2463 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002466#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2468 im_save_status(b_im_ptr);
2469 im_set_active(FALSE);
2470#endif
2471#ifdef FEAT_MOUSE
2472 setmouse();
2473#endif
2474#ifdef CURSOR_SHAPE
2475 ui_cursor_shape(); /* may show different cursor shape */
2476#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002477 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002479 {
2480 char_u *p = ccline.cmdbuff;
2481
2482 /* Make ccline empty, getcmdline() may try to use it. */
2483 ccline.cmdbuff = NULL;
2484 return p;
2485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486}
2487
2488#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2489/*
2490 * Get a command line with a prompt.
2491 * This is prepared to be called recursively from getcmdline() (e.g. by
2492 * f_input() when evaluating an expression from CTRL-R =).
2493 * Returns the command line in allocated memory, or NULL.
2494 */
2495 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002496getcmdline_prompt(
2497 int firstc,
2498 char_u *prompt, /* command line prompt */
2499 int attr, /* attributes for prompt */
2500 int xp_context, /* type of expansion */
2501 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 char_u *s;
2504 struct cmdline_info save_ccline;
2505 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002506 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002508 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 ccline.cmdprompt = prompt;
2510 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002511# ifdef FEAT_EVAL
2512 ccline.xp_context = xp_context;
2513 ccline.xp_arg = xp_arg;
2514 ccline.input_fn = (firstc == '@');
2515# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002516 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002518 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002519 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002520 /* Restore msg_col, the prompt from input() may have changed it.
2521 * But only if called recursively and the commandline is therefore being
2522 * restored to an old one; if not, the input() prompt stays on the screen,
2523 * so we need its modified msg_col left intact. */
2524 if (ccline.cmdbuff != NULL)
2525 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527 return s;
2528}
2529#endif
2530
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002531/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002532 * Return TRUE when the text must not be changed and we can't switch to
2533 * another window or buffer. Used when editing the command line, evaluating
2534 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002535 */
2536 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002537text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002538{
2539#ifdef FEAT_CMDWIN
2540 if (cmdwin_type != 0)
2541 return TRUE;
2542#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002543 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002544}
2545
2546/*
2547 * Give an error message for a command that isn't allowed while the cmdline
2548 * window is open or editing the cmdline in another way.
2549 */
2550 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002551text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002552{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002553 EMSG(_(get_text_locked_msg()));
2554}
2555
2556 char_u *
2557get_text_locked_msg(void)
2558{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002559#ifdef FEAT_CMDWIN
2560 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002561 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002562#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002563 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002564}
2565
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002566/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002567 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2568 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002569 */
2570 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002571curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002572{
2573 if (curbuf_lock > 0)
2574 {
2575 EMSG(_("E788: Not allowed to edit another buffer now"));
2576 return TRUE;
2577 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002578 return allbuf_locked();
2579}
2580
2581/*
2582 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2583 * message.
2584 */
2585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002586allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002587{
2588 if (allbuf_lock > 0)
2589 {
2590 EMSG(_("E811: Not allowed to change buffer information now"));
2591 return TRUE;
2592 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002593 return FALSE;
2594}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002597cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2600 if (cmdline_star > 0) /* showing '*', always 1 position */
2601 return 1;
2602#endif
2603 return ptr2cells(ccline.cmdbuff + idx);
2604}
2605
2606/*
2607 * Compute the offset of the cursor on the command line for the prompt and
2608 * indent.
2609 */
2610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002611set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002613 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 ccline.cmdspos = 1 + ccline.cmdindent;
2615 else
2616 ccline.cmdspos = 0 + ccline.cmdindent;
2617}
2618
2619/*
2620 * Compute the screen position for the cursor on the command line.
2621 */
2622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002623set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 int i, m, c;
2626
2627 set_cmdspos();
2628 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002631 if (m < 0) /* overflow, Columns or Rows at weird value */
2632 m = MAXCOL;
2633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 else
2635 m = MAXCOL;
2636 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2637 {
2638 c = cmdline_charsize(i);
2639#ifdef FEAT_MBYTE
2640 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2641 if (has_mbyte)
2642 correct_cmdspos(i, c);
2643#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002644 /* If the cmdline doesn't fit, show cursor on last visible char.
2645 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if ((ccline.cmdspos += c) >= m)
2647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 ccline.cmdspos -= c;
2649 break;
2650 }
2651#ifdef FEAT_MBYTE
2652 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002653 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654#endif
2655 }
2656}
2657
2658#ifdef FEAT_MBYTE
2659/*
2660 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2661 * character that doesn't fit, so that a ">" must be displayed.
2662 */
2663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002664correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002666 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2668 && ccline.cmdspos % Columns + cells > Columns)
2669 ccline.cmdspos++;
2670}
2671#endif
2672
2673/*
2674 * Get an Ex command line for the ":" command.
2675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002677getexline(
2678 int c, /* normally ':', NUL for ":append" */
2679 void *cookie UNUSED,
2680 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 /* When executing a register, remove ':' that's in front of each line. */
2683 if (exec_from_reg && vpeekc() == ':')
2684 (void)vgetc();
2685 return getcmdline(c, 1L, indent);
2686}
2687
2688/*
2689 * Get an Ex command line for Ex mode.
2690 * In Ex mode we only use the OS supplied line editing features and no
2691 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002692 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002695getexmodeline(
2696 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002698 void *cookie UNUSED,
2699 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002701 garray_T line_ga;
2702 char_u *pend;
2703 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002704 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002705 int escaped = FALSE; /* CTRL-V typed */
2706 int vcol = 0;
2707 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002708 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002709 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 /* Switch cursor on now. This avoids that it happens after the "\n", which
2712 * confuses the system function that computes tabstops. */
2713 cursor_on();
2714
2715 /* always start in column 0; write a newline if necessary */
2716 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002719 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002721 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if (p_prompt)
2723 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 while (indent-- > 0)
2725 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728
2729 ga_init2(&line_ga, 1, 30);
2730
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002731 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002733 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002734 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002735 while (indent >= 8)
2736 {
2737 ga_append(&line_ga, TAB);
2738 msg_puts((char_u *)" ");
2739 indent -= 8;
2740 }
2741 while (indent-- > 0)
2742 {
2743 ga_append(&line_ga, ' ');
2744 msg_putchar(' ');
2745 }
2746 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 ++no_mapping;
2748 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002749
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 /*
2751 * Get the line, one character at a time.
2752 */
2753 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002756 long sw;
2757 char_u *s;
2758
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if (ga_grow(&line_ga, 40) == FAIL)
2760 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
Bram Moolenaarba748c82017-02-26 14:00:07 +01002762 /*
2763 * Get one character at a time.
2764 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002765 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002766
2767 /* Check for a ":normal" command and no more characters left. */
2768 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2769 c1 = '\n';
2770 else
2771 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 * Handle line editing.
2775 * Previously this was left to the system, putting the terminal in
2776 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 msg_putchar('\n');
2781 break;
2782 }
2783
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002784 if (c1 == K_PS)
2785 {
2786 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2787 goto redraw;
2788 }
2789
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790 if (!escaped)
2791 {
2792 /* CR typed means "enter", which is NL */
2793 if (c1 == '\r')
2794 c1 = '\n';
2795
2796 if (c1 == BS || c1 == K_BS
2797 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799 if (line_ga.ga_len > 0)
2800 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002801#ifdef FEAT_MBYTE
2802 if (has_mbyte)
2803 {
2804 p = (char_u *)line_ga.ga_data;
2805 p[line_ga.ga_len] = NUL;
2806 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2807 line_ga.ga_len -= len;
2808 }
2809 else
2810#endif
2811 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002812 goto redraw;
2813 }
2814 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 msg_col = startcol;
2820 msg_clr_eos();
2821 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002822 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823 }
2824
2825 if (c1 == Ctrl_T)
2826 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002827 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828 p = (char_u *)line_ga.ga_data;
2829 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002830 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002831 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002833 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002835 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002836 p = (char_u *)line_ga.ga_data;
2837 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002838 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2839 *s = ' ';
2840 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002842redraw:
2843 /* redraw the line */
2844 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002846 p = (char_u *)line_ga.ga_data;
2847 p[line_ga.ga_len] = NUL;
2848 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 msg_putchar(' ');
2855 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002856 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002860 len = MB_PTR2LEN(p);
2861 msg_outtrans_len(p, len);
2862 vcol += ptr2cells(p);
2863 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002866 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002867 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 continue;
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 if (c1 == Ctrl_D)
2872 {
2873 /* Delete one shiftwidth. */
2874 p = (char_u *)line_ga.ga_data;
2875 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 if (prev_char == '^')
2878 ex_keep_indent = TRUE;
2879 indent = 0;
2880 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882 else
2883 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002884 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002885 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002886 if (indent > 0)
2887 {
2888 --indent;
2889 indent -= indent % get_sw_value(curbuf);
2890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002892 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002893 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002894 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2896 --line_ga.ga_len;
2897 }
2898 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900
2901 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2902 {
2903 escaped = TRUE;
2904 continue;
2905 }
2906
2907 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2908 if (IS_SPECIAL(c1))
2909 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911
2912 if (IS_SPECIAL(c1))
2913 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002914#ifdef FEAT_MBYTE
2915 if (has_mbyte)
2916 len = (*mb_char2bytes)(c1,
2917 (char_u *)line_ga.ga_data + line_ga.ga_len);
2918 else
2919#endif
2920 {
2921 len = 1;
2922 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2923 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002924 if (c1 == '\n')
2925 msg_putchar('\n');
2926 else if (c1 == TAB)
2927 {
2928 /* Don't use chartabsize(), 'ts' can be different */
2929 do
2930 {
2931 msg_putchar(' ');
2932 } while (++vcol % 8);
2933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002936 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002937 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002938 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002940 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002941 escaped = FALSE;
2942
2943 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002944 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002945
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002946 /* We are done when a NL is entered, but not when it comes after an
2947 * odd number of backslashes, that results in a NUL. */
2948 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002950 int bcount = 0;
2951
2952 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2953 ++bcount;
2954
2955 if (bcount > 0)
2956 {
2957 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2958 * "\NL", etc. */
2959 line_ga.ga_len -= (bcount + 1) / 2;
2960 pend -= (bcount + 1) / 2;
2961 pend[-1] = '\n';
2962 }
2963
2964 if ((bcount & 1) == 0)
2965 {
2966 --line_ga.ga_len;
2967 --pend;
2968 *pend = NUL;
2969 break;
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972 }
2973
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002974 --no_mapping;
2975 --allow_keys;
2976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 /* make following messages go to the next line */
2978 msg_didout = FALSE;
2979 msg_col = 0;
2980 if (msg_row < Rows - 1)
2981 ++msg_row;
2982 emsg_on_display = FALSE; /* don't want ui_delay() */
2983
2984 if (got_int)
2985 ga_clear(&line_ga);
2986
2987 return (char_u *)line_ga.ga_data;
2988}
2989
Bram Moolenaare344bea2005-09-01 20:46:49 +00002990# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2991 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
2993 * Return TRUE if ccline.overstrike is on.
2994 */
2995 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002996cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 return ccline.overstrike;
2999}
3000
3001/*
3002 * Return TRUE if the cursor is at the end of the cmdline.
3003 */
3004 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003005cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 return (ccline.cmdpos >= ccline.cmdlen);
3008}
3009#endif
3010
Bram Moolenaar9372a112005-12-06 19:59:18 +00003011#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012/*
3013 * Return the virtual column number at the current cursor position.
3014 * This is used by the IM code to obtain the start of the preedit string.
3015 */
3016 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003017cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3020 return MAXCOL;
3021
3022# ifdef FEAT_MBYTE
3023 if (has_mbyte)
3024 {
3025 colnr_T col;
3026 int i = 0;
3027
3028 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003029 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
3031 return col;
3032 }
3033 else
3034# endif
3035 return ccline.cmdpos;
3036}
3037#endif
3038
3039#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3040/*
3041 * If part of the command line is an IM preedit string, redraw it with
3042 * IM feedback attributes. The cursor position is restored after drawing.
3043 */
3044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003045redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 if ((State & CMDLINE)
3048 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003049 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 && !p_imdisable
3051 && im_is_preediting())
3052 {
3053 int cmdpos = 0;
3054 int cmdspos;
3055 int old_row;
3056 int old_col;
3057 colnr_T col;
3058
3059 old_row = msg_row;
3060 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003061 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063# ifdef FEAT_MBYTE
3064 if (has_mbyte)
3065 {
3066 for (col = 0; col < preedit_start_col
3067 && cmdpos < ccline.cmdlen; ++col)
3068 {
3069 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003070 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072 }
3073 else
3074# endif
3075 {
3076 cmdspos += preedit_start_col;
3077 cmdpos += preedit_start_col;
3078 }
3079
3080 msg_row = cmdline_row + (cmdspos / (int)Columns);
3081 msg_col = cmdspos % (int)Columns;
3082 if (msg_row >= Rows)
3083 msg_row = Rows - 1;
3084
3085 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3086 {
3087 int char_len;
3088 int char_attr;
3089
3090 char_attr = im_get_feedback_attr(col);
3091 if (char_attr < 0)
3092 break; /* end of preedit string */
3093
3094# ifdef FEAT_MBYTE
3095 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003096 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else
3098# endif
3099 char_len = 1;
3100
3101 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3102 cmdpos += char_len;
3103 }
3104
3105 msg_row = old_row;
3106 msg_col = old_col;
3107 }
3108}
3109#endif /* FEAT_XIM && FEAT_GUI_GTK */
3110
3111/*
3112 * Allocate a new command line buffer.
3113 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3114 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3115 */
3116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003117alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 /*
3120 * give some extra space to avoid having to allocate all the time
3121 */
3122 if (len < 80)
3123 len = 100;
3124 else
3125 len += 20;
3126
3127 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3128 ccline.cmdbufflen = len;
3129}
3130
3131/*
3132 * Re-allocate the command line to length len + something extra.
3133 * return FAIL for failure, OK otherwise
3134 */
3135 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003136realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 char_u *p;
3139
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003140 if (len < ccline.cmdbufflen)
3141 return OK; /* no need to resize */
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 p = ccline.cmdbuff;
3144 alloc_cmdbuff(len); /* will get some more */
3145 if (ccline.cmdbuff == NULL) /* out of memory */
3146 {
3147 ccline.cmdbuff = p; /* keep the old one */
3148 return FAIL;
3149 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003150 /* There isn't always a NUL after the command, but it may need to be
3151 * there, thus copy up to the NUL and add a NUL. */
3152 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3153 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003155
3156 if (ccline.xpc != NULL
3157 && ccline.xpc->xp_pattern != NULL
3158 && ccline.xpc->xp_context != EXPAND_NOTHING
3159 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3160 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003161 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003162
3163 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3164 * to point into the newly allocated memory. */
3165 if (i >= 0 && i <= ccline.cmdlen)
3166 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3167 }
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 return OK;
3170}
3171
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003172#if defined(FEAT_ARABIC) || defined(PROTO)
3173static char_u *arshape_buf = NULL;
3174
3175# if defined(EXITFREE) || defined(PROTO)
3176 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003177free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003178{
3179 vim_free(arshape_buf);
3180}
3181# endif
3182#endif
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184/*
3185 * Draw part of the cmdline at the current cursor position. But draw stars
3186 * when cmdline_star is TRUE.
3187 */
3188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003189draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3192 int i;
3193
3194 if (cmdline_star > 0)
3195 for (i = 0; i < len; ++i)
3196 {
3197 msg_putchar('*');
3198# ifdef FEAT_MBYTE
3199 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003200 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201# endif
3202 }
3203 else
3204#endif
3205#ifdef FEAT_ARABIC
3206 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 static int buflen = 0;
3209 char_u *p;
3210 int j;
3211 int newlen = 0;
3212 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003213 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 int prev_c = 0;
3215 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003216 int u8c;
3217 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 /*
3221 * Do arabic shaping into a temporary buffer. This is very
3222 * inefficient!
3223 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003224 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
3226 /* Re-allocate the buffer. We keep it around to avoid a lot of
3227 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003228 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003229 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003230 arshape_buf = alloc(buflen);
3231 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 return; /* out of memory */
3233 }
3234
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003235 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3236 {
3237 /* Prepend a space to draw the leading composing char on. */
3238 arshape_buf[0] = ' ';
3239 newlen = 1;
3240 }
3241
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 for (j = start; j < start + len; j += mb_l)
3243 {
3244 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003245 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003246 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (ARABIC_CHAR(u8c))
3248 {
3249 /* Do Arabic shaping. */
3250 if (cmdmsg_rl)
3251 {
3252 /* displaying from right to left */
3253 pc = prev_c;
3254 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003255 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (j + mb_l >= start + len)
3257 nc = NUL;
3258 else
3259 nc = utf_ptr2char(p + mb_l);
3260 }
3261 else
3262 {
3263 /* displaying from left to right */
3264 if (j + mb_l >= start + len)
3265 pc = NUL;
3266 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003267 {
3268 int pcc[MAX_MCO];
3269
3270 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003272 pc1 = pcc[0];
3273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 nc = prev_c;
3275 }
3276 prev_c = u8c;
3277
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003278 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003281 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003283 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3284 if (u8cc[1] != 0)
3285 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003286 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 }
3289 else
3290 {
3291 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003292 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 newlen += mb_l;
3294 }
3295 }
3296
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003297 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 else
3300#endif
3301 msg_outtrans_len(ccline.cmdbuff + start, len);
3302}
3303
3304/*
3305 * Put a character on the command line. Shifts the following text to the
3306 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3307 * "c" must be printable (fit in one display cell)!
3308 */
3309 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003310putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
3312 if (cmd_silent)
3313 return;
3314 msg_no_more = TRUE;
3315 msg_putchar(c);
3316 if (shift)
3317 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3318 msg_no_more = FALSE;
3319 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003320 extra_char = c;
3321 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322}
3323
3324/*
3325 * Undo a putcmdline(c, FALSE).
3326 */
3327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003328unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 if (cmd_silent)
3331 return;
3332 msg_no_more = TRUE;
3333 if (ccline.cmdlen == ccline.cmdpos)
3334 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003335#ifdef FEAT_MBYTE
3336 else if (has_mbyte)
3337 draw_cmdline(ccline.cmdpos,
3338 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
3341 draw_cmdline(ccline.cmdpos, 1);
3342 msg_no_more = FALSE;
3343 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003344 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347/*
3348 * Put the given string, of the given length, onto the command line.
3349 * If len is -1, then STRLEN() is used to calculate the length.
3350 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3351 * part will be redrawn, otherwise it will not. If this function is called
3352 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3353 * called afterwards.
3354 */
3355 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003356put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 int retval;
3359 int i;
3360 int m;
3361 int c;
3362
3363 if (len < 0)
3364 len = (int)STRLEN(str);
3365
3366 /* Check if ccline.cmdbuff needs to be longer */
3367 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003368 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 else
3370 retval = OK;
3371 if (retval == OK)
3372 {
3373 if (!ccline.overstrike)
3374 {
3375 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3376 ccline.cmdbuff + ccline.cmdpos,
3377 (size_t)(ccline.cmdlen - ccline.cmdpos));
3378 ccline.cmdlen += len;
3379 }
3380 else
3381 {
3382#ifdef FEAT_MBYTE
3383 if (has_mbyte)
3384 {
3385 /* Count nr of characters in the new string. */
3386 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003387 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 ++m;
3389 /* Count nr of bytes in cmdline that are overwritten by these
3390 * characters. */
3391 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003392 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 --m;
3394 if (i < ccline.cmdlen)
3395 {
3396 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3397 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3398 ccline.cmdlen += ccline.cmdpos + len - i;
3399 }
3400 else
3401 ccline.cmdlen = ccline.cmdpos + len;
3402 }
3403 else
3404#endif
3405 if (ccline.cmdpos + len > ccline.cmdlen)
3406 ccline.cmdlen = ccline.cmdpos + len;
3407 }
3408 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3409 ccline.cmdbuff[ccline.cmdlen] = NUL;
3410
3411#ifdef FEAT_MBYTE
3412 if (enc_utf8)
3413 {
3414 /* When the inserted text starts with a composing character,
3415 * backup to the character before it. There could be two of them.
3416 */
3417 i = 0;
3418 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3419 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3420 {
3421 i = (*mb_head_off)(ccline.cmdbuff,
3422 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3423 ccline.cmdpos -= i;
3424 len += i;
3425 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3426 }
3427# ifdef FEAT_ARABIC
3428 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3429 {
3430 /* Check the previous character for Arabic combining pair. */
3431 i = (*mb_head_off)(ccline.cmdbuff,
3432 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3433 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3434 + ccline.cmdpos - i), c))
3435 {
3436 ccline.cmdpos -= i;
3437 len += i;
3438 }
3439 else
3440 i = 0;
3441 }
3442# endif
3443 if (i != 0)
3444 {
3445 /* Also backup the cursor position. */
3446 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3447 ccline.cmdspos -= i;
3448 msg_col -= i;
3449 if (msg_col < 0)
3450 {
3451 msg_col += Columns;
3452 --msg_row;
3453 }
3454 }
3455 }
3456#endif
3457
3458 if (redraw && !cmd_silent)
3459 {
3460 msg_no_more = TRUE;
3461 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003462 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3464 /* Avoid clearing the rest of the line too often. */
3465 if (cmdline_row != i || ccline.overstrike)
3466 msg_clr_eos();
3467 msg_no_more = FALSE;
3468 }
3469#ifdef FEAT_FKMAP
3470 /*
3471 * If we are in Farsi command mode, the character input must be in
3472 * Insert mode. So do not advance the cmdpos.
3473 */
3474 if (!cmd_fkmap)
3475#endif
3476 {
3477 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003480 if (m < 0) /* overflow, Columns or Rows at weird value */
3481 m = MAXCOL;
3482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 else
3484 m = MAXCOL;
3485 for (i = 0; i < len; ++i)
3486 {
3487 c = cmdline_charsize(ccline.cmdpos);
3488#ifdef FEAT_MBYTE
3489 /* count ">" for a double-wide char that doesn't fit. */
3490 if (has_mbyte)
3491 correct_cmdspos(ccline.cmdpos, c);
3492#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003493 /* Stop cursor at the end of the screen, but do increment the
3494 * insert position, so that entering a very long command
3495 * works, even though you can't see it. */
3496 if (ccline.cmdspos + c < m)
3497 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#ifdef FEAT_MBYTE
3499 if (has_mbyte)
3500 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003501 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (c > len - i - 1)
3503 c = len - i - 1;
3504 ccline.cmdpos += c;
3505 i += c;
3506 }
3507#endif
3508 ++ccline.cmdpos;
3509 }
3510 }
3511 }
3512 if (redraw)
3513 msg_check();
3514 return retval;
3515}
3516
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003517static struct cmdline_info prev_ccline;
3518static int prev_ccline_used = FALSE;
3519
3520/*
3521 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3522 * and overwrite it. But get_cmdline_str() may need it, thus make it
3523 * available globally in prev_ccline.
3524 */
3525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003526save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003527{
3528 if (!prev_ccline_used)
3529 {
3530 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3531 prev_ccline_used = TRUE;
3532 }
3533 *ccp = prev_ccline;
3534 prev_ccline = ccline;
3535 ccline.cmdbuff = NULL;
3536 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003537 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003538}
3539
3540/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003541 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003542 */
3543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003544restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003545{
3546 ccline = prev_ccline;
3547 prev_ccline = *ccp;
3548}
3549
Bram Moolenaar5a305422006-04-28 22:38:25 +00003550#if defined(FEAT_EVAL) || defined(PROTO)
3551/*
3552 * Save the command line into allocated memory. Returns a pointer to be
3553 * passed to restore_cmdline_alloc() later.
3554 * Returns NULL when failed.
3555 */
3556 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003557save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003558{
3559 struct cmdline_info *p;
3560
3561 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3562 if (p != NULL)
3563 save_cmdline(p);
3564 return (char_u *)p;
3565}
3566
3567/*
3568 * Restore the command line from the return value of save_cmdline_alloc().
3569 */
3570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003571restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003572{
3573 if (p != NULL)
3574 {
3575 restore_cmdline((struct cmdline_info *)p);
3576 vim_free(p);
3577 }
3578}
3579#endif
3580
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003582 * Paste a yank register into the command line.
3583 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003584 * insert_reg() can't be used here, because special characters from the
3585 * register contents will be interpreted as commands.
3586 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003587 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003588 */
3589 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003590cmdline_paste(
3591 int regname,
3592 int literally, /* Insert text literally instead of "as typed" */
3593 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003594{
3595 long i;
3596 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003597 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003598 int allocated;
3599 struct cmdline_info save_ccline;
3600
3601 /* check for valid regname; also accept special characters for CTRL-R in
3602 * the command line */
3603 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003604 && regname != Ctrl_A && regname != Ctrl_L
3605 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003606 return FAIL;
3607
3608 /* A register containing CTRL-R can cause an endless loop. Allow using
3609 * CTRL-C to break the loop. */
3610 line_breakcheck();
3611 if (got_int)
3612 return FAIL;
3613
3614#ifdef FEAT_CLIPBOARD
3615 regname = may_get_selection(regname);
3616#endif
3617
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003618 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003619 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003620 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003621 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003622 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003623 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003624 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003625
3626 if (i)
3627 {
3628 /* Got the value of a special register in "arg". */
3629 if (arg == NULL)
3630 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003631
3632 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3633 * part of the word. */
3634 p = arg;
3635 if (p_is && regname == Ctrl_W)
3636 {
3637 char_u *w;
3638 int len;
3639
3640 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003641 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003642 {
3643#ifdef FEAT_MBYTE
3644 if (has_mbyte)
3645 {
3646 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3647 if (!vim_iswordc(mb_ptr2char(w - len)))
3648 break;
3649 w -= len;
3650 }
3651 else
3652#endif
3653 {
3654 if (!vim_iswordc(w[-1]))
3655 break;
3656 --w;
3657 }
3658 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003659 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003660 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3661 p += len;
3662 }
3663
3664 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003665 if (allocated)
3666 vim_free(arg);
3667 return OK;
3668 }
3669
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003670 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003671}
3672
3673/*
3674 * Put a string on the command line.
3675 * When "literally" is TRUE, insert literally.
3676 * When "literally" is FALSE, insert as typed, but don't leave the command
3677 * line.
3678 */
3679 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003680cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003681{
3682 int c, cv;
3683
3684 if (literally)
3685 put_on_cmdline(s, -1, TRUE);
3686 else
3687 while (*s != NUL)
3688 {
3689 cv = *s;
3690 if (cv == Ctrl_V && s[1])
3691 ++s;
3692#ifdef FEAT_MBYTE
3693 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003694 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003695 else
3696#endif
3697 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003698 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3699 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003700#ifdef UNIX
3701 || c == intr_char
3702#endif
3703 || (c == Ctrl_BSL && *s == Ctrl_N))
3704 stuffcharReadbuff(Ctrl_V);
3705 stuffcharReadbuff(c);
3706 }
3707}
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709#ifdef FEAT_WILDMENU
3710/*
3711 * Delete characters on the command line, from "from" to the current
3712 * position.
3713 */
3714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003715cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
3717 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3718 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3719 ccline.cmdlen -= ccline.cmdpos - from;
3720 ccline.cmdpos = from;
3721}
3722#endif
3723
3724/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003725 * This function is called when the screen size changes and with incremental
3726 * search and in other situations where the command line may have been
3727 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 */
3729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003730redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003732 redrawcmdline_ex(TRUE);
3733}
3734
3735 void
3736redrawcmdline_ex(int do_compute_cmdrow)
3737{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (cmd_silent)
3739 return;
3740 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003741 if (do_compute_cmdrow)
3742 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 redrawcmd();
3744 cursorcmd();
3745}
3746
3747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003748redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
3750 int i;
3751
3752 if (cmd_silent)
3753 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003754 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 msg_putchar(ccline.cmdfirstc);
3756 if (ccline.cmdprompt != NULL)
3757 {
3758 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3759 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3760 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003761 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 --ccline.cmdindent;
3763 }
3764 else
3765 for (i = ccline.cmdindent; i > 0; --i)
3766 msg_putchar(' ');
3767}
3768
3769/*
3770 * Redraw what is currently on the command line.
3771 */
3772 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003773redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 if (cmd_silent)
3776 return;
3777
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003778 /* when 'incsearch' is set there may be no command line while redrawing */
3779 if (ccline.cmdbuff == NULL)
3780 {
3781 windgoto(cmdline_row, 0);
3782 msg_clr_eos();
3783 return;
3784 }
3785
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 msg_start();
3787 redrawcmdprompt();
3788
3789 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3790 msg_no_more = TRUE;
3791 draw_cmdline(0, ccline.cmdlen);
3792 msg_clr_eos();
3793 msg_no_more = FALSE;
3794
3795 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003796 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003797 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 /*
3800 * An emsg() before may have set msg_scroll. This is used in normal mode,
3801 * in cmdline mode we can reset them now.
3802 */
3803 msg_scroll = FALSE; /* next message overwrites cmdline */
3804
3805 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3806 * in cmdline mode */
3807 skip_redraw = FALSE;
3808}
3809
3810 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003811compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003813 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 cmdline_row = Rows - 1;
3815 else
3816 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003817 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818}
3819
3820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003821cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
3823 if (cmd_silent)
3824 return;
3825
3826#ifdef FEAT_RIGHTLEFT
3827 if (cmdmsg_rl)
3828 {
3829 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3830 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3831 if (msg_row <= 0)
3832 msg_row = Rows - 1;
3833 }
3834 else
3835#endif
3836 {
3837 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3838 msg_col = ccline.cmdspos % (int)Columns;
3839 if (msg_row >= Rows)
3840 msg_row = Rows - 1;
3841 }
3842
3843 windgoto(msg_row, msg_col);
3844#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003845 if (p_imst == IM_ON_THE_SPOT)
3846 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#endif
3848#ifdef MCH_CURSOR_SHAPE
3849 mch_update_cursor();
3850#endif
3851}
3852
3853 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003854gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 msg_start();
3857#ifdef FEAT_RIGHTLEFT
3858 if (cmdmsg_rl)
3859 msg_col = Columns - 1;
3860 else
3861#endif
3862 msg_col = 0; /* always start in column 0 */
3863 if (clr) /* clear the bottom line(s) */
3864 msg_clr_eos(); /* will reset clear_cmdline */
3865 windgoto(cmdline_row, 0);
3866}
3867
3868/*
3869 * Check the word in front of the cursor for an abbreviation.
3870 * Called when the non-id character "c" has been entered.
3871 * When an abbreviation is recognized it is removed from the text with
3872 * backspaces and the replacement string is inserted, followed by "c".
3873 */
3874 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003875ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003877 int spos = 0;
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3880 return FALSE;
3881
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003882 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3883 * Actually accepts any mark. */
3884 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3885 spos++;
3886 if (ccline.cmdlen - spos > 5
3887 && ccline.cmdbuff[spos] == '\''
3888 && ccline.cmdbuff[spos + 2] == ','
3889 && ccline.cmdbuff[spos + 3] == '\'')
3890 spos += 5;
3891 else
3892 /* check abbreviation from the beginning of the commandline */
3893 spos = 0;
3894
3895 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896}
3897
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003898#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3899 static int
3900#ifdef __BORLANDC__
3901_RTLENTRYF
3902#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003903sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003904{
3905 char_u *p1 = *(char_u **)s1;
3906 char_u *p2 = *(char_u **)s2;
3907
3908 if (*p1 != '<' && *p2 == '<') return -1;
3909 if (*p1 == '<' && *p2 != '<') return 1;
3910 return STRCMP(p1, p2);
3911}
3912#endif
3913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914/*
3915 * Return FAIL if this is not an appropriate context in which to do
3916 * completion of anything, return OK if it is (even if there are no matches).
3917 * For the caller, this means that the character is just passed through like a
3918 * normal character (instead of being expanded). This allows :s/^I^D etc.
3919 */
3920 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003921nextwild(
3922 expand_T *xp,
3923 int type,
3924 int options, /* extra options for ExpandOne() */
3925 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927 int i, j;
3928 char_u *p1;
3929 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 int difflen;
3931 int v;
3932
3933 if (xp->xp_numfiles == -1)
3934 {
3935 set_expand_context(xp);
3936 cmd_showtail = expand_showtail(xp);
3937 }
3938
3939 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3940 {
3941 beep_flush();
3942 return OK; /* Something illegal on command line */
3943 }
3944 if (xp->xp_context == EXPAND_NOTHING)
3945 {
3946 /* Caller can use the character as a normal char instead */
3947 return FAIL;
3948 }
3949
3950 MSG_PUTS("..."); /* show that we are busy */
3951 out_flush();
3952
3953 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003954 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
3956 if (type == WILD_NEXT || type == WILD_PREV)
3957 {
3958 /*
3959 * Get next/previous match for a previous expanded pattern.
3960 */
3961 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3962 }
3963 else
3964 {
3965 /*
3966 * Translate string into pattern and expand it.
3967 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003968 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3969 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 p2 = NULL;
3971 else
3972 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003973 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003974 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3975 if (escape)
3976 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003977
3978 if (p_wic)
3979 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003980 p2 = ExpandOne(xp, p1,
3981 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003982 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003984 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (p2 != NULL && type == WILD_LONGEST)
3986 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003987 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (ccline.cmdbuff[i + j] == '*'
3989 || ccline.cmdbuff[i + j] == '?')
3990 break;
3991 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003992 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 }
3995 }
3996
3997 if (p2 != NULL && !got_int)
3998 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003999 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004000 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004002 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 xp->xp_pattern = ccline.cmdbuff + i;
4004 }
4005 else
4006 v = OK;
4007 if (v == OK)
4008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004009 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4010 &ccline.cmdbuff[ccline.cmdpos],
4011 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4012 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 ccline.cmdlen += difflen;
4014 ccline.cmdpos += difflen;
4015 }
4016 }
4017 vim_free(p2);
4018
4019 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004020 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 /* When expanding a ":map" command and no matches are found, assume that
4023 * the key is supposed to be inserted literally */
4024 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4025 return FAIL;
4026
4027 if (xp->xp_numfiles <= 0 && p2 == NULL)
4028 beep_flush();
4029 else if (xp->xp_numfiles == 1)
4030 /* free expanded pattern */
4031 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4032
4033 return OK;
4034}
4035
4036/*
4037 * Do wildcard expansion on the string 'str'.
4038 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004039 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 * Return NULL for failure.
4041 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004042 * "orig" is the originally expanded string, copied to allocated memory. It
4043 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4044 * WILD_PREV "orig" should be NULL.
4045 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004046 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4047 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 *
4049 * mode = WILD_FREE: just free previously expanded matches
4050 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4051 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4052 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4053 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4054 * mode = WILD_ALL: return all matches concatenated
4055 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004056 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 *
4058 * options = WILD_LIST_NOTFOUND: list entries without a match
4059 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4060 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4061 * options = WILD_NO_BEEP: Don't beep for multiple matches
4062 * options = WILD_ADD_SLASH: add a slash after directory names
4063 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4064 * options = WILD_SILENT: don't print warning messages
4065 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004066 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 *
4068 * The variables xp->xp_context and xp->xp_backslash must have been set!
4069 */
4070 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004071ExpandOne(
4072 expand_T *xp,
4073 char_u *str,
4074 char_u *orig, /* allocated copy of original of expanded string */
4075 int options,
4076 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077{
4078 char_u *ss = NULL;
4079 static int findex;
4080 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004081 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 int i;
4083 long_u len;
4084 int non_suf_match; /* number without matching suffix */
4085
4086 /*
4087 * first handle the case of using an old match
4088 */
4089 if (mode == WILD_NEXT || mode == WILD_PREV)
4090 {
4091 if (xp->xp_numfiles > 0)
4092 {
4093 if (mode == WILD_PREV)
4094 {
4095 if (findex == -1)
4096 findex = xp->xp_numfiles;
4097 --findex;
4098 }
4099 else /* mode == WILD_NEXT */
4100 ++findex;
4101
4102 /*
4103 * When wrapping around, return the original string, set findex to
4104 * -1.
4105 */
4106 if (findex < 0)
4107 {
4108 if (orig_save == NULL)
4109 findex = xp->xp_numfiles - 1;
4110 else
4111 findex = -1;
4112 }
4113 if (findex >= xp->xp_numfiles)
4114 {
4115 if (orig_save == NULL)
4116 findex = 0;
4117 else
4118 findex = -1;
4119 }
4120#ifdef FEAT_WILDMENU
4121 if (p_wmnu)
4122 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4123 findex, cmd_showtail);
4124#endif
4125 if (findex == -1)
4126 return vim_strsave(orig_save);
4127 return vim_strsave(xp->xp_files[findex]);
4128 }
4129 else
4130 return NULL;
4131 }
4132
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004133 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4135 {
4136 FreeWild(xp->xp_numfiles, xp->xp_files);
4137 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004138 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 findex = 0;
4141
4142 if (mode == WILD_FREE) /* only release file name */
4143 return NULL;
4144
4145 if (xp->xp_numfiles == -1)
4146 {
4147 vim_free(orig_save);
4148 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004149 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
4151 /*
4152 * Do the expansion.
4153 */
4154 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4155 options) == FAIL)
4156 {
4157#ifdef FNAME_ILLEGAL
4158 /* Illegal file name has been silently skipped. But when there
4159 * are wildcards, the real problem is that there was no match,
4160 * causing the pattern to be added, which has illegal characters.
4161 */
4162 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4163 EMSG2(_(e_nomatch2), str);
4164#endif
4165 }
4166 else if (xp->xp_numfiles == 0)
4167 {
4168 if (!(options & WILD_SILENT))
4169 EMSG2(_(e_nomatch2), str);
4170 }
4171 else
4172 {
4173 /* Escape the matches for use on the command line. */
4174 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4175
4176 /*
4177 * Check for matching suffixes in file names.
4178 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004179 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4180 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
4182 if (xp->xp_numfiles)
4183 non_suf_match = xp->xp_numfiles;
4184 else
4185 non_suf_match = 1;
4186 if ((xp->xp_context == EXPAND_FILES
4187 || xp->xp_context == EXPAND_DIRECTORIES)
4188 && xp->xp_numfiles > 1)
4189 {
4190 /*
4191 * More than one match; check suffix.
4192 * The files will have been sorted on matching suffix in
4193 * expand_wildcards, only need to check the first two.
4194 */
4195 non_suf_match = 0;
4196 for (i = 0; i < 2; ++i)
4197 if (match_suffix(xp->xp_files[i]))
4198 ++non_suf_match;
4199 }
4200 if (non_suf_match != 1)
4201 {
4202 /* Can we ever get here unless it's while expanding
4203 * interactively? If not, we can get rid of this all
4204 * together. Don't really want to wait for this message
4205 * (and possibly have to hit return to continue!).
4206 */
4207 if (!(options & WILD_SILENT))
4208 EMSG(_(e_toomany));
4209 else if (!(options & WILD_NO_BEEP))
4210 beep_flush();
4211 }
4212 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4213 ss = vim_strsave(xp->xp_files[0]);
4214 }
4215 }
4216 }
4217
4218 /* Find longest common part */
4219 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4220 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004221 int mb_len = 1;
4222 int c0, ci;
4223
4224 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004226#ifdef FEAT_MBYTE
4227 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004229 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4230 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4231 }
4232 else
4233#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004234 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004235 for (i = 1; i < xp->xp_numfiles; ++i)
4236 {
4237#ifdef FEAT_MBYTE
4238 if (has_mbyte)
4239 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4240 else
4241#endif
4242 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004243 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004245 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004246 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004248 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 break;
4250 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004251 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 break;
4253 }
4254 if (i < xp->xp_numfiles)
4255 {
4256 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004257 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 break;
4259 }
4260 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 ss = alloc((unsigned)len + 1);
4263 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004264 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 findex = -1; /* next p_wc gets first one */
4266 }
4267
4268 /* Concatenate all matching names */
4269 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4270 {
4271 len = 0;
4272 for (i = 0; i < xp->xp_numfiles; ++i)
4273 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4274 ss = lalloc(len, TRUE);
4275 if (ss != NULL)
4276 {
4277 *ss = NUL;
4278 for (i = 0; i < xp->xp_numfiles; ++i)
4279 {
4280 STRCAT(ss, xp->xp_files[i]);
4281 if (i != xp->xp_numfiles - 1)
4282 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4283 }
4284 }
4285 }
4286
4287 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4288 ExpandCleanup(xp);
4289
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004290 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004291 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004292 vim_free(orig);
4293
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 return ss;
4295}
4296
4297/*
4298 * Prepare an expand structure for use.
4299 */
4300 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004301ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004303 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004304 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004306#ifndef BACKSLASH_IN_FILENAME
4307 xp->xp_shell = FALSE;
4308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 xp->xp_numfiles = -1;
4310 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004311#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4312 xp->xp_arg = NULL;
4313#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004314 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315}
4316
4317/*
4318 * Cleanup an expand structure after use.
4319 */
4320 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004321ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
4323 if (xp->xp_numfiles >= 0)
4324 {
4325 FreeWild(xp->xp_numfiles, xp->xp_files);
4326 xp->xp_numfiles = -1;
4327 }
4328}
4329
4330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004331ExpandEscape(
4332 expand_T *xp,
4333 char_u *str,
4334 int numfiles,
4335 char_u **files,
4336 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337{
4338 int i;
4339 char_u *p;
4340
4341 /*
4342 * May change home directory back to "~"
4343 */
4344 if (options & WILD_HOME_REPLACE)
4345 tilde_replace(str, numfiles, files);
4346
4347 if (options & WILD_ESCAPE)
4348 {
4349 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004350 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004351 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 || xp->xp_context == EXPAND_BUFFERS
4353 || xp->xp_context == EXPAND_DIRECTORIES)
4354 {
4355 /*
4356 * Insert a backslash into a file name before a space, \, %, #
4357 * and wildmatch characters, except '~'.
4358 */
4359 for (i = 0; i < numfiles; ++i)
4360 {
4361 /* for ":set path=" we need to escape spaces twice */
4362 if (xp->xp_backslash == XP_BS_THREE)
4363 {
4364 p = vim_strsave_escaped(files[i], (char_u *)" ");
4365 if (p != NULL)
4366 {
4367 vim_free(files[i]);
4368 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004369#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 p = vim_strsave_escaped(files[i], (char_u *)" ");
4371 if (p != NULL)
4372 {
4373 vim_free(files[i]);
4374 files[i] = p;
4375 }
4376#endif
4377 }
4378 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004379#ifdef BACKSLASH_IN_FILENAME
4380 p = vim_strsave_fnameescape(files[i], FALSE);
4381#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004382 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (p != NULL)
4385 {
4386 vim_free(files[i]);
4387 files[i] = p;
4388 }
4389
4390 /* If 'str' starts with "\~", replace "~" at start of
4391 * files[i] with "\~". */
4392 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004393 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004396
4397 /* If the first file starts with a '+' escape it. Otherwise it
4398 * could be seen as "+cmd". */
4399 if (*files[0] == '+')
4400 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 else if (xp->xp_context == EXPAND_TAGS)
4403 {
4404 /*
4405 * Insert a backslash before characters in a tag name that
4406 * would terminate the ":tag" command.
4407 */
4408 for (i = 0; i < numfiles; ++i)
4409 {
4410 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4411 if (p != NULL)
4412 {
4413 vim_free(files[i]);
4414 files[i] = p;
4415 }
4416 }
4417 }
4418 }
4419}
4420
4421/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004422 * Escape special characters in "fname" for when used as a file name argument
4423 * after a Vim command, or, when "shell" is non-zero, a shell command.
4424 * Returns the result in allocated memory.
4425 */
4426 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004427vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004428{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004429 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004430#ifdef BACKSLASH_IN_FILENAME
4431 char_u buf[20];
4432 int j = 0;
4433
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004434 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004435 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004436 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004437 buf[j++] = *p;
4438 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004439 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004440#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004441 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4442 if (shell && csh_like_shell() && p != NULL)
4443 {
4444 char_u *s;
4445
4446 /* For csh and similar shells need to put two backslashes before '!'.
4447 * One is taken by Vim, one by the shell. */
4448 s = vim_strsave_escaped(p, (char_u *)"!");
4449 vim_free(p);
4450 p = s;
4451 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004452#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004453
4454 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4455 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004456 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004457 escape_fname(&p);
4458
4459 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004460}
4461
4462/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004463 * Put a backslash before the file name in "pp", which is in allocated memory.
4464 */
4465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004466escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004467{
4468 char_u *p;
4469
4470 p = alloc((unsigned)(STRLEN(*pp) + 2));
4471 if (p != NULL)
4472 {
4473 p[0] = '\\';
4474 STRCPY(p + 1, *pp);
4475 vim_free(*pp);
4476 *pp = p;
4477 }
4478}
4479
4480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 * For each file name in files[num_files]:
4482 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4483 */
4484 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004485tilde_replace(
4486 char_u *orig_pat,
4487 int num_files,
4488 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
4490 int i;
4491 char_u *p;
4492
4493 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4494 {
4495 for (i = 0; i < num_files; ++i)
4496 {
4497 p = home_replace_save(NULL, files[i]);
4498 if (p != NULL)
4499 {
4500 vim_free(files[i]);
4501 files[i] = p;
4502 }
4503 }
4504 }
4505}
4506
4507/*
4508 * Show all matches for completion on the command line.
4509 * Returns EXPAND_NOTHING when the character that triggered expansion should
4510 * be inserted like a normal character.
4511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004513showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514{
4515#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4516 int num_files;
4517 char_u **files_found;
4518 int i, j, k;
4519 int maxlen;
4520 int lines;
4521 int columns;
4522 char_u *p;
4523 int lastlen;
4524 int attr;
4525 int showtail;
4526
4527 if (xp->xp_numfiles == -1)
4528 {
4529 set_expand_context(xp);
4530 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4531 &num_files, &files_found);
4532 showtail = expand_showtail(xp);
4533 if (i != EXPAND_OK)
4534 return i;
4535
4536 }
4537 else
4538 {
4539 num_files = xp->xp_numfiles;
4540 files_found = xp->xp_files;
4541 showtail = cmd_showtail;
4542 }
4543
4544#ifdef FEAT_WILDMENU
4545 if (!wildmenu)
4546 {
4547#endif
4548 msg_didany = FALSE; /* lines_left will be set */
4549 msg_start(); /* prepare for paging */
4550 msg_putchar('\n');
4551 out_flush();
4552 cmdline_row = msg_row;
4553 msg_didany = FALSE; /* lines_left will be set again */
4554 msg_start(); /* prepare for paging */
4555#ifdef FEAT_WILDMENU
4556 }
4557#endif
4558
4559 if (got_int)
4560 got_int = FALSE; /* only int. the completion, not the cmd line */
4561#ifdef FEAT_WILDMENU
4562 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004563 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564#endif
4565 else
4566 {
4567 /* find the length of the longest file name */
4568 maxlen = 0;
4569 for (i = 0; i < num_files; ++i)
4570 {
4571 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004572 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 || xp->xp_context == EXPAND_BUFFERS))
4574 {
4575 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4576 j = vim_strsize(NameBuff);
4577 }
4578 else
4579 j = vim_strsize(L_SHOWFILE(i));
4580 if (j > maxlen)
4581 maxlen = j;
4582 }
4583
4584 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4585 lines = num_files;
4586 else
4587 {
4588 /* compute the number of columns and lines for the listing */
4589 maxlen += 2; /* two spaces between file names */
4590 columns = ((int)Columns + 2) / maxlen;
4591 if (columns < 1)
4592 columns = 1;
4593 lines = (num_files + columns - 1) / columns;
4594 }
4595
Bram Moolenaar8820b482017-03-16 17:23:31 +01004596 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4599 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004600 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 msg_clr_eos();
4602 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004603 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605
4606 /* list the files line by line */
4607 for (i = 0; i < lines; ++i)
4608 {
4609 lastlen = 999;
4610 for (k = i; k < num_files; k += lines)
4611 {
4612 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4613 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004614 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 p = files_found[k] + STRLEN(files_found[k]) + 1;
4616 msg_advance(maxlen + 1);
4617 msg_puts(p);
4618 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004619 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 break;
4621 }
4622 for (j = maxlen - lastlen; --j >= 0; )
4623 msg_putchar(' ');
4624 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004625 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 || xp->xp_context == EXPAND_BUFFERS)
4627 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004628 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004629 if (xp->xp_numfiles != -1)
4630 {
4631 char_u *halved_slash;
4632 char_u *exp_path;
4633
4634 /* Expansion was done before and special characters
4635 * were escaped, need to halve backslashes. Also
4636 * $HOME has been replaced with ~/. */
4637 exp_path = expand_env_save_opt(files_found[k], TRUE);
4638 halved_slash = backslash_halve_save(
4639 exp_path != NULL ? exp_path : files_found[k]);
4640 j = mch_isdir(halved_slash != NULL ? halved_slash
4641 : files_found[k]);
4642 vim_free(exp_path);
4643 vim_free(halved_slash);
4644 }
4645 else
4646 /* Expansion was done here, file names are literal. */
4647 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (showtail)
4649 p = L_SHOWFILE(k);
4650 else
4651 {
4652 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4653 TRUE);
4654 p = NameBuff;
4655 }
4656 }
4657 else
4658 {
4659 j = FALSE;
4660 p = L_SHOWFILE(k);
4661 }
4662 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4663 }
4664 if (msg_col > 0) /* when not wrapped around */
4665 {
4666 msg_clr_eos();
4667 msg_putchar('\n');
4668 }
4669 out_flush(); /* show one line at a time */
4670 if (got_int)
4671 {
4672 got_int = FALSE;
4673 break;
4674 }
4675 }
4676
4677 /*
4678 * we redraw the command below the lines that we have just listed
4679 * This is a bit tricky, but it saves a lot of screen updating.
4680 */
4681 cmdline_row = msg_row; /* will put it back later */
4682 }
4683
4684 if (xp->xp_numfiles == -1)
4685 FreeWild(num_files, files_found);
4686
4687 return EXPAND_OK;
4688}
4689
4690/*
4691 * Private gettail for showmatches() (and win_redr_status_matches()):
4692 * Find tail of file name path, but ignore trailing "/".
4693 */
4694 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004695sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 char_u *p;
4698 char_u *t = s;
4699 int had_sep = FALSE;
4700
4701 for (p = s; *p != NUL; )
4702 {
4703 if (vim_ispathsep(*p)
4704#ifdef BACKSLASH_IN_FILENAME
4705 && !rem_backslash(p)
4706#endif
4707 )
4708 had_sep = TRUE;
4709 else if (had_sep)
4710 {
4711 t = p;
4712 had_sep = FALSE;
4713 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004714 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716 return t;
4717}
4718
4719/*
4720 * Return TRUE if we only need to show the tail of completion matches.
4721 * When not completing file names or there is a wildcard in the path FALSE is
4722 * returned.
4723 */
4724 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004725expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 char_u *s;
4728 char_u *end;
4729
4730 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004731 if (xp->xp_context != EXPAND_FILES
4732 && xp->xp_context != EXPAND_SHELLCMD
4733 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 return FALSE;
4735
4736 end = gettail(xp->xp_pattern);
4737 if (end == xp->xp_pattern) /* there is no path separator */
4738 return FALSE;
4739
4740 for (s = xp->xp_pattern; s < end; s++)
4741 {
4742 /* Skip escaped wildcards. Only when the backslash is not a path
4743 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4744 if (rem_backslash(s))
4745 ++s;
4746 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4747 return FALSE;
4748 }
4749 return TRUE;
4750}
4751
4752/*
4753 * Prepare a string for expansion.
4754 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004755 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 * When expanding other names: The string will be used with regcomp(). Copy
4757 * the name into allocated memory and prepend "^".
4758 */
4759 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004760addstar(
4761 char_u *fname,
4762 int len,
4763 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 char_u *retval;
4766 int i, j;
4767 int new_len;
4768 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004769 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004771 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004772 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004773 && context != EXPAND_SHELLCMD
4774 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 /*
4777 * Matching will be done internally (on something other than files).
4778 * So we convert the file-matching-type wildcards into our kind for
4779 * use with vim_regcomp(). First work out how long it will be:
4780 */
4781
4782 /* For help tags the translation is done in find_help_tags().
4783 * For a tag pattern starting with "/" no translation is needed. */
4784 if (context == EXPAND_HELP
4785 || context == EXPAND_COLORS
4786 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004787 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004788 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004789 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004790 || ((context == EXPAND_TAGS_LISTFILES
4791 || context == EXPAND_TAGS)
4792 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 retval = vim_strnsave(fname, len);
4794 else
4795 {
4796 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4797 for (i = 0; i < len; i++)
4798 {
4799 if (fname[i] == '*' || fname[i] == '~')
4800 new_len++; /* '*' needs to be replaced by ".*"
4801 '~' needs to be replaced by "\~" */
4802
4803 /* Buffer names are like file names. "." should be literal */
4804 if (context == EXPAND_BUFFERS && fname[i] == '.')
4805 new_len++; /* "." becomes "\." */
4806
4807 /* Custom expansion takes care of special things, match
4808 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004809 if ((context == EXPAND_USER_DEFINED
4810 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 new_len++; /* '\' becomes "\\" */
4812 }
4813 retval = alloc(new_len);
4814 if (retval != NULL)
4815 {
4816 retval[0] = '^';
4817 j = 1;
4818 for (i = 0; i < len; i++, j++)
4819 {
4820 /* Skip backslash. But why? At least keep it for custom
4821 * expansion. */
4822 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004823 && context != EXPAND_USER_LIST
4824 && fname[i] == '\\'
4825 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 break;
4827
4828 switch (fname[i])
4829 {
4830 case '*': retval[j++] = '.';
4831 break;
4832 case '~': retval[j++] = '\\';
4833 break;
4834 case '?': retval[j] = '.';
4835 continue;
4836 case '.': if (context == EXPAND_BUFFERS)
4837 retval[j++] = '\\';
4838 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004839 case '\\': if (context == EXPAND_USER_DEFINED
4840 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 retval[j++] = '\\';
4842 break;
4843 }
4844 retval[j] = fname[i];
4845 }
4846 retval[j] = NUL;
4847 }
4848 }
4849 }
4850 else
4851 {
4852 retval = alloc(len + 4);
4853 if (retval != NULL)
4854 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004855 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
4857 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004858 * Don't add a star to *, ~, ~user, $var or `cmd`.
4859 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 * ~ would be at the start of the file name, but not the tail.
4861 * $ could be anywhere in the tail.
4862 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004863 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 */
4865 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004866 ends_in_star = (len > 0 && retval[len - 1] == '*');
4867#ifndef BACKSLASH_IN_FILENAME
4868 for (i = len - 2; i >= 0; --i)
4869 {
4870 if (retval[i] != '\\')
4871 break;
4872 ends_in_star = !ends_in_star;
4873 }
4874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004876 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 && vim_strchr(tail, '$') == NULL
4878 && vim_strchr(retval, '`') == NULL)
4879 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004880 else if (len > 0 && retval[len - 1] == '$')
4881 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 retval[len] = NUL;
4883 }
4884 }
4885 return retval;
4886}
4887
4888/*
4889 * Must parse the command line so far to work out what context we are in.
4890 * Completion can then be done based on that context.
4891 * This routine sets the variables:
4892 * xp->xp_pattern The start of the pattern to be expanded within
4893 * the command line (ends at the cursor).
4894 * xp->xp_context The type of thing to expand. Will be one of:
4895 *
4896 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4897 * the command line, like an unknown command. Caller
4898 * should beep.
4899 * EXPAND_NOTHING Unrecognised context for completion, use char like
4900 * a normal char, rather than for completion. eg
4901 * :s/^I/
4902 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4903 * it.
4904 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4905 * EXPAND_FILES After command with XFILE set, or after setting
4906 * with P_EXPAND set. eg :e ^I, :w>>^I
4907 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4908 * when we know only directories are of interest. eg
4909 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004910 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4912 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4913 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4914 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4915 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4916 * EXPAND_EVENTS Complete event names
4917 * EXPAND_SYNTAX Complete :syntax command arguments
4918 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4919 * EXPAND_AUGROUP Complete autocommand group names
4920 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4921 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4922 * eg :unmap a^I , :cunab x^I
4923 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4924 * eg :call sub^I
4925 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4926 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4927 * names in expressions, eg :while s^I
4928 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004929 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 */
4931 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004932set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004934 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (ccline.cmdfirstc != ':'
4936#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004937 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004938 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#endif
4940 )
4941 {
4942 xp->xp_context = EXPAND_NOTHING;
4943 return;
4944 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004945 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946}
4947
4948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004949set_cmd_context(
4950 expand_T *xp,
4951 char_u *str, /* start of command line */
4952 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004953 int col, /* position of cursor */
4954 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955{
4956 int old_char = NUL;
4957 char_u *nextcomm;
4958
4959 /*
4960 * Avoid a UMR warning from Purify, only save the character if it has been
4961 * written before.
4962 */
4963 if (col < len)
4964 old_char = str[col];
4965 str[col] = NUL;
4966 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004967
4968#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004969 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004970 {
4971# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004972 /* pass CMD_SIZE because there is no real command */
4973 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004974# endif
4975 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004976 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004977 {
4978 xp->xp_context = ccline.xp_context;
4979 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004980# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004981 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004982# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004983 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004984 else
4985#endif
4986 while (nextcomm != NULL)
4987 nextcomm = set_one_cmd_context(xp, nextcomm);
4988
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004989 /* Store the string here so that call_user_expand_func() can get to them
4990 * easily. */
4991 xp->xp_line = str;
4992 xp->xp_col = col;
4993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 str[col] = old_char;
4995}
4996
4997/*
4998 * Expand the command line "str" from context "xp".
4999 * "xp" must have been set by set_cmd_context().
5000 * xp->xp_pattern points into "str", to where the text that is to be expanded
5001 * starts.
5002 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
5003 * cursor.
5004 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
5005 * key that triggered expansion literally.
5006 * Returns EXPAND_OK otherwise.
5007 */
5008 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005009expand_cmdline(
5010 expand_T *xp,
5011 char_u *str, /* start of command line */
5012 int col, /* position of cursor */
5013 int *matchcount, /* return: nr of matches */
5014 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
5016 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005017 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5020 {
5021 beep_flush();
5022 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5023 }
5024 if (xp->xp_context == EXPAND_NOTHING)
5025 {
5026 /* Caller can use the character as a normal char instead */
5027 return EXPAND_NOTHING;
5028 }
5029
5030 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005031 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5032 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (file_str == NULL)
5034 return EXPAND_UNSUCCESSFUL;
5035
Bram Moolenaar94950a92010-12-02 16:01:29 +01005036 if (p_wic)
5037 options += WILD_ICASE;
5038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005040 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
5042 *matchcount = 0;
5043 *matches = NULL;
5044 }
5045 vim_free(file_str);
5046
5047 return EXPAND_OK;
5048}
5049
5050#ifdef FEAT_MULTI_LANG
5051/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005052 * Cleanup matches for help tags:
5053 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5054 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005056static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005059cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 int i, j;
5062 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005063 char_u buf[4];
5064 char_u *p = buf;
5065
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005066 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005067 {
5068 *p++ = '@';
5069 *p++ = p_hlg[0];
5070 *p++ = p_hlg[1];
5071 }
5072 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 for (i = 0; i < num_file; ++i)
5075 {
5076 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005077 if (len <= 0)
5078 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005079 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 /* Sorting on priority means the same item in another language may
5082 * be anywhere. Search all items for a match up to the "@en". */
5083 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005084 if (j != i && (int)STRLEN(file[j]) == len + 3
5085 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005088 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 file[i][len] = NUL;
5090 }
5091 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005092
5093 if (*buf != NUL)
5094 for (i = 0; i < num_file; ++i)
5095 {
5096 len = (int)STRLEN(file[i]) - 3;
5097 if (len <= 0)
5098 continue;
5099 if (STRCMP(file[i] + len, buf) == 0)
5100 {
5101 /* remove the default language */
5102 file[i][len] = NUL;
5103 }
5104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105}
5106#endif
5107
5108/*
5109 * Do the expansion based on xp->xp_context and "pat".
5110 */
5111 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005112ExpandFromContext(
5113 expand_T *xp,
5114 char_u *pat,
5115 int *num_file,
5116 char_u ***file,
5117 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
5119#ifdef FEAT_CMDL_COMPL
5120 regmatch_T regmatch;
5121#endif
5122 int ret;
5123 int flags;
5124
5125 flags = EW_DIR; /* include directories */
5126 if (options & WILD_LIST_NOTFOUND)
5127 flags |= EW_NOTFOUND;
5128 if (options & WILD_ADD_SLASH)
5129 flags |= EW_ADDSLASH;
5130 if (options & WILD_KEEP_ALL)
5131 flags |= EW_KEEPALL;
5132 if (options & WILD_SILENT)
5133 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005134 if (options & WILD_ALLLINKS)
5135 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005137 if (xp->xp_context == EXPAND_FILES
5138 || xp->xp_context == EXPAND_DIRECTORIES
5139 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 /*
5142 * Expand file or directory names.
5143 */
5144 int free_pat = FALSE;
5145 int i;
5146
5147 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5148 * space */
5149 if (xp->xp_backslash != XP_BS_NONE)
5150 {
5151 free_pat = TRUE;
5152 pat = vim_strsave(pat);
5153 for (i = 0; pat[i]; ++i)
5154 if (pat[i] == '\\')
5155 {
5156 if (xp->xp_backslash == XP_BS_THREE
5157 && pat[i + 1] == '\\'
5158 && pat[i + 2] == '\\'
5159 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005160 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (xp->xp_backslash == XP_BS_ONE
5162 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005163 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 }
5166
5167 if (xp->xp_context == EXPAND_FILES)
5168 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005169 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5170 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 else
5172 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005173 if (options & WILD_ICASE)
5174 flags |= EW_ICASE;
5175
Bram Moolenaard7834d32009-12-02 16:14:36 +00005176 /* Expand wildcards, supporting %:h and the like. */
5177 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (free_pat)
5179 vim_free(pat);
5180 return ret;
5181 }
5182
5183 *file = (char_u **)"";
5184 *num_file = 0;
5185 if (xp->xp_context == EXPAND_HELP)
5186 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005187 /* With an empty argument we would get all the help tags, which is
5188 * very slow. Get matches for "help" instead. */
5189 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5190 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
5192#ifdef FEAT_MULTI_LANG
5193 cleanup_help_tags(*num_file, *file);
5194#endif
5195 return OK;
5196 }
5197 return FAIL;
5198 }
5199
5200#ifndef FEAT_CMDL_COMPL
5201 return FAIL;
5202#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005203 if (xp->xp_context == EXPAND_SHELLCMD)
5204 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (xp->xp_context == EXPAND_OLD_SETTING)
5206 return ExpandOldSetting(num_file, file);
5207 if (xp->xp_context == EXPAND_BUFFERS)
5208 return ExpandBufnames(pat, num_file, file, options);
5209 if (xp->xp_context == EXPAND_TAGS
5210 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5211 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5212 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005213 {
5214 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005215 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5216 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005219 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005220 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005221 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005222 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005223 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005224 {
5225 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005226 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005227 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005228 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005229 {
5230 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005231 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005232 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005233# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5234 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005235 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005236# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005237 if (xp->xp_context == EXPAND_PACKADD)
5238 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5241 if (regmatch.regprog == NULL)
5242 return FAIL;
5243
5244 /* set ignore-case according to p_ic, p_scs and pat */
5245 regmatch.rm_ic = ignorecase(pat);
5246
5247 if (xp->xp_context == EXPAND_SETTINGS
5248 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5249 ret = ExpandSettings(xp, &regmatch, num_file, file);
5250 else if (xp->xp_context == EXPAND_MAPPINGS)
5251 ret = ExpandMappings(&regmatch, num_file, file);
5252# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5253 else if (xp->xp_context == EXPAND_USER_DEFINED)
5254 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5255# endif
5256 else
5257 {
5258 static struct expgen
5259 {
5260 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005261 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005263 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 } tab[] =
5265 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005266 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5267 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005268 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005269 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005270#ifdef FEAT_CMDHIST
5271 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005274 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005275 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005276 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5277 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5278 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
5280#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005281 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5282 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5283 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5284 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
5286#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005287 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5288 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#endif
5290#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005291 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005293#ifdef FEAT_PROFILE
5294 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5295#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005296 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005297 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5298 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005299#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005300 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005301#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005302#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005303 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005304#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005305#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005306 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5309 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005310 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5311 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005313 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005314 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005315 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 };
5317 int i;
5318
5319 /*
5320 * Find a context in the table and call the ExpandGeneric() with the
5321 * right function to do the expansion.
5322 */
5323 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005324 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 if (xp->xp_context == tab[i].context)
5326 {
5327 if (tab[i].ic)
5328 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005329 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005330 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 break;
5332 }
5333 }
5334
Bram Moolenaar473de612013-06-08 18:19:48 +02005335 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 return ret;
5338#endif /* FEAT_CMDL_COMPL */
5339}
5340
5341#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5342/*
5343 * Expand a list of names.
5344 *
5345 * Generic function for command line completion. It calls a function to
5346 * obtain strings, one by one. The strings are matched against a regexp
5347 * program. Matching strings are copied into an array, which is returned.
5348 *
5349 * Returns OK when no problems encountered, FAIL for error (out of memory).
5350 */
5351 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352ExpandGeneric(
5353 expand_T *xp,
5354 regmatch_T *regmatch,
5355 int *num_file,
5356 char_u ***file,
5357 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 int i;
5362 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005363 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 char_u *str;
5365
5366 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005367 * round == 0: count the number of matching names
5368 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005370 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
5372 for (i = 0; ; ++i)
5373 {
5374 str = (*func)(xp, i);
5375 if (str == NULL) /* end of list */
5376 break;
5377 if (*str == NUL) /* skip empty strings */
5378 continue;
5379
5380 if (vim_regexec(regmatch, str, (colnr_T)0))
5381 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005382 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005384 if (escaped)
5385 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5386 else
5387 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 (*file)[count] = str;
5389#ifdef FEAT_MENU
5390 if (func == get_menu_names && str != NULL)
5391 {
5392 /* test for separator added by get_menu_names() */
5393 str += STRLEN(str) - 1;
5394 if (*str == '\001')
5395 *str = '.';
5396 }
5397#endif
5398 }
5399 ++count;
5400 }
5401 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005402 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
5404 if (count == 0)
5405 return OK;
5406 *num_file = count;
5407 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5408 if (*file == NULL)
5409 {
5410 *file = (char_u **)"";
5411 return FAIL;
5412 }
5413 count = 0;
5414 }
5415 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005416
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005417 /* Sort the results. Keep menu's in the specified order. */
5418 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005419 {
5420 if (xp->xp_context == EXPAND_EXPRESSION
5421 || xp->xp_context == EXPAND_FUNCTIONS
5422 || xp->xp_context == EXPAND_USER_FUNC)
5423 /* <SNR> functions should be sorted to the end. */
5424 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5425 sort_func_compare);
5426 else
5427 sort_strings(*file, *num_file);
5428 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005429
Bram Moolenaar4f688582007-07-24 12:34:30 +00005430#ifdef FEAT_CMDL_COMPL
5431 /* Reset the variables used for special highlight names expansion, so that
5432 * they don't show up when getting normal highlight names by ID. */
5433 reset_expand_highlight();
5434#endif
5435
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 return OK;
5437}
5438
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005439/*
5440 * Complete a shell command.
5441 * Returns FAIL or OK;
5442 */
5443 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444expand_shellcmd(
5445 char_u *filepat, /* pattern to match with command names */
5446 int *num_file, /* return: number of matches */
5447 char_u ***file, /* return: array with matches */
5448 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005449{
5450 char_u *pat;
5451 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005452 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005453 int mustfree = FALSE;
5454 garray_T ga;
5455 char_u *buf = alloc(MAXPATHL);
5456 size_t l;
5457 char_u *s, *e;
5458 int flags = flagsarg;
5459 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005460 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005461 hashtab_T found_ht;
5462 hashitem_T *hi;
5463 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005464
5465 if (buf == NULL)
5466 return FAIL;
5467
5468 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5469 * space */
5470 pat = vim_strsave(filepat);
5471 for (i = 0; pat[i]; ++i)
5472 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005473 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005474
Bram Moolenaarb5971142015-03-21 17:32:19 +01005475 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005476
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005477 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5478 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005479 path = (char_u *)".";
5480 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005481 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005482 /* For an absolute name we don't use $PATH. */
5483 if (!mch_isFullName(pat))
5484 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005485 if (path == NULL)
5486 path = (char_u *)"";
5487 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005488
5489 /*
5490 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005491 * collect them in "ga". When "." is not in $PATH also expand for the
5492 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005493 */
5494 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005495 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005496 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005497 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005498#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005499 e = vim_strchr(s, ';');
5500#else
5501 e = vim_strchr(s, ':');
5502#endif
5503 if (e == NULL)
5504 e = s + STRLEN(s);
5505
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005506 if (*s == NUL)
5507 {
5508 if (did_curdir)
5509 break;
5510 // Find directories in the current directory, path is empty.
5511 did_curdir = TRUE;
5512 flags |= EW_DIR;
5513 }
5514 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5515 {
5516 did_curdir = TRUE;
5517 flags |= EW_DIR;
5518 }
5519 else
5520 // Do not match directories inside a $PATH item.
5521 flags &= ~EW_DIR;
5522
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005523 l = e - s;
5524 if (l > MAXPATHL - 5)
5525 break;
5526 vim_strncpy(buf, s, l);
5527 add_pathsep(buf);
5528 l = STRLEN(buf);
5529 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5530
5531 /* Expand matches in one directory of $PATH. */
5532 ret = expand_wildcards(1, &buf, num_file, file, flags);
5533 if (ret == OK)
5534 {
5535 if (ga_grow(&ga, *num_file) == FAIL)
5536 FreeWild(*num_file, *file);
5537 else
5538 {
5539 for (i = 0; i < *num_file; ++i)
5540 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005541 char_u *name = (*file)[i];
5542
5543 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005544 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005545 // Check if this name was already found.
5546 hash = hash_hash(name + l);
5547 hi = hash_lookup(&found_ht, name + l, hash);
5548 if (HASHITEM_EMPTY(hi))
5549 {
5550 // Remove the path that was prepended.
5551 STRMOVE(name, name + l);
5552 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5553 hash_add_item(&found_ht, hi, name, hash);
5554 name = NULL;
5555 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005556 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005557 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005558 }
5559 vim_free(*file);
5560 }
5561 }
5562 if (*e != NUL)
5563 ++e;
5564 }
5565 *file = ga.ga_data;
5566 *num_file = ga.ga_len;
5567
5568 vim_free(buf);
5569 vim_free(pat);
5570 if (mustfree)
5571 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005572 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005573 return OK;
5574}
5575
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5578/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005579 * Call "user_expand_func()" to invoke a user defined Vim script function and
5580 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005582 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005583call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005584 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585 expand_T *xp,
5586 int *num_file,
5587 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005589 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005590 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005592 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005593 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005594 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
Bram Moolenaarb7515462013-06-29 12:58:33 +02005596 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005597 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 *num_file = 0;
5599 *file = NULL;
5600
Bram Moolenaarb7515462013-06-29 12:58:33 +02005601 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005602 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005603 keep = ccline.cmdbuff[ccline.cmdlen];
5604 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005605 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005606
Bram Moolenaarffa96842018-06-12 22:05:14 +02005607 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5608
5609 args[0].v_type = VAR_STRING;
5610 args[0].vval.v_string = pat;
5611 args[1].v_type = VAR_STRING;
5612 args[1].vval.v_string = xp->xp_line;
5613 args[2].v_type = VAR_NUMBER;
5614 args[2].vval.v_number = xp->xp_col;
5615 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005617 /* Save the cmdline, we don't know what the function may do. */
5618 save_ccline = ccline;
5619 ccline.cmdbuff = NULL;
5620 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005622
Bram Moolenaarded27a12018-08-01 19:06:03 +02005623 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005624
5625 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005627 if (ccline.cmdbuff != NULL)
5628 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005629
Bram Moolenaarffa96842018-06-12 22:05:14 +02005630 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005631 return ret;
5632}
5633
5634/*
5635 * Expand names with a function defined by the user.
5636 */
5637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ExpandUserDefined(
5639 expand_T *xp,
5640 regmatch_T *regmatch,
5641 int *num_file,
5642 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005643{
5644 char_u *retstr;
5645 char_u *s;
5646 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005647 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005648 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005649 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005650
5651 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5652 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 return FAIL;
5654
5655 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005656 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 e = vim_strchr(s, '\n');
5659 if (e == NULL)
5660 e = s + STRLEN(s);
5661 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005662 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005664 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5665 *e = keep;
5666
5667 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005669 if (ga_grow(&ga, 1) == FAIL)
5670 break;
5671 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5672 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (*e != NUL)
5676 ++e;
5677 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005678 vim_free(retstr);
5679 *file = ga.ga_data;
5680 *num_file = ga.ga_len;
5681 return OK;
5682}
5683
5684/*
5685 * Expand names with a list returned by a function defined by the user.
5686 */
5687 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ExpandUserList(
5689 expand_T *xp,
5690 int *num_file,
5691 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005692{
5693 list_T *retlist;
5694 listitem_T *li;
5695 garray_T ga;
5696
5697 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5698 if (retlist == NULL)
5699 return FAIL;
5700
5701 ga_init2(&ga, (int)sizeof(char *), 3);
5702 /* Loop over the items in the list. */
5703 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5704 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005705 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5706 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005707
5708 if (ga_grow(&ga, 1) == FAIL)
5709 break;
5710
5711 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005712 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005713 ++ga.ga_len;
5714 }
5715 list_unref(retlist);
5716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 *file = ga.ga_data;
5718 *num_file = ga.ga_len;
5719 return OK;
5720}
5721#endif
5722
5723/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005724 * Expand color scheme, compiler or filetype names.
5725 * Search from 'runtimepath':
5726 * 'runtimepath'/{dirnames}/{pat}.vim
5727 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5728 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5729 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5730 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005731 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 */
5733 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005734ExpandRTDir(
5735 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005736 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005737 int *num_file,
5738 char_u ***file,
5739 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 char_u *s;
5742 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005743 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005745 int i;
5746 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 *num_file = 0;
5749 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005750 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005751 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005753 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005755 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5756 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005758 ga_clear_strings(&ga);
5759 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005761 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005762 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005763 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005765
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005766 if (flags & DIP_START) {
5767 for (i = 0; dirnames[i] != NULL; ++i)
5768 {
5769 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5770 if (s == NULL)
5771 {
5772 ga_clear_strings(&ga);
5773 return FAIL;
5774 }
5775 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5776 globpath(p_pp, s, &ga, 0);
5777 vim_free(s);
5778 }
5779 }
5780
5781 if (flags & DIP_OPT) {
5782 for (i = 0; dirnames[i] != NULL; ++i)
5783 {
5784 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5785 if (s == NULL)
5786 {
5787 ga_clear_strings(&ga);
5788 return FAIL;
5789 }
5790 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5791 globpath(p_pp, s, &ga, 0);
5792 vim_free(s);
5793 }
5794 }
5795
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005796 for (i = 0; i < ga.ga_len; ++i)
5797 {
5798 match = ((char_u **)ga.ga_data)[i];
5799 s = match;
5800 e = s + STRLEN(s);
5801 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5802 {
5803 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005804 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005805 if (s < match || vim_ispathsep(*s))
5806 break;
5807 ++s;
5808 *e = NUL;
5809 mch_memmove(match, s, e - s + 1);
5810 }
5811 }
5812
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005813 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005814 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005815
5816 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005817 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005818 remove_duplicates(&ga);
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 *file = ga.ga_data;
5821 *num_file = ga.ga_len;
5822 return OK;
5823}
5824
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005825/*
5826 * Expand loadplugin names:
5827 * 'packpath'/pack/ * /opt/{pat}
5828 */
5829 static int
5830ExpandPackAddDir(
5831 char_u *pat,
5832 int *num_file,
5833 char_u ***file)
5834{
5835 char_u *s;
5836 char_u *e;
5837 char_u *match;
5838 garray_T ga;
5839 int i;
5840 int pat_len;
5841
5842 *num_file = 0;
5843 *file = NULL;
5844 pat_len = (int)STRLEN(pat);
5845 ga_init2(&ga, (int)sizeof(char *), 10);
5846
5847 s = alloc((unsigned)(pat_len + 26));
5848 if (s == NULL)
5849 {
5850 ga_clear_strings(&ga);
5851 return FAIL;
5852 }
5853 sprintf((char *)s, "pack/*/opt/%s*", pat);
5854 globpath(p_pp, s, &ga, 0);
5855 vim_free(s);
5856
5857 for (i = 0; i < ga.ga_len; ++i)
5858 {
5859 match = ((char_u **)ga.ga_data)[i];
5860 s = gettail(match);
5861 e = s + STRLEN(s);
5862 mch_memmove(match, s, e - s + 1);
5863 }
5864
5865 if (ga.ga_len == 0)
5866 return FAIL;
5867
5868 /* Sort and remove duplicates which can happen when specifying multiple
5869 * directories in dirnames. */
5870 remove_duplicates(&ga);
5871
5872 *file = ga.ga_data;
5873 *num_file = ga.ga_len;
5874 return OK;
5875}
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#endif
5878
5879#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5880/*
5881 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005882 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005884 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005885globpath(
5886 char_u *path,
5887 char_u *file,
5888 garray_T *ga,
5889 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 expand_T xpc;
5892 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 int num_p;
5895 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 buf = alloc(MAXPATHL);
5898 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005901 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 /* Loop over all entries in {path}. */
5905 while (*path != NUL)
5906 {
5907 /* Copy one item of the path to buf[] and concatenate the file name. */
5908 copy_option_part(&path, buf, MAXPATHL, ",");
5909 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5910 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005911# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005912 /* Using the platform's path separator (\) makes vim incorrectly
5913 * treat it as an escape character, use '/' instead. */
5914 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5915 STRCAT(buf, "/");
5916# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005920 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5921 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005923 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005925 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 for (i = 0; i < num_p; ++i)
5928 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005929 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005930 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005931 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 FreeWild(num_p, p);
5936 }
5937 }
5938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
5940 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941}
5942
5943#endif
5944
5945#if defined(FEAT_CMDHIST) || defined(PROTO)
5946
5947/*********************************
5948 * Command line history stuff *
5949 *********************************/
5950
5951/*
5952 * Translate a history character to the associated type number.
5953 */
5954 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005955hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
5957 if (c == ':')
5958 return HIST_CMD;
5959 if (c == '=')
5960 return HIST_EXPR;
5961 if (c == '@')
5962 return HIST_INPUT;
5963 if (c == '>')
5964 return HIST_DEBUG;
5965 return HIST_SEARCH; /* must be '?' or '/' */
5966}
5967
5968/*
5969 * Table of history names.
5970 * These names are used in :history and various hist...() functions.
5971 * It is sufficient to give the significant prefix of a history name.
5972 */
5973
5974static char *(history_names[]) =
5975{
5976 "cmd",
5977 "search",
5978 "expr",
5979 "input",
5980 "debug",
5981 NULL
5982};
5983
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005984#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5985/*
5986 * Function given to ExpandGeneric() to obtain the possible first
5987 * arguments of the ":history command.
5988 */
5989 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005990get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005991{
5992 static char_u compl[2] = { NUL, NUL };
5993 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005994 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005995 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5996
5997 if (idx < short_names_count)
5998 {
5999 compl[0] = (char_u)short_names[idx];
6000 return compl;
6001 }
6002 if (idx < short_names_count + history_name_count)
6003 return (char_u *)history_names[idx - short_names_count];
6004 if (idx == short_names_count + history_name_count)
6005 return (char_u *)"all";
6006 return NULL;
6007}
6008#endif
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010/*
6011 * init_history() - Initialize the command line history.
6012 * Also used to re-allocate the history when the size changes.
6013 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 int newlen; /* new length of history table */
6018 histentry_T *temp;
6019 int i;
6020 int j;
6021 int type;
6022
6023 /*
6024 * If size of history table changed, reallocate it
6025 */
6026 newlen = (int)p_hi;
6027 if (newlen != hislen) /* history length changed */
6028 {
6029 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6030 {
6031 if (newlen)
6032 {
6033 temp = (histentry_T *)lalloc(
6034 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6035 if (temp == NULL) /* out of memory! */
6036 {
6037 if (type == 0) /* first one: just keep the old length */
6038 {
6039 newlen = hislen;
6040 break;
6041 }
6042 /* Already changed one table, now we can only have zero
6043 * length for all tables. */
6044 newlen = 0;
6045 type = -1;
6046 continue;
6047 }
6048 }
6049 else
6050 temp = NULL;
6051 if (newlen == 0 || temp != NULL)
6052 {
6053 if (hisidx[type] < 0) /* there are no entries yet */
6054 {
6055 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006056 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 }
6058 else if (newlen > hislen) /* array becomes bigger */
6059 {
6060 for (i = 0; i <= hisidx[type]; ++i)
6061 temp[i] = history[type][i];
6062 j = i;
6063 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006064 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 for ( ; j < hislen; ++i, ++j)
6066 temp[i] = history[type][j];
6067 }
6068 else /* array becomes smaller or 0 */
6069 {
6070 j = hisidx[type];
6071 for (i = newlen - 1; ; --i)
6072 {
6073 if (i >= 0) /* copy newest entries */
6074 temp[i] = history[type][j];
6075 else /* remove older entries */
6076 vim_free(history[type][j].hisstr);
6077 if (--j < 0)
6078 j = hislen - 1;
6079 if (j == hisidx[type])
6080 break;
6081 }
6082 hisidx[type] = newlen - 1;
6083 }
6084 vim_free(history[type]);
6085 history[type] = temp;
6086 }
6087 }
6088 hislen = newlen;
6089 }
6090}
6091
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006093clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006094{
6095 hisptr->hisnum = 0;
6096 hisptr->viminfo = FALSE;
6097 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006098 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006099}
6100
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101/*
6102 * Check if command line 'str' is already in history.
6103 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6104 */
6105 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006106in_history(
6107 int type,
6108 char_u *str,
6109 int move_to_front, /* Move the entry to the front if it exists */
6110 int sep,
6111 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112{
6113 int i;
6114 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006115 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
6117 if (hisidx[type] < 0)
6118 return FALSE;
6119 i = hisidx[type];
6120 do
6121 {
6122 if (history[type][i].hisstr == NULL)
6123 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006124
6125 /* For search history, check that the separator character matches as
6126 * well. */
6127 p = history[type][i].hisstr;
6128 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006129 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006130 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
6132 if (!move_to_front)
6133 return TRUE;
6134 last_i = i;
6135 break;
6136 }
6137 if (--i < 0)
6138 i = hislen - 1;
6139 } while (i != hisidx[type]);
6140
6141 if (last_i >= 0)
6142 {
6143 str = history[type][i].hisstr;
6144 while (i != hisidx[type])
6145 {
6146 if (++i >= hislen)
6147 i = 0;
6148 history[type][last_i] = history[type][i];
6149 last_i = i;
6150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006152 history[type][i].viminfo = FALSE;
6153 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006154 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 return TRUE;
6156 }
6157 return FALSE;
6158}
6159
6160/*
6161 * Convert history name (from table above) to its HIST_ equivalent.
6162 * When "name" is empty, return "cmd" history.
6163 * Returns -1 for unknown history name.
6164 */
6165 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006166get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 int i;
6169 int len = (int)STRLEN(name);
6170
6171 /* No argument: use current history. */
6172 if (len == 0)
6173 return hist_char2type(ccline.cmdfirstc);
6174
6175 for (i = 0; history_names[i] != NULL; ++i)
6176 if (STRNICMP(name, history_names[i], len) == 0)
6177 return i;
6178
6179 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6180 return hist_char2type(name[0]);
6181
6182 return -1;
6183}
6184
6185static int last_maptick = -1; /* last seen maptick */
6186
6187/*
6188 * Add the given string to the given history. If the string is already in the
6189 * history then it is moved to the front. "histype" may be one of he HIST_
6190 * values.
6191 */
6192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193add_to_history(
6194 int histype,
6195 char_u *new_entry,
6196 int in_map, /* consider maptick when inside a mapping */
6197 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 histentry_T *hisptr;
6200 int len;
6201
6202 if (hislen == 0) /* no history */
6203 return;
6204
Bram Moolenaara939e432013-11-09 05:30:26 +01006205 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6206 return;
6207
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 /*
6209 * Searches inside the same mapping overwrite each other, so that only
6210 * the last line is kept. Be careful not to remove a line that was moved
6211 * down, only lines that were added.
6212 */
6213 if (histype == HIST_SEARCH && in_map)
6214 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006215 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
6217 /* Current line is from the same mapping, remove it */
6218 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6219 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006220 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 --hisnum[histype];
6222 if (--hisidx[HIST_SEARCH] < 0)
6223 hisidx[HIST_SEARCH] = hislen - 1;
6224 }
6225 last_maptick = -1;
6226 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006227 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
6229 if (++hisidx[histype] == hislen)
6230 hisidx[histype] = 0;
6231 hisptr = &history[histype][hisidx[histype]];
6232 vim_free(hisptr->hisstr);
6233
6234 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006235 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6237 if (hisptr->hisstr != NULL)
6238 hisptr->hisstr[len + 1] = sep;
6239
6240 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006241 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006242 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (histype == HIST_SEARCH && in_map)
6244 last_maptick = maptick;
6245 }
6246}
6247
6248#if defined(FEAT_EVAL) || defined(PROTO)
6249
6250/*
6251 * Get identifier of newest history entry.
6252 * "histype" may be one of the HIST_ values.
6253 */
6254 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006255get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6258 || hisidx[histype] < 0)
6259 return -1;
6260
6261 return history[histype][hisidx[histype]].hisnum;
6262}
6263
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 * Calculate history index from a number:
6266 * num > 0: seen as identifying number of a history entry
6267 * num < 0: relative position in history wrt newest entry
6268 * "histype" may be one of the HIST_ values.
6269 */
6270 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006271calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 int i;
6274 histentry_T *hist;
6275 int wrapped = FALSE;
6276
6277 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6278 || (i = hisidx[histype]) < 0 || num == 0)
6279 return -1;
6280
6281 hist = history[histype];
6282 if (num > 0)
6283 {
6284 while (hist[i].hisnum > num)
6285 if (--i < 0)
6286 {
6287 if (wrapped)
6288 break;
6289 i += hislen;
6290 wrapped = TRUE;
6291 }
6292 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6293 return i;
6294 }
6295 else if (-num <= hislen)
6296 {
6297 i += num + 1;
6298 if (i < 0)
6299 i += hislen;
6300 if (hist[i].hisstr != NULL)
6301 return i;
6302 }
6303 return -1;
6304}
6305
6306/*
6307 * Get a history entry by its index.
6308 * "histype" may be one of the HIST_ values.
6309 */
6310 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006311get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 idx = calc_hist_idx(histype, idx);
6314 if (idx >= 0)
6315 return history[histype][idx].hisstr;
6316 else
6317 return (char_u *)"";
6318}
6319
6320/*
6321 * Clear all entries of a history.
6322 * "histype" may be one of the HIST_ values.
6323 */
6324 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 int i;
6328 histentry_T *hisptr;
6329
6330 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6331 {
6332 hisptr = history[histype];
6333 for (i = hislen; i--;)
6334 {
6335 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006336 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006337 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339 hisidx[histype] = -1; /* mark history as cleared */
6340 hisnum[histype] = 0; /* reset identifier counter */
6341 return OK;
6342 }
6343 return FAIL;
6344}
6345
6346/*
6347 * Remove all entries matching {str} from a history.
6348 * "histype" may be one of the HIST_ values.
6349 */
6350 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006351del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 regmatch_T regmatch;
6354 histentry_T *hisptr;
6355 int idx;
6356 int i;
6357 int last;
6358 int found = FALSE;
6359
6360 regmatch.regprog = NULL;
6361 regmatch.rm_ic = FALSE; /* always match case */
6362 if (hislen != 0
6363 && histype >= 0
6364 && histype < HIST_COUNT
6365 && *str != NUL
6366 && (idx = hisidx[histype]) >= 0
6367 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6368 != NULL)
6369 {
6370 i = last = idx;
6371 do
6372 {
6373 hisptr = &history[histype][i];
6374 if (hisptr->hisstr == NULL)
6375 break;
6376 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6377 {
6378 found = TRUE;
6379 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006380 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382 else
6383 {
6384 if (i != last)
6385 {
6386 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006387 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
6389 if (--last < 0)
6390 last += hislen;
6391 }
6392 if (--i < 0)
6393 i += hislen;
6394 } while (i != idx);
6395 if (history[histype][idx].hisstr == NULL)
6396 hisidx[histype] = -1;
6397 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006398 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 return found;
6400}
6401
6402/*
6403 * Remove an indexed entry from a history.
6404 * "histype" may be one of the HIST_ values.
6405 */
6406 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006407del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 int i, j;
6410
6411 i = calc_hist_idx(histype, idx);
6412 if (i < 0)
6413 return FALSE;
6414 idx = hisidx[histype];
6415 vim_free(history[histype][i].hisstr);
6416
6417 /* When deleting the last added search string in a mapping, reset
6418 * last_maptick, so that the last added search string isn't deleted again.
6419 */
6420 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6421 last_maptick = -1;
6422
6423 while (i != idx)
6424 {
6425 j = (i + 1) % hislen;
6426 history[histype][i] = history[histype][j];
6427 i = j;
6428 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006429 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (--i < 0)
6431 i += hislen;
6432 hisidx[histype] = i;
6433 return TRUE;
6434}
6435
6436#endif /* FEAT_EVAL */
6437
6438#if defined(FEAT_CRYPT) || defined(PROTO)
6439/*
6440 * Very specific function to remove the value in ":set key=val" from the
6441 * history.
6442 */
6443 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006444remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 char_u *p;
6447 int i;
6448
6449 i = hisidx[HIST_CMD];
6450 if (i < 0)
6451 return;
6452 p = history[HIST_CMD][i].hisstr;
6453 if (p != NULL)
6454 for ( ; *p; ++p)
6455 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6456 {
6457 p = vim_strchr(p + 3, '=');
6458 if (p == NULL)
6459 break;
6460 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006461 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 if (p[i] == '\\' && p[i + 1])
6463 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006464 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 --p;
6466 }
6467}
6468#endif
6469
6470#endif /* FEAT_CMDHIST */
6471
Bram Moolenaar064154c2016-03-19 22:50:43 +01006472#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006473/*
6474 * Get pointer to the command line info to use. cmdline_paste() may clear
6475 * ccline and put the previous value in prev_ccline.
6476 */
6477 static struct cmdline_info *
6478get_ccline_ptr(void)
6479{
6480 if ((State & CMDLINE) == 0)
6481 return NULL;
6482 if (ccline.cmdbuff != NULL)
6483 return &ccline;
6484 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6485 return &prev_ccline;
6486 return NULL;
6487}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006488#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006489
Bram Moolenaar064154c2016-03-19 22:50:43 +01006490#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006491/*
6492 * Get the current command line in allocated memory.
6493 * Only works when the command line is being edited.
6494 * Returns NULL when something is wrong.
6495 */
6496 char_u *
6497get_cmdline_str(void)
6498{
6499 struct cmdline_info *p = get_ccline_ptr();
6500
6501 if (p == NULL)
6502 return NULL;
6503 return vim_strnsave(p->cmdbuff, p->cmdlen);
6504}
6505
6506/*
6507 * Get the current command line position, counted in bytes.
6508 * Zero is the first position.
6509 * Only works when the command line is being edited.
6510 * Returns -1 when something is wrong.
6511 */
6512 int
6513get_cmdline_pos(void)
6514{
6515 struct cmdline_info *p = get_ccline_ptr();
6516
6517 if (p == NULL)
6518 return -1;
6519 return p->cmdpos;
6520}
6521
6522/*
6523 * Set the command line byte position to "pos". Zero is the first position.
6524 * Only works when the command line is being edited.
6525 * Returns 1 when failed, 0 when OK.
6526 */
6527 int
6528set_cmdline_pos(
6529 int pos)
6530{
6531 struct cmdline_info *p = get_ccline_ptr();
6532
6533 if (p == NULL)
6534 return 1;
6535
6536 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6537 * changed the command line. */
6538 if (pos < 0)
6539 new_cmdpos = 0;
6540 else
6541 new_cmdpos = pos;
6542 return 0;
6543}
6544#endif
6545
6546#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6547/*
6548 * Get the current command-line type.
6549 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6550 * Only works when the command line is being edited.
6551 * Returns NUL when something is wrong.
6552 */
6553 int
6554get_cmdline_type(void)
6555{
6556 struct cmdline_info *p = get_ccline_ptr();
6557
6558 if (p == NULL)
6559 return NUL;
6560 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006561 return
6562# ifdef FEAT_EVAL
6563 (p->input_fn) ? '@' :
6564# endif
6565 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006566 return p->cmdfirstc;
6567}
6568#endif
6569
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6571/*
6572 * Get indices "num1,num2" that specify a range within a list (not a range of
6573 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6574 * Returns OK if parsed successfully, otherwise FAIL.
6575 */
6576 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006577get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 int len;
6580 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006581 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582
6583 *str = skipwhite(*str);
6584 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6585 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006586 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 *str += len;
6588 *num1 = (int)num;
6589 first = TRUE;
6590 }
6591 *str = skipwhite(*str);
6592 if (**str == ',') /* parse "to" part of range */
6593 {
6594 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006595 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (len > 0)
6597 {
6598 *num2 = (int)num;
6599 *str = skipwhite(*str + len);
6600 }
6601 else if (!first) /* no number given at all */
6602 return FAIL;
6603 }
6604 else if (first) /* only one number given */
6605 *num2 = *num1;
6606 return OK;
6607}
6608#endif
6609
6610#if defined(FEAT_CMDHIST) || defined(PROTO)
6611/*
6612 * :history command - print a history
6613 */
6614 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006615ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 histentry_T *hist;
6618 int histype1 = HIST_CMD;
6619 int histype2 = HIST_CMD;
6620 int hisidx1 = 1;
6621 int hisidx2 = -1;
6622 int idx;
6623 int i, j, k;
6624 char_u *end;
6625 char_u *arg = eap->arg;
6626
6627 if (hislen == 0)
6628 {
6629 MSG(_("'history' option is zero"));
6630 return;
6631 }
6632
6633 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6634 {
6635 end = arg;
6636 while (ASCII_ISALPHA(*end)
6637 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6638 end++;
6639 i = *end;
6640 *end = NUL;
6641 histype1 = get_histtype(arg);
6642 if (histype1 == -1)
6643 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006644 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
6646 histype1 = 0;
6647 histype2 = HIST_COUNT-1;
6648 }
6649 else
6650 {
6651 *end = i;
6652 EMSG(_(e_trailing));
6653 return;
6654 }
6655 }
6656 else
6657 histype2 = histype1;
6658 *end = i;
6659 }
6660 else
6661 end = arg;
6662 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6663 {
6664 EMSG(_(e_trailing));
6665 return;
6666 }
6667
6668 for (; !got_int && histype1 <= histype2; ++histype1)
6669 {
6670 STRCPY(IObuff, "\n # ");
6671 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6672 MSG_PUTS_TITLE(IObuff);
6673 idx = hisidx[histype1];
6674 hist = history[histype1];
6675 j = hisidx1;
6676 k = hisidx2;
6677 if (j < 0)
6678 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6679 if (k < 0)
6680 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6681 if (idx >= 0 && j <= k)
6682 for (i = idx + 1; !got_int; ++i)
6683 {
6684 if (i == hislen)
6685 i = 0;
6686 if (hist[i].hisstr != NULL
6687 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6688 {
6689 msg_putchar('\n');
6690 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6691 hist[i].hisnum);
6692 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6693 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006694 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 else
6696 STRCAT(IObuff, hist[i].hisstr);
6697 msg_outtrans(IObuff);
6698 out_flush();
6699 }
6700 if (i == idx)
6701 break;
6702 }
6703 }
6704}
6705#endif
6706
6707#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006708/*
6709 * Buffers for history read from a viminfo file. Only valid while reading.
6710 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006711static histentry_T *viminfo_history[HIST_COUNT] =
6712 {NULL, NULL, NULL, NULL, NULL};
6713static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6714static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715static int viminfo_add_at_front = FALSE;
6716
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006717static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719/*
6720 * Translate a history type number to the associated character.
6721 */
6722 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006723hist_type2char(
6724 int type,
6725 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 if (type == HIST_CMD)
6728 return ':';
6729 if (type == HIST_SEARCH)
6730 {
6731 if (use_question)
6732 return '?';
6733 else
6734 return '/';
6735 }
6736 if (type == HIST_EXPR)
6737 return '=';
6738 return '@';
6739}
6740
6741/*
6742 * Prepare for reading the history from the viminfo file.
6743 * This allocates history arrays to store the read history lines.
6744 */
6745 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006746prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
6748 int i;
6749 int num;
6750 int type;
6751 int len;
6752
6753 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006754 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (asklen > hislen)
6756 asklen = hislen;
6757
6758 for (type = 0; type < HIST_COUNT; ++type)
6759 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006760 /* Count the number of empty spaces in the history list. Entries read
6761 * from viminfo previously are also considered empty. If there are
6762 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006764 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 num++;
6766 len = asklen;
6767 if (num > len)
6768 len = num;
6769 if (len <= 0)
6770 viminfo_history[type] = NULL;
6771 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006772 viminfo_history[type] = (histentry_T *)lalloc(
6773 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 if (viminfo_history[type] == NULL)
6775 len = 0;
6776 viminfo_hislen[type] = len;
6777 viminfo_hisidx[type] = 0;
6778 }
6779}
6780
6781/*
6782 * Accept a line from the viminfo, store it in the history array when it's
6783 * new.
6784 */
6785 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006786read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787{
6788 int type;
6789 long_u len;
6790 char_u *val;
6791 char_u *p;
6792
6793 type = hist_char2type(virp->vir_line[0]);
6794 if (viminfo_hisidx[type] < viminfo_hislen[type])
6795 {
6796 val = viminfo_readstring(virp, 1, TRUE);
6797 if (val != NULL && *val != NUL)
6798 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006799 int sep = (*val == ' ' ? NUL : *val);
6800
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006802 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
6804 /* Need to re-allocate to append the separator byte. */
6805 len = STRLEN(val);
6806 p = lalloc(len + 2, TRUE);
6807 if (p != NULL)
6808 {
6809 if (type == HIST_SEARCH)
6810 {
6811 /* Search entry: Move the separator from the first
6812 * column to after the NUL. */
6813 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006814 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816 else
6817 {
6818 /* Not a search entry: No separator in the viminfo
6819 * file, add a NUL separator. */
6820 mch_memmove(p, val, (size_t)len + 1);
6821 p[len + 1] = NUL;
6822 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006823 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6824 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006825 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6826 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006827 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829 }
6830 }
6831 vim_free(val);
6832 }
6833 return viminfo_readline(virp);
6834}
6835
Bram Moolenaar07219f92013-04-14 23:19:36 +02006836/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006837 * Accept a new style history line from the viminfo, store it in the history
6838 * array when it's new.
6839 */
6840 void
6841handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006842 garray_T *values,
6843 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006844{
6845 int type;
6846 long_u len;
6847 char_u *val;
6848 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006849 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006850
6851 /* Check the format:
6852 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006853 if (values->ga_len < 4
6854 || vp[0].bv_type != BVAL_NR
6855 || vp[1].bv_type != BVAL_NR
6856 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6857 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006858 return;
6859
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006860 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006861 if (type >= HIST_COUNT)
6862 return;
6863 if (viminfo_hisidx[type] < viminfo_hislen[type])
6864 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006865 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006866 if (val != NULL && *val != NUL)
6867 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006868 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6869 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006870 int idx;
6871 int overwrite = FALSE;
6872
6873 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6874 {
6875 /* If lines were written by an older Vim we need to avoid
6876 * getting duplicates. See if the entry already exists. */
6877 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6878 {
6879 p = viminfo_history[type][idx].hisstr;
6880 if (STRCMP(val, p) == 0
6881 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6882 {
6883 overwrite = TRUE;
6884 break;
6885 }
6886 }
6887
6888 if (!overwrite)
6889 {
6890 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006891 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006892 p = lalloc(len + 2, TRUE);
6893 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006894 else
6895 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006896 if (p != NULL)
6897 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006898 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006899 if (!overwrite)
6900 {
6901 mch_memmove(p, val, (size_t)len + 1);
6902 /* Put the separator after the NUL. */
6903 p[len + 1] = sep;
6904 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006905 viminfo_history[type][idx].hisnum = 0;
6906 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006907 viminfo_hisidx[type]++;
6908 }
6909 }
6910 }
6911 }
6912 }
6913}
6914
6915/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006916 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006917 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006918 static void
6919concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 int idx;
6922 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006923
6924 idx = hisidx[type] + viminfo_hisidx[type];
6925 if (idx >= hislen)
6926 idx -= hislen;
6927 else if (idx < 0)
6928 idx = hislen - 1;
6929 if (viminfo_add_at_front)
6930 hisidx[type] = idx;
6931 else
6932 {
6933 if (hisidx[type] == -1)
6934 hisidx[type] = hislen - 1;
6935 do
6936 {
6937 if (history[type][idx].hisstr != NULL
6938 || history[type][idx].viminfo)
6939 break;
6940 if (++idx == hislen)
6941 idx = 0;
6942 } while (idx != hisidx[type]);
6943 if (idx != hisidx[type] && --idx < 0)
6944 idx = hislen - 1;
6945 }
6946 for (i = 0; i < viminfo_hisidx[type]; i++)
6947 {
6948 vim_free(history[type][idx].hisstr);
6949 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6950 history[type][idx].viminfo = TRUE;
6951 history[type][idx].time_set = viminfo_history[type][i].time_set;
6952 if (--idx < 0)
6953 idx = hislen - 1;
6954 }
6955 idx += 1;
6956 idx %= hislen;
6957 for (i = 0; i < viminfo_hisidx[type]; i++)
6958 {
6959 history[type][idx++].hisnum = ++hisnum[type];
6960 idx %= hislen;
6961 }
6962}
6963
6964#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6965 static int
6966#ifdef __BORLANDC__
6967_RTLENTRYF
6968#endif
6969sort_hist(const void *s1, const void *s2)
6970{
6971 histentry_T *p1 = *(histentry_T **)s1;
6972 histentry_T *p2 = *(histentry_T **)s2;
6973
6974 if (p1->time_set < p2->time_set) return -1;
6975 if (p1->time_set > p2->time_set) return 1;
6976 return 0;
6977}
6978#endif
6979
6980/*
6981 * Merge history lines from viminfo and lines typed in this Vim based on the
6982 * timestamp;
6983 */
6984 static void
6985merge_history(int type)
6986{
6987 int max_len;
6988 histentry_T **tot_hist;
6989 histentry_T *new_hist;
6990 int i;
6991 int len;
6992
6993 /* Make one long list with all entries. */
6994 max_len = hislen + viminfo_hisidx[type];
6995 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6996 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6997 if (tot_hist == NULL || new_hist == NULL)
6998 {
6999 vim_free(tot_hist);
7000 vim_free(new_hist);
7001 return;
7002 }
7003 for (i = 0; i < viminfo_hisidx[type]; i++)
7004 tot_hist[i] = &viminfo_history[type][i];
7005 len = i;
7006 for (i = 0; i < hislen; i++)
7007 if (history[type][i].hisstr != NULL)
7008 tot_hist[len++] = &history[type][i];
7009
7010 /* Sort the list on timestamp. */
7011 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
7012
7013 /* Keep the newest ones. */
7014 for (i = 0; i < hislen; i++)
7015 {
7016 if (i < len)
7017 {
7018 new_hist[i] = *tot_hist[i];
7019 tot_hist[i]->hisstr = NULL;
7020 if (new_hist[i].hisnum == 0)
7021 new_hist[i].hisnum = ++hisnum[type];
7022 }
7023 else
7024 clear_hist_entry(&new_hist[i]);
7025 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007026 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007027
7028 /* Free what is not kept. */
7029 for (i = 0; i < viminfo_hisidx[type]; i++)
7030 vim_free(viminfo_history[type][i].hisstr);
7031 for (i = 0; i < hislen; i++)
7032 vim_free(history[type][i].hisstr);
7033 vim_free(history[type]);
7034 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007035 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007036}
7037
7038/*
7039 * Finish reading history lines from viminfo. Not used when writing viminfo.
7040 */
7041 void
7042finish_viminfo_history(vir_T *virp)
7043{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007045 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
7047 for (type = 0; type < HIST_COUNT; ++type)
7048 {
7049 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007050 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007051
7052 if (merge)
7053 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007055 concat_history(type);
7056
Bram Moolenaard23a8232018-02-10 18:45:26 +01007057 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007058 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060}
7061
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007062/*
7063 * Write history to viminfo file in "fp".
7064 * When "merge" is TRUE merge history lines with a previously read viminfo
7065 * file, data is in viminfo_history[].
7066 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007069write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 int i;
7072 int type;
7073 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007074 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
7076 init_history();
7077 if (hislen == 0)
7078 return;
7079 for (type = 0; type < HIST_COUNT; ++type)
7080 {
7081 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7082 if (num_saved == 0)
7083 continue;
7084 if (num_saved < 0) /* Use default */
7085 num_saved = hislen;
7086 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7087 type == HIST_CMD ? _("Command Line") :
7088 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007089 type == HIST_EXPR ? _("Expression") :
7090 type == HIST_INPUT ? _("Input Line") :
7091 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 if (num_saved > hislen)
7093 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007094
7095 /*
7096 * Merge typed and viminfo history:
7097 * round 1: history of typed commands.
7098 * round 2: history from recently read viminfo.
7099 */
7100 for (round = 1; round <= 2; ++round)
7101 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007102 if (round == 1)
7103 /* start at newest entry, somewhere in the list */
7104 i = hisidx[type];
7105 else if (viminfo_hisidx[type] > 0)
7106 /* start at newest entry, first in the list */
7107 i = 0;
7108 else
7109 /* empty list */
7110 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007111 if (i >= 0)
7112 while (num_saved > 0
7113 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007115 char_u *p;
7116 time_t timestamp;
7117 int c = NUL;
7118
7119 if (round == 1)
7120 {
7121 p = history[type][i].hisstr;
7122 timestamp = history[type][i].time_set;
7123 }
7124 else
7125 {
7126 p = viminfo_history[type] == NULL ? NULL
7127 : viminfo_history[type][i].hisstr;
7128 timestamp = viminfo_history[type] == NULL ? 0
7129 : viminfo_history[type][i].time_set;
7130 }
7131
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007132 if (p != NULL && (round == 2
7133 || !merge
7134 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007136 --num_saved;
7137 fputc(hist_type2char(type, TRUE), fp);
7138 /* For the search history: put the separator in the
7139 * second column; use a space if there isn't one. */
7140 if (type == HIST_SEARCH)
7141 {
7142 c = p[STRLEN(p) + 1];
7143 putc(c == NUL ? ' ' : c, fp);
7144 }
7145 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007146
7147 {
7148 char cbuf[NUMBUFLEN];
7149
7150 /* New style history with a bar line. Format:
7151 * |{bartype},{histtype},{timestamp},{separator},"text" */
7152 if (c == NUL)
7153 cbuf[0] = NUL;
7154 else
7155 sprintf(cbuf, "%d", c);
7156 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7157 type, (long)timestamp, cbuf);
7158 barline_writestring(fp, p, LSIZE - 20);
7159 putc('\n', fp);
7160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007162 if (round == 1)
7163 {
7164 /* Decrement index, loop around and stop when back at
7165 * the start. */
7166 if (--i < 0)
7167 i = hislen - 1;
7168 if (i == hisidx[type])
7169 break;
7170 }
7171 else
7172 {
7173 /* Increment index. Stop at the end in the while. */
7174 ++i;
7175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007177 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007178 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007179 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007180 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007181 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007182 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184}
7185#endif /* FEAT_VIMINFO */
7186
7187#if defined(FEAT_FKMAP) || defined(PROTO)
7188/*
7189 * Write a character at the current cursor+offset position.
7190 * It is directly written into the command buffer block.
7191 */
7192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7196 {
7197 EMSG(_("E198: cmd_pchar beyond the command length"));
7198 return;
7199 }
7200 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7201 ccline.cmdbuff[ccline.cmdlen] = NUL;
7202}
7203
7204 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007205cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7208 {
7209 /* EMSG(_("cmd_gchar beyond the command length")); */
7210 return NUL;
7211 }
7212 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7213}
7214#endif
7215
7216#if defined(FEAT_CMDWIN) || defined(PROTO)
7217/*
7218 * Open a window on the current command line and history. Allow editing in
7219 * the window. Returns when the window is closed.
7220 * Returns:
7221 * CR if the command is to be executed
7222 * Ctrl_C if it is to be abandoned
7223 * K_IGNORE if editing continues
7224 */
7225 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007226open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007229 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007231 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 win_T *wp;
7233 int i;
7234 linenr_T lnum;
7235 int histtype;
7236 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 int save_restart_edit = restart_edit;
7238 int save_State = State;
7239 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007240#ifdef FEAT_RIGHTLEFT
7241 int save_cmdmsg_rl = cmdmsg_rl;
7242#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007243#ifdef FEAT_FOLDING
7244 int save_KeyTyped;
7245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 /* Can't do this recursively. Can't do it when typing a password. */
7248 if (cmdwin_type != 0
7249# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7250 || cmdline_star > 0
7251# endif
7252 )
7253 {
7254 beep_flush();
7255 return K_IGNORE;
7256 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007257 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259 /* Save current window sizes. */
7260 win_size_save(&winsizes);
7261
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007263 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007264
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007265 /* don't use a new tab page */
7266 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007267 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007268
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 /* Create a window for the command-line buffer. */
7270 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7271 {
7272 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007273 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 return K_IGNORE;
7275 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007276 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
7278 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007279 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007280 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007283#ifdef FEAT_FOLDING
7284 curwin->w_p_fen = FALSE;
7285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007287 curwin->w_p_rl = cmdmsg_rl;
7288 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007290 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007293 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007294 /* But don't allow switching to another buffer. */
7295 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar46152342005-09-07 21:18:43 +00007297 /* Showing the prompt may have set need_wait_return, reset it. */
7298 need_wait_return = FALSE;
7299
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007300 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7302 {
7303 if (p_wc == TAB)
7304 {
7305 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7306 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7307 }
7308 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7309 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007310 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007312 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7313 * sets 'textwidth' to 78). */
7314 curbuf->b_p_tw = 0;
7315
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 /* Fill the buffer with the history. */
7317 init_history();
7318 if (hislen > 0)
7319 {
7320 i = hisidx[histtype];
7321 if (i >= 0)
7322 {
7323 lnum = 0;
7324 do
7325 {
7326 if (++i == hislen)
7327 i = 0;
7328 if (history[histtype][i].hisstr != NULL)
7329 ml_append(lnum++, history[histtype][i].hisstr,
7330 (colnr_T)0, FALSE);
7331 }
7332 while (i != hisidx[histtype]);
7333 }
7334 }
7335
7336 /* Replace the empty last line with the current command-line and put the
7337 * cursor there. */
7338 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7339 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7340 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007341 changed_line_abv_curs();
7342 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007343 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007346 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 /* No Ex mode here! */
7349 exmode_active = 0;
7350
7351 State = NORMAL;
7352# ifdef FEAT_MOUSE
7353 setmouse();
7354# endif
7355
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007357 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007358 if (restart_edit != 0) /* autocmd with ":startinsert" */
7359 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
7361 i = RedrawingDisabled;
7362 RedrawingDisabled = 0;
7363
7364 /*
7365 * Call the main loop until <CR> or CTRL-C is typed.
7366 */
7367 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007368 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
7370 RedrawingDisabled = i;
7371
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007372# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007373 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007374# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007375
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007377 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007378
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007379# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007380 /* Restore KeyTyped in case it is modified by autocommands */
7381 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382# endif
7383
Bram Moolenaarccc18222007-05-10 18:25:20 +00007384 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007385 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 cmdwin_type = 0;
7387
7388 exmode_active = save_exmode;
7389
Bram Moolenaarf9821062008-06-20 16:31:07 +00007390 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007392 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
7394 cmdwin_result = Ctrl_C;
7395 EMSG(_("E199: Active window or buffer deleted"));
7396 }
7397 else
7398 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007399# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 /* autocmds may abort script processing */
7401 if (aborting() && cmdwin_result != K_IGNORE)
7402 cmdwin_result = Ctrl_C;
7403# endif
7404 /* Set the new command line from the cmdline buffer. */
7405 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007406 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007408 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7409
7410 if (histtype == HIST_CMD)
7411 {
7412 /* Execute the command directly. */
7413 ccline.cmdbuff = vim_strsave((char_u *)p);
7414 cmdwin_result = CAR;
7415 }
7416 else
7417 {
7418 /* First need to cancel what we were doing. */
7419 ccline.cmdbuff = NULL;
7420 stuffcharReadbuff(':');
7421 stuffReadbuff((char_u *)p);
7422 stuffcharReadbuff(CAR);
7423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425 else if (cmdwin_result == K_XF2) /* :qa typed */
7426 {
7427 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7428 cmdwin_result = CAR;
7429 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007430 else if (cmdwin_result == Ctrl_C)
7431 {
7432 /* :q or :close, don't execute any command
7433 * and don't modify the cmd window. */
7434 ccline.cmdbuff = NULL;
7435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 else
7437 ccline.cmdbuff = vim_strsave(ml_get_curline());
7438 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007439 {
7440 ccline.cmdbuff = vim_strsave((char_u *)"");
7441 ccline.cmdlen = 0;
7442 ccline.cmdbufflen = 1;
7443 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 else
7447 {
7448 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7449 ccline.cmdbufflen = ccline.cmdlen + 1;
7450 ccline.cmdpos = curwin->w_cursor.col;
7451 if (ccline.cmdpos > ccline.cmdlen)
7452 ccline.cmdpos = ccline.cmdlen;
7453 if (cmdwin_result == K_IGNORE)
7454 {
7455 set_cmdspos_cursor();
7456 redrawcmd();
7457 }
7458 }
7459
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007461 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007462# ifdef FEAT_CONCEAL
7463 /* Avoid command-line window first character being concealed. */
7464 curwin->w_p_cole = 0;
7465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007467 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 win_goto(old_curwin);
7469 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007470
7471 /* win_close() may have already wiped the buffer when 'bh' is
7472 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007473 if (bufref_valid(&bufref))
7474 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 /* Restore window sizes. */
7477 win_size_restore(&winsizes);
7478
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007479 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 }
7481
7482 ga_clear(&winsizes);
7483 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007484# ifdef FEAT_RIGHTLEFT
7485 cmdmsg_rl = save_cmdmsg_rl;
7486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
7488 State = save_State;
7489# ifdef FEAT_MOUSE
7490 setmouse();
7491# endif
7492
7493 return cmdwin_result;
7494}
7495#endif /* FEAT_CMDWIN */
7496
7497/*
7498 * Used for commands that either take a simple command string argument, or:
7499 * cmd << endmarker
7500 * {script}
7501 * endmarker
7502 * Returns a pointer to allocated memory with {script} or NULL.
7503 */
7504 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007505script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 char_u *theline;
7508 char *end_pattern = NULL;
7509 char dot[] = ".";
7510 garray_T ga;
7511
7512 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7513 return NULL;
7514
7515 ga_init2(&ga, 1, 0x400);
7516
7517 if (cmd[2] != NUL)
7518 end_pattern = (char *)skipwhite(cmd + 2);
7519 else
7520 end_pattern = dot;
7521
7522 for (;;)
7523 {
7524 theline = eap->getline(
7525#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007526 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527#endif
7528 NUL, eap->cookie, 0);
7529
7530 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007531 {
7532 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535
7536 ga_concat(&ga, theline);
7537 ga_append(&ga, '\n');
7538 vim_free(theline);
7539 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007540 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541
7542 return (char_u *)ga.ga_data;
7543}