blob: e37ebe2c9eecbd5b572949ec01cab6120fd1262a [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 Moolenaar438d1762018-09-30 17:11:48 +020047// 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().
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000050static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar438d1762018-09-30 17:11:48 +020052static int cmd_showtail; /* Only show path tail in lists ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef FEAT_EVAL
55static int new_cmdpos; /* position set by set_cmdline_pos() */
56#endif
57
Bram Moolenaara92522f2017-07-15 15:21:38 +020058static int extra_char = NUL; /* extra character to display when redrawing
59 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020060static int extra_char_shift;
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_CMDHIST
63typedef struct hist_entry
64{
65 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020066 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020068 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069} histentry_T;
70
71static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
72static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
73static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
74 /* identifying (unique) number of newest history entry */
75static int hislen = 0; /* actual length of history tables */
76
Bram Moolenaard25c16e2016-01-29 22:13:30 +010077static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
80#ifdef FEAT_RIGHTLEFT
81static int cmd_hkmap = 0; /* Hebrew mapping during command line */
82#endif
83
84#ifdef FEAT_FKMAP
85static int cmd_fkmap = 0; /* Farsi mapping during command line */
86#endif
87
Bram Moolenaar438d1762018-09-30 17:11:48 +020088static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static int cmdline_charsize(int idx);
90static void set_cmdspos(void);
91static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void alloc_cmdbuff(int len);
96static int realloc_cmdbuff(int len);
97static void draw_cmdline(int start, int len);
98static void save_cmdline(struct cmdline_info *ccp);
99static void restore_cmdline(struct cmdline_info *ccp);
100static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100104static void redrawcmdprompt(void);
105static void cursorcmd(void);
106static int ccheck_abbr(int);
107static int nextwild(expand_T *xp, int type, int options, int escape);
108static void escape_fname(char_u **pp);
109static int showmatches(expand_T *xp, int wildmenu);
110static void set_expand_context(expand_T *xp);
111static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
112static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100114static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100115static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100116static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200117# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100121static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
122static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123# endif
124#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200125#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200130static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
134static int
135#ifdef __BORLANDC__
136_RTLENTRYF
137#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100138sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200139#endif
140
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200141
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200142 static void
143trigger_cmd_autocmd(int typechar, int evt)
144{
145 char_u typestr[2];
146
147 typestr[0] = typechar;
148 typestr[1] = NUL;
149 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
150}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200153 * Abandon the command line.
154 */
155 static void
156abandon_cmdline(void)
157{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100158 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200159 if (msg_scrolled == 0)
160 compute_cmdrow();
161 MSG("");
162 redraw_cmdline = TRUE;
163}
164
Bram Moolenaaree219b02017-12-17 14:55:01 +0100165#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200166/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100167 * Guess that the pattern matches everything. Only finds specific cases, such
168 * as a trailing \|, which can happen while typing a pattern.
169 */
170 static int
171empty_pattern(char_u *p)
172{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100173 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100174
175 /* remove trailing \v and the like */
176 while (n >= 2 && p[n - 2] == '\\'
177 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
178 n -= 2;
179 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
180}
181
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200182// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200183typedef struct {
184 colnr_T vs_curswant;
185 colnr_T vs_leftcol;
186 linenr_T vs_topline;
187# ifdef FEAT_DIFF
188 int vs_topfill;
189# endif
190 linenr_T vs_botline;
191 linenr_T vs_empty_rows;
192} viewstate_T;
193
194 static void
195save_viewstate(viewstate_T *vs)
196{
197 vs->vs_curswant = curwin->w_curswant;
198 vs->vs_leftcol = curwin->w_leftcol;
199 vs->vs_topline = curwin->w_topline;
200# ifdef FEAT_DIFF
201 vs->vs_topfill = curwin->w_topfill;
202# endif
203 vs->vs_botline = curwin->w_botline;
204 vs->vs_empty_rows = curwin->w_empty_rows;
205}
206
207 static void
208restore_viewstate(viewstate_T *vs)
209{
210 curwin->w_curswant = vs->vs_curswant;
211 curwin->w_leftcol = vs->vs_leftcol;
212 curwin->w_topline = vs->vs_topline;
213# ifdef FEAT_DIFF
214 curwin->w_topfill = vs->vs_topfill;
215# endif
216 curwin->w_botline = vs->vs_botline;
217 curwin->w_empty_rows = vs->vs_empty_rows;
218}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200219
220// Struct to store the state of 'incsearch' highlighting.
221typedef struct {
222 pos_T search_start; // where 'incsearch' starts searching
223 pos_T save_cursor;
224 viewstate_T init_viewstate;
225 viewstate_T old_viewstate;
226 pos_T match_start;
227 pos_T match_end;
228 int did_incsearch;
229 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200230 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200231} incsearch_state_T;
232
233 static void
234init_incsearch_state(incsearch_state_T *is_state)
235{
236 is_state->match_start = curwin->w_cursor;
237 is_state->did_incsearch = FALSE;
238 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200239 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200240 CLEAR_POS(&is_state->match_end);
241 is_state->save_cursor = curwin->w_cursor; // may be restored later
242 is_state->search_start = curwin->w_cursor;
243 save_viewstate(&is_state->init_viewstate);
244 save_viewstate(&is_state->old_viewstate);
245}
246
247/*
248 * First move cursor to end of match, then to the start. This
249 * moves the whole match onto the screen when 'nowrap' is set.
250 */
251 static void
252set_search_match(pos_T *t)
253{
254 t->lnum += search_match_lines;
255 t->col = search_match_endcol;
256 if (t->lnum > curbuf->b_ml.ml_line_count)
257 {
258 t->lnum = curbuf->b_ml.ml_line_count;
259 coladvance((colnr_T)MAXCOL);
260 }
261}
262
263/*
264 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200265 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200266 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200267 */
268 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200269do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
270 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200271{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200272 char_u *cmd;
273 cmdmod_T save_cmdmod = cmdmod;
274 char_u *p;
275 int delim_optional = FALSE;
276 int delim;
277 char_u *end;
278 char_u *dummy;
279 exarg_T ea;
280 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200281 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200282
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200283 *skiplen = 0;
284 *patlen = ccline.cmdlen;
285
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200286 if (!p_is || cmd_silent)
287 return FALSE;
288
289 // by default search all lines
290 search_first_line = 0;
291 search_last_line = MAXLNUM;
292
293 if (firstc == '/' || firstc == '?')
294 return TRUE;
295 if (firstc != ':')
296 return FALSE;
297
298 vim_memset(&ea, 0, sizeof(ea));
299 ea.line1 = 1;
300 ea.line2 = 1;
301 ea.cmd = ccline.cmdbuff;
302 ea.addr_type = ADDR_LINES;
303
304 parse_command_modifiers(&ea, &dummy, TRUE);
305 cmdmod = save_cmdmod;
306
307 cmd = skip_range(ea.cmd, NULL);
308 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
309 return FALSE;
310
311 // Skip over "substitute" to find the pattern separator.
312 for (p = cmd; ASCII_ISALPHA(*p); ++p)
313 ;
314 if (*skipwhite(p) == NUL)
315 return FALSE;
316
317 if (STRNCMP(cmd, "substitute", p - cmd) == 0
318 || STRNCMP(cmd, "smagic", p - cmd) == 0
319 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
320 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200321 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200322 if (*cmd == 's' && cmd[1] == 'm')
323 p_magic = TRUE;
324 else if (*cmd == 's' && cmd[1] == 'n')
325 p_magic = FALSE;
326 }
327 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
328 {
329 // skip over flags
330 while (ASCII_ISALPHA(*(p = skipwhite(p))))
331 ++p;
332 if (*p == NUL)
333 return FALSE;
334 }
335 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
336 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
337 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
338 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
339 || STRNCMP(cmd, "global", p - cmd) == 0)
340 {
341 // skip over "!"
342 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200343 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200344 p++;
345 if (*skipwhite(p) == NUL)
346 return FALSE;
347 }
348 if (*cmd != 'g')
349 delim_optional = TRUE;
350 }
351 else
352 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200353
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200354 p = skipwhite(p);
355 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
356 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200357
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200358 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200359
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200360 if (end == p && !use_last_pat)
361 return FALSE;
362
363 // Don't do 'hlsearch' highlighting if the pattern matches everything.
364 if (!use_last_pat)
365 {
366 char c = *end;
367 int empty;
368
369 *end = NUL;
370 empty = empty_pattern(p);
371 *end = c;
372 if (empty)
373 return FALSE;
374 }
375
376 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200377 *skiplen = (int)(p - ccline.cmdbuff);
378 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200379
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200380 // parse the address range
381 save_cursor = curwin->w_cursor;
382 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200383 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200384 if (ea.addr_count > 0)
385 {
386 // Allow for reverse match.
387 if (ea.line2 < ea.line1)
388 {
389 search_first_line = ea.line2;
390 search_last_line = ea.line1;
391 }
392 else
393 {
394 search_first_line = ea.line1;
395 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200396 }
397 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200398 else if (cmd[0] == 's' && cmd[1] != 'o')
399 {
400 // :s defaults to the current line
401 search_first_line = curwin->w_cursor.lnum;
402 search_last_line = curwin->w_cursor.lnum;
403 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200405 curwin->w_cursor = save_cursor;
406 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407}
408
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200409 static void
410finish_incsearch_highlighting(
411 int gotesc,
412 incsearch_state_T *is_state,
413 int call_update_screen)
414{
415 if (is_state->did_incsearch)
416 {
417 is_state->did_incsearch = FALSE;
418 if (gotesc)
419 curwin->w_cursor = is_state->save_cursor;
420 else
421 {
422 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
423 {
424 // put the '" mark at the original position
425 curwin->w_cursor = is_state->save_cursor;
426 setpcmark();
427 }
428 curwin->w_cursor = is_state->search_start;
429 }
430 restore_viewstate(&is_state->old_viewstate);
431 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200432
433 // by default search all lines
434 search_first_line = 0;
435 search_last_line = MAXLNUM;
436
437 p_magic = is_state->magic_save;
438
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200439 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200440 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200441 if (call_update_screen)
442 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200443 }
444}
445
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200446/*
447 * Do 'incsearch' highlighting if desired.
448 */
449 static void
450may_do_incsearch_highlighting(
451 int firstc,
452 long count,
453 incsearch_state_T *is_state)
454{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200455 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200456 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200458#ifdef FEAT_RELTIME
459 proftime_T tm;
460#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200461 int next_char;
462 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200463
Bram Moolenaar198cb662018-09-06 21:44:17 +0200464 // Parsing range may already set the last search pattern.
465 save_last_search_pattern();
466
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200467 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200468 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200469 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200470 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200471 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200472 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473
474 // If there is a character waiting, search and redraw later.
475 if (char_avail())
476 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200477 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200478 is_state->incsearch_postponed = TRUE;
479 return;
480 }
481 is_state->incsearch_postponed = FALSE;
482
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200483 if (search_first_line == 0)
484 // start at the original cursor position
485 curwin->w_cursor = is_state->search_start;
486 else
487 {
488 // start at the first line in the range
489 curwin->w_cursor.lnum = search_first_line;
490 curwin->w_cursor.col = 0;
491 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200492
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200493 // Use the previous pattern for ":s//".
494 next_char = ccline.cmdbuff[skiplen + patlen];
495 use_last_pat = patlen == 0 && skiplen > 0
496 && ccline.cmdbuff[skiplen - 1] == next_char;
497
498 // If there is no pattern, don't do anything.
499 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200500 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200501 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502 set_no_hlsearch(TRUE); // turn off previous highlight
503 redraw_all_later(SOME_VALID);
504 }
505 else
506 {
507 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
508
509 cursor_off(); // so the user knows we're busy
510 out_flush();
511 ++emsg_off; // so it doesn't beep if bad expr
512#ifdef FEAT_RELTIME
513 // Set the time limit to half a second.
514 profile_setlimit(500L, &tm);
515#endif
516 if (!p_hls)
517 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200518 if (search_first_line != 0)
519 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200520 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200521 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200522 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200523#ifdef FEAT_RELTIME
524 &tm, NULL
525#else
526 NULL, NULL
527#endif
528 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200529 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200530 --emsg_off;
531
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200532 if (curwin->w_cursor.lnum < search_first_line
533 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200534 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200535 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200536 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200537 curwin->w_cursor = is_state->search_start;
538 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200539
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200540 // if interrupted while searching, behave like it failed
541 if (got_int)
542 {
543 (void)vpeekc(); // remove <C-C> from input stream
544 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200545 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200546 }
547 else if (char_avail())
548 // cancelled searching because a char was typed
549 is_state->incsearch_postponed = TRUE;
550 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200551 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200552 highlight_match = TRUE; // highlight position
553 else
554 highlight_match = FALSE; // remove highlight
555
556 // First restore the old curwin values, so the screen is positioned in the
557 // same way as the actual search command.
558 restore_viewstate(&is_state->old_viewstate);
559 changed_cline_bef_curs();
560 update_topline();
561
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200562 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200563 {
564 pos_T save_pos = curwin->w_cursor;
565
566 is_state->match_start = curwin->w_cursor;
567 set_search_match(&curwin->w_cursor);
568 validate_cursor();
569 end_pos = curwin->w_cursor;
570 is_state->match_end = end_pos;
571 curwin->w_cursor = save_pos;
572 }
573 else
574 end_pos = curwin->w_cursor; // shutup gcc 4
575
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200576 // Disable 'hlsearch' highlighting if the pattern matches everything.
577 // Avoids a flash when typing "foo\|".
578 if (!use_last_pat)
579 {
580 next_char = ccline.cmdbuff[skiplen + patlen];
581 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200582 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
583 {
584 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200585 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200586 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200587 ccline.cmdbuff[skiplen + patlen] = next_char;
588 }
589
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200590 validate_cursor();
591 // May redraw the status line to show the cursor position.
592 if (p_ru && curwin->w_status_height > 0)
593 curwin->w_redr_status = TRUE;
594
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200595 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 restore_last_search_pattern();
597
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200598 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
599 // end of the pattern, e.g. for ":s/pat/".
600 if (ccline.cmdbuff[skiplen + patlen] != NUL)
601 curwin->w_cursor = is_state->search_start;
602 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200603 curwin->w_cursor = end_pos;
604
605 msg_starthere();
606 redrawcmdline();
607 is_state->did_incsearch = TRUE;
608}
609
610/*
611 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
612 * or previous match.
613 * Returns FAIL when jumping to cmdline_not_changed;
614 */
615 static int
616may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200617 int firstc,
618 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200619 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200620 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200621{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200622 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200623 pos_T t;
624 char_u *pat;
625 int search_flags = SEARCH_NOOF;
626 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200627 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628
Bram Moolenaar198cb662018-09-06 21:44:17 +0200629 // Parsing range may already set the last search pattern.
630 save_last_search_pattern();
631
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200632 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200633 {
634 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200635 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200636 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200637 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200638 {
639 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200640 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200641 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200642
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200643 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200644 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200645 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200646 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200647 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200648 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200649 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200650 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200652 cursor_off();
653 out_flush();
654 if (c == Ctrl_G)
655 {
656 t = is_state->match_end;
657 if (LT_POS(is_state->match_start, is_state->match_end))
658 // Start searching at the end of the match not at the beginning of
659 // the next column.
660 (void)decl(&t);
661 search_flags += SEARCH_COL;
662 }
663 else
664 t = is_state->match_start;
665 if (!p_hls)
666 search_flags += SEARCH_KEEP;
667 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200668 save = pat[patlen];
669 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200670 i = searchit(curwin, curbuf, &t,
671 c == Ctrl_G ? FORWARD : BACKWARD,
672 pat, count, search_flags,
673 RE_SEARCH, 0, NULL, NULL);
674 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200675 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200676 if (i)
677 {
678 is_state->search_start = is_state->match_start;
679 is_state->match_end = t;
680 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200681 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200682 {
683 // Move just before the current match, so that when nv_search
684 // finishes the cursor will be put back on the match.
685 is_state->search_start = t;
686 (void)decl(&is_state->search_start);
687 }
688 else if (c == Ctrl_G && firstc == '?')
689 {
690 // Move just after the current match, so that when nv_search
691 // finishes the cursor will be put back on the match.
692 is_state->search_start = t;
693 (void)incl(&is_state->search_start);
694 }
695 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
696 {
697 // wrap around
698 is_state->search_start = t;
699 if (firstc == '?')
700 (void)incl(&is_state->search_start);
701 else
702 (void)decl(&is_state->search_start);
703 }
704
705 set_search_match(&is_state->match_end);
706 curwin->w_cursor = is_state->match_start;
707 changed_cline_bef_curs();
708 update_topline();
709 validate_cursor();
710 highlight_match = TRUE;
711 save_viewstate(&is_state->old_viewstate);
712 update_screen(NOT_VALID);
713 redrawcmdline();
714 }
715 else
716 vim_beep(BO_ERROR);
717 restore_last_search_pattern();
718 return FAIL;
719}
720
721/*
722 * When CTRL-L typed: add character from the match to the pattern.
723 * May set "*c" to the added character.
724 * Return OK when jumping to cmdline_not_changed.
725 */
726 static int
727may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
728{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200729 int skiplen, patlen;
730
Bram Moolenaar198cb662018-09-06 21:44:17 +0200731 // Parsing range may already set the last search pattern.
732 save_last_search_pattern();
733
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200734 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200735 {
736 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200737 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200738 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200739
740 // Add a character from under the cursor for 'incsearch'.
741 if (is_state->did_incsearch)
742 {
743 curwin->w_cursor = is_state->match_end;
744 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
745 {
746 *c = gchar_cursor();
747
748 // If 'ignorecase' and 'smartcase' are set and the
749 // command line has no uppercase characters, convert
750 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200751 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200752 *c = MB_TOLOWER(*c);
753 if (*c != NUL)
754 {
755 if (*c == firstc || vim_strchr((char_u *)(
756 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
757 {
758 // put a backslash before special characters
759 stuffcharReadbuff(*c);
760 *c = '\\';
761 }
762 return FAIL;
763 }
764 }
765 }
766 return OK;
767}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200768#endif
769
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200770 void
771cmdline_init(void)
772{
773 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
774}
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,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200798 long count, // only used for incremental search
799 int indent) // indent for inside conditionals
800{
801 return getcmdline_int(firstc, count, indent, TRUE);
802}
803
804 static char_u *
805getcmdline_int(
806 int firstc,
807 long count UNUSED, // only used for incremental search
808 int indent, // indent for inside conditionals
809 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 int c;
812 int i;
813 int j;
814 int gotesc = FALSE; /* TRUE when <ESC> just typed */
815 int do_abbr; /* when TRUE check for abbr. */
816#ifdef FEAT_CMDHIST
817 char_u *lookfor = NULL; /* string to match */
818 int hiscnt; /* current history line in use */
819 int histype; /* history type to be used */
820#endif
821#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200822 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
824 int did_wild_list = FALSE; /* did wild_list() recently */
825 int wim_index = 0; /* index in wim_flags[] */
826 int res;
827 int save_msg_scroll = msg_scroll;
828 int save_State = State; /* remember State when called */
829 int some_key_typed = FALSE; /* one of the keys was typed */
830#ifdef FEAT_MOUSE
831 /* mouse drag and release events are ignored, unless they are
832 * preceded with a mouse down event */
833 int ignore_drag_release = TRUE;
834#endif
835#ifdef FEAT_EVAL
836 int break_ctrl_c = FALSE;
837#endif
838 expand_T xpc;
839 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000840 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200841 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200842 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar438d1762018-09-30 17:11:48 +0200844 if (ccline.cmdbuff != NULL)
845 {
846 // Being called recursively. Since ccline is global, we need to save
847 // the current buffer and restore it when returning.
848 save_cmdline(&save_ccline);
849 did_save_ccline = TRUE;
850 }
851 if (init_ccline)
852 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
853
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#ifdef FEAT_EVAL
855 if (firstc == -1)
856 {
857 firstc = NUL;
858 break_ctrl_c = TRUE;
859 }
860#endif
861#ifdef FEAT_RIGHTLEFT
862 /* start without Hebrew mapping for a command line */
863 if (firstc == ':' || firstc == '=' || firstc == '>')
864 cmd_hkmap = 0;
865#endif
866
867 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200870 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#endif
872
873 /*
874 * set some variables for redrawcmd()
875 */
876 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000877 ccline.cmdindent = (firstc > 0 ? indent : 0);
878
879 /* alloc initial ccline.cmdbuff */
880 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200882 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 ccline.cmdlen = ccline.cmdpos = 0;
884 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100885 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000887 /* autoindent for :insert and :append */
888 if (firstc <= 0)
889 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200890 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000891 ccline.cmdbuff[indent] = NUL;
892 ccline.cmdpos = indent;
893 ccline.cmdspos = indent;
894 ccline.cmdlen = indent;
895 }
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000898 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900#ifdef FEAT_RIGHTLEFT
901 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
902 && (firstc == '/' || firstc == '?'))
903 cmdmsg_rl = TRUE;
904 else
905 cmdmsg_rl = FALSE;
906#endif
907
908 redir_off = TRUE; /* don't redirect the typed command */
909 if (!cmd_silent)
910 {
911 i = msg_scrolled;
912 msg_scrolled = 0; /* avoid wait_return message */
913 gotocmdline(TRUE);
914 msg_scrolled += i;
915 redrawcmdprompt(); /* draw prompt or indent */
916 set_cmdspos();
917 }
918 xpc.xp_context = EXPAND_NOTHING;
919 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000920#ifndef BACKSLASH_IN_FILENAME
921 xpc.xp_shell = FALSE;
922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000924#if defined(FEAT_EVAL)
925 if (ccline.input_fn)
926 {
927 xpc.xp_context = ccline.xp_context;
928 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000929# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000930 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000931# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000932 }
933#endif
934
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 /*
936 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
937 * doing ":@0" when register 0 doesn't contain a CR.
938 */
939 msg_scroll = FALSE;
940
941 State = CMDLINE;
942
943 if (firstc == '/' || firstc == '?' || firstc == '@')
944 {
945 /* Use ":lmap" mappings for search pattern and input(). */
946 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
947 b_im_ptr = &curbuf->b_p_iminsert;
948 else
949 b_im_ptr = &curbuf->b_p_imsearch;
950 if (*b_im_ptr == B_IMODE_LMAP)
951 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100952#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 im_set_active(*b_im_ptr == B_IMODE_IM);
954#endif
955 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100956#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 else if (p_imcmdline)
958 im_set_active(TRUE);
959#endif
960
961#ifdef FEAT_MOUSE
962 setmouse();
963#endif
964#ifdef CURSOR_SHAPE
965 ui_cursor_shape(); /* may show different cursor shape */
966#endif
967
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000968 /* When inside an autocommand for writing "exiting" may be set and
969 * terminal mode set to cooked. Need to set raw mode here then. */
970 settmode(TMODE_RAW);
971
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200972 /* Trigger CmdlineEnter autocommands. */
973 cmdline_type = firstc == NUL ? '-' : firstc;
974 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#ifdef FEAT_CMDHIST
977 init_history();
978 hiscnt = hislen; /* set hiscnt to impossible history value */
979 histype = hist_char2type(firstc);
980#endif
981
982#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000983 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#endif
985
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200986 /* If something above caused an error, reset the flags, we do want to type
987 * and execute commands. Display may be messed up a bit. */
988 if (did_emsg)
989 redrawcmd();
990 did_emsg = FALSE;
991 got_int = FALSE;
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 /*
994 * Collect the command string, handling editing keys.
995 */
996 for (;;)
997 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000998 redir_off = TRUE; /* Don't redirect the typed command.
999 Repeated, because a ":redir" inside
1000 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef USE_ON_FLY_SCROLL
1002 dont_scroll = FALSE; /* allow scrolling here */
1003#endif
1004 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1005
Bram Moolenaar72532d32018-04-07 19:09:09 +02001006 did_emsg = FALSE; /* There can't really be a reason why an error
1007 that occurs while typing a command should
1008 cause the command not to be executed. */
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001011
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001012 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1013 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001014 do
1015 {
1016 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001017 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (KeyTyped)
1020 {
1021 some_key_typed = TRUE;
1022#ifdef FEAT_RIGHTLEFT
1023 if (cmd_hkmap)
1024 c = hkmap(c);
1025# ifdef FEAT_FKMAP
1026 if (cmd_fkmap)
1027 c = cmdl_fkmap(c);
1028# endif
1029 if (cmdmsg_rl && !KeyStuffed)
1030 {
1031 /* Invert horizontal movements and operations. Only when
1032 * typed by the user directly, not when the result of a
1033 * mapping. */
1034 switch (c)
1035 {
1036 case K_RIGHT: c = K_LEFT; break;
1037 case K_S_RIGHT: c = K_S_LEFT; break;
1038 case K_C_RIGHT: c = K_C_LEFT; break;
1039 case K_LEFT: c = K_RIGHT; break;
1040 case K_S_LEFT: c = K_S_RIGHT; break;
1041 case K_C_LEFT: c = K_C_RIGHT; break;
1042 }
1043 }
1044#endif
1045 }
1046
1047 /*
1048 * Ignore got_int when CTRL-C was typed here.
1049 * Don't ignore it in :global, we really need to break then, e.g., for
1050 * ":g/pat/normal /pat" (without the <CR>).
1051 * Don't ignore it for the input() function.
1052 */
1053 if ((c == Ctrl_C
1054#ifdef UNIX
1055 || c == intr_char
1056#endif
1057 )
1058#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1059 && firstc != '@'
1060#endif
1061#ifdef FEAT_EVAL
1062 && !break_ctrl_c
1063#endif
1064 && !global_busy)
1065 got_int = FALSE;
1066
1067#ifdef FEAT_CMDHIST
1068 /* free old command line when finished moving around in the history
1069 * list */
1070 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001071 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001072 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 && c != K_PAGEDOWN && c != K_PAGEUP
1074 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001075 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001077 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#endif
1079
1080 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001081 * When there are matching completions to select <S-Tab> works like
1082 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001084 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 c = Ctrl_P;
1086
1087#ifdef FEAT_WILDMENU
1088 /* Special translations for 'wildmenu' */
1089 if (did_wild_list && p_wmnu)
1090 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001091 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001093 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 c = Ctrl_N;
1095 }
1096 /* Hitting CR after "emenu Name.": complete submenu */
1097 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1098 && ccline.cmdpos > 1
1099 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1100 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1101 && (c == '\n' || c == '\r' || c == K_KENTER))
1102 c = K_DOWN;
1103#endif
1104
1105 /* free expanded names when finished walking through matches */
1106 if (xpc.xp_numfiles != -1
1107 && !(c == p_wc && KeyTyped) && c != p_wcm
1108 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1109 && c != Ctrl_L)
1110 {
1111 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1112 did_wild_list = FALSE;
1113#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001114 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#endif
1116 xpc.xp_context = EXPAND_NOTHING;
1117 wim_index = 0;
1118#ifdef FEAT_WILDMENU
1119 if (p_wmnu && wild_menu_showing != 0)
1120 {
1121 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001122 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001123
1124 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001125 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 if (wild_menu_showing == WM_SCROLLED)
1128 {
1129 /* Entered command line, move it up */
1130 cmdline_row--;
1131 redrawcmd();
1132 }
1133 else if (save_p_ls != -1)
1134 {
1135 /* restore 'laststatus' and 'winminheight' */
1136 p_ls = save_p_ls;
1137 p_wmh = save_p_wmh;
1138 last_status(FALSE);
1139 update_screen(VALID); /* redraw the screen NOW */
1140 redrawcmd();
1141 save_p_ls = -1;
1142 }
1143 else
1144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 redraw_statuslines();
1147 }
1148 KeyTyped = skt;
1149 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001150 if (ccline.input_fn)
1151 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
1153#endif
1154 }
1155
1156#ifdef FEAT_WILDMENU
1157 /* Special translations for 'wildmenu' */
1158 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1159 {
1160 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001161 if (c == K_DOWN && ccline.cmdpos > 0
1162 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001164 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 /* Hitting <Up>: Remove one submenu name in front of the
1167 * cursor */
1168 int found = FALSE;
1169
1170 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1171 i = 0;
1172 while (--j > 0)
1173 {
1174 /* check for start of menu name */
1175 if (ccline.cmdbuff[j] == ' '
1176 && ccline.cmdbuff[j - 1] != '\\')
1177 {
1178 i = j + 1;
1179 break;
1180 }
1181 /* check for start of submenu name */
1182 if (ccline.cmdbuff[j] == '.'
1183 && ccline.cmdbuff[j - 1] != '\\')
1184 {
1185 if (found)
1186 {
1187 i = j + 1;
1188 break;
1189 }
1190 else
1191 found = TRUE;
1192 }
1193 }
1194 if (i > 0)
1195 cmdline_del(i);
1196 c = p_wc;
1197 xpc.xp_context = EXPAND_NOTHING;
1198 }
1199 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001200 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001201 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001202 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
1204 char_u upseg[5];
1205
1206 upseg[0] = PATHSEP;
1207 upseg[1] = '.';
1208 upseg[2] = '.';
1209 upseg[3] = PATHSEP;
1210 upseg[4] = NUL;
1211
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001212 if (c == K_DOWN
1213 && ccline.cmdpos > 0
1214 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1215 && (ccline.cmdpos < 3
1216 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1218 {
1219 /* go down a directory */
1220 c = p_wc;
1221 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001222 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
1224 /* If in a direct ancestor, strip off one ../ to go down */
1225 int found = FALSE;
1226
1227 j = ccline.cmdpos;
1228 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1229 while (--j > i)
1230 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001231#ifdef FEAT_MBYTE
1232 if (has_mbyte)
1233 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (vim_ispathsep(ccline.cmdbuff[j]))
1236 {
1237 found = TRUE;
1238 break;
1239 }
1240 }
1241 if (found
1242 && ccline.cmdbuff[j - 1] == '.'
1243 && ccline.cmdbuff[j - 2] == '.'
1244 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1245 {
1246 cmdline_del(j - 2);
1247 c = p_wc;
1248 }
1249 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001250 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 /* go up a directory */
1253 int found = FALSE;
1254
1255 j = ccline.cmdpos - 1;
1256 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1257 while (--j > i)
1258 {
1259#ifdef FEAT_MBYTE
1260 if (has_mbyte)
1261 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1262#endif
1263 if (vim_ispathsep(ccline.cmdbuff[j])
1264#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001265 && vim_strchr((char_u *)" *?[{`$%#",
1266 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#endif
1268 )
1269 {
1270 if (found)
1271 {
1272 i = j + 1;
1273 break;
1274 }
1275 else
1276 found = TRUE;
1277 }
1278 }
1279
1280 if (!found)
1281 j = i;
1282 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1283 j += 4;
1284 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1285 && j == i)
1286 j += 3;
1287 else
1288 j = 0;
1289 if (j > 0)
1290 {
1291 /* TODO this is only for DOS/UNIX systems - need to put in
1292 * machine-specific stuff here and in upseg init */
1293 cmdline_del(j);
1294 put_on_cmdline(upseg + 1, 3, FALSE);
1295 }
1296 else if (ccline.cmdpos > i)
1297 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001298
1299 /* Now complete in the new directory. Set KeyTyped in case the
1300 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001302 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306#endif /* FEAT_WILDMENU */
1307
1308 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1309 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1310 if (c == Ctrl_BSL)
1311 {
1312 ++no_mapping;
1313 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001314 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 --no_mapping;
1316 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001317 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1318 * is in a mapping. */
1319 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001320 || (ccline.cmdfirstc == '=' && KeyTyped)
1321#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001322 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001323#endif
1324 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {
1326 vungetc(c);
1327 c = Ctrl_BSL;
1328 }
1329#ifdef FEAT_EVAL
1330 else if (c == 'e')
1331 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001332 char_u *p = NULL;
1333 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335 /*
1336 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001337 * Need to save and restore the current command line, to be
1338 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 */
1340 if (ccline.cmdpos == ccline.cmdlen)
1341 new_cmdpos = 99999; /* keep it at the end */
1342 else
1343 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (c == '=')
1347 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001348 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001349 * to avoid nasty things like going to another buffer when
1350 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001351 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001353 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001354
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001355 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001357 len = (int)STRLEN(p);
1358 if (realloc_cmdbuff(len + 1) == OK)
1359 {
1360 ccline.cmdlen = len;
1361 STRCPY(ccline.cmdbuff, p);
1362 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001364 /* Restore the cursor or use the position set with
1365 * set_cmdline_pos(). */
1366 if (new_cmdpos > ccline.cmdlen)
1367 ccline.cmdpos = ccline.cmdlen;
1368 else
1369 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001371 KeyTyped = FALSE; /* Don't do p_wc completion. */
1372 redrawcmd();
1373 goto cmdline_changed;
1374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 }
1377 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001378 got_int = FALSE; /* don't abandon the command line */
1379 did_emsg = FALSE;
1380 emsg_on_display = FALSE;
1381 redrawcmd();
1382 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384#endif
1385 else
1386 {
1387 if (c == Ctrl_G && p_im && restart_edit == 0)
1388 restart_edit = 'a';
1389 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1390 in history */
1391 goto returncmd; /* back to Normal mode */
1392 }
1393 }
1394
1395#ifdef FEAT_CMDWIN
1396 if (c == cedit_key || c == K_CMDWIN)
1397 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001398 if (ex_normal_busy == 0 && got_int == FALSE)
1399 {
1400 /*
1401 * Open a window to edit the command line (and history).
1402 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001403 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001404 some_key_typed = TRUE;
1405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407# ifdef FEAT_DIGRAPHS
1408 else
1409# endif
1410#endif
1411#ifdef FEAT_DIGRAPHS
1412 c = do_digraph(c);
1413#endif
1414
1415 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1416 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1417 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001418 /* In Ex mode a backslash escapes a newline. */
1419 if (exmode_active
1420 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001421 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001422 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001423 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001425 if (c == K_KENTER)
1426 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001428 else
1429 {
1430 gotesc = FALSE; /* Might have typed ESC previously, don't
1431 truncate the cmdline now. */
1432 if (ccheck_abbr(c + ABBR_OFF))
1433 goto cmdline_changed;
1434 if (!cmd_silent)
1435 {
1436 windgoto(msg_row, 0);
1437 out_flush();
1438 }
1439 break;
1440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442
1443 /*
1444 * Completion for 'wildchar' or 'wildcharm' key.
1445 * - hitting <ESC> twice means: abandon command line.
1446 * - wildcard expansion is only done when the 'wildchar' key is really
1447 * typed, not when it comes from a macro
1448 */
1449 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1450 {
1451 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1452 {
1453 /* if 'wildmode' contains "list" may still need to list */
1454 if (xpc.xp_numfiles > 1
1455 && !did_wild_list
1456 && (wim_flags[wim_index] & WIM_LIST))
1457 {
1458 (void)showmatches(&xpc, FALSE);
1459 redrawcmd();
1460 did_wild_list = TRUE;
1461 }
1462 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001463 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1464 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001466 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1467 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 else
1469 res = OK; /* don't insert 'wildchar' now */
1470 }
1471 else /* typed p_wc first time */
1472 {
1473 wim_index = 0;
1474 j = ccline.cmdpos;
1475 /* if 'wildmode' first contains "longest", get longest
1476 * common part */
1477 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001478 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1479 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001481 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1482 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 /* if interrupted while completing, behave like it failed */
1485 if (got_int)
1486 {
1487 (void)vpeekc(); /* remove <C-C> from input stream */
1488 got_int = FALSE; /* don't abandon the command line */
1489 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1490#ifdef FEAT_WILDMENU
1491 xpc.xp_context = EXPAND_NOTHING;
1492#endif
1493 goto cmdline_changed;
1494 }
1495
1496 /* when more than one match, and 'wildmode' first contains
1497 * "list", or no change and 'wildmode' contains "longest,list",
1498 * list all matches */
1499 if (res == OK && xpc.xp_numfiles > 1)
1500 {
1501 /* a "longest" that didn't do anything is skipped (but not
1502 * "list:longest") */
1503 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1504 wim_index = 1;
1505 if ((wim_flags[wim_index] & WIM_LIST)
1506#ifdef FEAT_WILDMENU
1507 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1508#endif
1509 )
1510 {
1511 if (!(wim_flags[0] & WIM_LONGEST))
1512 {
1513#ifdef FEAT_WILDMENU
1514 int p_wmnu_save = p_wmnu;
1515 p_wmnu = 0;
1516#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001517 /* remove match */
1518 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#ifdef FEAT_WILDMENU
1520 p_wmnu = p_wmnu_save;
1521#endif
1522 }
1523#ifdef FEAT_WILDMENU
1524 (void)showmatches(&xpc, p_wmnu
1525 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1526#else
1527 (void)showmatches(&xpc, FALSE);
1528#endif
1529 redrawcmd();
1530 did_wild_list = TRUE;
1531 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001532 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1533 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001535 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1536 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001539 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
1541#ifdef FEAT_WILDMENU
1542 else if (xpc.xp_numfiles == -1)
1543 xpc.xp_context = EXPAND_NOTHING;
1544#endif
1545 }
1546 if (wim_index < 3)
1547 ++wim_index;
1548 if (c == ESC)
1549 gotesc = TRUE;
1550 if (res == OK)
1551 goto cmdline_changed;
1552 }
1553
1554 gotesc = FALSE;
1555
1556 /* <S-Tab> goes to last match, in a clumsy way */
1557 if (c == K_S_TAB && KeyTyped)
1558 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001559 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1560 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1561 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 goto cmdline_changed;
1563 }
1564
1565 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1566 c = NL;
1567
1568 do_abbr = TRUE; /* default: check for abbreviation */
1569
1570 /*
1571 * Big switch for a typed command line character.
1572 */
1573 switch (c)
1574 {
1575 case K_BS:
1576 case Ctrl_H:
1577 case K_DEL:
1578 case K_KDEL:
1579 case Ctrl_W:
1580#ifdef FEAT_FKMAP
1581 if (cmd_fkmap && c == K_BS)
1582 c = K_DEL;
1583#endif
1584 if (c == K_KDEL)
1585 c = K_DEL;
1586
1587 /*
1588 * delete current character is the same as backspace on next
1589 * character, except at end of line
1590 */
1591 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1592 ++ccline.cmdpos;
1593#ifdef FEAT_MBYTE
1594 if (has_mbyte && c == K_DEL)
1595 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1596 ccline.cmdbuff + ccline.cmdpos);
1597#endif
1598 if (ccline.cmdpos > 0)
1599 {
1600 char_u *p;
1601
1602 j = ccline.cmdpos;
1603 p = ccline.cmdbuff + j;
1604#ifdef FEAT_MBYTE
1605 if (has_mbyte)
1606 {
1607 p = mb_prevptr(ccline.cmdbuff, p);
1608 if (c == Ctrl_W)
1609 {
1610 while (p > ccline.cmdbuff && vim_isspace(*p))
1611 p = mb_prevptr(ccline.cmdbuff, p);
1612 i = mb_get_class(p);
1613 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1614 p = mb_prevptr(ccline.cmdbuff, p);
1615 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001616 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 }
1618 }
1619 else
1620#endif
1621 if (c == Ctrl_W)
1622 {
1623 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1624 --p;
1625 i = vim_iswordc(p[-1]);
1626 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1627 && vim_iswordc(p[-1]) == i)
1628 --p;
1629 }
1630 else
1631 --p;
1632 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1633 ccline.cmdlen -= j - ccline.cmdpos;
1634 i = ccline.cmdpos;
1635 while (i < ccline.cmdlen)
1636 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1637
1638 /* Truncate at the end, required for multi-byte chars. */
1639 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001640#ifdef FEAT_SEARCH_EXTRA
1641 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001642 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001643 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001644 /* save view settings, so that the screen
1645 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001646 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001647 }
1648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 redrawcmd();
1650 }
1651 else if (ccline.cmdlen == 0 && c != Ctrl_W
1652 && ccline.cmdprompt == NULL && indent == 0)
1653 {
1654 /* In ex and debug mode it doesn't make sense to return. */
1655 if (exmode_active
1656#ifdef FEAT_EVAL
1657 || ccline.cmdfirstc == '>'
1658#endif
1659 )
1660 goto cmdline_not_changed;
1661
Bram Moolenaard23a8232018-02-10 18:45:26 +01001662 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (!cmd_silent)
1664 {
1665#ifdef FEAT_RIGHTLEFT
1666 if (cmdmsg_rl)
1667 msg_col = Columns;
1668 else
1669#endif
1670 msg_col = 0;
1671 msg_putchar(' '); /* delete ':' */
1672 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001673#ifdef FEAT_SEARCH_EXTRA
1674 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001675 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 redraw_cmdline = TRUE;
1678 goto returncmd; /* back to cmd mode */
1679 }
1680 goto cmdline_changed;
1681
1682 case K_INS:
1683 case K_KINS:
1684#ifdef FEAT_FKMAP
1685 /* if Farsi mode set, we are in reverse insert mode -
1686 Do not change the mode */
1687 if (cmd_fkmap)
1688 beep_flush();
1689 else
1690#endif
1691 ccline.overstrike = !ccline.overstrike;
1692#ifdef CURSOR_SHAPE
1693 ui_cursor_shape(); /* may show different cursor shape */
1694#endif
1695 goto cmdline_not_changed;
1696
1697 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001698 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 /* ":lmap" mappings exists, toggle use of mappings. */
1701 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 im_set_active(FALSE); /* Disable input method */
1704#endif
1705 if (b_im_ptr != NULL)
1706 {
1707 if (State & LANGMAP)
1708 *b_im_ptr = B_IMODE_LMAP;
1709 else
1710 *b_im_ptr = B_IMODE_NONE;
1711 }
1712 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001713#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 else
1715 {
1716 /* There are no ":lmap" mappings, toggle IM. When
1717 * 'imdisable' is set don't try getting the status, it's
1718 * always off. */
1719 if ((p_imdisable && b_im_ptr != NULL)
1720 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1721 {
1722 im_set_active(FALSE); /* Disable input method */
1723 if (b_im_ptr != NULL)
1724 *b_im_ptr = B_IMODE_NONE;
1725 }
1726 else
1727 {
1728 im_set_active(TRUE); /* Enable input method */
1729 if (b_im_ptr != NULL)
1730 *b_im_ptr = B_IMODE_IM;
1731 }
1732 }
1733#endif
1734 if (b_im_ptr != NULL)
1735 {
1736 if (b_im_ptr == &curbuf->b_p_iminsert)
1737 set_iminsert_global();
1738 else
1739 set_imsearch_global();
1740 }
1741#ifdef CURSOR_SHAPE
1742 ui_cursor_shape(); /* may show different cursor shape */
1743#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001744#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 /* Show/unshow value of 'keymap' in status lines later. */
1746 status_redraw_curbuf();
1747#endif
1748 goto cmdline_not_changed;
1749
1750/* case '@': only in very old vi */
1751 case Ctrl_U:
1752 /* delete all characters left of the cursor */
1753 j = ccline.cmdpos;
1754 ccline.cmdlen -= j;
1755 i = ccline.cmdpos = 0;
1756 while (i < ccline.cmdlen)
1757 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1758 /* Truncate at the end, required for multi-byte chars. */
1759 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001760#ifdef FEAT_SEARCH_EXTRA
1761 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001762 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 redrawcmd();
1765 goto cmdline_changed;
1766
1767#ifdef FEAT_CLIPBOARD
1768 case Ctrl_Y:
1769 /* Copy the modeless selection, if there is one. */
1770 if (clip_star.state != SELECT_CLEARED)
1771 {
1772 if (clip_star.state == SELECT_DONE)
1773 clip_copy_modeless_selection(TRUE);
1774 goto cmdline_not_changed;
1775 }
1776 break;
1777#endif
1778
1779 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1780 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001781 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001782 * ":normal" runs out of characters. */
1783 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001784 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 goto cmdline_not_changed;
1786
1787 gotesc = TRUE; /* will free ccline.cmdbuff after
1788 putting it in history */
1789 goto returncmd; /* back to cmd mode */
1790
1791 case Ctrl_R: /* insert register */
1792#ifdef USE_ON_FLY_SCROLL
1793 dont_scroll = TRUE; /* disallow scrolling here */
1794#endif
1795 putcmdline('"', TRUE);
1796 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001797 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (i == Ctrl_O)
1799 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1800 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001801 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001802 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 --no_mapping;
1804#ifdef FEAT_EVAL
1805 /*
1806 * Insert the result of an expression.
1807 * Need to save the current command line, to be able to enter
1808 * a new one...
1809 */
1810 new_cmdpos = -1;
1811 if (c == '=')
1812 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001813 if (ccline.cmdfirstc == '=' // can't do this recursively
1814 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816 beep_flush();
1817 c = ESC;
1818 }
1819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
1822#endif
1823 if (c != ESC) /* use ESC to cancel inserting register */
1824 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001825 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001826
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001827#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001828 /* When there was a serious error abort getting the
1829 * command line. */
1830 if (aborting())
1831 {
1832 gotesc = TRUE; /* will free ccline.cmdbuff after
1833 putting it in history */
1834 goto returncmd; /* back to cmd mode */
1835 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 KeyTyped = FALSE; /* Don't do p_wc completion. */
1838#ifdef FEAT_EVAL
1839 if (new_cmdpos >= 0)
1840 {
1841 /* set_cmdline_pos() was used */
1842 if (new_cmdpos > ccline.cmdlen)
1843 ccline.cmdpos = ccline.cmdlen;
1844 else
1845 ccline.cmdpos = new_cmdpos;
1846 }
1847#endif
1848 }
1849 redrawcmd();
1850 goto cmdline_changed;
1851
1852 case Ctrl_D:
1853 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1854 break; /* Use ^D as normal char instead */
1855
1856 redrawcmd();
1857 continue; /* don't do incremental search now */
1858
1859 case K_RIGHT:
1860 case K_S_RIGHT:
1861 case K_C_RIGHT:
1862 do
1863 {
1864 if (ccline.cmdpos >= ccline.cmdlen)
1865 break;
1866 i = cmdline_charsize(ccline.cmdpos);
1867 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1868 break;
1869 ccline.cmdspos += i;
1870#ifdef FEAT_MBYTE
1871 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001872 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 + ccline.cmdpos);
1874 else
1875#endif
1876 ++ccline.cmdpos;
1877 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001878 while ((c == K_S_RIGHT || c == K_C_RIGHT
1879 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1881#ifdef FEAT_MBYTE
1882 if (has_mbyte)
1883 set_cmdspos_cursor();
1884#endif
1885 goto cmdline_not_changed;
1886
1887 case K_LEFT:
1888 case K_S_LEFT:
1889 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001890 if (ccline.cmdpos == 0)
1891 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 do
1893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 --ccline.cmdpos;
1895#ifdef FEAT_MBYTE
1896 if (has_mbyte) /* move to first byte of char */
1897 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1898 ccline.cmdbuff + ccline.cmdpos);
1899#endif
1900 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1901 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001902 while (ccline.cmdpos > 0
1903 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001904 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1906#ifdef FEAT_MBYTE
1907 if (has_mbyte)
1908 set_cmdspos_cursor();
1909#endif
1910 goto cmdline_not_changed;
1911
1912 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001913 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001914 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar4770d092006-01-12 23:22:24 +00001916#ifdef FEAT_GUI_W32
1917 /* On Win32 ignore <M-F4>, we get it when closing the window was
1918 * cancelled. */
1919 case K_F4:
1920 if (mod_mask == MOD_MASK_ALT)
1921 {
1922 redrawcmd(); /* somehow the cmdline is cleared */
1923 goto cmdline_not_changed;
1924 }
1925 break;
1926#endif
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#ifdef FEAT_MOUSE
1929 case K_MIDDLEDRAG:
1930 case K_MIDDLERELEASE:
1931 goto cmdline_not_changed; /* Ignore mouse */
1932
1933 case K_MIDDLEMOUSE:
1934# ifdef FEAT_GUI
1935 /* When GUI is active, also paste when 'mouse' is empty */
1936 if (!gui.in_use)
1937# endif
1938 if (!mouse_has(MOUSE_COMMAND))
1939 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001940# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001942 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001944# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001945 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 redrawcmd();
1947 goto cmdline_changed;
1948
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001949# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001951 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 redrawcmd();
1953 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956 case K_LEFTDRAG:
1957 case K_LEFTRELEASE:
1958 case K_RIGHTDRAG:
1959 case K_RIGHTRELEASE:
1960 /* Ignore drag and release events when the button-down wasn't
1961 * seen before. */
1962 if (ignore_drag_release)
1963 goto cmdline_not_changed;
1964 /* FALLTHROUGH */
1965 case K_LEFTMOUSE:
1966 case K_RIGHTMOUSE:
1967 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1968 ignore_drag_release = TRUE;
1969 else
1970 ignore_drag_release = FALSE;
1971# ifdef FEAT_GUI
1972 /* When GUI is active, also move when 'mouse' is empty */
1973 if (!gui.in_use)
1974# endif
1975 if (!mouse_has(MOUSE_COMMAND))
1976 goto cmdline_not_changed; /* Ignore mouse */
1977# ifdef FEAT_CLIPBOARD
1978 if (mouse_row < cmdline_row && clip_star.available)
1979 {
1980 int button, is_click, is_drag;
1981
1982 /*
1983 * Handle modeless selection.
1984 */
1985 button = get_mouse_button(KEY2TERMCAP1(c),
1986 &is_click, &is_drag);
1987 if (mouse_model_popup() && button == MOUSE_LEFT
1988 && (mod_mask & MOD_MASK_SHIFT))
1989 {
1990 /* Translate shift-left to right button. */
1991 button = MOUSE_RIGHT;
1992 mod_mask &= ~MOD_MASK_SHIFT;
1993 }
1994 clip_modeless(button, is_click, is_drag);
1995 goto cmdline_not_changed;
1996 }
1997# endif
1998
1999 set_cmdspos();
2000 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2001 ++ccline.cmdpos)
2002 {
2003 i = cmdline_charsize(ccline.cmdpos);
2004 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2005 && mouse_col < ccline.cmdspos % Columns + i)
2006 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002007# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (has_mbyte)
2009 {
2010 /* Count ">" for double-wide char that doesn't fit. */
2011 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002012 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 + ccline.cmdpos) - 1;
2014 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 ccline.cmdspos += i;
2017 }
2018 goto cmdline_not_changed;
2019
2020 /* Mouse scroll wheel: ignored here */
2021 case K_MOUSEDOWN:
2022 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002023 case K_MOUSELEFT:
2024 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 /* Alternate buttons ignored here */
2026 case K_X1MOUSE:
2027 case K_X1DRAG:
2028 case K_X1RELEASE:
2029 case K_X2MOUSE:
2030 case K_X2DRAG:
2031 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002032 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 goto cmdline_not_changed;
2034
2035#endif /* FEAT_MOUSE */
2036
2037#ifdef FEAT_GUI
2038 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2039 case K_LEFTRELEASE_NM:
2040 goto cmdline_not_changed;
2041
2042 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002043 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
2045 gui_do_scroll();
2046 redrawcmd();
2047 }
2048 goto cmdline_not_changed;
2049
2050 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002051 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002053 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 redrawcmd();
2055 }
2056 goto cmdline_not_changed;
2057#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002058#ifdef FEAT_GUI_TABLINE
2059 case K_TABLINE:
2060 case K_TABMENU:
2061 /* Don't want to change any tabs here. Make sure the same tab
2062 * is still selected. */
2063 if (gui_use_tabline())
2064 gui_mch_set_curtab(tabpage_index(curtab));
2065 goto cmdline_not_changed;
2066#endif
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 case K_SELECT: /* end of Select mode mapping - ignore */
2069 goto cmdline_not_changed;
2070
2071 case Ctrl_B: /* begin of command line */
2072 case K_HOME:
2073 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 case K_S_HOME:
2075 case K_C_HOME:
2076 ccline.cmdpos = 0;
2077 set_cmdspos();
2078 goto cmdline_not_changed;
2079
2080 case Ctrl_E: /* end of command line */
2081 case K_END:
2082 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 case K_S_END:
2084 case K_C_END:
2085 ccline.cmdpos = ccline.cmdlen;
2086 set_cmdspos_cursor();
2087 goto cmdline_not_changed;
2088
2089 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002090 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 break;
2092 goto cmdline_changed;
2093
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002094 case Ctrl_L:
2095#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002096 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002097 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002098#endif
2099
2100 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002101 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 break;
2103 goto cmdline_changed;
2104
2105 case Ctrl_N: /* next match */
2106 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002107 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002109 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2110 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002112 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002115 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 case K_UP:
2117 case K_DOWN:
2118 case K_S_UP:
2119 case K_S_DOWN:
2120 case K_PAGEUP:
2121 case K_KPAGEUP:
2122 case K_PAGEDOWN:
2123 case K_KPAGEDOWN:
2124 if (hislen == 0 || firstc == NUL) /* no history */
2125 goto cmdline_not_changed;
2126
2127 i = hiscnt;
2128
2129 /* save current command string so it can be restored later */
2130 if (lookfor == NULL)
2131 {
2132 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2133 goto cmdline_not_changed;
2134 lookfor[ccline.cmdpos] = NUL;
2135 }
2136
2137 j = (int)STRLEN(lookfor);
2138 for (;;)
2139 {
2140 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002141 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002142 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 if (hiscnt == hislen) /* first time */
2145 hiscnt = hisidx[histype];
2146 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2147 hiscnt = hislen - 1;
2148 else if (hiscnt != hisidx[histype] + 1)
2149 --hiscnt;
2150 else /* at top of list */
2151 {
2152 hiscnt = i;
2153 break;
2154 }
2155 }
2156 else /* one step forwards */
2157 {
2158 /* on last entry, clear the line */
2159 if (hiscnt == hisidx[histype])
2160 {
2161 hiscnt = hislen;
2162 break;
2163 }
2164
2165 /* not on a history line, nothing to do */
2166 if (hiscnt == hislen)
2167 break;
2168 if (hiscnt == hislen - 1) /* wrap around */
2169 hiscnt = 0;
2170 else
2171 ++hiscnt;
2172 }
2173 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2174 {
2175 hiscnt = i;
2176 break;
2177 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002178 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002179 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 || STRNCMP(history[histype][hiscnt].hisstr,
2181 lookfor, (size_t)j) == 0)
2182 break;
2183 }
2184
2185 if (hiscnt != i) /* jumped to other entry */
2186 {
2187 char_u *p;
2188 int len;
2189 int old_firstc;
2190
Bram Moolenaar438d1762018-09-30 17:11:48 +02002191 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002192 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 if (hiscnt == hislen)
2194 p = lookfor; /* back to the old one */
2195 else
2196 p = history[histype][hiscnt].hisstr;
2197
2198 if (histype == HIST_SEARCH
2199 && p != lookfor
2200 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2201 {
2202 /* Correct for the separator character used when
2203 * adding the history entry vs the one used now.
2204 * First loop: count length.
2205 * Second loop: copy the characters. */
2206 for (i = 0; i <= 1; ++i)
2207 {
2208 len = 0;
2209 for (j = 0; p[j] != NUL; ++j)
2210 {
2211 /* Replace old sep with new sep, unless it is
2212 * escaped. */
2213 if (p[j] == old_firstc
2214 && (j == 0 || p[j - 1] != '\\'))
2215 {
2216 if (i > 0)
2217 ccline.cmdbuff[len] = firstc;
2218 }
2219 else
2220 {
2221 /* Escape new sep, unless it is already
2222 * escaped. */
2223 if (p[j] == firstc
2224 && (j == 0 || p[j - 1] != '\\'))
2225 {
2226 if (i > 0)
2227 ccline.cmdbuff[len] = '\\';
2228 ++len;
2229 }
2230 if (i > 0)
2231 ccline.cmdbuff[len] = p[j];
2232 }
2233 ++len;
2234 }
2235 if (i == 0)
2236 {
2237 alloc_cmdbuff(len);
2238 if (ccline.cmdbuff == NULL)
2239 goto returncmd;
2240 }
2241 }
2242 ccline.cmdbuff[len] = NUL;
2243 }
2244 else
2245 {
2246 alloc_cmdbuff((int)STRLEN(p));
2247 if (ccline.cmdbuff == NULL)
2248 goto returncmd;
2249 STRCPY(ccline.cmdbuff, p);
2250 }
2251
2252 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2253 redrawcmd();
2254 goto cmdline_changed;
2255 }
2256 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002257#endif
2258 goto cmdline_not_changed;
2259
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002260#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002261 case Ctrl_G: /* next match */
2262 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002263 if (may_adjust_incsearch_highlighting(
2264 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002265 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#endif
2268
2269 case Ctrl_V:
2270 case Ctrl_Q:
2271#ifdef FEAT_MOUSE
2272 ignore_drag_release = TRUE;
2273#endif
2274 putcmdline('^', TRUE);
2275 c = get_literal(); /* get next (two) character(s) */
2276 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002277 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#ifdef FEAT_MBYTE
2279 /* may need to remove ^ when composing char was typed */
2280 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2281 {
2282 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2283 msg_putchar(' ');
2284 cursorcmd();
2285 }
2286#endif
2287 break;
2288
2289#ifdef FEAT_DIGRAPHS
2290 case Ctrl_K:
2291#ifdef FEAT_MOUSE
2292 ignore_drag_release = TRUE;
2293#endif
2294 putcmdline('?', TRUE);
2295#ifdef USE_ON_FLY_SCROLL
2296 dont_scroll = TRUE; /* disallow scrolling here */
2297#endif
2298 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002299 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (c != NUL)
2301 break;
2302
2303 redrawcmd();
2304 goto cmdline_not_changed;
2305#endif /* FEAT_DIGRAPHS */
2306
2307#ifdef FEAT_RIGHTLEFT
2308 case Ctrl__: /* CTRL-_: switch language mode */
2309 if (!p_ari)
2310 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002311# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (p_altkeymap)
2313 {
2314 cmd_fkmap = !cmd_fkmap;
2315 if (cmd_fkmap) /* in Farsi always in Insert mode */
2316 ccline.overstrike = FALSE;
2317 }
2318 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 cmd_hkmap = !cmd_hkmap;
2321 goto cmdline_not_changed;
2322#endif
2323
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002324 case K_PS:
2325 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2326 goto cmdline_changed;
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 default:
2329#ifdef UNIX
2330 if (c == intr_char)
2331 {
2332 gotesc = TRUE; /* will free ccline.cmdbuff after
2333 putting it in history */
2334 goto returncmd; /* back to Normal mode */
2335 }
2336#endif
2337 /*
2338 * Normal character with no special meaning. Just set mod_mask
2339 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2340 * the string <S-Space>. This should only happen after ^V.
2341 */
2342 if (!IS_SPECIAL(c))
2343 mod_mask = 0x0;
2344 break;
2345 }
2346 /*
2347 * End of switch on command line character.
2348 * We come here if we have a normal character.
2349 */
2350
Bram Moolenaarede3e632013-06-23 16:16:19 +02002351 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#ifdef FEAT_MBYTE
2353 /* Add ABBR_OFF for characters above 0x100, this is
2354 * what check_abbr() expects. */
2355 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2356#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002357 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 goto cmdline_changed;
2359
2360 /*
2361 * put the character in the command line
2362 */
2363 if (IS_SPECIAL(c) || mod_mask != 0)
2364 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2365 else
2366 {
2367#ifdef FEAT_MBYTE
2368 if (has_mbyte)
2369 {
2370 j = (*mb_char2bytes)(c, IObuff);
2371 IObuff[j] = NUL; /* exclude composing chars */
2372 put_on_cmdline(IObuff, j, TRUE);
2373 }
2374 else
2375#endif
2376 {
2377 IObuff[0] = c;
2378 put_on_cmdline(IObuff, 1, TRUE);
2379 }
2380 }
2381 goto cmdline_changed;
2382
2383/*
2384 * This part implements incremental searches for "/" and "?"
2385 * Jump to cmdline_not_changed when a character has been read but the command
2386 * line did not change. Then we only search and redraw if something changed in
2387 * the past.
2388 * Jump to cmdline_changed when the command line did change.
2389 * (Sorry for the goto's, I know it is ugly).
2390 */
2391cmdline_not_changed:
2392#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002393 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 continue;
2395#endif
2396
2397cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002398 /* Trigger CmdlineChanged autocommands. */
2399 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002402 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#endif
2404
2405#ifdef FEAT_RIGHTLEFT
2406 if (cmdmsg_rl
2407# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002408 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409# endif
2410 )
2411 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002412 * right-left typing. Not efficient, but it works.
2413 * Do it only when there are no characters left to read
2414 * to avoid useless intermediate redraws. */
2415 if (vpeekc() == NUL)
2416 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417#endif
2418 }
2419
2420returncmd:
2421
2422#ifdef FEAT_RIGHTLEFT
2423 cmdmsg_rl = FALSE;
2424#endif
2425
2426#ifdef FEAT_FKMAP
2427 cmd_fkmap = 0;
2428#endif
2429
2430 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002431 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002434 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435#endif
2436
2437 if (ccline.cmdbuff != NULL)
2438 {
2439 /*
2440 * Put line in history buffer (":" and "=" only when it was typed).
2441 */
2442#ifdef FEAT_CMDHIST
2443 if (ccline.cmdlen && firstc != NUL
2444 && (some_key_typed || histype == HIST_SEARCH))
2445 {
2446 add_to_history(histype, ccline.cmdbuff, TRUE,
2447 histype == HIST_SEARCH ? firstc : NUL);
2448 if (firstc == ':')
2449 {
2450 vim_free(new_last_cmdline);
2451 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2452 }
2453 }
2454#endif
2455
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002456 if (gotesc)
2457 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459
2460 /*
2461 * If the screen was shifted up, redraw the whole screen (later).
2462 * If the line is too long, clear it, so ruler and shown command do
2463 * not get printed in the middle of it.
2464 */
2465 msg_check();
2466 msg_scroll = save_msg_scroll;
2467 redir_off = FALSE;
2468
2469 /* When the command line was typed, no need for a wait-return prompt. */
2470 if (some_key_typed)
2471 need_wait_return = FALSE;
2472
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002473 /* Trigger CmdlineLeave autocommands. */
2474 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002475
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002477#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2479 im_save_status(b_im_ptr);
2480 im_set_active(FALSE);
2481#endif
2482#ifdef FEAT_MOUSE
2483 setmouse();
2484#endif
2485#ifdef CURSOR_SHAPE
2486 ui_cursor_shape(); /* may show different cursor shape */
2487#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002488 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
Bram Moolenaar438d1762018-09-30 17:11:48 +02002490theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002491 {
2492 char_u *p = ccline.cmdbuff;
2493
Bram Moolenaar438d1762018-09-30 17:11:48 +02002494 if (did_save_ccline)
2495 restore_cmdline(&save_ccline);
2496 else
2497 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002498 return p;
2499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
2502#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2503/*
2504 * Get a command line with a prompt.
2505 * This is prepared to be called recursively from getcmdline() (e.g. by
2506 * f_input() when evaluating an expression from CTRL-R =).
2507 * Returns the command line in allocated memory, or NULL.
2508 */
2509 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002510getcmdline_prompt(
2511 int firstc,
2512 char_u *prompt, /* command line prompt */
2513 int attr, /* attributes for prompt */
2514 int xp_context, /* type of expansion */
2515 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 char_u *s;
2518 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002519 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002521 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaar438d1762018-09-30 17:11:48 +02002523 if (ccline.cmdbuff != NULL)
2524 {
2525 // Save the values of the current cmdline and restore them below.
2526 save_cmdline(&save_ccline);
2527 did_save_ccline = TRUE;
2528 }
2529
2530 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 ccline.cmdprompt = prompt;
2532 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002533# ifdef FEAT_EVAL
2534 ccline.xp_context = xp_context;
2535 ccline.xp_arg = xp_arg;
2536 ccline.input_fn = (firstc == '@');
2537# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002538 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002539 s = getcmdline_int(firstc, 1L, 0, FALSE);
2540
2541 if (did_save_ccline)
2542 restore_cmdline(&save_ccline);
2543
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002544 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002545 /* Restore msg_col, the prompt from input() may have changed it.
2546 * But only if called recursively and the commandline is therefore being
2547 * restored to an old one; if not, the input() prompt stays on the screen,
2548 * so we need its modified msg_col left intact. */
2549 if (ccline.cmdbuff != NULL)
2550 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 return s;
2553}
2554#endif
2555
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002556/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002557 * Return TRUE when the text must not be changed and we can't switch to
2558 * another window or buffer. Used when editing the command line, evaluating
2559 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002560 */
2561 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002562text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002563{
2564#ifdef FEAT_CMDWIN
2565 if (cmdwin_type != 0)
2566 return TRUE;
2567#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002568 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002569}
2570
2571/*
2572 * Give an error message for a command that isn't allowed while the cmdline
2573 * window is open or editing the cmdline in another way.
2574 */
2575 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002576text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002577{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002578 EMSG(_(get_text_locked_msg()));
2579}
2580
2581 char_u *
2582get_text_locked_msg(void)
2583{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002584#ifdef FEAT_CMDWIN
2585 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002586 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002587#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002588 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002589}
2590
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002591/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002592 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2593 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002594 */
2595 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002596curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002597{
2598 if (curbuf_lock > 0)
2599 {
2600 EMSG(_("E788: Not allowed to edit another buffer now"));
2601 return TRUE;
2602 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002603 return allbuf_locked();
2604}
2605
2606/*
2607 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2608 * message.
2609 */
2610 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002611allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002612{
2613 if (allbuf_lock > 0)
2614 {
2615 EMSG(_("E811: Not allowed to change buffer information now"));
2616 return TRUE;
2617 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002618 return FALSE;
2619}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002622cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2625 if (cmdline_star > 0) /* showing '*', always 1 position */
2626 return 1;
2627#endif
2628 return ptr2cells(ccline.cmdbuff + idx);
2629}
2630
2631/*
2632 * Compute the offset of the cursor on the command line for the prompt and
2633 * indent.
2634 */
2635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002636set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002638 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ccline.cmdspos = 1 + ccline.cmdindent;
2640 else
2641 ccline.cmdspos = 0 + ccline.cmdindent;
2642}
2643
2644/*
2645 * Compute the screen position for the cursor on the command line.
2646 */
2647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002648set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 int i, m, c;
2651
2652 set_cmdspos();
2653 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002656 if (m < 0) /* overflow, Columns or Rows at weird value */
2657 m = MAXCOL;
2658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 else
2660 m = MAXCOL;
2661 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2662 {
2663 c = cmdline_charsize(i);
2664#ifdef FEAT_MBYTE
2665 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2666 if (has_mbyte)
2667 correct_cmdspos(i, c);
2668#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002669 /* If the cmdline doesn't fit, show cursor on last visible char.
2670 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if ((ccline.cmdspos += c) >= m)
2672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 ccline.cmdspos -= c;
2674 break;
2675 }
2676#ifdef FEAT_MBYTE
2677 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002678 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#endif
2680 }
2681}
2682
2683#ifdef FEAT_MBYTE
2684/*
2685 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2686 * character that doesn't fit, so that a ">" must be displayed.
2687 */
2688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002689correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002691 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2693 && ccline.cmdspos % Columns + cells > Columns)
2694 ccline.cmdspos++;
2695}
2696#endif
2697
2698/*
2699 * Get an Ex command line for the ":" command.
2700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002702getexline(
2703 int c, /* normally ':', NUL for ":append" */
2704 void *cookie UNUSED,
2705 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 /* When executing a register, remove ':' that's in front of each line. */
2708 if (exec_from_reg && vpeekc() == ':')
2709 (void)vgetc();
2710 return getcmdline(c, 1L, indent);
2711}
2712
2713/*
2714 * Get an Ex command line for Ex mode.
2715 * In Ex mode we only use the OS supplied line editing features and no
2716 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002720getexmodeline(
2721 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002723 void *cookie UNUSED,
2724 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002726 garray_T line_ga;
2727 char_u *pend;
2728 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002729 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 int escaped = FALSE; /* CTRL-V typed */
2731 int vcol = 0;
2732 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002733 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002734 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /* Switch cursor on now. This avoids that it happens after the "\n", which
2737 * confuses the system function that computes tabstops. */
2738 cursor_on();
2739
2740 /* always start in column 0; write a newline if necessary */
2741 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002742 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002746 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 if (p_prompt)
2748 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 while (indent-- > 0)
2750 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
2753
2754 ga_init2(&line_ga, 1, 30);
2755
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002756 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002758 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002759 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002760 while (indent >= 8)
2761 {
2762 ga_append(&line_ga, TAB);
2763 msg_puts((char_u *)" ");
2764 indent -= 8;
2765 }
2766 while (indent-- > 0)
2767 {
2768 ga_append(&line_ga, ' ');
2769 msg_putchar(' ');
2770 }
2771 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002772 ++no_mapping;
2773 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 /*
2776 * Get the line, one character at a time.
2777 */
2778 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002781 long sw;
2782 char_u *s;
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (ga_grow(&line_ga, 40) == FAIL)
2785 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
Bram Moolenaarba748c82017-02-26 14:00:07 +01002787 /*
2788 * Get one character at a time.
2789 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002790 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002791
2792 /* Check for a ":normal" command and no more characters left. */
2793 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2794 c1 = '\n';
2795 else
2796 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799 * Handle line editing.
2800 * Previously this was left to the system, putting the terminal in
2801 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 msg_putchar('\n');
2806 break;
2807 }
2808
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002809 if (c1 == K_PS)
2810 {
2811 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2812 goto redraw;
2813 }
2814
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815 if (!escaped)
2816 {
2817 /* CR typed means "enter", which is NL */
2818 if (c1 == '\r')
2819 c1 = '\n';
2820
2821 if (c1 == BS || c1 == K_BS
2822 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 if (line_ga.ga_len > 0)
2825 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002826#ifdef FEAT_MBYTE
2827 if (has_mbyte)
2828 {
2829 p = (char_u *)line_ga.ga_data;
2830 p[line_ga.ga_len] = NUL;
2831 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2832 line_ga.ga_len -= len;
2833 }
2834 else
2835#endif
2836 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002837 goto redraw;
2838 }
2839 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002842 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844 msg_col = startcol;
2845 msg_clr_eos();
2846 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002847 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 }
2849
2850 if (c1 == Ctrl_T)
2851 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002852 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 p = (char_u *)line_ga.ga_data;
2854 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002855 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002856 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002858 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002860 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002861 p = (char_u *)line_ga.ga_data;
2862 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2864 *s = ' ';
2865 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002867redraw:
2868 /* redraw the line */
2869 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002871 p = (char_u *)line_ga.ga_data;
2872 p[line_ga.ga_len] = NUL;
2873 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002879 msg_putchar(' ');
2880 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002881 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002885 len = MB_PTR2LEN(p);
2886 msg_outtrans_len(p, len);
2887 vcol += ptr2cells(p);
2888 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
2890 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002891 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002892 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002893 continue;
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002896 if (c1 == Ctrl_D)
2897 {
2898 /* Delete one shiftwidth. */
2899 p = (char_u *)line_ga.ga_data;
2900 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902 if (prev_char == '^')
2903 ex_keep_indent = TRUE;
2904 indent = 0;
2905 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
2907 else
2908 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002910 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002911 if (indent > 0)
2912 {
2913 --indent;
2914 indent -= indent % get_sw_value(curbuf);
2915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002917 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002918 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002919 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2921 --line_ga.ga_len;
2922 }
2923 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002925
2926 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2927 {
2928 escaped = TRUE;
2929 continue;
2930 }
2931
2932 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2933 if (IS_SPECIAL(c1))
2934 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002936
2937 if (IS_SPECIAL(c1))
2938 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002939#ifdef FEAT_MBYTE
2940 if (has_mbyte)
2941 len = (*mb_char2bytes)(c1,
2942 (char_u *)line_ga.ga_data + line_ga.ga_len);
2943 else
2944#endif
2945 {
2946 len = 1;
2947 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2948 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002949 if (c1 == '\n')
2950 msg_putchar('\n');
2951 else if (c1 == TAB)
2952 {
2953 /* Don't use chartabsize(), 'ts' can be different */
2954 do
2955 {
2956 msg_putchar(' ');
2957 } while (++vcol % 8);
2958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002961 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002962 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002963 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002965 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002966 escaped = FALSE;
2967
2968 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002969 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002970
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002971 /* We are done when a NL is entered, but not when it comes after an
2972 * odd number of backslashes, that results in a NUL. */
2973 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002975 int bcount = 0;
2976
2977 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2978 ++bcount;
2979
2980 if (bcount > 0)
2981 {
2982 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2983 * "\NL", etc. */
2984 line_ga.ga_len -= (bcount + 1) / 2;
2985 pend -= (bcount + 1) / 2;
2986 pend[-1] = '\n';
2987 }
2988
2989 if ((bcount & 1) == 0)
2990 {
2991 --line_ga.ga_len;
2992 --pend;
2993 *pend = NUL;
2994 break;
2995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997 }
2998
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002999 --no_mapping;
3000 --allow_keys;
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 /* make following messages go to the next line */
3003 msg_didout = FALSE;
3004 msg_col = 0;
3005 if (msg_row < Rows - 1)
3006 ++msg_row;
3007 emsg_on_display = FALSE; /* don't want ui_delay() */
3008
3009 if (got_int)
3010 ga_clear(&line_ga);
3011
3012 return (char_u *)line_ga.ga_data;
3013}
3014
Bram Moolenaare344bea2005-09-01 20:46:49 +00003015# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3016 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017/*
3018 * Return TRUE if ccline.overstrike is on.
3019 */
3020 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003021cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 return ccline.overstrike;
3024}
3025
3026/*
3027 * Return TRUE if the cursor is at the end of the cmdline.
3028 */
3029 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003030cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 return (ccline.cmdpos >= ccline.cmdlen);
3033}
3034#endif
3035
Bram Moolenaar9372a112005-12-06 19:59:18 +00003036#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037/*
3038 * Return the virtual column number at the current cursor position.
3039 * This is used by the IM code to obtain the start of the preedit string.
3040 */
3041 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3045 return MAXCOL;
3046
3047# ifdef FEAT_MBYTE
3048 if (has_mbyte)
3049 {
3050 colnr_T col;
3051 int i = 0;
3052
3053 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003054 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 return col;
3057 }
3058 else
3059# endif
3060 return ccline.cmdpos;
3061}
3062#endif
3063
3064#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3065/*
3066 * If part of the command line is an IM preedit string, redraw it with
3067 * IM feedback attributes. The cursor position is restored after drawing.
3068 */
3069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003070redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 if ((State & CMDLINE)
3073 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003074 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 && !p_imdisable
3076 && im_is_preediting())
3077 {
3078 int cmdpos = 0;
3079 int cmdspos;
3080 int old_row;
3081 int old_col;
3082 colnr_T col;
3083
3084 old_row = msg_row;
3085 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003086 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088# ifdef FEAT_MBYTE
3089 if (has_mbyte)
3090 {
3091 for (col = 0; col < preedit_start_col
3092 && cmdpos < ccline.cmdlen; ++col)
3093 {
3094 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003095 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
3097 }
3098 else
3099# endif
3100 {
3101 cmdspos += preedit_start_col;
3102 cmdpos += preedit_start_col;
3103 }
3104
3105 msg_row = cmdline_row + (cmdspos / (int)Columns);
3106 msg_col = cmdspos % (int)Columns;
3107 if (msg_row >= Rows)
3108 msg_row = Rows - 1;
3109
3110 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3111 {
3112 int char_len;
3113 int char_attr;
3114
3115 char_attr = im_get_feedback_attr(col);
3116 if (char_attr < 0)
3117 break; /* end of preedit string */
3118
3119# ifdef FEAT_MBYTE
3120 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003121 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 else
3123# endif
3124 char_len = 1;
3125
3126 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3127 cmdpos += char_len;
3128 }
3129
3130 msg_row = old_row;
3131 msg_col = old_col;
3132 }
3133}
3134#endif /* FEAT_XIM && FEAT_GUI_GTK */
3135
3136/*
3137 * Allocate a new command line buffer.
3138 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 */
3140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003141alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 /*
3144 * give some extra space to avoid having to allocate all the time
3145 */
3146 if (len < 80)
3147 len = 100;
3148 else
3149 len += 20;
3150
3151 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3152 ccline.cmdbufflen = len;
3153}
3154
3155/*
3156 * Re-allocate the command line to length len + something extra.
3157 * return FAIL for failure, OK otherwise
3158 */
3159 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003160realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 char_u *p;
3163
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003164 if (len < ccline.cmdbufflen)
3165 return OK; /* no need to resize */
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 p = ccline.cmdbuff;
3168 alloc_cmdbuff(len); /* will get some more */
3169 if (ccline.cmdbuff == NULL) /* out of memory */
3170 {
3171 ccline.cmdbuff = p; /* keep the old one */
3172 return FAIL;
3173 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003174 /* There isn't always a NUL after the command, but it may need to be
3175 * there, thus copy up to the NUL and add a NUL. */
3176 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3177 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003179
3180 if (ccline.xpc != NULL
3181 && ccline.xpc->xp_pattern != NULL
3182 && ccline.xpc->xp_context != EXPAND_NOTHING
3183 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3184 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003185 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003186
3187 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3188 * to point into the newly allocated memory. */
3189 if (i >= 0 && i <= ccline.cmdlen)
3190 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3191 }
3192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 return OK;
3194}
3195
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003196#if defined(FEAT_ARABIC) || defined(PROTO)
3197static char_u *arshape_buf = NULL;
3198
3199# if defined(EXITFREE) || defined(PROTO)
3200 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003201free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202{
3203 vim_free(arshape_buf);
3204}
3205# endif
3206#endif
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208/*
3209 * Draw part of the cmdline at the current cursor position. But draw stars
3210 * when cmdline_star is TRUE.
3211 */
3212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003213draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3216 int i;
3217
3218 if (cmdline_star > 0)
3219 for (i = 0; i < len; ++i)
3220 {
3221 msg_putchar('*');
3222# ifdef FEAT_MBYTE
3223 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003224 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225# endif
3226 }
3227 else
3228#endif
3229#ifdef FEAT_ARABIC
3230 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 static int buflen = 0;
3233 char_u *p;
3234 int j;
3235 int newlen = 0;
3236 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003237 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 int prev_c = 0;
3239 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003240 int u8c;
3241 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 /*
3245 * Do arabic shaping into a temporary buffer. This is very
3246 * inefficient!
3247 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003248 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 /* Re-allocate the buffer. We keep it around to avoid a lot of
3251 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003252 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003253 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 arshape_buf = alloc(buflen);
3255 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 return; /* out of memory */
3257 }
3258
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003259 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3260 {
3261 /* Prepend a space to draw the leading composing char on. */
3262 arshape_buf[0] = ' ';
3263 newlen = 1;
3264 }
3265
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 for (j = start; j < start + len; j += mb_l)
3267 {
3268 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003269 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003270 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (ARABIC_CHAR(u8c))
3272 {
3273 /* Do Arabic shaping. */
3274 if (cmdmsg_rl)
3275 {
3276 /* displaying from right to left */
3277 pc = prev_c;
3278 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003279 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (j + mb_l >= start + len)
3281 nc = NUL;
3282 else
3283 nc = utf_ptr2char(p + mb_l);
3284 }
3285 else
3286 {
3287 /* displaying from left to right */
3288 if (j + mb_l >= start + len)
3289 pc = NUL;
3290 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003291 {
3292 int pcc[MAX_MCO];
3293
3294 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003296 pc1 = pcc[0];
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 nc = prev_c;
3299 }
3300 prev_c = u8c;
3301
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003302 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003304 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003305 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003307 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3308 if (u8cc[1] != 0)
3309 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312 }
3313 else
3314 {
3315 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 newlen += mb_l;
3318 }
3319 }
3320
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323 else
3324#endif
3325 msg_outtrans_len(ccline.cmdbuff + start, len);
3326}
3327
3328/*
3329 * Put a character on the command line. Shifts the following text to the
3330 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3331 * "c" must be printable (fit in one display cell)!
3332 */
3333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003334putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 if (cmd_silent)
3337 return;
3338 msg_no_more = TRUE;
3339 msg_putchar(c);
3340 if (shift)
3341 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3342 msg_no_more = FALSE;
3343 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003344 extra_char = c;
3345 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346}
3347
3348/*
3349 * Undo a putcmdline(c, FALSE).
3350 */
3351 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003352unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 if (cmd_silent)
3355 return;
3356 msg_no_more = TRUE;
3357 if (ccline.cmdlen == ccline.cmdpos)
3358 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003359#ifdef FEAT_MBYTE
3360 else if (has_mbyte)
3361 draw_cmdline(ccline.cmdpos,
3362 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else
3365 draw_cmdline(ccline.cmdpos, 1);
3366 msg_no_more = FALSE;
3367 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003368 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369}
3370
3371/*
3372 * Put the given string, of the given length, onto the command line.
3373 * If len is -1, then STRLEN() is used to calculate the length.
3374 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3375 * part will be redrawn, otherwise it will not. If this function is called
3376 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3377 * called afterwards.
3378 */
3379 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003380put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 int retval;
3383 int i;
3384 int m;
3385 int c;
3386
3387 if (len < 0)
3388 len = (int)STRLEN(str);
3389
3390 /* Check if ccline.cmdbuff needs to be longer */
3391 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003392 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 else
3394 retval = OK;
3395 if (retval == OK)
3396 {
3397 if (!ccline.overstrike)
3398 {
3399 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3400 ccline.cmdbuff + ccline.cmdpos,
3401 (size_t)(ccline.cmdlen - ccline.cmdpos));
3402 ccline.cmdlen += len;
3403 }
3404 else
3405 {
3406#ifdef FEAT_MBYTE
3407 if (has_mbyte)
3408 {
3409 /* Count nr of characters in the new string. */
3410 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003411 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 ++m;
3413 /* Count nr of bytes in cmdline that are overwritten by these
3414 * characters. */
3415 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003416 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 --m;
3418 if (i < ccline.cmdlen)
3419 {
3420 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3421 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3422 ccline.cmdlen += ccline.cmdpos + len - i;
3423 }
3424 else
3425 ccline.cmdlen = ccline.cmdpos + len;
3426 }
3427 else
3428#endif
3429 if (ccline.cmdpos + len > ccline.cmdlen)
3430 ccline.cmdlen = ccline.cmdpos + len;
3431 }
3432 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3433 ccline.cmdbuff[ccline.cmdlen] = NUL;
3434
3435#ifdef FEAT_MBYTE
3436 if (enc_utf8)
3437 {
3438 /* When the inserted text starts with a composing character,
3439 * backup to the character before it. There could be two of them.
3440 */
3441 i = 0;
3442 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3443 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3444 {
3445 i = (*mb_head_off)(ccline.cmdbuff,
3446 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3447 ccline.cmdpos -= i;
3448 len += i;
3449 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3450 }
3451# ifdef FEAT_ARABIC
3452 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3453 {
3454 /* Check the previous character for Arabic combining pair. */
3455 i = (*mb_head_off)(ccline.cmdbuff,
3456 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3457 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3458 + ccline.cmdpos - i), c))
3459 {
3460 ccline.cmdpos -= i;
3461 len += i;
3462 }
3463 else
3464 i = 0;
3465 }
3466# endif
3467 if (i != 0)
3468 {
3469 /* Also backup the cursor position. */
3470 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3471 ccline.cmdspos -= i;
3472 msg_col -= i;
3473 if (msg_col < 0)
3474 {
3475 msg_col += Columns;
3476 --msg_row;
3477 }
3478 }
3479 }
3480#endif
3481
3482 if (redraw && !cmd_silent)
3483 {
3484 msg_no_more = TRUE;
3485 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003486 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3488 /* Avoid clearing the rest of the line too often. */
3489 if (cmdline_row != i || ccline.overstrike)
3490 msg_clr_eos();
3491 msg_no_more = FALSE;
3492 }
3493#ifdef FEAT_FKMAP
3494 /*
3495 * If we are in Farsi command mode, the character input must be in
3496 * Insert mode. So do not advance the cmdpos.
3497 */
3498 if (!cmd_fkmap)
3499#endif
3500 {
3501 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003504 if (m < 0) /* overflow, Columns or Rows at weird value */
3505 m = MAXCOL;
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 else
3508 m = MAXCOL;
3509 for (i = 0; i < len; ++i)
3510 {
3511 c = cmdline_charsize(ccline.cmdpos);
3512#ifdef FEAT_MBYTE
3513 /* count ">" for a double-wide char that doesn't fit. */
3514 if (has_mbyte)
3515 correct_cmdspos(ccline.cmdpos, c);
3516#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003517 /* Stop cursor at the end of the screen, but do increment the
3518 * insert position, so that entering a very long command
3519 * works, even though you can't see it. */
3520 if (ccline.cmdspos + c < m)
3521 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#ifdef FEAT_MBYTE
3523 if (has_mbyte)
3524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003525 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (c > len - i - 1)
3527 c = len - i - 1;
3528 ccline.cmdpos += c;
3529 i += c;
3530 }
3531#endif
3532 ++ccline.cmdpos;
3533 }
3534 }
3535 }
3536 if (redraw)
3537 msg_check();
3538 return retval;
3539}
3540
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003541static struct cmdline_info prev_ccline;
3542static int prev_ccline_used = FALSE;
3543
3544/*
3545 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3546 * and overwrite it. But get_cmdline_str() may need it, thus make it
3547 * available globally in prev_ccline.
3548 */
3549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003550save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003551{
3552 if (!prev_ccline_used)
3553 {
3554 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3555 prev_ccline_used = TRUE;
3556 }
3557 *ccp = prev_ccline;
3558 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003559 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003560}
3561
3562/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003563 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003564 */
3565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003566restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003567{
3568 ccline = prev_ccline;
3569 prev_ccline = *ccp;
3570}
3571
Bram Moolenaar8299df92004-07-10 09:47:34 +00003572/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003573 * Paste a yank register into the command line.
3574 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003575 * insert_reg() can't be used here, because special characters from the
3576 * register contents will be interpreted as commands.
3577 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003578 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003579 */
3580 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003581cmdline_paste(
3582 int regname,
3583 int literally, /* Insert text literally instead of "as typed" */
3584 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003585{
3586 long i;
3587 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003588 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003589 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003590
3591 /* check for valid regname; also accept special characters for CTRL-R in
3592 * the command line */
3593 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003594 && regname != Ctrl_A && regname != Ctrl_L
3595 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596 return FAIL;
3597
3598 /* A register containing CTRL-R can cause an endless loop. Allow using
3599 * CTRL-C to break the loop. */
3600 line_breakcheck();
3601 if (got_int)
3602 return FAIL;
3603
3604#ifdef FEAT_CLIPBOARD
3605 regname = may_get_selection(regname);
3606#endif
3607
Bram Moolenaar438d1762018-09-30 17:11:48 +02003608 // Need to set "textlock" to avoid nasty things like going to another
3609 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003610 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003611 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003612 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003613
3614 if (i)
3615 {
3616 /* Got the value of a special register in "arg". */
3617 if (arg == NULL)
3618 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003619
3620 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3621 * part of the word. */
3622 p = arg;
3623 if (p_is && regname == Ctrl_W)
3624 {
3625 char_u *w;
3626 int len;
3627
3628 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003629 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003630 {
3631#ifdef FEAT_MBYTE
3632 if (has_mbyte)
3633 {
3634 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3635 if (!vim_iswordc(mb_ptr2char(w - len)))
3636 break;
3637 w -= len;
3638 }
3639 else
3640#endif
3641 {
3642 if (!vim_iswordc(w[-1]))
3643 break;
3644 --w;
3645 }
3646 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003647 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003648 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3649 p += len;
3650 }
3651
3652 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003653 if (allocated)
3654 vim_free(arg);
3655 return OK;
3656 }
3657
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003658 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003659}
3660
3661/*
3662 * Put a string on the command line.
3663 * When "literally" is TRUE, insert literally.
3664 * When "literally" is FALSE, insert as typed, but don't leave the command
3665 * line.
3666 */
3667 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003668cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003669{
3670 int c, cv;
3671
3672 if (literally)
3673 put_on_cmdline(s, -1, TRUE);
3674 else
3675 while (*s != NUL)
3676 {
3677 cv = *s;
3678 if (cv == Ctrl_V && s[1])
3679 ++s;
3680#ifdef FEAT_MBYTE
3681 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003682 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003683 else
3684#endif
3685 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003686 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3687 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003688#ifdef UNIX
3689 || c == intr_char
3690#endif
3691 || (c == Ctrl_BSL && *s == Ctrl_N))
3692 stuffcharReadbuff(Ctrl_V);
3693 stuffcharReadbuff(c);
3694 }
3695}
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697#ifdef FEAT_WILDMENU
3698/*
3699 * Delete characters on the command line, from "from" to the current
3700 * position.
3701 */
3702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003703cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3706 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3707 ccline.cmdlen -= ccline.cmdpos - from;
3708 ccline.cmdpos = from;
3709}
3710#endif
3711
3712/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003713 * This function is called when the screen size changes and with incremental
3714 * search and in other situations where the command line may have been
3715 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 */
3717 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003718redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003720 redrawcmdline_ex(TRUE);
3721}
3722
3723 void
3724redrawcmdline_ex(int do_compute_cmdrow)
3725{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (cmd_silent)
3727 return;
3728 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003729 if (do_compute_cmdrow)
3730 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 redrawcmd();
3732 cursorcmd();
3733}
3734
3735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003736redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
3738 int i;
3739
3740 if (cmd_silent)
3741 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003742 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 msg_putchar(ccline.cmdfirstc);
3744 if (ccline.cmdprompt != NULL)
3745 {
3746 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3747 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3748 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003749 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 --ccline.cmdindent;
3751 }
3752 else
3753 for (i = ccline.cmdindent; i > 0; --i)
3754 msg_putchar(' ');
3755}
3756
3757/*
3758 * Redraw what is currently on the command line.
3759 */
3760 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003761redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
3763 if (cmd_silent)
3764 return;
3765
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003766 /* when 'incsearch' is set there may be no command line while redrawing */
3767 if (ccline.cmdbuff == NULL)
3768 {
3769 windgoto(cmdline_row, 0);
3770 msg_clr_eos();
3771 return;
3772 }
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 msg_start();
3775 redrawcmdprompt();
3776
3777 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3778 msg_no_more = TRUE;
3779 draw_cmdline(0, ccline.cmdlen);
3780 msg_clr_eos();
3781 msg_no_more = FALSE;
3782
3783 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003784 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003785 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
3787 /*
3788 * An emsg() before may have set msg_scroll. This is used in normal mode,
3789 * in cmdline mode we can reset them now.
3790 */
3791 msg_scroll = FALSE; /* next message overwrites cmdline */
3792
3793 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3794 * in cmdline mode */
3795 skip_redraw = FALSE;
3796}
3797
3798 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003799compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003801 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 cmdline_row = Rows - 1;
3803 else
3804 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003805 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806}
3807
3808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003809cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
3811 if (cmd_silent)
3812 return;
3813
3814#ifdef FEAT_RIGHTLEFT
3815 if (cmdmsg_rl)
3816 {
3817 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3818 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3819 if (msg_row <= 0)
3820 msg_row = Rows - 1;
3821 }
3822 else
3823#endif
3824 {
3825 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3826 msg_col = ccline.cmdspos % (int)Columns;
3827 if (msg_row >= Rows)
3828 msg_row = Rows - 1;
3829 }
3830
3831 windgoto(msg_row, msg_col);
3832#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003833 if (p_imst == IM_ON_THE_SPOT)
3834 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#endif
3836#ifdef MCH_CURSOR_SHAPE
3837 mch_update_cursor();
3838#endif
3839}
3840
3841 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003842gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
3844 msg_start();
3845#ifdef FEAT_RIGHTLEFT
3846 if (cmdmsg_rl)
3847 msg_col = Columns - 1;
3848 else
3849#endif
3850 msg_col = 0; /* always start in column 0 */
3851 if (clr) /* clear the bottom line(s) */
3852 msg_clr_eos(); /* will reset clear_cmdline */
3853 windgoto(cmdline_row, 0);
3854}
3855
3856/*
3857 * Check the word in front of the cursor for an abbreviation.
3858 * Called when the non-id character "c" has been entered.
3859 * When an abbreviation is recognized it is removed from the text with
3860 * backspaces and the replacement string is inserted, followed by "c".
3861 */
3862 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003863ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003865 int spos = 0;
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3868 return FALSE;
3869
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003870 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3871 * Actually accepts any mark. */
3872 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3873 spos++;
3874 if (ccline.cmdlen - spos > 5
3875 && ccline.cmdbuff[spos] == '\''
3876 && ccline.cmdbuff[spos + 2] == ','
3877 && ccline.cmdbuff[spos + 3] == '\'')
3878 spos += 5;
3879 else
3880 /* check abbreviation from the beginning of the commandline */
3881 spos = 0;
3882
3883 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003886#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3887 static int
3888#ifdef __BORLANDC__
3889_RTLENTRYF
3890#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003891sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003892{
3893 char_u *p1 = *(char_u **)s1;
3894 char_u *p2 = *(char_u **)s2;
3895
3896 if (*p1 != '<' && *p2 == '<') return -1;
3897 if (*p1 == '<' && *p2 != '<') return 1;
3898 return STRCMP(p1, p2);
3899}
3900#endif
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902/*
3903 * Return FAIL if this is not an appropriate context in which to do
3904 * completion of anything, return OK if it is (even if there are no matches).
3905 * For the caller, this means that the character is just passed through like a
3906 * normal character (instead of being expanded). This allows :s/^I^D etc.
3907 */
3908 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003909nextwild(
3910 expand_T *xp,
3911 int type,
3912 int options, /* extra options for ExpandOne() */
3913 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
3915 int i, j;
3916 char_u *p1;
3917 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 int difflen;
3919 int v;
3920
3921 if (xp->xp_numfiles == -1)
3922 {
3923 set_expand_context(xp);
3924 cmd_showtail = expand_showtail(xp);
3925 }
3926
3927 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3928 {
3929 beep_flush();
3930 return OK; /* Something illegal on command line */
3931 }
3932 if (xp->xp_context == EXPAND_NOTHING)
3933 {
3934 /* Caller can use the character as a normal char instead */
3935 return FAIL;
3936 }
3937
3938 MSG_PUTS("..."); /* show that we are busy */
3939 out_flush();
3940
3941 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003942 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 if (type == WILD_NEXT || type == WILD_PREV)
3945 {
3946 /*
3947 * Get next/previous match for a previous expanded pattern.
3948 */
3949 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3950 }
3951 else
3952 {
3953 /*
3954 * Translate string into pattern and expand it.
3955 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003956 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3957 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 p2 = NULL;
3959 else
3960 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003961 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003962 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3963 if (escape)
3964 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003965
3966 if (p_wic)
3967 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003968 p2 = ExpandOne(xp, p1,
3969 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003970 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003972 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (p2 != NULL && type == WILD_LONGEST)
3974 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003975 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (ccline.cmdbuff[i + j] == '*'
3977 || ccline.cmdbuff[i + j] == '?')
3978 break;
3979 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003980 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 }
3983 }
3984
3985 if (p2 != NULL && !got_int)
3986 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003987 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003988 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003990 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 xp->xp_pattern = ccline.cmdbuff + i;
3992 }
3993 else
3994 v = OK;
3995 if (v == OK)
3996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003997 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3998 &ccline.cmdbuff[ccline.cmdpos],
3999 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4000 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 ccline.cmdlen += difflen;
4002 ccline.cmdpos += difflen;
4003 }
4004 }
4005 vim_free(p2);
4006
4007 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004008 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 /* When expanding a ":map" command and no matches are found, assume that
4011 * the key is supposed to be inserted literally */
4012 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4013 return FAIL;
4014
4015 if (xp->xp_numfiles <= 0 && p2 == NULL)
4016 beep_flush();
4017 else if (xp->xp_numfiles == 1)
4018 /* free expanded pattern */
4019 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4020
4021 return OK;
4022}
4023
4024/*
4025 * Do wildcard expansion on the string 'str'.
4026 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004027 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * Return NULL for failure.
4029 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004030 * "orig" is the originally expanded string, copied to allocated memory. It
4031 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4032 * WILD_PREV "orig" should be NULL.
4033 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004034 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4035 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 *
4037 * mode = WILD_FREE: just free previously expanded matches
4038 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4039 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4040 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4041 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4042 * mode = WILD_ALL: return all matches concatenated
4043 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004044 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 *
4046 * options = WILD_LIST_NOTFOUND: list entries without a match
4047 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4048 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4049 * options = WILD_NO_BEEP: Don't beep for multiple matches
4050 * options = WILD_ADD_SLASH: add a slash after directory names
4051 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4052 * options = WILD_SILENT: don't print warning messages
4053 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004054 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 *
4056 * The variables xp->xp_context and xp->xp_backslash must have been set!
4057 */
4058 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004059ExpandOne(
4060 expand_T *xp,
4061 char_u *str,
4062 char_u *orig, /* allocated copy of original of expanded string */
4063 int options,
4064 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
4066 char_u *ss = NULL;
4067 static int findex;
4068 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004069 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 int i;
4071 long_u len;
4072 int non_suf_match; /* number without matching suffix */
4073
4074 /*
4075 * first handle the case of using an old match
4076 */
4077 if (mode == WILD_NEXT || mode == WILD_PREV)
4078 {
4079 if (xp->xp_numfiles > 0)
4080 {
4081 if (mode == WILD_PREV)
4082 {
4083 if (findex == -1)
4084 findex = xp->xp_numfiles;
4085 --findex;
4086 }
4087 else /* mode == WILD_NEXT */
4088 ++findex;
4089
4090 /*
4091 * When wrapping around, return the original string, set findex to
4092 * -1.
4093 */
4094 if (findex < 0)
4095 {
4096 if (orig_save == NULL)
4097 findex = xp->xp_numfiles - 1;
4098 else
4099 findex = -1;
4100 }
4101 if (findex >= xp->xp_numfiles)
4102 {
4103 if (orig_save == NULL)
4104 findex = 0;
4105 else
4106 findex = -1;
4107 }
4108#ifdef FEAT_WILDMENU
4109 if (p_wmnu)
4110 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4111 findex, cmd_showtail);
4112#endif
4113 if (findex == -1)
4114 return vim_strsave(orig_save);
4115 return vim_strsave(xp->xp_files[findex]);
4116 }
4117 else
4118 return NULL;
4119 }
4120
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004121 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4123 {
4124 FreeWild(xp->xp_numfiles, xp->xp_files);
4125 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004126 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128 findex = 0;
4129
4130 if (mode == WILD_FREE) /* only release file name */
4131 return NULL;
4132
4133 if (xp->xp_numfiles == -1)
4134 {
4135 vim_free(orig_save);
4136 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004137 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 /*
4140 * Do the expansion.
4141 */
4142 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4143 options) == FAIL)
4144 {
4145#ifdef FNAME_ILLEGAL
4146 /* Illegal file name has been silently skipped. But when there
4147 * are wildcards, the real problem is that there was no match,
4148 * causing the pattern to be added, which has illegal characters.
4149 */
4150 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4151 EMSG2(_(e_nomatch2), str);
4152#endif
4153 }
4154 else if (xp->xp_numfiles == 0)
4155 {
4156 if (!(options & WILD_SILENT))
4157 EMSG2(_(e_nomatch2), str);
4158 }
4159 else
4160 {
4161 /* Escape the matches for use on the command line. */
4162 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4163
4164 /*
4165 * Check for matching suffixes in file names.
4166 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004167 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4168 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 if (xp->xp_numfiles)
4171 non_suf_match = xp->xp_numfiles;
4172 else
4173 non_suf_match = 1;
4174 if ((xp->xp_context == EXPAND_FILES
4175 || xp->xp_context == EXPAND_DIRECTORIES)
4176 && xp->xp_numfiles > 1)
4177 {
4178 /*
4179 * More than one match; check suffix.
4180 * The files will have been sorted on matching suffix in
4181 * expand_wildcards, only need to check the first two.
4182 */
4183 non_suf_match = 0;
4184 for (i = 0; i < 2; ++i)
4185 if (match_suffix(xp->xp_files[i]))
4186 ++non_suf_match;
4187 }
4188 if (non_suf_match != 1)
4189 {
4190 /* Can we ever get here unless it's while expanding
4191 * interactively? If not, we can get rid of this all
4192 * together. Don't really want to wait for this message
4193 * (and possibly have to hit return to continue!).
4194 */
4195 if (!(options & WILD_SILENT))
4196 EMSG(_(e_toomany));
4197 else if (!(options & WILD_NO_BEEP))
4198 beep_flush();
4199 }
4200 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4201 ss = vim_strsave(xp->xp_files[0]);
4202 }
4203 }
4204 }
4205
4206 /* Find longest common part */
4207 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4208 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004209 int mb_len = 1;
4210 int c0, ci;
4211
4212 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004214#ifdef FEAT_MBYTE
4215 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004217 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4218 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4219 }
4220 else
4221#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004222 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004223 for (i = 1; i < xp->xp_numfiles; ++i)
4224 {
4225#ifdef FEAT_MBYTE
4226 if (has_mbyte)
4227 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4228 else
4229#endif
4230 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004231 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004233 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004234 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004236 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 break;
4238 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004239 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 break;
4241 }
4242 if (i < xp->xp_numfiles)
4243 {
4244 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004245 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 break;
4247 }
4248 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004249
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 ss = alloc((unsigned)len + 1);
4251 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004252 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 findex = -1; /* next p_wc gets first one */
4254 }
4255
4256 /* Concatenate all matching names */
4257 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4258 {
4259 len = 0;
4260 for (i = 0; i < xp->xp_numfiles; ++i)
4261 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4262 ss = lalloc(len, TRUE);
4263 if (ss != NULL)
4264 {
4265 *ss = NUL;
4266 for (i = 0; i < xp->xp_numfiles; ++i)
4267 {
4268 STRCAT(ss, xp->xp_files[i]);
4269 if (i != xp->xp_numfiles - 1)
4270 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4271 }
4272 }
4273 }
4274
4275 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4276 ExpandCleanup(xp);
4277
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004278 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004279 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004280 vim_free(orig);
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 return ss;
4283}
4284
4285/*
4286 * Prepare an expand structure for use.
4287 */
4288 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004289ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004291 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004292 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004294#ifndef BACKSLASH_IN_FILENAME
4295 xp->xp_shell = FALSE;
4296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 xp->xp_numfiles = -1;
4298 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004299#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4300 xp->xp_arg = NULL;
4301#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004302 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303}
4304
4305/*
4306 * Cleanup an expand structure after use.
4307 */
4308 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004309ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
4311 if (xp->xp_numfiles >= 0)
4312 {
4313 FreeWild(xp->xp_numfiles, xp->xp_files);
4314 xp->xp_numfiles = -1;
4315 }
4316}
4317
4318 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004319ExpandEscape(
4320 expand_T *xp,
4321 char_u *str,
4322 int numfiles,
4323 char_u **files,
4324 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325{
4326 int i;
4327 char_u *p;
4328
4329 /*
4330 * May change home directory back to "~"
4331 */
4332 if (options & WILD_HOME_REPLACE)
4333 tilde_replace(str, numfiles, files);
4334
4335 if (options & WILD_ESCAPE)
4336 {
4337 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004338 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004339 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 || xp->xp_context == EXPAND_BUFFERS
4341 || xp->xp_context == EXPAND_DIRECTORIES)
4342 {
4343 /*
4344 * Insert a backslash into a file name before a space, \, %, #
4345 * and wildmatch characters, except '~'.
4346 */
4347 for (i = 0; i < numfiles; ++i)
4348 {
4349 /* for ":set path=" we need to escape spaces twice */
4350 if (xp->xp_backslash == XP_BS_THREE)
4351 {
4352 p = vim_strsave_escaped(files[i], (char_u *)" ");
4353 if (p != NULL)
4354 {
4355 vim_free(files[i]);
4356 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004357#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 p = vim_strsave_escaped(files[i], (char_u *)" ");
4359 if (p != NULL)
4360 {
4361 vim_free(files[i]);
4362 files[i] = p;
4363 }
4364#endif
4365 }
4366 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004367#ifdef BACKSLASH_IN_FILENAME
4368 p = vim_strsave_fnameescape(files[i], FALSE);
4369#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004370 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (p != NULL)
4373 {
4374 vim_free(files[i]);
4375 files[i] = p;
4376 }
4377
4378 /* If 'str' starts with "\~", replace "~" at start of
4379 * files[i] with "\~". */
4380 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004381 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004384
4385 /* If the first file starts with a '+' escape it. Otherwise it
4386 * could be seen as "+cmd". */
4387 if (*files[0] == '+')
4388 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 else if (xp->xp_context == EXPAND_TAGS)
4391 {
4392 /*
4393 * Insert a backslash before characters in a tag name that
4394 * would terminate the ":tag" command.
4395 */
4396 for (i = 0; i < numfiles; ++i)
4397 {
4398 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4399 if (p != NULL)
4400 {
4401 vim_free(files[i]);
4402 files[i] = p;
4403 }
4404 }
4405 }
4406 }
4407}
4408
4409/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004410 * Escape special characters in "fname" for when used as a file name argument
4411 * after a Vim command, or, when "shell" is non-zero, a shell command.
4412 * Returns the result in allocated memory.
4413 */
4414 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004415vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004416{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004417 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004418#ifdef BACKSLASH_IN_FILENAME
4419 char_u buf[20];
4420 int j = 0;
4421
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004422 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004423 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004424 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004425 buf[j++] = *p;
4426 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004427 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004428#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004429 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4430 if (shell && csh_like_shell() && p != NULL)
4431 {
4432 char_u *s;
4433
4434 /* For csh and similar shells need to put two backslashes before '!'.
4435 * One is taken by Vim, one by the shell. */
4436 s = vim_strsave_escaped(p, (char_u *)"!");
4437 vim_free(p);
4438 p = s;
4439 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004440#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004441
4442 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4443 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004444 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004445 escape_fname(&p);
4446
4447 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004448}
4449
4450/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004451 * Put a backslash before the file name in "pp", which is in allocated memory.
4452 */
4453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004454escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004455{
4456 char_u *p;
4457
4458 p = alloc((unsigned)(STRLEN(*pp) + 2));
4459 if (p != NULL)
4460 {
4461 p[0] = '\\';
4462 STRCPY(p + 1, *pp);
4463 vim_free(*pp);
4464 *pp = p;
4465 }
4466}
4467
4468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 * For each file name in files[num_files]:
4470 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4471 */
4472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004473tilde_replace(
4474 char_u *orig_pat,
4475 int num_files,
4476 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
4478 int i;
4479 char_u *p;
4480
4481 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4482 {
4483 for (i = 0; i < num_files; ++i)
4484 {
4485 p = home_replace_save(NULL, files[i]);
4486 if (p != NULL)
4487 {
4488 vim_free(files[i]);
4489 files[i] = p;
4490 }
4491 }
4492 }
4493}
4494
4495/*
4496 * Show all matches for completion on the command line.
4497 * Returns EXPAND_NOTHING when the character that triggered expansion should
4498 * be inserted like a normal character.
4499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004501showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502{
4503#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4504 int num_files;
4505 char_u **files_found;
4506 int i, j, k;
4507 int maxlen;
4508 int lines;
4509 int columns;
4510 char_u *p;
4511 int lastlen;
4512 int attr;
4513 int showtail;
4514
4515 if (xp->xp_numfiles == -1)
4516 {
4517 set_expand_context(xp);
4518 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4519 &num_files, &files_found);
4520 showtail = expand_showtail(xp);
4521 if (i != EXPAND_OK)
4522 return i;
4523
4524 }
4525 else
4526 {
4527 num_files = xp->xp_numfiles;
4528 files_found = xp->xp_files;
4529 showtail = cmd_showtail;
4530 }
4531
4532#ifdef FEAT_WILDMENU
4533 if (!wildmenu)
4534 {
4535#endif
4536 msg_didany = FALSE; /* lines_left will be set */
4537 msg_start(); /* prepare for paging */
4538 msg_putchar('\n');
4539 out_flush();
4540 cmdline_row = msg_row;
4541 msg_didany = FALSE; /* lines_left will be set again */
4542 msg_start(); /* prepare for paging */
4543#ifdef FEAT_WILDMENU
4544 }
4545#endif
4546
4547 if (got_int)
4548 got_int = FALSE; /* only int. the completion, not the cmd line */
4549#ifdef FEAT_WILDMENU
4550 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004551 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#endif
4553 else
4554 {
4555 /* find the length of the longest file name */
4556 maxlen = 0;
4557 for (i = 0; i < num_files; ++i)
4558 {
4559 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004560 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 || xp->xp_context == EXPAND_BUFFERS))
4562 {
4563 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4564 j = vim_strsize(NameBuff);
4565 }
4566 else
4567 j = vim_strsize(L_SHOWFILE(i));
4568 if (j > maxlen)
4569 maxlen = j;
4570 }
4571
4572 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4573 lines = num_files;
4574 else
4575 {
4576 /* compute the number of columns and lines for the listing */
4577 maxlen += 2; /* two spaces between file names */
4578 columns = ((int)Columns + 2) / maxlen;
4579 if (columns < 1)
4580 columns = 1;
4581 lines = (num_files + columns - 1) / columns;
4582 }
4583
Bram Moolenaar8820b482017-03-16 17:23:31 +01004584 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4587 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004588 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 msg_clr_eos();
4590 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004591 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
4594 /* list the files line by line */
4595 for (i = 0; i < lines; ++i)
4596 {
4597 lastlen = 999;
4598 for (k = i; k < num_files; k += lines)
4599 {
4600 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4601 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004602 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 p = files_found[k] + STRLEN(files_found[k]) + 1;
4604 msg_advance(maxlen + 1);
4605 msg_puts(p);
4606 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004607 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 break;
4609 }
4610 for (j = maxlen - lastlen; --j >= 0; )
4611 msg_putchar(' ');
4612 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004613 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 || xp->xp_context == EXPAND_BUFFERS)
4615 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004616 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004617 if (xp->xp_numfiles != -1)
4618 {
4619 char_u *halved_slash;
4620 char_u *exp_path;
4621
4622 /* Expansion was done before and special characters
4623 * were escaped, need to halve backslashes. Also
4624 * $HOME has been replaced with ~/. */
4625 exp_path = expand_env_save_opt(files_found[k], TRUE);
4626 halved_slash = backslash_halve_save(
4627 exp_path != NULL ? exp_path : files_found[k]);
4628 j = mch_isdir(halved_slash != NULL ? halved_slash
4629 : files_found[k]);
4630 vim_free(exp_path);
4631 vim_free(halved_slash);
4632 }
4633 else
4634 /* Expansion was done here, file names are literal. */
4635 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (showtail)
4637 p = L_SHOWFILE(k);
4638 else
4639 {
4640 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4641 TRUE);
4642 p = NameBuff;
4643 }
4644 }
4645 else
4646 {
4647 j = FALSE;
4648 p = L_SHOWFILE(k);
4649 }
4650 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4651 }
4652 if (msg_col > 0) /* when not wrapped around */
4653 {
4654 msg_clr_eos();
4655 msg_putchar('\n');
4656 }
4657 out_flush(); /* show one line at a time */
4658 if (got_int)
4659 {
4660 got_int = FALSE;
4661 break;
4662 }
4663 }
4664
4665 /*
4666 * we redraw the command below the lines that we have just listed
4667 * This is a bit tricky, but it saves a lot of screen updating.
4668 */
4669 cmdline_row = msg_row; /* will put it back later */
4670 }
4671
4672 if (xp->xp_numfiles == -1)
4673 FreeWild(num_files, files_found);
4674
4675 return EXPAND_OK;
4676}
4677
4678/*
4679 * Private gettail for showmatches() (and win_redr_status_matches()):
4680 * Find tail of file name path, but ignore trailing "/".
4681 */
4682 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004683sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 char_u *p;
4686 char_u *t = s;
4687 int had_sep = FALSE;
4688
4689 for (p = s; *p != NUL; )
4690 {
4691 if (vim_ispathsep(*p)
4692#ifdef BACKSLASH_IN_FILENAME
4693 && !rem_backslash(p)
4694#endif
4695 )
4696 had_sep = TRUE;
4697 else if (had_sep)
4698 {
4699 t = p;
4700 had_sep = FALSE;
4701 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004702 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 return t;
4705}
4706
4707/*
4708 * Return TRUE if we only need to show the tail of completion matches.
4709 * When not completing file names or there is a wildcard in the path FALSE is
4710 * returned.
4711 */
4712 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 char_u *s;
4716 char_u *end;
4717
4718 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004719 if (xp->xp_context != EXPAND_FILES
4720 && xp->xp_context != EXPAND_SHELLCMD
4721 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return FALSE;
4723
4724 end = gettail(xp->xp_pattern);
4725 if (end == xp->xp_pattern) /* there is no path separator */
4726 return FALSE;
4727
4728 for (s = xp->xp_pattern; s < end; s++)
4729 {
4730 /* Skip escaped wildcards. Only when the backslash is not a path
4731 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4732 if (rem_backslash(s))
4733 ++s;
4734 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4735 return FALSE;
4736 }
4737 return TRUE;
4738}
4739
4740/*
4741 * Prepare a string for expansion.
4742 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004743 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * When expanding other names: The string will be used with regcomp(). Copy
4745 * the name into allocated memory and prepend "^".
4746 */
4747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004748addstar(
4749 char_u *fname,
4750 int len,
4751 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 char_u *retval;
4754 int i, j;
4755 int new_len;
4756 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004757 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004759 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004760 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004761 && context != EXPAND_SHELLCMD
4762 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 /*
4765 * Matching will be done internally (on something other than files).
4766 * So we convert the file-matching-type wildcards into our kind for
4767 * use with vim_regcomp(). First work out how long it will be:
4768 */
4769
4770 /* For help tags the translation is done in find_help_tags().
4771 * For a tag pattern starting with "/" no translation is needed. */
4772 if (context == EXPAND_HELP
4773 || context == EXPAND_COLORS
4774 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004775 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004776 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004777 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004778 || ((context == EXPAND_TAGS_LISTFILES
4779 || context == EXPAND_TAGS)
4780 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 retval = vim_strnsave(fname, len);
4782 else
4783 {
4784 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4785 for (i = 0; i < len; i++)
4786 {
4787 if (fname[i] == '*' || fname[i] == '~')
4788 new_len++; /* '*' needs to be replaced by ".*"
4789 '~' needs to be replaced by "\~" */
4790
4791 /* Buffer names are like file names. "." should be literal */
4792 if (context == EXPAND_BUFFERS && fname[i] == '.')
4793 new_len++; /* "." becomes "\." */
4794
4795 /* Custom expansion takes care of special things, match
4796 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004797 if ((context == EXPAND_USER_DEFINED
4798 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 new_len++; /* '\' becomes "\\" */
4800 }
4801 retval = alloc(new_len);
4802 if (retval != NULL)
4803 {
4804 retval[0] = '^';
4805 j = 1;
4806 for (i = 0; i < len; i++, j++)
4807 {
4808 /* Skip backslash. But why? At least keep it for custom
4809 * expansion. */
4810 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004811 && context != EXPAND_USER_LIST
4812 && fname[i] == '\\'
4813 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 break;
4815
4816 switch (fname[i])
4817 {
4818 case '*': retval[j++] = '.';
4819 break;
4820 case '~': retval[j++] = '\\';
4821 break;
4822 case '?': retval[j] = '.';
4823 continue;
4824 case '.': if (context == EXPAND_BUFFERS)
4825 retval[j++] = '\\';
4826 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004827 case '\\': if (context == EXPAND_USER_DEFINED
4828 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 retval[j++] = '\\';
4830 break;
4831 }
4832 retval[j] = fname[i];
4833 }
4834 retval[j] = NUL;
4835 }
4836 }
4837 }
4838 else
4839 {
4840 retval = alloc(len + 4);
4841 if (retval != NULL)
4842 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004843 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004846 * Don't add a star to *, ~, ~user, $var or `cmd`.
4847 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 * ~ would be at the start of the file name, but not the tail.
4849 * $ could be anywhere in the tail.
4850 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004851 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 */
4853 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004854 ends_in_star = (len > 0 && retval[len - 1] == '*');
4855#ifndef BACKSLASH_IN_FILENAME
4856 for (i = len - 2; i >= 0; --i)
4857 {
4858 if (retval[i] != '\\')
4859 break;
4860 ends_in_star = !ends_in_star;
4861 }
4862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004864 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 && vim_strchr(tail, '$') == NULL
4866 && vim_strchr(retval, '`') == NULL)
4867 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004868 else if (len > 0 && retval[len - 1] == '$')
4869 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 retval[len] = NUL;
4871 }
4872 }
4873 return retval;
4874}
4875
4876/*
4877 * Must parse the command line so far to work out what context we are in.
4878 * Completion can then be done based on that context.
4879 * This routine sets the variables:
4880 * xp->xp_pattern The start of the pattern to be expanded within
4881 * the command line (ends at the cursor).
4882 * xp->xp_context The type of thing to expand. Will be one of:
4883 *
4884 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4885 * the command line, like an unknown command. Caller
4886 * should beep.
4887 * EXPAND_NOTHING Unrecognised context for completion, use char like
4888 * a normal char, rather than for completion. eg
4889 * :s/^I/
4890 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4891 * it.
4892 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4893 * EXPAND_FILES After command with XFILE set, or after setting
4894 * with P_EXPAND set. eg :e ^I, :w>>^I
4895 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4896 * when we know only directories are of interest. eg
4897 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004898 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4900 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4901 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4902 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4903 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4904 * EXPAND_EVENTS Complete event names
4905 * EXPAND_SYNTAX Complete :syntax command arguments
4906 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4907 * EXPAND_AUGROUP Complete autocommand group names
4908 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4909 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4910 * eg :unmap a^I , :cunab x^I
4911 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4912 * eg :call sub^I
4913 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4914 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4915 * names in expressions, eg :while s^I
4916 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004917 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 */
4919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004920set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004922 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (ccline.cmdfirstc != ':'
4924#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004925 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004926 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#endif
4928 )
4929 {
4930 xp->xp_context = EXPAND_NOTHING;
4931 return;
4932 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004933 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934}
4935
4936 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004937set_cmd_context(
4938 expand_T *xp,
4939 char_u *str, /* start of command line */
4940 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004941 int col, /* position of cursor */
4942 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
4944 int old_char = NUL;
4945 char_u *nextcomm;
4946
4947 /*
4948 * Avoid a UMR warning from Purify, only save the character if it has been
4949 * written before.
4950 */
4951 if (col < len)
4952 old_char = str[col];
4953 str[col] = NUL;
4954 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004955
4956#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004957 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004958 {
4959# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004960 /* pass CMD_SIZE because there is no real command */
4961 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004962# endif
4963 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004964 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004965 {
4966 xp->xp_context = ccline.xp_context;
4967 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004968# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004969 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004970# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004971 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004972 else
4973#endif
4974 while (nextcomm != NULL)
4975 nextcomm = set_one_cmd_context(xp, nextcomm);
4976
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004977 /* Store the string here so that call_user_expand_func() can get to them
4978 * easily. */
4979 xp->xp_line = str;
4980 xp->xp_col = col;
4981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 str[col] = old_char;
4983}
4984
4985/*
4986 * Expand the command line "str" from context "xp".
4987 * "xp" must have been set by set_cmd_context().
4988 * xp->xp_pattern points into "str", to where the text that is to be expanded
4989 * starts.
4990 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4991 * cursor.
4992 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4993 * key that triggered expansion literally.
4994 * Returns EXPAND_OK otherwise.
4995 */
4996 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997expand_cmdline(
4998 expand_T *xp,
4999 char_u *str, /* start of command line */
5000 int col, /* position of cursor */
5001 int *matchcount, /* return: nr of matches */
5002 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
5004 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005005 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
5007 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5008 {
5009 beep_flush();
5010 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5011 }
5012 if (xp->xp_context == EXPAND_NOTHING)
5013 {
5014 /* Caller can use the character as a normal char instead */
5015 return EXPAND_NOTHING;
5016 }
5017
5018 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005019 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5020 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (file_str == NULL)
5022 return EXPAND_UNSUCCESSFUL;
5023
Bram Moolenaar94950a92010-12-02 16:01:29 +01005024 if (p_wic)
5025 options += WILD_ICASE;
5026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005028 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
5030 *matchcount = 0;
5031 *matches = NULL;
5032 }
5033 vim_free(file_str);
5034
5035 return EXPAND_OK;
5036}
5037
5038#ifdef FEAT_MULTI_LANG
5039/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005040 * Cleanup matches for help tags:
5041 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5042 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
5047 int i, j;
5048 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005049 char_u buf[4];
5050 char_u *p = buf;
5051
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005052 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005053 {
5054 *p++ = '@';
5055 *p++ = p_hlg[0];
5056 *p++ = p_hlg[1];
5057 }
5058 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 for (i = 0; i < num_file; ++i)
5061 {
5062 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005063 if (len <= 0)
5064 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005065 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 /* Sorting on priority means the same item in another language may
5068 * be anywhere. Search all items for a match up to the "@en". */
5069 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005070 if (j != i && (int)STRLEN(file[j]) == len + 3
5071 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005074 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 file[i][len] = NUL;
5076 }
5077 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005078
5079 if (*buf != NUL)
5080 for (i = 0; i < num_file; ++i)
5081 {
5082 len = (int)STRLEN(file[i]) - 3;
5083 if (len <= 0)
5084 continue;
5085 if (STRCMP(file[i] + len, buf) == 0)
5086 {
5087 /* remove the default language */
5088 file[i][len] = NUL;
5089 }
5090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091}
5092#endif
5093
5094/*
5095 * Do the expansion based on xp->xp_context and "pat".
5096 */
5097 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005098ExpandFromContext(
5099 expand_T *xp,
5100 char_u *pat,
5101 int *num_file,
5102 char_u ***file,
5103 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104{
5105#ifdef FEAT_CMDL_COMPL
5106 regmatch_T regmatch;
5107#endif
5108 int ret;
5109 int flags;
5110
5111 flags = EW_DIR; /* include directories */
5112 if (options & WILD_LIST_NOTFOUND)
5113 flags |= EW_NOTFOUND;
5114 if (options & WILD_ADD_SLASH)
5115 flags |= EW_ADDSLASH;
5116 if (options & WILD_KEEP_ALL)
5117 flags |= EW_KEEPALL;
5118 if (options & WILD_SILENT)
5119 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005120 if (options & WILD_ALLLINKS)
5121 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005123 if (xp->xp_context == EXPAND_FILES
5124 || xp->xp_context == EXPAND_DIRECTORIES
5125 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 /*
5128 * Expand file or directory names.
5129 */
5130 int free_pat = FALSE;
5131 int i;
5132
5133 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5134 * space */
5135 if (xp->xp_backslash != XP_BS_NONE)
5136 {
5137 free_pat = TRUE;
5138 pat = vim_strsave(pat);
5139 for (i = 0; pat[i]; ++i)
5140 if (pat[i] == '\\')
5141 {
5142 if (xp->xp_backslash == XP_BS_THREE
5143 && pat[i + 1] == '\\'
5144 && pat[i + 2] == '\\'
5145 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005146 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (xp->xp_backslash == XP_BS_ONE
5148 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005149 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 }
5152
5153 if (xp->xp_context == EXPAND_FILES)
5154 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005155 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5156 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 else
5158 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005159 if (options & WILD_ICASE)
5160 flags |= EW_ICASE;
5161
Bram Moolenaard7834d32009-12-02 16:14:36 +00005162 /* Expand wildcards, supporting %:h and the like. */
5163 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (free_pat)
5165 vim_free(pat);
5166 return ret;
5167 }
5168
5169 *file = (char_u **)"";
5170 *num_file = 0;
5171 if (xp->xp_context == EXPAND_HELP)
5172 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005173 /* With an empty argument we would get all the help tags, which is
5174 * very slow. Get matches for "help" instead. */
5175 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5176 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
5178#ifdef FEAT_MULTI_LANG
5179 cleanup_help_tags(*num_file, *file);
5180#endif
5181 return OK;
5182 }
5183 return FAIL;
5184 }
5185
5186#ifndef FEAT_CMDL_COMPL
5187 return FAIL;
5188#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005189 if (xp->xp_context == EXPAND_SHELLCMD)
5190 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (xp->xp_context == EXPAND_OLD_SETTING)
5192 return ExpandOldSetting(num_file, file);
5193 if (xp->xp_context == EXPAND_BUFFERS)
5194 return ExpandBufnames(pat, num_file, file, options);
5195 if (xp->xp_context == EXPAND_TAGS
5196 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5197 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5198 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005199 {
5200 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005201 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5202 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005205 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005206 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005207 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005208 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005209 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005210 {
5211 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005212 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005213 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005214 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005215 {
5216 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005217 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005218 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005219# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5220 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005221 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005222# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005223 if (xp->xp_context == EXPAND_PACKADD)
5224 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
5226 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5227 if (regmatch.regprog == NULL)
5228 return FAIL;
5229
5230 /* set ignore-case according to p_ic, p_scs and pat */
5231 regmatch.rm_ic = ignorecase(pat);
5232
5233 if (xp->xp_context == EXPAND_SETTINGS
5234 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5235 ret = ExpandSettings(xp, &regmatch, num_file, file);
5236 else if (xp->xp_context == EXPAND_MAPPINGS)
5237 ret = ExpandMappings(&regmatch, num_file, file);
5238# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5239 else if (xp->xp_context == EXPAND_USER_DEFINED)
5240 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5241# endif
5242 else
5243 {
5244 static struct expgen
5245 {
5246 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005247 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005249 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 } tab[] =
5251 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005252 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5253 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005254 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005255 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005256#ifdef FEAT_CMDHIST
5257 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005260 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005261 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005262 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5263 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5264 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#endif
5266#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005267 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5268 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5269 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5270 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#endif
5272#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005273 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5274 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275#endif
5276#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005277 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005279#ifdef FEAT_PROFILE
5280 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5281#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005282 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005283 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5284 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005285#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005286 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005287#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005288#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005289 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005290#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005291#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005292 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5295 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005296 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5297 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005299 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005300 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005301 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 };
5303 int i;
5304
5305 /*
5306 * Find a context in the table and call the ExpandGeneric() with the
5307 * right function to do the expansion.
5308 */
5309 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005310 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (xp->xp_context == tab[i].context)
5312 {
5313 if (tab[i].ic)
5314 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005315 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005316 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 break;
5318 }
5319 }
5320
Bram Moolenaar473de612013-06-08 18:19:48 +02005321 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 return ret;
5324#endif /* FEAT_CMDL_COMPL */
5325}
5326
5327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5328/*
5329 * Expand a list of names.
5330 *
5331 * Generic function for command line completion. It calls a function to
5332 * obtain strings, one by one. The strings are matched against a regexp
5333 * program. Matching strings are copied into an array, which is returned.
5334 *
5335 * Returns OK when no problems encountered, FAIL for error (out of memory).
5336 */
5337 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005338ExpandGeneric(
5339 expand_T *xp,
5340 regmatch_T *regmatch,
5341 int *num_file,
5342 char_u ***file,
5343 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 int i;
5348 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005349 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 char_u *str;
5351
5352 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005353 * round == 0: count the number of matching names
5354 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005356 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
5358 for (i = 0; ; ++i)
5359 {
5360 str = (*func)(xp, i);
5361 if (str == NULL) /* end of list */
5362 break;
5363 if (*str == NUL) /* skip empty strings */
5364 continue;
5365
5366 if (vim_regexec(regmatch, str, (colnr_T)0))
5367 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005368 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005370 if (escaped)
5371 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5372 else
5373 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 (*file)[count] = str;
5375#ifdef FEAT_MENU
5376 if (func == get_menu_names && str != NULL)
5377 {
5378 /* test for separator added by get_menu_names() */
5379 str += STRLEN(str) - 1;
5380 if (*str == '\001')
5381 *str = '.';
5382 }
5383#endif
5384 }
5385 ++count;
5386 }
5387 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005388 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
5390 if (count == 0)
5391 return OK;
5392 *num_file = count;
5393 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5394 if (*file == NULL)
5395 {
5396 *file = (char_u **)"";
5397 return FAIL;
5398 }
5399 count = 0;
5400 }
5401 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005402
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005403 /* Sort the results. Keep menu's in the specified order. */
5404 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005405 {
5406 if (xp->xp_context == EXPAND_EXPRESSION
5407 || xp->xp_context == EXPAND_FUNCTIONS
5408 || xp->xp_context == EXPAND_USER_FUNC)
5409 /* <SNR> functions should be sorted to the end. */
5410 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5411 sort_func_compare);
5412 else
5413 sort_strings(*file, *num_file);
5414 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005415
Bram Moolenaar4f688582007-07-24 12:34:30 +00005416#ifdef FEAT_CMDL_COMPL
5417 /* Reset the variables used for special highlight names expansion, so that
5418 * they don't show up when getting normal highlight names by ID. */
5419 reset_expand_highlight();
5420#endif
5421
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 return OK;
5423}
5424
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005425/*
5426 * Complete a shell command.
5427 * Returns FAIL or OK;
5428 */
5429 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430expand_shellcmd(
5431 char_u *filepat, /* pattern to match with command names */
5432 int *num_file, /* return: number of matches */
5433 char_u ***file, /* return: array with matches */
5434 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005435{
5436 char_u *pat;
5437 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005438 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005439 int mustfree = FALSE;
5440 garray_T ga;
5441 char_u *buf = alloc(MAXPATHL);
5442 size_t l;
5443 char_u *s, *e;
5444 int flags = flagsarg;
5445 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005446 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005447 hashtab_T found_ht;
5448 hashitem_T *hi;
5449 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005450
5451 if (buf == NULL)
5452 return FAIL;
5453
5454 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5455 * space */
5456 pat = vim_strsave(filepat);
5457 for (i = 0; pat[i]; ++i)
5458 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005459 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005460
Bram Moolenaarb5971142015-03-21 17:32:19 +01005461 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005462
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005463 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5464 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005465 path = (char_u *)".";
5466 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005467 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005468 /* For an absolute name we don't use $PATH. */
5469 if (!mch_isFullName(pat))
5470 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005471 if (path == NULL)
5472 path = (char_u *)"";
5473 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005474
5475 /*
5476 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005477 * collect them in "ga". When "." is not in $PATH also expand for the
5478 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005479 */
5480 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005481 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005482 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005483 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005484#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005485 e = vim_strchr(s, ';');
5486#else
5487 e = vim_strchr(s, ':');
5488#endif
5489 if (e == NULL)
5490 e = s + STRLEN(s);
5491
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005492 if (*s == NUL)
5493 {
5494 if (did_curdir)
5495 break;
5496 // Find directories in the current directory, path is empty.
5497 did_curdir = TRUE;
5498 flags |= EW_DIR;
5499 }
5500 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5501 {
5502 did_curdir = TRUE;
5503 flags |= EW_DIR;
5504 }
5505 else
5506 // Do not match directories inside a $PATH item.
5507 flags &= ~EW_DIR;
5508
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005509 l = e - s;
5510 if (l > MAXPATHL - 5)
5511 break;
5512 vim_strncpy(buf, s, l);
5513 add_pathsep(buf);
5514 l = STRLEN(buf);
5515 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5516
5517 /* Expand matches in one directory of $PATH. */
5518 ret = expand_wildcards(1, &buf, num_file, file, flags);
5519 if (ret == OK)
5520 {
5521 if (ga_grow(&ga, *num_file) == FAIL)
5522 FreeWild(*num_file, *file);
5523 else
5524 {
5525 for (i = 0; i < *num_file; ++i)
5526 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005527 char_u *name = (*file)[i];
5528
5529 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005530 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005531 // Check if this name was already found.
5532 hash = hash_hash(name + l);
5533 hi = hash_lookup(&found_ht, name + l, hash);
5534 if (HASHITEM_EMPTY(hi))
5535 {
5536 // Remove the path that was prepended.
5537 STRMOVE(name, name + l);
5538 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5539 hash_add_item(&found_ht, hi, name, hash);
5540 name = NULL;
5541 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005542 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005543 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005544 }
5545 vim_free(*file);
5546 }
5547 }
5548 if (*e != NUL)
5549 ++e;
5550 }
5551 *file = ga.ga_data;
5552 *num_file = ga.ga_len;
5553
5554 vim_free(buf);
5555 vim_free(pat);
5556 if (mustfree)
5557 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005558 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005559 return OK;
5560}
5561
5562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5564/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005565 * Call "user_expand_func()" to invoke a user defined Vim script function and
5566 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005568 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005569call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005570 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005571 expand_T *xp,
5572 int *num_file,
5573 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005575 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005576 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005577 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005578 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005579 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
Bram Moolenaarb7515462013-06-29 12:58:33 +02005581 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005582 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 *num_file = 0;
5584 *file = NULL;
5585
Bram Moolenaarb7515462013-06-29 12:58:33 +02005586 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005587 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005588 keep = ccline.cmdbuff[ccline.cmdlen];
5589 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005590 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005591
Bram Moolenaarffa96842018-06-12 22:05:14 +02005592 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5593
5594 args[0].v_type = VAR_STRING;
5595 args[0].vval.v_string = pat;
5596 args[1].v_type = VAR_STRING;
5597 args[1].vval.v_string = xp->xp_line;
5598 args[2].v_type = VAR_NUMBER;
5599 args[2].vval.v_number = xp->xp_col;
5600 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005602 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005603
Bram Moolenaarded27a12018-08-01 19:06:03 +02005604 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005605
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005606 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005607 if (ccline.cmdbuff != NULL)
5608 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005609
Bram Moolenaarffa96842018-06-12 22:05:14 +02005610 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005611 return ret;
5612}
5613
5614/*
5615 * Expand names with a function defined by the user.
5616 */
5617 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618ExpandUserDefined(
5619 expand_T *xp,
5620 regmatch_T *regmatch,
5621 int *num_file,
5622 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005623{
5624 char_u *retstr;
5625 char_u *s;
5626 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005627 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005628 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005629 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005630
5631 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5632 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 return FAIL;
5634
5635 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005636 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
5638 e = vim_strchr(s, '\n');
5639 if (e == NULL)
5640 e = s + STRLEN(s);
5641 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005642 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005644 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5645 *e = keep;
5646
5647 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005649 if (ga_grow(&ga, 1) == FAIL)
5650 break;
5651 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5652 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
5654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (*e != NUL)
5656 ++e;
5657 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005658 vim_free(retstr);
5659 *file = ga.ga_data;
5660 *num_file = ga.ga_len;
5661 return OK;
5662}
5663
5664/*
5665 * Expand names with a list returned by a function defined by the user.
5666 */
5667 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668ExpandUserList(
5669 expand_T *xp,
5670 int *num_file,
5671 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005672{
5673 list_T *retlist;
5674 listitem_T *li;
5675 garray_T ga;
5676
5677 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5678 if (retlist == NULL)
5679 return FAIL;
5680
5681 ga_init2(&ga, (int)sizeof(char *), 3);
5682 /* Loop over the items in the list. */
5683 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5684 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005685 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5686 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005687
5688 if (ga_grow(&ga, 1) == FAIL)
5689 break;
5690
5691 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005692 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005693 ++ga.ga_len;
5694 }
5695 list_unref(retlist);
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 *file = ga.ga_data;
5698 *num_file = ga.ga_len;
5699 return OK;
5700}
5701#endif
5702
5703/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005704 * Expand color scheme, compiler or filetype names.
5705 * Search from 'runtimepath':
5706 * 'runtimepath'/{dirnames}/{pat}.vim
5707 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5708 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5709 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5710 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005711 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 */
5713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005714ExpandRTDir(
5715 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005716 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005717 int *num_file,
5718 char_u ***file,
5719 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 char_u *s;
5722 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005723 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005725 int i;
5726 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
5728 *num_file = 0;
5729 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005730 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005731 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005733 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005735 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5736 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005738 ga_clear_strings(&ga);
5739 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005741 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005742 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005743 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005745
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005746 if (flags & DIP_START) {
5747 for (i = 0; dirnames[i] != NULL; ++i)
5748 {
5749 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5750 if (s == NULL)
5751 {
5752 ga_clear_strings(&ga);
5753 return FAIL;
5754 }
5755 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5756 globpath(p_pp, s, &ga, 0);
5757 vim_free(s);
5758 }
5759 }
5760
5761 if (flags & DIP_OPT) {
5762 for (i = 0; dirnames[i] != NULL; ++i)
5763 {
5764 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5765 if (s == NULL)
5766 {
5767 ga_clear_strings(&ga);
5768 return FAIL;
5769 }
5770 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5771 globpath(p_pp, s, &ga, 0);
5772 vim_free(s);
5773 }
5774 }
5775
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005776 for (i = 0; i < ga.ga_len; ++i)
5777 {
5778 match = ((char_u **)ga.ga_data)[i];
5779 s = match;
5780 e = s + STRLEN(s);
5781 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5782 {
5783 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005784 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005785 if (s < match || vim_ispathsep(*s))
5786 break;
5787 ++s;
5788 *e = NUL;
5789 mch_memmove(match, s, e - s + 1);
5790 }
5791 }
5792
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005793 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005794 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005795
5796 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005797 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005798 remove_duplicates(&ga);
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 *file = ga.ga_data;
5801 *num_file = ga.ga_len;
5802 return OK;
5803}
5804
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005805/*
5806 * Expand loadplugin names:
5807 * 'packpath'/pack/ * /opt/{pat}
5808 */
5809 static int
5810ExpandPackAddDir(
5811 char_u *pat,
5812 int *num_file,
5813 char_u ***file)
5814{
5815 char_u *s;
5816 char_u *e;
5817 char_u *match;
5818 garray_T ga;
5819 int i;
5820 int pat_len;
5821
5822 *num_file = 0;
5823 *file = NULL;
5824 pat_len = (int)STRLEN(pat);
5825 ga_init2(&ga, (int)sizeof(char *), 10);
5826
5827 s = alloc((unsigned)(pat_len + 26));
5828 if (s == NULL)
5829 {
5830 ga_clear_strings(&ga);
5831 return FAIL;
5832 }
5833 sprintf((char *)s, "pack/*/opt/%s*", pat);
5834 globpath(p_pp, s, &ga, 0);
5835 vim_free(s);
5836
5837 for (i = 0; i < ga.ga_len; ++i)
5838 {
5839 match = ((char_u **)ga.ga_data)[i];
5840 s = gettail(match);
5841 e = s + STRLEN(s);
5842 mch_memmove(match, s, e - s + 1);
5843 }
5844
5845 if (ga.ga_len == 0)
5846 return FAIL;
5847
5848 /* Sort and remove duplicates which can happen when specifying multiple
5849 * directories in dirnames. */
5850 remove_duplicates(&ga);
5851
5852 *file = ga.ga_data;
5853 *num_file = ga.ga_len;
5854 return OK;
5855}
5856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857#endif
5858
5859#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5860/*
5861 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005862 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005864 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005865globpath(
5866 char_u *path,
5867 char_u *file,
5868 garray_T *ga,
5869 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 expand_T xpc;
5872 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 int num_p;
5875 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
5877 buf = alloc(MAXPATHL);
5878 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005879 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005881 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005883
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 /* Loop over all entries in {path}. */
5885 while (*path != NUL)
5886 {
5887 /* Copy one item of the path to buf[] and concatenate the file name. */
5888 copy_option_part(&path, buf, MAXPATHL, ",");
5889 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5890 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005891# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005892 /* Using the platform's path separator (\) makes vim incorrectly
5893 * treat it as an escape character, use '/' instead. */
5894 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5895 STRCAT(buf, "/");
5896# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005900 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5901 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005903 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005905 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 for (i = 0; i < num_p; ++i)
5908 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005909 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005910 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005911 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 FreeWild(num_p, p);
5916 }
5917 }
5918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921}
5922
5923#endif
5924
5925#if defined(FEAT_CMDHIST) || defined(PROTO)
5926
5927/*********************************
5928 * Command line history stuff *
5929 *********************************/
5930
5931/*
5932 * Translate a history character to the associated type number.
5933 */
5934 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 if (c == ':')
5938 return HIST_CMD;
5939 if (c == '=')
5940 return HIST_EXPR;
5941 if (c == '@')
5942 return HIST_INPUT;
5943 if (c == '>')
5944 return HIST_DEBUG;
5945 return HIST_SEARCH; /* must be '?' or '/' */
5946}
5947
5948/*
5949 * Table of history names.
5950 * These names are used in :history and various hist...() functions.
5951 * It is sufficient to give the significant prefix of a history name.
5952 */
5953
5954static char *(history_names[]) =
5955{
5956 "cmd",
5957 "search",
5958 "expr",
5959 "input",
5960 "debug",
5961 NULL
5962};
5963
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005964#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5965/*
5966 * Function given to ExpandGeneric() to obtain the possible first
5967 * arguments of the ":history command.
5968 */
5969 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005970get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005971{
5972 static char_u compl[2] = { NUL, NUL };
5973 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005974 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005975 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5976
5977 if (idx < short_names_count)
5978 {
5979 compl[0] = (char_u)short_names[idx];
5980 return compl;
5981 }
5982 if (idx < short_names_count + history_name_count)
5983 return (char_u *)history_names[idx - short_names_count];
5984 if (idx == short_names_count + history_name_count)
5985 return (char_u *)"all";
5986 return NULL;
5987}
5988#endif
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990/*
5991 * init_history() - Initialize the command line history.
5992 * Also used to re-allocate the history when the size changes.
5993 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005994 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005995init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997 int newlen; /* new length of history table */
5998 histentry_T *temp;
5999 int i;
6000 int j;
6001 int type;
6002
6003 /*
6004 * If size of history table changed, reallocate it
6005 */
6006 newlen = (int)p_hi;
6007 if (newlen != hislen) /* history length changed */
6008 {
6009 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6010 {
6011 if (newlen)
6012 {
6013 temp = (histentry_T *)lalloc(
6014 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6015 if (temp == NULL) /* out of memory! */
6016 {
6017 if (type == 0) /* first one: just keep the old length */
6018 {
6019 newlen = hislen;
6020 break;
6021 }
6022 /* Already changed one table, now we can only have zero
6023 * length for all tables. */
6024 newlen = 0;
6025 type = -1;
6026 continue;
6027 }
6028 }
6029 else
6030 temp = NULL;
6031 if (newlen == 0 || temp != NULL)
6032 {
6033 if (hisidx[type] < 0) /* there are no entries yet */
6034 {
6035 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006036 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 }
6038 else if (newlen > hislen) /* array becomes bigger */
6039 {
6040 for (i = 0; i <= hisidx[type]; ++i)
6041 temp[i] = history[type][i];
6042 j = i;
6043 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006044 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 for ( ; j < hislen; ++i, ++j)
6046 temp[i] = history[type][j];
6047 }
6048 else /* array becomes smaller or 0 */
6049 {
6050 j = hisidx[type];
6051 for (i = newlen - 1; ; --i)
6052 {
6053 if (i >= 0) /* copy newest entries */
6054 temp[i] = history[type][j];
6055 else /* remove older entries */
6056 vim_free(history[type][j].hisstr);
6057 if (--j < 0)
6058 j = hislen - 1;
6059 if (j == hisidx[type])
6060 break;
6061 }
6062 hisidx[type] = newlen - 1;
6063 }
6064 vim_free(history[type]);
6065 history[type] = temp;
6066 }
6067 }
6068 hislen = newlen;
6069 }
6070}
6071
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006074{
6075 hisptr->hisnum = 0;
6076 hisptr->viminfo = FALSE;
6077 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006078 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006079}
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081/*
6082 * Check if command line 'str' is already in history.
6083 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6084 */
6085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086in_history(
6087 int type,
6088 char_u *str,
6089 int move_to_front, /* Move the entry to the front if it exists */
6090 int sep,
6091 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
6093 int i;
6094 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006095 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
6097 if (hisidx[type] < 0)
6098 return FALSE;
6099 i = hisidx[type];
6100 do
6101 {
6102 if (history[type][i].hisstr == NULL)
6103 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006104
6105 /* For search history, check that the separator character matches as
6106 * well. */
6107 p = history[type][i].hisstr;
6108 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006109 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006110 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {
6112 if (!move_to_front)
6113 return TRUE;
6114 last_i = i;
6115 break;
6116 }
6117 if (--i < 0)
6118 i = hislen - 1;
6119 } while (i != hisidx[type]);
6120
6121 if (last_i >= 0)
6122 {
6123 str = history[type][i].hisstr;
6124 while (i != hisidx[type])
6125 {
6126 if (++i >= hislen)
6127 i = 0;
6128 history[type][last_i] = history[type][i];
6129 last_i = i;
6130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006132 history[type][i].viminfo = FALSE;
6133 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006134 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 return TRUE;
6136 }
6137 return FALSE;
6138}
6139
6140/*
6141 * Convert history name (from table above) to its HIST_ equivalent.
6142 * When "name" is empty, return "cmd" history.
6143 * Returns -1 for unknown history name.
6144 */
6145 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006146get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 int i;
6149 int len = (int)STRLEN(name);
6150
6151 /* No argument: use current history. */
6152 if (len == 0)
6153 return hist_char2type(ccline.cmdfirstc);
6154
6155 for (i = 0; history_names[i] != NULL; ++i)
6156 if (STRNICMP(name, history_names[i], len) == 0)
6157 return i;
6158
6159 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6160 return hist_char2type(name[0]);
6161
6162 return -1;
6163}
6164
6165static int last_maptick = -1; /* last seen maptick */
6166
6167/*
6168 * Add the given string to the given history. If the string is already in the
6169 * history then it is moved to the front. "histype" may be one of he HIST_
6170 * values.
6171 */
6172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006173add_to_history(
6174 int histype,
6175 char_u *new_entry,
6176 int in_map, /* consider maptick when inside a mapping */
6177 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 histentry_T *hisptr;
6180 int len;
6181
6182 if (hislen == 0) /* no history */
6183 return;
6184
Bram Moolenaara939e432013-11-09 05:30:26 +01006185 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6186 return;
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 /*
6189 * Searches inside the same mapping overwrite each other, so that only
6190 * the last line is kept. Be careful not to remove a line that was moved
6191 * down, only lines that were added.
6192 */
6193 if (histype == HIST_SEARCH && in_map)
6194 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006195 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 /* Current line is from the same mapping, remove it */
6198 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6199 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006200 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 --hisnum[histype];
6202 if (--hisidx[HIST_SEARCH] < 0)
6203 hisidx[HIST_SEARCH] = hislen - 1;
6204 }
6205 last_maptick = -1;
6206 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006207 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
6209 if (++hisidx[histype] == hislen)
6210 hisidx[histype] = 0;
6211 hisptr = &history[histype][hisidx[histype]];
6212 vim_free(hisptr->hisstr);
6213
6214 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006215 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6217 if (hisptr->hisstr != NULL)
6218 hisptr->hisstr[len + 1] = sep;
6219
6220 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006221 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006222 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (histype == HIST_SEARCH && in_map)
6224 last_maptick = maptick;
6225 }
6226}
6227
6228#if defined(FEAT_EVAL) || defined(PROTO)
6229
6230/*
6231 * Get identifier of newest history entry.
6232 * "histype" may be one of the HIST_ values.
6233 */
6234 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006235get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6238 || hisidx[histype] < 0)
6239 return -1;
6240
6241 return history[histype][hisidx[histype]].hisnum;
6242}
6243
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 * Calculate history index from a number:
6246 * num > 0: seen as identifying number of a history entry
6247 * num < 0: relative position in history wrt newest entry
6248 * "histype" may be one of the HIST_ values.
6249 */
6250 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006251calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 int i;
6254 histentry_T *hist;
6255 int wrapped = FALSE;
6256
6257 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6258 || (i = hisidx[histype]) < 0 || num == 0)
6259 return -1;
6260
6261 hist = history[histype];
6262 if (num > 0)
6263 {
6264 while (hist[i].hisnum > num)
6265 if (--i < 0)
6266 {
6267 if (wrapped)
6268 break;
6269 i += hislen;
6270 wrapped = TRUE;
6271 }
6272 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6273 return i;
6274 }
6275 else if (-num <= hislen)
6276 {
6277 i += num + 1;
6278 if (i < 0)
6279 i += hislen;
6280 if (hist[i].hisstr != NULL)
6281 return i;
6282 }
6283 return -1;
6284}
6285
6286/*
6287 * Get a history entry by its index.
6288 * "histype" may be one of the HIST_ values.
6289 */
6290 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 idx = calc_hist_idx(histype, idx);
6294 if (idx >= 0)
6295 return history[histype][idx].hisstr;
6296 else
6297 return (char_u *)"";
6298}
6299
6300/*
6301 * Clear all entries of a history.
6302 * "histype" may be one of the HIST_ values.
6303 */
6304 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006305clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
6307 int i;
6308 histentry_T *hisptr;
6309
6310 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6311 {
6312 hisptr = history[histype];
6313 for (i = hislen; i--;)
6314 {
6315 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006316 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006317 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 }
6319 hisidx[histype] = -1; /* mark history as cleared */
6320 hisnum[histype] = 0; /* reset identifier counter */
6321 return OK;
6322 }
6323 return FAIL;
6324}
6325
6326/*
6327 * Remove all entries matching {str} from a history.
6328 * "histype" may be one of the HIST_ values.
6329 */
6330 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006331del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 regmatch_T regmatch;
6334 histentry_T *hisptr;
6335 int idx;
6336 int i;
6337 int last;
6338 int found = FALSE;
6339
6340 regmatch.regprog = NULL;
6341 regmatch.rm_ic = FALSE; /* always match case */
6342 if (hislen != 0
6343 && histype >= 0
6344 && histype < HIST_COUNT
6345 && *str != NUL
6346 && (idx = hisidx[histype]) >= 0
6347 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6348 != NULL)
6349 {
6350 i = last = idx;
6351 do
6352 {
6353 hisptr = &history[histype][i];
6354 if (hisptr->hisstr == NULL)
6355 break;
6356 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6357 {
6358 found = TRUE;
6359 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006360 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362 else
6363 {
6364 if (i != last)
6365 {
6366 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006367 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369 if (--last < 0)
6370 last += hislen;
6371 }
6372 if (--i < 0)
6373 i += hislen;
6374 } while (i != idx);
6375 if (history[histype][idx].hisstr == NULL)
6376 hisidx[histype] = -1;
6377 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006378 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 return found;
6380}
6381
6382/*
6383 * Remove an indexed entry from a history.
6384 * "histype" may be one of the HIST_ values.
6385 */
6386 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int i, j;
6390
6391 i = calc_hist_idx(histype, idx);
6392 if (i < 0)
6393 return FALSE;
6394 idx = hisidx[histype];
6395 vim_free(history[histype][i].hisstr);
6396
6397 /* When deleting the last added search string in a mapping, reset
6398 * last_maptick, so that the last added search string isn't deleted again.
6399 */
6400 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6401 last_maptick = -1;
6402
6403 while (i != idx)
6404 {
6405 j = (i + 1) % hislen;
6406 history[histype][i] = history[histype][j];
6407 i = j;
6408 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006409 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (--i < 0)
6411 i += hislen;
6412 hisidx[histype] = i;
6413 return TRUE;
6414}
6415
6416#endif /* FEAT_EVAL */
6417
6418#if defined(FEAT_CRYPT) || defined(PROTO)
6419/*
6420 * Very specific function to remove the value in ":set key=val" from the
6421 * history.
6422 */
6423 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006424remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 char_u *p;
6427 int i;
6428
6429 i = hisidx[HIST_CMD];
6430 if (i < 0)
6431 return;
6432 p = history[HIST_CMD][i].hisstr;
6433 if (p != NULL)
6434 for ( ; *p; ++p)
6435 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6436 {
6437 p = vim_strchr(p + 3, '=');
6438 if (p == NULL)
6439 break;
6440 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006441 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 if (p[i] == '\\' && p[i + 1])
6443 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006444 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 --p;
6446 }
6447}
6448#endif
6449
6450#endif /* FEAT_CMDHIST */
6451
Bram Moolenaar064154c2016-03-19 22:50:43 +01006452#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006453/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006454 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006455 * ccline and put the previous value in prev_ccline.
6456 */
6457 static struct cmdline_info *
6458get_ccline_ptr(void)
6459{
6460 if ((State & CMDLINE) == 0)
6461 return NULL;
6462 if (ccline.cmdbuff != NULL)
6463 return &ccline;
6464 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6465 return &prev_ccline;
6466 return NULL;
6467}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006468#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006469
Bram Moolenaar064154c2016-03-19 22:50:43 +01006470#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006471/*
6472 * Get the current command line in allocated memory.
6473 * Only works when the command line is being edited.
6474 * Returns NULL when something is wrong.
6475 */
6476 char_u *
6477get_cmdline_str(void)
6478{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006479 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006480
Bram Moolenaaree91c332018-09-25 22:27:35 +02006481 if (cmdline_star > 0)
6482 return NULL;
6483 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006484 if (p == NULL)
6485 return NULL;
6486 return vim_strnsave(p->cmdbuff, p->cmdlen);
6487}
6488
6489/*
6490 * Get the current command line position, counted in bytes.
6491 * Zero is the first position.
6492 * Only works when the command line is being edited.
6493 * Returns -1 when something is wrong.
6494 */
6495 int
6496get_cmdline_pos(void)
6497{
6498 struct cmdline_info *p = get_ccline_ptr();
6499
6500 if (p == NULL)
6501 return -1;
6502 return p->cmdpos;
6503}
6504
6505/*
6506 * Set the command line byte position to "pos". Zero is the first position.
6507 * Only works when the command line is being edited.
6508 * Returns 1 when failed, 0 when OK.
6509 */
6510 int
6511set_cmdline_pos(
6512 int pos)
6513{
6514 struct cmdline_info *p = get_ccline_ptr();
6515
6516 if (p == NULL)
6517 return 1;
6518
6519 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6520 * changed the command line. */
6521 if (pos < 0)
6522 new_cmdpos = 0;
6523 else
6524 new_cmdpos = pos;
6525 return 0;
6526}
6527#endif
6528
6529#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6530/*
6531 * Get the current command-line type.
6532 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6533 * Only works when the command line is being edited.
6534 * Returns NUL when something is wrong.
6535 */
6536 int
6537get_cmdline_type(void)
6538{
6539 struct cmdline_info *p = get_ccline_ptr();
6540
6541 if (p == NULL)
6542 return NUL;
6543 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006544 return
6545# ifdef FEAT_EVAL
6546 (p->input_fn) ? '@' :
6547# endif
6548 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006549 return p->cmdfirstc;
6550}
6551#endif
6552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6554/*
6555 * Get indices "num1,num2" that specify a range within a list (not a range of
6556 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6557 * Returns OK if parsed successfully, otherwise FAIL.
6558 */
6559 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006560get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 int len;
6563 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006564 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566 *str = skipwhite(*str);
6567 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6568 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006569 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 *str += len;
6571 *num1 = (int)num;
6572 first = TRUE;
6573 }
6574 *str = skipwhite(*str);
6575 if (**str == ',') /* parse "to" part of range */
6576 {
6577 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006578 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (len > 0)
6580 {
6581 *num2 = (int)num;
6582 *str = skipwhite(*str + len);
6583 }
6584 else if (!first) /* no number given at all */
6585 return FAIL;
6586 }
6587 else if (first) /* only one number given */
6588 *num2 = *num1;
6589 return OK;
6590}
6591#endif
6592
6593#if defined(FEAT_CMDHIST) || defined(PROTO)
6594/*
6595 * :history command - print a history
6596 */
6597 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006598ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
6600 histentry_T *hist;
6601 int histype1 = HIST_CMD;
6602 int histype2 = HIST_CMD;
6603 int hisidx1 = 1;
6604 int hisidx2 = -1;
6605 int idx;
6606 int i, j, k;
6607 char_u *end;
6608 char_u *arg = eap->arg;
6609
6610 if (hislen == 0)
6611 {
6612 MSG(_("'history' option is zero"));
6613 return;
6614 }
6615
6616 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6617 {
6618 end = arg;
6619 while (ASCII_ISALPHA(*end)
6620 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6621 end++;
6622 i = *end;
6623 *end = NUL;
6624 histype1 = get_histtype(arg);
6625 if (histype1 == -1)
6626 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006627 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
6629 histype1 = 0;
6630 histype2 = HIST_COUNT-1;
6631 }
6632 else
6633 {
6634 *end = i;
6635 EMSG(_(e_trailing));
6636 return;
6637 }
6638 }
6639 else
6640 histype2 = histype1;
6641 *end = i;
6642 }
6643 else
6644 end = arg;
6645 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6646 {
6647 EMSG(_(e_trailing));
6648 return;
6649 }
6650
6651 for (; !got_int && histype1 <= histype2; ++histype1)
6652 {
6653 STRCPY(IObuff, "\n # ");
6654 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6655 MSG_PUTS_TITLE(IObuff);
6656 idx = hisidx[histype1];
6657 hist = history[histype1];
6658 j = hisidx1;
6659 k = hisidx2;
6660 if (j < 0)
6661 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6662 if (k < 0)
6663 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6664 if (idx >= 0 && j <= k)
6665 for (i = idx + 1; !got_int; ++i)
6666 {
6667 if (i == hislen)
6668 i = 0;
6669 if (hist[i].hisstr != NULL
6670 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6671 {
6672 msg_putchar('\n');
6673 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6674 hist[i].hisnum);
6675 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6676 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006677 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 else
6679 STRCAT(IObuff, hist[i].hisstr);
6680 msg_outtrans(IObuff);
6681 out_flush();
6682 }
6683 if (i == idx)
6684 break;
6685 }
6686 }
6687}
6688#endif
6689
6690#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006691/*
6692 * Buffers for history read from a viminfo file. Only valid while reading.
6693 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006694static histentry_T *viminfo_history[HIST_COUNT] =
6695 {NULL, NULL, NULL, NULL, NULL};
6696static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6697static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698static int viminfo_add_at_front = FALSE;
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700/*
6701 * Translate a history type number to the associated character.
6702 */
6703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006704hist_type2char(
6705 int type,
6706 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 if (type == HIST_CMD)
6709 return ':';
6710 if (type == HIST_SEARCH)
6711 {
6712 if (use_question)
6713 return '?';
6714 else
6715 return '/';
6716 }
6717 if (type == HIST_EXPR)
6718 return '=';
6719 return '@';
6720}
6721
6722/*
6723 * Prepare for reading the history from the viminfo file.
6724 * This allocates history arrays to store the read history lines.
6725 */
6726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006727prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 int i;
6730 int num;
6731 int type;
6732 int len;
6733
6734 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006735 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (asklen > hislen)
6737 asklen = hislen;
6738
6739 for (type = 0; type < HIST_COUNT; ++type)
6740 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006741 /* Count the number of empty spaces in the history list. Entries read
6742 * from viminfo previously are also considered empty. If there are
6743 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006745 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 num++;
6747 len = asklen;
6748 if (num > len)
6749 len = num;
6750 if (len <= 0)
6751 viminfo_history[type] = NULL;
6752 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006753 viminfo_history[type] = (histentry_T *)lalloc(
6754 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (viminfo_history[type] == NULL)
6756 len = 0;
6757 viminfo_hislen[type] = len;
6758 viminfo_hisidx[type] = 0;
6759 }
6760}
6761
6762/*
6763 * Accept a line from the viminfo, store it in the history array when it's
6764 * new.
6765 */
6766 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 int type;
6770 long_u len;
6771 char_u *val;
6772 char_u *p;
6773
6774 type = hist_char2type(virp->vir_line[0]);
6775 if (viminfo_hisidx[type] < viminfo_hislen[type])
6776 {
6777 val = viminfo_readstring(virp, 1, TRUE);
6778 if (val != NULL && *val != NUL)
6779 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006780 int sep = (*val == ' ' ? NUL : *val);
6781
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006783 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
6785 /* Need to re-allocate to append the separator byte. */
6786 len = STRLEN(val);
6787 p = lalloc(len + 2, TRUE);
6788 if (p != NULL)
6789 {
6790 if (type == HIST_SEARCH)
6791 {
6792 /* Search entry: Move the separator from the first
6793 * column to after the NUL. */
6794 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006795 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
6797 else
6798 {
6799 /* Not a search entry: No separator in the viminfo
6800 * file, add a NUL separator. */
6801 mch_memmove(p, val, (size_t)len + 1);
6802 p[len + 1] = NUL;
6803 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006804 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6805 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006806 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6807 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 }
6811 }
6812 vim_free(val);
6813 }
6814 return viminfo_readline(virp);
6815}
6816
Bram Moolenaar07219f92013-04-14 23:19:36 +02006817/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006818 * Accept a new style history line from the viminfo, store it in the history
6819 * array when it's new.
6820 */
6821 void
6822handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006823 garray_T *values,
6824 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006825{
6826 int type;
6827 long_u len;
6828 char_u *val;
6829 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006830 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006831
6832 /* Check the format:
6833 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006834 if (values->ga_len < 4
6835 || vp[0].bv_type != BVAL_NR
6836 || vp[1].bv_type != BVAL_NR
6837 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6838 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006839 return;
6840
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006841 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006842 if (type >= HIST_COUNT)
6843 return;
6844 if (viminfo_hisidx[type] < viminfo_hislen[type])
6845 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006846 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006847 if (val != NULL && *val != NUL)
6848 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006849 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6850 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006851 int idx;
6852 int overwrite = FALSE;
6853
6854 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6855 {
6856 /* If lines were written by an older Vim we need to avoid
6857 * getting duplicates. See if the entry already exists. */
6858 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6859 {
6860 p = viminfo_history[type][idx].hisstr;
6861 if (STRCMP(val, p) == 0
6862 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6863 {
6864 overwrite = TRUE;
6865 break;
6866 }
6867 }
6868
6869 if (!overwrite)
6870 {
6871 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006872 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006873 p = lalloc(len + 2, TRUE);
6874 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006875 else
6876 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006877 if (p != NULL)
6878 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006879 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006880 if (!overwrite)
6881 {
6882 mch_memmove(p, val, (size_t)len + 1);
6883 /* Put the separator after the NUL. */
6884 p[len + 1] = sep;
6885 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006886 viminfo_history[type][idx].hisnum = 0;
6887 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006888 viminfo_hisidx[type]++;
6889 }
6890 }
6891 }
6892 }
6893 }
6894}
6895
6896/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006897 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006898 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006899 static void
6900concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
6902 int idx;
6903 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006904
6905 idx = hisidx[type] + viminfo_hisidx[type];
6906 if (idx >= hislen)
6907 idx -= hislen;
6908 else if (idx < 0)
6909 idx = hislen - 1;
6910 if (viminfo_add_at_front)
6911 hisidx[type] = idx;
6912 else
6913 {
6914 if (hisidx[type] == -1)
6915 hisidx[type] = hislen - 1;
6916 do
6917 {
6918 if (history[type][idx].hisstr != NULL
6919 || history[type][idx].viminfo)
6920 break;
6921 if (++idx == hislen)
6922 idx = 0;
6923 } while (idx != hisidx[type]);
6924 if (idx != hisidx[type] && --idx < 0)
6925 idx = hislen - 1;
6926 }
6927 for (i = 0; i < viminfo_hisidx[type]; i++)
6928 {
6929 vim_free(history[type][idx].hisstr);
6930 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6931 history[type][idx].viminfo = TRUE;
6932 history[type][idx].time_set = viminfo_history[type][i].time_set;
6933 if (--idx < 0)
6934 idx = hislen - 1;
6935 }
6936 idx += 1;
6937 idx %= hislen;
6938 for (i = 0; i < viminfo_hisidx[type]; i++)
6939 {
6940 history[type][idx++].hisnum = ++hisnum[type];
6941 idx %= hislen;
6942 }
6943}
6944
6945#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6946 static int
6947#ifdef __BORLANDC__
6948_RTLENTRYF
6949#endif
6950sort_hist(const void *s1, const void *s2)
6951{
6952 histentry_T *p1 = *(histentry_T **)s1;
6953 histentry_T *p2 = *(histentry_T **)s2;
6954
6955 if (p1->time_set < p2->time_set) return -1;
6956 if (p1->time_set > p2->time_set) return 1;
6957 return 0;
6958}
6959#endif
6960
6961/*
6962 * Merge history lines from viminfo and lines typed in this Vim based on the
6963 * timestamp;
6964 */
6965 static void
6966merge_history(int type)
6967{
6968 int max_len;
6969 histentry_T **tot_hist;
6970 histentry_T *new_hist;
6971 int i;
6972 int len;
6973
6974 /* Make one long list with all entries. */
6975 max_len = hislen + viminfo_hisidx[type];
6976 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6977 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6978 if (tot_hist == NULL || new_hist == NULL)
6979 {
6980 vim_free(tot_hist);
6981 vim_free(new_hist);
6982 return;
6983 }
6984 for (i = 0; i < viminfo_hisidx[type]; i++)
6985 tot_hist[i] = &viminfo_history[type][i];
6986 len = i;
6987 for (i = 0; i < hislen; i++)
6988 if (history[type][i].hisstr != NULL)
6989 tot_hist[len++] = &history[type][i];
6990
6991 /* Sort the list on timestamp. */
6992 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6993
6994 /* Keep the newest ones. */
6995 for (i = 0; i < hislen; i++)
6996 {
6997 if (i < len)
6998 {
6999 new_hist[i] = *tot_hist[i];
7000 tot_hist[i]->hisstr = NULL;
7001 if (new_hist[i].hisnum == 0)
7002 new_hist[i].hisnum = ++hisnum[type];
7003 }
7004 else
7005 clear_hist_entry(&new_hist[i]);
7006 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007007 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007008
7009 /* Free what is not kept. */
7010 for (i = 0; i < viminfo_hisidx[type]; i++)
7011 vim_free(viminfo_history[type][i].hisstr);
7012 for (i = 0; i < hislen; i++)
7013 vim_free(history[type][i].hisstr);
7014 vim_free(history[type]);
7015 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007016 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007017}
7018
7019/*
7020 * Finish reading history lines from viminfo. Not used when writing viminfo.
7021 */
7022 void
7023finish_viminfo_history(vir_T *virp)
7024{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007026 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
7028 for (type = 0; type < HIST_COUNT; ++type)
7029 {
7030 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007031 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007032
7033 if (merge)
7034 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007036 concat_history(type);
7037
Bram Moolenaard23a8232018-02-10 18:45:26 +01007038 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007039 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041}
7042
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007043/*
7044 * Write history to viminfo file in "fp".
7045 * When "merge" is TRUE merge history lines with a previously read viminfo
7046 * file, data is in viminfo_history[].
7047 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007050write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int i;
7053 int type;
7054 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007055 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057 init_history();
7058 if (hislen == 0)
7059 return;
7060 for (type = 0; type < HIST_COUNT; ++type)
7061 {
7062 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7063 if (num_saved == 0)
7064 continue;
7065 if (num_saved < 0) /* Use default */
7066 num_saved = hislen;
7067 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7068 type == HIST_CMD ? _("Command Line") :
7069 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007070 type == HIST_EXPR ? _("Expression") :
7071 type == HIST_INPUT ? _("Input Line") :
7072 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (num_saved > hislen)
7074 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007075
7076 /*
7077 * Merge typed and viminfo history:
7078 * round 1: history of typed commands.
7079 * round 2: history from recently read viminfo.
7080 */
7081 for (round = 1; round <= 2; ++round)
7082 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007083 if (round == 1)
7084 /* start at newest entry, somewhere in the list */
7085 i = hisidx[type];
7086 else if (viminfo_hisidx[type] > 0)
7087 /* start at newest entry, first in the list */
7088 i = 0;
7089 else
7090 /* empty list */
7091 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007092 if (i >= 0)
7093 while (num_saved > 0
7094 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007096 char_u *p;
7097 time_t timestamp;
7098 int c = NUL;
7099
7100 if (round == 1)
7101 {
7102 p = history[type][i].hisstr;
7103 timestamp = history[type][i].time_set;
7104 }
7105 else
7106 {
7107 p = viminfo_history[type] == NULL ? NULL
7108 : viminfo_history[type][i].hisstr;
7109 timestamp = viminfo_history[type] == NULL ? 0
7110 : viminfo_history[type][i].time_set;
7111 }
7112
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007113 if (p != NULL && (round == 2
7114 || !merge
7115 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007117 --num_saved;
7118 fputc(hist_type2char(type, TRUE), fp);
7119 /* For the search history: put the separator in the
7120 * second column; use a space if there isn't one. */
7121 if (type == HIST_SEARCH)
7122 {
7123 c = p[STRLEN(p) + 1];
7124 putc(c == NUL ? ' ' : c, fp);
7125 }
7126 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007127
7128 {
7129 char cbuf[NUMBUFLEN];
7130
7131 /* New style history with a bar line. Format:
7132 * |{bartype},{histtype},{timestamp},{separator},"text" */
7133 if (c == NUL)
7134 cbuf[0] = NUL;
7135 else
7136 sprintf(cbuf, "%d", c);
7137 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7138 type, (long)timestamp, cbuf);
7139 barline_writestring(fp, p, LSIZE - 20);
7140 putc('\n', fp);
7141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007143 if (round == 1)
7144 {
7145 /* Decrement index, loop around and stop when back at
7146 * the start. */
7147 if (--i < 0)
7148 i = hislen - 1;
7149 if (i == hisidx[type])
7150 break;
7151 }
7152 else
7153 {
7154 /* Increment index. Stop at the end in the while. */
7155 ++i;
7156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007158 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007159 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007160 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007161 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007162 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007163 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
7165}
7166#endif /* FEAT_VIMINFO */
7167
7168#if defined(FEAT_FKMAP) || defined(PROTO)
7169/*
7170 * Write a character at the current cursor+offset position.
7171 * It is directly written into the command buffer block.
7172 */
7173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7177 {
7178 EMSG(_("E198: cmd_pchar beyond the command length"));
7179 return;
7180 }
7181 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7182 ccline.cmdbuff[ccline.cmdlen] = NUL;
7183}
7184
7185 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7189 {
7190 /* EMSG(_("cmd_gchar beyond the command length")); */
7191 return NUL;
7192 }
7193 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7194}
7195#endif
7196
7197#if defined(FEAT_CMDWIN) || defined(PROTO)
7198/*
7199 * Open a window on the current command line and history. Allow editing in
7200 * the window. Returns when the window is closed.
7201 * Returns:
7202 * CR if the command is to be executed
7203 * Ctrl_C if it is to be abandoned
7204 * K_IGNORE if editing continues
7205 */
7206 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007207open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007209 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007211 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 win_T *wp;
7213 int i;
7214 linenr_T lnum;
7215 int histtype;
7216 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 int save_restart_edit = restart_edit;
7218 int save_State = State;
7219 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007220#ifdef FEAT_RIGHTLEFT
7221 int save_cmdmsg_rl = cmdmsg_rl;
7222#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007223#ifdef FEAT_FOLDING
7224 int save_KeyTyped;
7225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227 /* Can't do this recursively. Can't do it when typing a password. */
7228 if (cmdwin_type != 0
7229# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7230 || cmdline_star > 0
7231# endif
7232 )
7233 {
7234 beep_flush();
7235 return K_IGNORE;
7236 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007237 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
7239 /* Save current window sizes. */
7240 win_size_save(&winsizes);
7241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007243 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007244
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007245 /* don't use a new tab page */
7246 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007247 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 /* Create a window for the command-line buffer. */
7250 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7251 {
7252 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007253 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 return K_IGNORE;
7255 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007256 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
7258 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007259 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007260 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007263#ifdef FEAT_FOLDING
7264 curwin->w_p_fen = FALSE;
7265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007267 curwin->w_p_rl = cmdmsg_rl;
7268 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007270 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007273 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007274 /* But don't allow switching to another buffer. */
7275 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
Bram Moolenaar46152342005-09-07 21:18:43 +00007277 /* Showing the prompt may have set need_wait_return, reset it. */
7278 need_wait_return = FALSE;
7279
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007280 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7282 {
7283 if (p_wc == TAB)
7284 {
7285 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7286 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7287 }
7288 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7289 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007290 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007292 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7293 * sets 'textwidth' to 78). */
7294 curbuf->b_p_tw = 0;
7295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 /* Fill the buffer with the history. */
7297 init_history();
7298 if (hislen > 0)
7299 {
7300 i = hisidx[histtype];
7301 if (i >= 0)
7302 {
7303 lnum = 0;
7304 do
7305 {
7306 if (++i == hislen)
7307 i = 0;
7308 if (history[histtype][i].hisstr != NULL)
7309 ml_append(lnum++, history[histtype][i].hisstr,
7310 (colnr_T)0, FALSE);
7311 }
7312 while (i != hisidx[histtype]);
7313 }
7314 }
7315
7316 /* Replace the empty last line with the current command-line and put the
7317 * cursor there. */
7318 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7319 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7320 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007321 changed_line_abv_curs();
7322 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007323 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 /* No Ex mode here! */
7326 exmode_active = 0;
7327
7328 State = NORMAL;
7329# ifdef FEAT_MOUSE
7330 setmouse();
7331# endif
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007334 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007335 if (restart_edit != 0) /* autocmd with ":startinsert" */
7336 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
7338 i = RedrawingDisabled;
7339 RedrawingDisabled = 0;
7340
7341 /*
7342 * Call the main loop until <CR> or CTRL-C is typed.
7343 */
7344 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007345 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
7347 RedrawingDisabled = i;
7348
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007349# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007350 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007351# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007354 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007355
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007356# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007357 /* Restore KeyTyped in case it is modified by autocommands */
7358 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359# endif
7360
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 exmode_active = save_exmode;
7363
Bram Moolenaarf9821062008-06-20 16:31:07 +00007364 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007366 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
7368 cmdwin_result = Ctrl_C;
7369 EMSG(_("E199: Active window or buffer deleted"));
7370 }
7371 else
7372 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007373# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 /* autocmds may abort script processing */
7375 if (aborting() && cmdwin_result != K_IGNORE)
7376 cmdwin_result = Ctrl_C;
7377# endif
7378 /* Set the new command line from the cmdline buffer. */
7379 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007380 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007382 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7383
7384 if (histtype == HIST_CMD)
7385 {
7386 /* Execute the command directly. */
7387 ccline.cmdbuff = vim_strsave((char_u *)p);
7388 cmdwin_result = CAR;
7389 }
7390 else
7391 {
7392 /* First need to cancel what we were doing. */
7393 ccline.cmdbuff = NULL;
7394 stuffcharReadbuff(':');
7395 stuffReadbuff((char_u *)p);
7396 stuffcharReadbuff(CAR);
7397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
7399 else if (cmdwin_result == K_XF2) /* :qa typed */
7400 {
7401 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7402 cmdwin_result = CAR;
7403 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007404 else if (cmdwin_result == Ctrl_C)
7405 {
7406 /* :q or :close, don't execute any command
7407 * and don't modify the cmd window. */
7408 ccline.cmdbuff = NULL;
7409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 else
7411 ccline.cmdbuff = vim_strsave(ml_get_curline());
7412 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007413 {
7414 ccline.cmdbuff = vim_strsave((char_u *)"");
7415 ccline.cmdlen = 0;
7416 ccline.cmdbufflen = 1;
7417 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 else
7421 {
7422 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7423 ccline.cmdbufflen = ccline.cmdlen + 1;
7424 ccline.cmdpos = curwin->w_cursor.col;
7425 if (ccline.cmdpos > ccline.cmdlen)
7426 ccline.cmdpos = ccline.cmdlen;
7427 if (cmdwin_result == K_IGNORE)
7428 {
7429 set_cmdspos_cursor();
7430 redrawcmd();
7431 }
7432 }
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007435 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007436# ifdef FEAT_CONCEAL
7437 /* Avoid command-line window first character being concealed. */
7438 curwin->w_p_cole = 0;
7439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007441 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 win_goto(old_curwin);
7443 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007444
7445 /* win_close() may have already wiped the buffer when 'bh' is
7446 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007447 if (bufref_valid(&bufref))
7448 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
7450 /* Restore window sizes. */
7451 win_size_restore(&winsizes);
7452
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007453 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 }
7455
7456 ga_clear(&winsizes);
7457 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007458# ifdef FEAT_RIGHTLEFT
7459 cmdmsg_rl = save_cmdmsg_rl;
7460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 State = save_State;
7463# ifdef FEAT_MOUSE
7464 setmouse();
7465# endif
7466
7467 return cmdwin_result;
7468}
7469#endif /* FEAT_CMDWIN */
7470
7471/*
7472 * Used for commands that either take a simple command string argument, or:
7473 * cmd << endmarker
7474 * {script}
7475 * endmarker
7476 * Returns a pointer to allocated memory with {script} or NULL.
7477 */
7478 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481 char_u *theline;
7482 char *end_pattern = NULL;
7483 char dot[] = ".";
7484 garray_T ga;
7485
7486 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7487 return NULL;
7488
7489 ga_init2(&ga, 1, 0x400);
7490
7491 if (cmd[2] != NUL)
7492 end_pattern = (char *)skipwhite(cmd + 2);
7493 else
7494 end_pattern = dot;
7495
7496 for (;;)
7497 {
7498 theline = eap->getline(
7499#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007500 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501#endif
7502 NUL, eap->cookie, 0);
7503
7504 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007505 {
7506 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
7510 ga_concat(&ga, theline);
7511 ga_append(&ga, '\n');
7512 vim_free(theline);
7513 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007514 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
7516 return (char_u *)ga.ga_data;
7517}