blob: 644259863a95d13295c285ebecc524c2a8c8c63c [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
Bram Moolenaard25c16e2016-01-29 22:13:30 +010079static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010081static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082# endif
83#endif
84
85#ifdef FEAT_RIGHTLEFT
86static int cmd_hkmap = 0; /* Hebrew mapping during command line */
87#endif
88
89#ifdef FEAT_FKMAP
90static int cmd_fkmap = 0; /* Farsi mapping during command line */
91#endif
92
Bram Moolenaar438d1762018-09-30 17:11:48 +020093static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static int cmdline_charsize(int idx);
95static void set_cmdspos(void);
96static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void alloc_cmdbuff(int len);
101static int realloc_cmdbuff(int len);
102static void draw_cmdline(int start, int len);
103static void save_cmdline(struct cmdline_info *ccp);
104static void restore_cmdline(struct cmdline_info *ccp);
105static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100107static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100110static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static void redrawcmdprompt(void);
113static void cursorcmd(void);
114static int ccheck_abbr(int);
115static int nextwild(expand_T *xp, int type, int options, int escape);
116static void escape_fname(char_u **pp);
117static int showmatches(expand_T *xp, int wildmenu);
118static void set_expand_context(expand_T *xp);
119static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
120static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100123static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100124static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200125# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100129static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
130static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# endif
132#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200133#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100134static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200138static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
142static int
143#ifdef __BORLANDC__
144_RTLENTRYF
145#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100146sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147#endif
148
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200149
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200161 * Abandon the command line.
162 */
163 static void
164abandon_cmdline(void)
165{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100166 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200167 if (msg_scrolled == 0)
168 compute_cmdrow();
169 MSG("");
170 redraw_cmdline = TRUE;
171}
172
Bram Moolenaaree219b02017-12-17 14:55:01 +0100173#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200174/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100175 * Guess that the pattern matches everything. Only finds specific cases, such
176 * as a trailing \|, which can happen while typing a pattern.
177 */
178 static int
179empty_pattern(char_u *p)
180{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100181 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100182
183 /* remove trailing \v and the like */
184 while (n >= 2 && p[n - 2] == '\\'
185 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
186 n -= 2;
187 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
188}
189
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200190// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200191typedef struct {
192 colnr_T vs_curswant;
193 colnr_T vs_leftcol;
194 linenr_T vs_topline;
195# ifdef FEAT_DIFF
196 int vs_topfill;
197# endif
198 linenr_T vs_botline;
199 linenr_T vs_empty_rows;
200} viewstate_T;
201
202 static void
203save_viewstate(viewstate_T *vs)
204{
205 vs->vs_curswant = curwin->w_curswant;
206 vs->vs_leftcol = curwin->w_leftcol;
207 vs->vs_topline = curwin->w_topline;
208# ifdef FEAT_DIFF
209 vs->vs_topfill = curwin->w_topfill;
210# endif
211 vs->vs_botline = curwin->w_botline;
212 vs->vs_empty_rows = curwin->w_empty_rows;
213}
214
215 static void
216restore_viewstate(viewstate_T *vs)
217{
218 curwin->w_curswant = vs->vs_curswant;
219 curwin->w_leftcol = vs->vs_leftcol;
220 curwin->w_topline = vs->vs_topline;
221# ifdef FEAT_DIFF
222 curwin->w_topfill = vs->vs_topfill;
223# endif
224 curwin->w_botline = vs->vs_botline;
225 curwin->w_empty_rows = vs->vs_empty_rows;
226}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200227
228// Struct to store the state of 'incsearch' highlighting.
229typedef struct {
230 pos_T search_start; // where 'incsearch' starts searching
231 pos_T save_cursor;
232 viewstate_T init_viewstate;
233 viewstate_T old_viewstate;
234 pos_T match_start;
235 pos_T match_end;
236 int did_incsearch;
237 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200238 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200239} incsearch_state_T;
240
241 static void
242init_incsearch_state(incsearch_state_T *is_state)
243{
244 is_state->match_start = curwin->w_cursor;
245 is_state->did_incsearch = FALSE;
246 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200247 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200248 CLEAR_POS(&is_state->match_end);
249 is_state->save_cursor = curwin->w_cursor; // may be restored later
250 is_state->search_start = curwin->w_cursor;
251 save_viewstate(&is_state->init_viewstate);
252 save_viewstate(&is_state->old_viewstate);
253}
254
255/*
256 * First move cursor to end of match, then to the start. This
257 * moves the whole match onto the screen when 'nowrap' is set.
258 */
259 static void
260set_search_match(pos_T *t)
261{
262 t->lnum += search_match_lines;
263 t->col = search_match_endcol;
264 if (t->lnum > curbuf->b_ml.ml_line_count)
265 {
266 t->lnum = curbuf->b_ml.ml_line_count;
267 coladvance((colnr_T)MAXCOL);
268 }
269}
270
271/*
272 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200274 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200275 */
276 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200277do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
278 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200279{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200280 char_u *cmd;
281 cmdmod_T save_cmdmod = cmdmod;
282 char_u *p;
283 int delim_optional = FALSE;
284 int delim;
285 char_u *end;
286 char_u *dummy;
287 exarg_T ea;
288 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200289 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200290
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200291 *skiplen = 0;
292 *patlen = ccline.cmdlen;
293
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200294 if (!p_is || cmd_silent)
295 return FALSE;
296
297 // by default search all lines
298 search_first_line = 0;
299 search_last_line = MAXLNUM;
300
301 if (firstc == '/' || firstc == '?')
302 return TRUE;
303 if (firstc != ':')
304 return FALSE;
305
306 vim_memset(&ea, 0, sizeof(ea));
307 ea.line1 = 1;
308 ea.line2 = 1;
309 ea.cmd = ccline.cmdbuff;
310 ea.addr_type = ADDR_LINES;
311
312 parse_command_modifiers(&ea, &dummy, TRUE);
313 cmdmod = save_cmdmod;
314
315 cmd = skip_range(ea.cmd, NULL);
316 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
317 return FALSE;
318
319 // Skip over "substitute" to find the pattern separator.
320 for (p = cmd; ASCII_ISALPHA(*p); ++p)
321 ;
322 if (*skipwhite(p) == NUL)
323 return FALSE;
324
325 if (STRNCMP(cmd, "substitute", p - cmd) == 0
326 || STRNCMP(cmd, "smagic", p - cmd) == 0
327 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
328 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200329 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200330 if (*cmd == 's' && cmd[1] == 'm')
331 p_magic = TRUE;
332 else if (*cmd == 's' && cmd[1] == 'n')
333 p_magic = FALSE;
334 }
335 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
336 {
337 // skip over flags
338 while (ASCII_ISALPHA(*(p = skipwhite(p))))
339 ++p;
340 if (*p == NUL)
341 return FALSE;
342 }
343 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
344 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
345 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
346 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
347 || STRNCMP(cmd, "global", p - cmd) == 0)
348 {
349 // skip over "!"
350 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200351 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200352 p++;
353 if (*skipwhite(p) == NUL)
354 return FALSE;
355 }
356 if (*cmd != 'g')
357 delim_optional = TRUE;
358 }
359 else
360 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200361
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200362 p = skipwhite(p);
363 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
364 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200365
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200366 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200367
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200368 if (end == p && !use_last_pat)
369 return FALSE;
370
371 // Don't do 'hlsearch' highlighting if the pattern matches everything.
372 if (!use_last_pat)
373 {
374 char c = *end;
375 int empty;
376
377 *end = NUL;
378 empty = empty_pattern(p);
379 *end = c;
380 if (empty)
381 return FALSE;
382 }
383
384 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200385 *skiplen = (int)(p - ccline.cmdbuff);
386 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200387
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200388 // parse the address range
389 save_cursor = curwin->w_cursor;
390 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200391 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200392 if (ea.addr_count > 0)
393 {
394 // Allow for reverse match.
395 if (ea.line2 < ea.line1)
396 {
397 search_first_line = ea.line2;
398 search_last_line = ea.line1;
399 }
400 else
401 {
402 search_first_line = ea.line1;
403 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404 }
405 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200406 else if (cmd[0] == 's' && cmd[1] != 'o')
407 {
408 // :s defaults to the current line
409 search_first_line = curwin->w_cursor.lnum;
410 search_last_line = curwin->w_cursor.lnum;
411 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200412
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200413 curwin->w_cursor = save_cursor;
414 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200415}
416
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200417 static void
418finish_incsearch_highlighting(
419 int gotesc,
420 incsearch_state_T *is_state,
421 int call_update_screen)
422{
423 if (is_state->did_incsearch)
424 {
425 is_state->did_incsearch = FALSE;
426 if (gotesc)
427 curwin->w_cursor = is_state->save_cursor;
428 else
429 {
430 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
431 {
432 // put the '" mark at the original position
433 curwin->w_cursor = is_state->save_cursor;
434 setpcmark();
435 }
436 curwin->w_cursor = is_state->search_start;
437 }
438 restore_viewstate(&is_state->old_viewstate);
439 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200440
441 // by default search all lines
442 search_first_line = 0;
443 search_last_line = MAXLNUM;
444
445 p_magic = is_state->magic_save;
446
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200447 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200448 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200449 if (call_update_screen)
450 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200451 }
452}
453
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200454/*
455 * Do 'incsearch' highlighting if desired.
456 */
457 static void
458may_do_incsearch_highlighting(
459 int firstc,
460 long count,
461 incsearch_state_T *is_state)
462{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200463 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200464 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200465 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466#ifdef FEAT_RELTIME
467 proftime_T tm;
468#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200469 int next_char;
470 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200471
Bram Moolenaar198cb662018-09-06 21:44:17 +0200472 // Parsing range may already set the last search pattern.
473 save_last_search_pattern();
474
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200475 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200476 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200477 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200478 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200480 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200481
482 // If there is a character waiting, search and redraw later.
483 if (char_avail())
484 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200485 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200486 is_state->incsearch_postponed = TRUE;
487 return;
488 }
489 is_state->incsearch_postponed = FALSE;
490
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200491 if (search_first_line == 0)
492 // start at the original cursor position
493 curwin->w_cursor = is_state->search_start;
494 else
495 {
496 // start at the first line in the range
497 curwin->w_cursor.lnum = search_first_line;
498 curwin->w_cursor.col = 0;
499 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200500
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200501 // Use the previous pattern for ":s//".
502 next_char = ccline.cmdbuff[skiplen + patlen];
503 use_last_pat = patlen == 0 && skiplen > 0
504 && ccline.cmdbuff[skiplen - 1] == next_char;
505
506 // If there is no pattern, don't do anything.
507 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200508 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200509 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200510 set_no_hlsearch(TRUE); // turn off previous highlight
511 redraw_all_later(SOME_VALID);
512 }
513 else
514 {
515 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
516
517 cursor_off(); // so the user knows we're busy
518 out_flush();
519 ++emsg_off; // so it doesn't beep if bad expr
520#ifdef FEAT_RELTIME
521 // Set the time limit to half a second.
522 profile_setlimit(500L, &tm);
523#endif
524 if (!p_hls)
525 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200526 if (search_first_line != 0)
527 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200528 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200529 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200530 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200531#ifdef FEAT_RELTIME
532 &tm, NULL
533#else
534 NULL, NULL
535#endif
536 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200537 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200538 --emsg_off;
539
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200540 if (curwin->w_cursor.lnum < search_first_line
541 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200542 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200543 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200544 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200545 curwin->w_cursor = is_state->search_start;
546 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200547
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200548 // if interrupted while searching, behave like it failed
549 if (got_int)
550 {
551 (void)vpeekc(); // remove <C-C> from input stream
552 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200553 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200554 }
555 else if (char_avail())
556 // cancelled searching because a char was typed
557 is_state->incsearch_postponed = TRUE;
558 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200559 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200560 highlight_match = TRUE; // highlight position
561 else
562 highlight_match = FALSE; // remove highlight
563
564 // First restore the old curwin values, so the screen is positioned in the
565 // same way as the actual search command.
566 restore_viewstate(&is_state->old_viewstate);
567 changed_cline_bef_curs();
568 update_topline();
569
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200570 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200571 {
572 pos_T save_pos = curwin->w_cursor;
573
574 is_state->match_start = curwin->w_cursor;
575 set_search_match(&curwin->w_cursor);
576 validate_cursor();
577 end_pos = curwin->w_cursor;
578 is_state->match_end = end_pos;
579 curwin->w_cursor = save_pos;
580 }
581 else
582 end_pos = curwin->w_cursor; // shutup gcc 4
583
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200584 // Disable 'hlsearch' highlighting if the pattern matches everything.
585 // Avoids a flash when typing "foo\|".
586 if (!use_last_pat)
587 {
588 next_char = ccline.cmdbuff[skiplen + patlen];
589 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200590 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
591 {
592 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200593 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200594 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200595 ccline.cmdbuff[skiplen + patlen] = next_char;
596 }
597
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200598 validate_cursor();
599 // May redraw the status line to show the cursor position.
600 if (p_ru && curwin->w_status_height > 0)
601 curwin->w_redr_status = TRUE;
602
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200603 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 restore_last_search_pattern();
605
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200606 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
607 // end of the pattern, e.g. for ":s/pat/".
608 if (ccline.cmdbuff[skiplen + patlen] != NUL)
609 curwin->w_cursor = is_state->search_start;
610 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200611 curwin->w_cursor = end_pos;
612
613 msg_starthere();
614 redrawcmdline();
615 is_state->did_incsearch = TRUE;
616}
617
618/*
619 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
620 * or previous match.
621 * Returns FAIL when jumping to cmdline_not_changed;
622 */
623 static int
624may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200625 int firstc,
626 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200627 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200630 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200631 pos_T t;
632 char_u *pat;
633 int search_flags = SEARCH_NOOF;
634 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200635 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200636
Bram Moolenaar198cb662018-09-06 21:44:17 +0200637 // Parsing range may already set the last search pattern.
638 save_last_search_pattern();
639
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200640 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200641 {
642 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200643 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200644 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200645 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200646 {
647 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200648 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200649 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200650
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200651 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200652 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200654 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200655 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200656 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200657 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200658 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200659
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 cursor_off();
661 out_flush();
662 if (c == Ctrl_G)
663 {
664 t = is_state->match_end;
665 if (LT_POS(is_state->match_start, is_state->match_end))
666 // Start searching at the end of the match not at the beginning of
667 // the next column.
668 (void)decl(&t);
669 search_flags += SEARCH_COL;
670 }
671 else
672 t = is_state->match_start;
673 if (!p_hls)
674 search_flags += SEARCH_KEEP;
675 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200676 save = pat[patlen];
677 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200678 i = searchit(curwin, curbuf, &t,
679 c == Ctrl_G ? FORWARD : BACKWARD,
680 pat, count, search_flags,
681 RE_SEARCH, 0, NULL, NULL);
682 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200683 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200684 if (i)
685 {
686 is_state->search_start = is_state->match_start;
687 is_state->match_end = t;
688 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200689 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200690 {
691 // Move just before the current match, so that when nv_search
692 // finishes the cursor will be put back on the match.
693 is_state->search_start = t;
694 (void)decl(&is_state->search_start);
695 }
696 else if (c == Ctrl_G && firstc == '?')
697 {
698 // Move just after the current match, so that when nv_search
699 // finishes the cursor will be put back on the match.
700 is_state->search_start = t;
701 (void)incl(&is_state->search_start);
702 }
703 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
704 {
705 // wrap around
706 is_state->search_start = t;
707 if (firstc == '?')
708 (void)incl(&is_state->search_start);
709 else
710 (void)decl(&is_state->search_start);
711 }
712
713 set_search_match(&is_state->match_end);
714 curwin->w_cursor = is_state->match_start;
715 changed_cline_bef_curs();
716 update_topline();
717 validate_cursor();
718 highlight_match = TRUE;
719 save_viewstate(&is_state->old_viewstate);
720 update_screen(NOT_VALID);
721 redrawcmdline();
722 }
723 else
724 vim_beep(BO_ERROR);
725 restore_last_search_pattern();
726 return FAIL;
727}
728
729/*
730 * When CTRL-L typed: add character from the match to the pattern.
731 * May set "*c" to the added character.
732 * Return OK when jumping to cmdline_not_changed.
733 */
734 static int
735may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
736{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200737 int skiplen, patlen;
738
Bram Moolenaar198cb662018-09-06 21:44:17 +0200739 // Parsing range may already set the last search pattern.
740 save_last_search_pattern();
741
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200742 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200743 {
744 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200745 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200746 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200747
748 // Add a character from under the cursor for 'incsearch'.
749 if (is_state->did_incsearch)
750 {
751 curwin->w_cursor = is_state->match_end;
752 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
753 {
754 *c = gchar_cursor();
755
756 // If 'ignorecase' and 'smartcase' are set and the
757 // command line has no uppercase characters, convert
758 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200759 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200760 *c = MB_TOLOWER(*c);
761 if (*c != NUL)
762 {
763 if (*c == firstc || vim_strchr((char_u *)(
764 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
765 {
766 // put a backslash before special characters
767 stuffcharReadbuff(*c);
768 *c = '\\';
769 }
770 return FAIL;
771 }
772 }
773 }
774 return OK;
775}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200776#endif
777
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200778 void
779cmdline_init(void)
780{
781 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
782}
783
Bram Moolenaar66216052017-12-16 16:33:44 +0100784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 * getcmdline() - accept a command line starting with firstc.
786 *
787 * firstc == ':' get ":" command line.
788 * firstc == '/' or '?' get search pattern
789 * firstc == '=' get expression
790 * firstc == '@' get text for input() function
791 * firstc == '>' get text for debug mode
792 * firstc == NUL get text for :insert command
793 * firstc == -1 like NUL, and break on CTRL-C
794 *
795 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
796 * command line.
797 *
798 * Careful: getcmdline() can be called recursively!
799 *
800 * Return pointer to allocated string if there is a commandline, NULL
801 * otherwise.
802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100804getcmdline(
805 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200806 long count, // only used for incremental search
807 int indent) // indent for inside conditionals
808{
809 return getcmdline_int(firstc, count, indent, TRUE);
810}
811
812 static char_u *
813getcmdline_int(
814 int firstc,
815 long count UNUSED, // only used for incremental search
816 int indent, // indent for inside conditionals
817 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
819 int c;
820 int i;
821 int j;
822 int gotesc = FALSE; /* TRUE when <ESC> just typed */
823 int do_abbr; /* when TRUE check for abbr. */
824#ifdef FEAT_CMDHIST
825 char_u *lookfor = NULL; /* string to match */
826 int hiscnt; /* current history line in use */
827 int histype; /* history type to be used */
828#endif
829#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200830 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#endif
832 int did_wild_list = FALSE; /* did wild_list() recently */
833 int wim_index = 0; /* index in wim_flags[] */
834 int res;
835 int save_msg_scroll = msg_scroll;
836 int save_State = State; /* remember State when called */
837 int some_key_typed = FALSE; /* one of the keys was typed */
838#ifdef FEAT_MOUSE
839 /* mouse drag and release events are ignored, unless they are
840 * preceded with a mouse down event */
841 int ignore_drag_release = TRUE;
842#endif
843#ifdef FEAT_EVAL
844 int break_ctrl_c = FALSE;
845#endif
846 expand_T xpc;
847 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000848 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200849 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200850 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar438d1762018-09-30 17:11:48 +0200852 if (ccline.cmdbuff != NULL)
853 {
854 // Being called recursively. Since ccline is global, we need to save
855 // the current buffer and restore it when returning.
856 save_cmdline(&save_ccline);
857 did_save_ccline = TRUE;
858 }
859 if (init_ccline)
860 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#ifdef FEAT_EVAL
863 if (firstc == -1)
864 {
865 firstc = NUL;
866 break_ctrl_c = TRUE;
867 }
868#endif
869#ifdef FEAT_RIGHTLEFT
870 /* start without Hebrew mapping for a command line */
871 if (firstc == ':' || firstc == '=' || firstc == '>')
872 cmd_hkmap = 0;
873#endif
874
875 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200876
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200878 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880
881 /*
882 * set some variables for redrawcmd()
883 */
884 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000885 ccline.cmdindent = (firstc > 0 ? indent : 0);
886
887 /* alloc initial ccline.cmdbuff */
888 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200890 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 ccline.cmdlen = ccline.cmdpos = 0;
892 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100893 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000895 /* autoindent for :insert and :append */
896 if (firstc <= 0)
897 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200898 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000899 ccline.cmdbuff[indent] = NUL;
900 ccline.cmdpos = indent;
901 ccline.cmdspos = indent;
902 ccline.cmdlen = indent;
903 }
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000906 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
908#ifdef FEAT_RIGHTLEFT
909 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
910 && (firstc == '/' || firstc == '?'))
911 cmdmsg_rl = TRUE;
912 else
913 cmdmsg_rl = FALSE;
914#endif
915
916 redir_off = TRUE; /* don't redirect the typed command */
917 if (!cmd_silent)
918 {
919 i = msg_scrolled;
920 msg_scrolled = 0; /* avoid wait_return message */
921 gotocmdline(TRUE);
922 msg_scrolled += i;
923 redrawcmdprompt(); /* draw prompt or indent */
924 set_cmdspos();
925 }
926 xpc.xp_context = EXPAND_NOTHING;
927 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000928#ifndef BACKSLASH_IN_FILENAME
929 xpc.xp_shell = FALSE;
930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000932#if defined(FEAT_EVAL)
933 if (ccline.input_fn)
934 {
935 xpc.xp_context = ccline.xp_context;
936 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000937# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000938 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000939# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000940 }
941#endif
942
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 /*
944 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
945 * doing ":@0" when register 0 doesn't contain a CR.
946 */
947 msg_scroll = FALSE;
948
949 State = CMDLINE;
950
951 if (firstc == '/' || firstc == '?' || firstc == '@')
952 {
953 /* Use ":lmap" mappings for search pattern and input(). */
954 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
955 b_im_ptr = &curbuf->b_p_iminsert;
956 else
957 b_im_ptr = &curbuf->b_p_imsearch;
958 if (*b_im_ptr == B_IMODE_LMAP)
959 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100960#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 im_set_active(*b_im_ptr == B_IMODE_IM);
962#endif
963 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100964#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 else if (p_imcmdline)
966 im_set_active(TRUE);
967#endif
968
969#ifdef FEAT_MOUSE
970 setmouse();
971#endif
972#ifdef CURSOR_SHAPE
973 ui_cursor_shape(); /* may show different cursor shape */
974#endif
975
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000976 /* When inside an autocommand for writing "exiting" may be set and
977 * terminal mode set to cooked. Need to set raw mode here then. */
978 settmode(TMODE_RAW);
979
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200980 /* Trigger CmdlineEnter autocommands. */
981 cmdline_type = firstc == NUL ? '-' : firstc;
982 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef FEAT_CMDHIST
985 init_history();
986 hiscnt = hislen; /* set hiscnt to impossible history value */
987 histype = hist_char2type(firstc);
988#endif
989
990#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000991 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#endif
993
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200994 /* If something above caused an error, reset the flags, we do want to type
995 * and execute commands. Display may be messed up a bit. */
996 if (did_emsg)
997 redrawcmd();
998 did_emsg = FALSE;
999 got_int = FALSE;
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * Collect the command string, handling editing keys.
1003 */
1004 for (;;)
1005 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001006 redir_off = TRUE; /* Don't redirect the typed command.
1007 Repeated, because a ":redir" inside
1008 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#ifdef USE_ON_FLY_SCROLL
1010 dont_scroll = FALSE; /* allow scrolling here */
1011#endif
1012 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1013
Bram Moolenaar72532d32018-04-07 19:09:09 +02001014 did_emsg = FALSE; /* There can't really be a reason why an error
1015 that occurs while typing a command should
1016 cause the command not to be executed. */
1017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001019
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001020 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1021 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001022 do
1023 {
1024 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001025 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (KeyTyped)
1028 {
1029 some_key_typed = TRUE;
1030#ifdef FEAT_RIGHTLEFT
1031 if (cmd_hkmap)
1032 c = hkmap(c);
1033# ifdef FEAT_FKMAP
1034 if (cmd_fkmap)
1035 c = cmdl_fkmap(c);
1036# endif
1037 if (cmdmsg_rl && !KeyStuffed)
1038 {
1039 /* Invert horizontal movements and operations. Only when
1040 * typed by the user directly, not when the result of a
1041 * mapping. */
1042 switch (c)
1043 {
1044 case K_RIGHT: c = K_LEFT; break;
1045 case K_S_RIGHT: c = K_S_LEFT; break;
1046 case K_C_RIGHT: c = K_C_LEFT; break;
1047 case K_LEFT: c = K_RIGHT; break;
1048 case K_S_LEFT: c = K_S_RIGHT; break;
1049 case K_C_LEFT: c = K_C_RIGHT; break;
1050 }
1051 }
1052#endif
1053 }
1054
1055 /*
1056 * Ignore got_int when CTRL-C was typed here.
1057 * Don't ignore it in :global, we really need to break then, e.g., for
1058 * ":g/pat/normal /pat" (without the <CR>).
1059 * Don't ignore it for the input() function.
1060 */
1061 if ((c == Ctrl_C
1062#ifdef UNIX
1063 || c == intr_char
1064#endif
1065 )
1066#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1067 && firstc != '@'
1068#endif
1069#ifdef FEAT_EVAL
1070 && !break_ctrl_c
1071#endif
1072 && !global_busy)
1073 got_int = FALSE;
1074
1075#ifdef FEAT_CMDHIST
1076 /* free old command line when finished moving around in the history
1077 * list */
1078 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001079 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001080 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 && c != K_PAGEDOWN && c != K_PAGEUP
1082 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001083 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001085 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086#endif
1087
1088 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001089 * When there are matching completions to select <S-Tab> works like
1090 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001092 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 c = Ctrl_P;
1094
1095#ifdef FEAT_WILDMENU
1096 /* Special translations for 'wildmenu' */
1097 if (did_wild_list && p_wmnu)
1098 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001099 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001101 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 c = Ctrl_N;
1103 }
1104 /* Hitting CR after "emenu Name.": complete submenu */
1105 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1106 && ccline.cmdpos > 1
1107 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1108 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1109 && (c == '\n' || c == '\r' || c == K_KENTER))
1110 c = K_DOWN;
1111#endif
1112
1113 /* free expanded names when finished walking through matches */
1114 if (xpc.xp_numfiles != -1
1115 && !(c == p_wc && KeyTyped) && c != p_wcm
1116 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1117 && c != Ctrl_L)
1118 {
1119 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1120 did_wild_list = FALSE;
1121#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001122 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif
1124 xpc.xp_context = EXPAND_NOTHING;
1125 wim_index = 0;
1126#ifdef FEAT_WILDMENU
1127 if (p_wmnu && wild_menu_showing != 0)
1128 {
1129 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001130 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001131
1132 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001133 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 if (wild_menu_showing == WM_SCROLLED)
1136 {
1137 /* Entered command line, move it up */
1138 cmdline_row--;
1139 redrawcmd();
1140 }
1141 else if (save_p_ls != -1)
1142 {
1143 /* restore 'laststatus' and 'winminheight' */
1144 p_ls = save_p_ls;
1145 p_wmh = save_p_wmh;
1146 last_status(FALSE);
1147 update_screen(VALID); /* redraw the screen NOW */
1148 redrawcmd();
1149 save_p_ls = -1;
1150 }
1151 else
1152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 redraw_statuslines();
1155 }
1156 KeyTyped = skt;
1157 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001158 if (ccline.input_fn)
1159 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161#endif
1162 }
1163
1164#ifdef FEAT_WILDMENU
1165 /* Special translations for 'wildmenu' */
1166 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1167 {
1168 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001169 if (c == K_DOWN && ccline.cmdpos > 0
1170 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001172 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 /* Hitting <Up>: Remove one submenu name in front of the
1175 * cursor */
1176 int found = FALSE;
1177
1178 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1179 i = 0;
1180 while (--j > 0)
1181 {
1182 /* check for start of menu name */
1183 if (ccline.cmdbuff[j] == ' '
1184 && ccline.cmdbuff[j - 1] != '\\')
1185 {
1186 i = j + 1;
1187 break;
1188 }
1189 /* check for start of submenu name */
1190 if (ccline.cmdbuff[j] == '.'
1191 && ccline.cmdbuff[j - 1] != '\\')
1192 {
1193 if (found)
1194 {
1195 i = j + 1;
1196 break;
1197 }
1198 else
1199 found = TRUE;
1200 }
1201 }
1202 if (i > 0)
1203 cmdline_del(i);
1204 c = p_wc;
1205 xpc.xp_context = EXPAND_NOTHING;
1206 }
1207 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001208 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001209 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001210 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
1212 char_u upseg[5];
1213
1214 upseg[0] = PATHSEP;
1215 upseg[1] = '.';
1216 upseg[2] = '.';
1217 upseg[3] = PATHSEP;
1218 upseg[4] = NUL;
1219
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001220 if (c == K_DOWN
1221 && ccline.cmdpos > 0
1222 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1223 && (ccline.cmdpos < 3
1224 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1226 {
1227 /* go down a directory */
1228 c = p_wc;
1229 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001230 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
1232 /* If in a direct ancestor, strip off one ../ to go down */
1233 int found = FALSE;
1234
1235 j = ccline.cmdpos;
1236 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1237 while (--j > i)
1238 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001239#ifdef FEAT_MBYTE
1240 if (has_mbyte)
1241 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (vim_ispathsep(ccline.cmdbuff[j]))
1244 {
1245 found = TRUE;
1246 break;
1247 }
1248 }
1249 if (found
1250 && ccline.cmdbuff[j - 1] == '.'
1251 && ccline.cmdbuff[j - 2] == '.'
1252 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1253 {
1254 cmdline_del(j - 2);
1255 c = p_wc;
1256 }
1257 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001258 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
1260 /* go up a directory */
1261 int found = FALSE;
1262
1263 j = ccline.cmdpos - 1;
1264 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1265 while (--j > i)
1266 {
1267#ifdef FEAT_MBYTE
1268 if (has_mbyte)
1269 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1270#endif
1271 if (vim_ispathsep(ccline.cmdbuff[j])
1272#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001273 && vim_strchr((char_u *)" *?[{`$%#",
1274 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#endif
1276 )
1277 {
1278 if (found)
1279 {
1280 i = j + 1;
1281 break;
1282 }
1283 else
1284 found = TRUE;
1285 }
1286 }
1287
1288 if (!found)
1289 j = i;
1290 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1291 j += 4;
1292 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1293 && j == i)
1294 j += 3;
1295 else
1296 j = 0;
1297 if (j > 0)
1298 {
1299 /* TODO this is only for DOS/UNIX systems - need to put in
1300 * machine-specific stuff here and in upseg init */
1301 cmdline_del(j);
1302 put_on_cmdline(upseg + 1, 3, FALSE);
1303 }
1304 else if (ccline.cmdpos > i)
1305 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001306
1307 /* Now complete in the new directory. Set KeyTyped in case the
1308 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001310 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314#endif /* FEAT_WILDMENU */
1315
1316 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1317 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1318 if (c == Ctrl_BSL)
1319 {
1320 ++no_mapping;
1321 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001322 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 --no_mapping;
1324 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001325 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1326 * is in a mapping. */
1327 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001328 || (ccline.cmdfirstc == '=' && KeyTyped)
1329#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001330 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001331#endif
1332 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 vungetc(c);
1335 c = Ctrl_BSL;
1336 }
1337#ifdef FEAT_EVAL
1338 else if (c == 'e')
1339 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001340 char_u *p = NULL;
1341 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 /*
1344 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001345 * Need to save and restore the current command line, to be
1346 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 */
1348 if (ccline.cmdpos == ccline.cmdlen)
1349 new_cmdpos = 99999; /* keep it at the end */
1350 else
1351 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (c == '=')
1355 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001356 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001357 * to avoid nasty things like going to another buffer when
1358 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001359 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001361 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001362
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001363 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001365 len = (int)STRLEN(p);
1366 if (realloc_cmdbuff(len + 1) == OK)
1367 {
1368 ccline.cmdlen = len;
1369 STRCPY(ccline.cmdbuff, p);
1370 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001372 /* Restore the cursor or use the position set with
1373 * set_cmdline_pos(). */
1374 if (new_cmdpos > ccline.cmdlen)
1375 ccline.cmdpos = ccline.cmdlen;
1376 else
1377 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001379 KeyTyped = FALSE; /* Don't do p_wc completion. */
1380 redrawcmd();
1381 goto cmdline_changed;
1382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 }
1385 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001386 got_int = FALSE; /* don't abandon the command line */
1387 did_emsg = FALSE;
1388 emsg_on_display = FALSE;
1389 redrawcmd();
1390 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392#endif
1393 else
1394 {
1395 if (c == Ctrl_G && p_im && restart_edit == 0)
1396 restart_edit = 'a';
1397 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1398 in history */
1399 goto returncmd; /* back to Normal mode */
1400 }
1401 }
1402
1403#ifdef FEAT_CMDWIN
1404 if (c == cedit_key || c == K_CMDWIN)
1405 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001406 if (ex_normal_busy == 0 && got_int == FALSE)
1407 {
1408 /*
1409 * Open a window to edit the command line (and history).
1410 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001411 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001412 some_key_typed = TRUE;
1413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415# ifdef FEAT_DIGRAPHS
1416 else
1417# endif
1418#endif
1419#ifdef FEAT_DIGRAPHS
1420 c = do_digraph(c);
1421#endif
1422
1423 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1424 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1425 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001426 /* In Ex mode a backslash escapes a newline. */
1427 if (exmode_active
1428 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001429 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001430 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001431 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001433 if (c == K_KENTER)
1434 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001436 else
1437 {
1438 gotesc = FALSE; /* Might have typed ESC previously, don't
1439 truncate the cmdline now. */
1440 if (ccheck_abbr(c + ABBR_OFF))
1441 goto cmdline_changed;
1442 if (!cmd_silent)
1443 {
1444 windgoto(msg_row, 0);
1445 out_flush();
1446 }
1447 break;
1448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450
1451 /*
1452 * Completion for 'wildchar' or 'wildcharm' key.
1453 * - hitting <ESC> twice means: abandon command line.
1454 * - wildcard expansion is only done when the 'wildchar' key is really
1455 * typed, not when it comes from a macro
1456 */
1457 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1458 {
1459 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1460 {
1461 /* if 'wildmode' contains "list" may still need to list */
1462 if (xpc.xp_numfiles > 1
1463 && !did_wild_list
1464 && (wim_flags[wim_index] & WIM_LIST))
1465 {
1466 (void)showmatches(&xpc, FALSE);
1467 redrawcmd();
1468 did_wild_list = TRUE;
1469 }
1470 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001471 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1472 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001474 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1475 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 else
1477 res = OK; /* don't insert 'wildchar' now */
1478 }
1479 else /* typed p_wc first time */
1480 {
1481 wim_index = 0;
1482 j = ccline.cmdpos;
1483 /* if 'wildmode' first contains "longest", get longest
1484 * common part */
1485 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001486 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1487 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001489 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1490 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 /* if interrupted while completing, behave like it failed */
1493 if (got_int)
1494 {
1495 (void)vpeekc(); /* remove <C-C> from input stream */
1496 got_int = FALSE; /* don't abandon the command line */
1497 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1498#ifdef FEAT_WILDMENU
1499 xpc.xp_context = EXPAND_NOTHING;
1500#endif
1501 goto cmdline_changed;
1502 }
1503
1504 /* when more than one match, and 'wildmode' first contains
1505 * "list", or no change and 'wildmode' contains "longest,list",
1506 * list all matches */
1507 if (res == OK && xpc.xp_numfiles > 1)
1508 {
1509 /* a "longest" that didn't do anything is skipped (but not
1510 * "list:longest") */
1511 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1512 wim_index = 1;
1513 if ((wim_flags[wim_index] & WIM_LIST)
1514#ifdef FEAT_WILDMENU
1515 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1516#endif
1517 )
1518 {
1519 if (!(wim_flags[0] & WIM_LONGEST))
1520 {
1521#ifdef FEAT_WILDMENU
1522 int p_wmnu_save = p_wmnu;
1523 p_wmnu = 0;
1524#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001525 /* remove match */
1526 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#ifdef FEAT_WILDMENU
1528 p_wmnu = p_wmnu_save;
1529#endif
1530 }
1531#ifdef FEAT_WILDMENU
1532 (void)showmatches(&xpc, p_wmnu
1533 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1534#else
1535 (void)showmatches(&xpc, FALSE);
1536#endif
1537 redrawcmd();
1538 did_wild_list = TRUE;
1539 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001540 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1541 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001543 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1544 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001547 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549#ifdef FEAT_WILDMENU
1550 else if (xpc.xp_numfiles == -1)
1551 xpc.xp_context = EXPAND_NOTHING;
1552#endif
1553 }
1554 if (wim_index < 3)
1555 ++wim_index;
1556 if (c == ESC)
1557 gotesc = TRUE;
1558 if (res == OK)
1559 goto cmdline_changed;
1560 }
1561
1562 gotesc = FALSE;
1563
1564 /* <S-Tab> goes to last match, in a clumsy way */
1565 if (c == K_S_TAB && KeyTyped)
1566 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001567 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1568 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1569 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 goto cmdline_changed;
1571 }
1572
1573 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1574 c = NL;
1575
1576 do_abbr = TRUE; /* default: check for abbreviation */
1577
1578 /*
1579 * Big switch for a typed command line character.
1580 */
1581 switch (c)
1582 {
1583 case K_BS:
1584 case Ctrl_H:
1585 case K_DEL:
1586 case K_KDEL:
1587 case Ctrl_W:
1588#ifdef FEAT_FKMAP
1589 if (cmd_fkmap && c == K_BS)
1590 c = K_DEL;
1591#endif
1592 if (c == K_KDEL)
1593 c = K_DEL;
1594
1595 /*
1596 * delete current character is the same as backspace on next
1597 * character, except at end of line
1598 */
1599 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1600 ++ccline.cmdpos;
1601#ifdef FEAT_MBYTE
1602 if (has_mbyte && c == K_DEL)
1603 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1604 ccline.cmdbuff + ccline.cmdpos);
1605#endif
1606 if (ccline.cmdpos > 0)
1607 {
1608 char_u *p;
1609
1610 j = ccline.cmdpos;
1611 p = ccline.cmdbuff + j;
1612#ifdef FEAT_MBYTE
1613 if (has_mbyte)
1614 {
1615 p = mb_prevptr(ccline.cmdbuff, p);
1616 if (c == Ctrl_W)
1617 {
1618 while (p > ccline.cmdbuff && vim_isspace(*p))
1619 p = mb_prevptr(ccline.cmdbuff, p);
1620 i = mb_get_class(p);
1621 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1622 p = mb_prevptr(ccline.cmdbuff, p);
1623 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001624 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627 else
1628#endif
1629 if (c == Ctrl_W)
1630 {
1631 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1632 --p;
1633 i = vim_iswordc(p[-1]);
1634 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1635 && vim_iswordc(p[-1]) == i)
1636 --p;
1637 }
1638 else
1639 --p;
1640 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1641 ccline.cmdlen -= j - ccline.cmdpos;
1642 i = ccline.cmdpos;
1643 while (i < ccline.cmdlen)
1644 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1645
1646 /* Truncate at the end, required for multi-byte chars. */
1647 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001648#ifdef FEAT_SEARCH_EXTRA
1649 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001650 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001651 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001652 /* save view settings, so that the screen
1653 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001654 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001655 }
1656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 redrawcmd();
1658 }
1659 else if (ccline.cmdlen == 0 && c != Ctrl_W
1660 && ccline.cmdprompt == NULL && indent == 0)
1661 {
1662 /* In ex and debug mode it doesn't make sense to return. */
1663 if (exmode_active
1664#ifdef FEAT_EVAL
1665 || ccline.cmdfirstc == '>'
1666#endif
1667 )
1668 goto cmdline_not_changed;
1669
Bram Moolenaard23a8232018-02-10 18:45:26 +01001670 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (!cmd_silent)
1672 {
1673#ifdef FEAT_RIGHTLEFT
1674 if (cmdmsg_rl)
1675 msg_col = Columns;
1676 else
1677#endif
1678 msg_col = 0;
1679 msg_putchar(' '); /* delete ':' */
1680 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001681#ifdef FEAT_SEARCH_EXTRA
1682 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001683 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 redraw_cmdline = TRUE;
1686 goto returncmd; /* back to cmd mode */
1687 }
1688 goto cmdline_changed;
1689
1690 case K_INS:
1691 case K_KINS:
1692#ifdef FEAT_FKMAP
1693 /* if Farsi mode set, we are in reverse insert mode -
1694 Do not change the mode */
1695 if (cmd_fkmap)
1696 beep_flush();
1697 else
1698#endif
1699 ccline.overstrike = !ccline.overstrike;
1700#ifdef CURSOR_SHAPE
1701 ui_cursor_shape(); /* may show different cursor shape */
1702#endif
1703 goto cmdline_not_changed;
1704
1705 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001706 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
1708 /* ":lmap" mappings exists, toggle use of mappings. */
1709 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001710#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 im_set_active(FALSE); /* Disable input method */
1712#endif
1713 if (b_im_ptr != NULL)
1714 {
1715 if (State & LANGMAP)
1716 *b_im_ptr = B_IMODE_LMAP;
1717 else
1718 *b_im_ptr = B_IMODE_NONE;
1719 }
1720 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001721#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 else
1723 {
1724 /* There are no ":lmap" mappings, toggle IM. When
1725 * 'imdisable' is set don't try getting the status, it's
1726 * always off. */
1727 if ((p_imdisable && b_im_ptr != NULL)
1728 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1729 {
1730 im_set_active(FALSE); /* Disable input method */
1731 if (b_im_ptr != NULL)
1732 *b_im_ptr = B_IMODE_NONE;
1733 }
1734 else
1735 {
1736 im_set_active(TRUE); /* Enable input method */
1737 if (b_im_ptr != NULL)
1738 *b_im_ptr = B_IMODE_IM;
1739 }
1740 }
1741#endif
1742 if (b_im_ptr != NULL)
1743 {
1744 if (b_im_ptr == &curbuf->b_p_iminsert)
1745 set_iminsert_global();
1746 else
1747 set_imsearch_global();
1748 }
1749#ifdef CURSOR_SHAPE
1750 ui_cursor_shape(); /* may show different cursor shape */
1751#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001752#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 /* Show/unshow value of 'keymap' in status lines later. */
1754 status_redraw_curbuf();
1755#endif
1756 goto cmdline_not_changed;
1757
1758/* case '@': only in very old vi */
1759 case Ctrl_U:
1760 /* delete all characters left of the cursor */
1761 j = ccline.cmdpos;
1762 ccline.cmdlen -= j;
1763 i = ccline.cmdpos = 0;
1764 while (i < ccline.cmdlen)
1765 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1766 /* Truncate at the end, required for multi-byte chars. */
1767 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001768#ifdef FEAT_SEARCH_EXTRA
1769 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001770 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 redrawcmd();
1773 goto cmdline_changed;
1774
1775#ifdef FEAT_CLIPBOARD
1776 case Ctrl_Y:
1777 /* Copy the modeless selection, if there is one. */
1778 if (clip_star.state != SELECT_CLEARED)
1779 {
1780 if (clip_star.state == SELECT_DONE)
1781 clip_copy_modeless_selection(TRUE);
1782 goto cmdline_not_changed;
1783 }
1784 break;
1785#endif
1786
1787 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1788 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001789 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001790 * ":normal" runs out of characters. */
1791 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001792 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 goto cmdline_not_changed;
1794
1795 gotesc = TRUE; /* will free ccline.cmdbuff after
1796 putting it in history */
1797 goto returncmd; /* back to cmd mode */
1798
1799 case Ctrl_R: /* insert register */
1800#ifdef USE_ON_FLY_SCROLL
1801 dont_scroll = TRUE; /* disallow scrolling here */
1802#endif
1803 putcmdline('"', TRUE);
1804 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001805 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (i == Ctrl_O)
1807 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1808 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001809 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001810 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 --no_mapping;
1812#ifdef FEAT_EVAL
1813 /*
1814 * Insert the result of an expression.
1815 * Need to save the current command line, to be able to enter
1816 * a new one...
1817 */
1818 new_cmdpos = -1;
1819 if (c == '=')
1820 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001821 if (ccline.cmdfirstc == '=' // can't do this recursively
1822 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 beep_flush();
1825 c = ESC;
1826 }
1827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
1830#endif
1831 if (c != ESC) /* use ESC to cancel inserting register */
1832 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001833 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001834
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001835#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001836 /* When there was a serious error abort getting the
1837 * command line. */
1838 if (aborting())
1839 {
1840 gotesc = TRUE; /* will free ccline.cmdbuff after
1841 putting it in history */
1842 goto returncmd; /* back to cmd mode */
1843 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 KeyTyped = FALSE; /* Don't do p_wc completion. */
1846#ifdef FEAT_EVAL
1847 if (new_cmdpos >= 0)
1848 {
1849 /* set_cmdline_pos() was used */
1850 if (new_cmdpos > ccline.cmdlen)
1851 ccline.cmdpos = ccline.cmdlen;
1852 else
1853 ccline.cmdpos = new_cmdpos;
1854 }
1855#endif
1856 }
1857 redrawcmd();
1858 goto cmdline_changed;
1859
1860 case Ctrl_D:
1861 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1862 break; /* Use ^D as normal char instead */
1863
1864 redrawcmd();
1865 continue; /* don't do incremental search now */
1866
1867 case K_RIGHT:
1868 case K_S_RIGHT:
1869 case K_C_RIGHT:
1870 do
1871 {
1872 if (ccline.cmdpos >= ccline.cmdlen)
1873 break;
1874 i = cmdline_charsize(ccline.cmdpos);
1875 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1876 break;
1877 ccline.cmdspos += i;
1878#ifdef FEAT_MBYTE
1879 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001880 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 + ccline.cmdpos);
1882 else
1883#endif
1884 ++ccline.cmdpos;
1885 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001886 while ((c == K_S_RIGHT || c == K_C_RIGHT
1887 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1889#ifdef FEAT_MBYTE
1890 if (has_mbyte)
1891 set_cmdspos_cursor();
1892#endif
1893 goto cmdline_not_changed;
1894
1895 case K_LEFT:
1896 case K_S_LEFT:
1897 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001898 if (ccline.cmdpos == 0)
1899 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 do
1901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --ccline.cmdpos;
1903#ifdef FEAT_MBYTE
1904 if (has_mbyte) /* move to first byte of char */
1905 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1906 ccline.cmdbuff + ccline.cmdpos);
1907#endif
1908 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1909 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001910 while (ccline.cmdpos > 0
1911 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001912 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1914#ifdef FEAT_MBYTE
1915 if (has_mbyte)
1916 set_cmdspos_cursor();
1917#endif
1918 goto cmdline_not_changed;
1919
1920 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001921 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001922 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaar4770d092006-01-12 23:22:24 +00001924#ifdef FEAT_GUI_W32
1925 /* On Win32 ignore <M-F4>, we get it when closing the window was
1926 * cancelled. */
1927 case K_F4:
1928 if (mod_mask == MOD_MASK_ALT)
1929 {
1930 redrawcmd(); /* somehow the cmdline is cleared */
1931 goto cmdline_not_changed;
1932 }
1933 break;
1934#endif
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef FEAT_MOUSE
1937 case K_MIDDLEDRAG:
1938 case K_MIDDLERELEASE:
1939 goto cmdline_not_changed; /* Ignore mouse */
1940
1941 case K_MIDDLEMOUSE:
1942# ifdef FEAT_GUI
1943 /* When GUI is active, also paste when 'mouse' is empty */
1944 if (!gui.in_use)
1945# endif
1946 if (!mouse_has(MOUSE_COMMAND))
1947 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001948# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001950 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001952# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001953 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 redrawcmd();
1955 goto cmdline_changed;
1956
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001957# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001959 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 redrawcmd();
1961 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001962# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 case K_LEFTDRAG:
1965 case K_LEFTRELEASE:
1966 case K_RIGHTDRAG:
1967 case K_RIGHTRELEASE:
1968 /* Ignore drag and release events when the button-down wasn't
1969 * seen before. */
1970 if (ignore_drag_release)
1971 goto cmdline_not_changed;
1972 /* FALLTHROUGH */
1973 case K_LEFTMOUSE:
1974 case K_RIGHTMOUSE:
1975 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1976 ignore_drag_release = TRUE;
1977 else
1978 ignore_drag_release = FALSE;
1979# ifdef FEAT_GUI
1980 /* When GUI is active, also move when 'mouse' is empty */
1981 if (!gui.in_use)
1982# endif
1983 if (!mouse_has(MOUSE_COMMAND))
1984 goto cmdline_not_changed; /* Ignore mouse */
1985# ifdef FEAT_CLIPBOARD
1986 if (mouse_row < cmdline_row && clip_star.available)
1987 {
1988 int button, is_click, is_drag;
1989
1990 /*
1991 * Handle modeless selection.
1992 */
1993 button = get_mouse_button(KEY2TERMCAP1(c),
1994 &is_click, &is_drag);
1995 if (mouse_model_popup() && button == MOUSE_LEFT
1996 && (mod_mask & MOD_MASK_SHIFT))
1997 {
1998 /* Translate shift-left to right button. */
1999 button = MOUSE_RIGHT;
2000 mod_mask &= ~MOD_MASK_SHIFT;
2001 }
2002 clip_modeless(button, is_click, is_drag);
2003 goto cmdline_not_changed;
2004 }
2005# endif
2006
2007 set_cmdspos();
2008 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2009 ++ccline.cmdpos)
2010 {
2011 i = cmdline_charsize(ccline.cmdpos);
2012 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2013 && mouse_col < ccline.cmdspos % Columns + i)
2014 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002015# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (has_mbyte)
2017 {
2018 /* Count ">" for double-wide char that doesn't fit. */
2019 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002020 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 + ccline.cmdpos) - 1;
2022 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 ccline.cmdspos += i;
2025 }
2026 goto cmdline_not_changed;
2027
2028 /* Mouse scroll wheel: ignored here */
2029 case K_MOUSEDOWN:
2030 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002031 case K_MOUSELEFT:
2032 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 /* Alternate buttons ignored here */
2034 case K_X1MOUSE:
2035 case K_X1DRAG:
2036 case K_X1RELEASE:
2037 case K_X2MOUSE:
2038 case K_X2DRAG:
2039 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002040 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 goto cmdline_not_changed;
2042
2043#endif /* FEAT_MOUSE */
2044
2045#ifdef FEAT_GUI
2046 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2047 case K_LEFTRELEASE_NM:
2048 goto cmdline_not_changed;
2049
2050 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002051 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 gui_do_scroll();
2054 redrawcmd();
2055 }
2056 goto cmdline_not_changed;
2057
2058 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002059 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002061 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 redrawcmd();
2063 }
2064 goto cmdline_not_changed;
2065#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002066#ifdef FEAT_GUI_TABLINE
2067 case K_TABLINE:
2068 case K_TABMENU:
2069 /* Don't want to change any tabs here. Make sure the same tab
2070 * is still selected. */
2071 if (gui_use_tabline())
2072 gui_mch_set_curtab(tabpage_index(curtab));
2073 goto cmdline_not_changed;
2074#endif
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 case K_SELECT: /* end of Select mode mapping - ignore */
2077 goto cmdline_not_changed;
2078
2079 case Ctrl_B: /* begin of command line */
2080 case K_HOME:
2081 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 case K_S_HOME:
2083 case K_C_HOME:
2084 ccline.cmdpos = 0;
2085 set_cmdspos();
2086 goto cmdline_not_changed;
2087
2088 case Ctrl_E: /* end of command line */
2089 case K_END:
2090 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 case K_S_END:
2092 case K_C_END:
2093 ccline.cmdpos = ccline.cmdlen;
2094 set_cmdspos_cursor();
2095 goto cmdline_not_changed;
2096
2097 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002098 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 break;
2100 goto cmdline_changed;
2101
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002102 case Ctrl_L:
2103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002104 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002105 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002106#endif
2107
2108 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002109 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 break;
2111 goto cmdline_changed;
2112
2113 case Ctrl_N: /* next match */
2114 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002115 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002117 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2118 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002120 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002123 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 case K_UP:
2125 case K_DOWN:
2126 case K_S_UP:
2127 case K_S_DOWN:
2128 case K_PAGEUP:
2129 case K_KPAGEUP:
2130 case K_PAGEDOWN:
2131 case K_KPAGEDOWN:
2132 if (hislen == 0 || firstc == NUL) /* no history */
2133 goto cmdline_not_changed;
2134
2135 i = hiscnt;
2136
2137 /* save current command string so it can be restored later */
2138 if (lookfor == NULL)
2139 {
2140 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2141 goto cmdline_not_changed;
2142 lookfor[ccline.cmdpos] = NUL;
2143 }
2144
2145 j = (int)STRLEN(lookfor);
2146 for (;;)
2147 {
2148 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002149 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002150 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 if (hiscnt == hislen) /* first time */
2153 hiscnt = hisidx[histype];
2154 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2155 hiscnt = hislen - 1;
2156 else if (hiscnt != hisidx[histype] + 1)
2157 --hiscnt;
2158 else /* at top of list */
2159 {
2160 hiscnt = i;
2161 break;
2162 }
2163 }
2164 else /* one step forwards */
2165 {
2166 /* on last entry, clear the line */
2167 if (hiscnt == hisidx[histype])
2168 {
2169 hiscnt = hislen;
2170 break;
2171 }
2172
2173 /* not on a history line, nothing to do */
2174 if (hiscnt == hislen)
2175 break;
2176 if (hiscnt == hislen - 1) /* wrap around */
2177 hiscnt = 0;
2178 else
2179 ++hiscnt;
2180 }
2181 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2182 {
2183 hiscnt = i;
2184 break;
2185 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002186 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002187 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 || STRNCMP(history[histype][hiscnt].hisstr,
2189 lookfor, (size_t)j) == 0)
2190 break;
2191 }
2192
2193 if (hiscnt != i) /* jumped to other entry */
2194 {
2195 char_u *p;
2196 int len;
2197 int old_firstc;
2198
Bram Moolenaar438d1762018-09-30 17:11:48 +02002199 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002200 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (hiscnt == hislen)
2202 p = lookfor; /* back to the old one */
2203 else
2204 p = history[histype][hiscnt].hisstr;
2205
2206 if (histype == HIST_SEARCH
2207 && p != lookfor
2208 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2209 {
2210 /* Correct for the separator character used when
2211 * adding the history entry vs the one used now.
2212 * First loop: count length.
2213 * Second loop: copy the characters. */
2214 for (i = 0; i <= 1; ++i)
2215 {
2216 len = 0;
2217 for (j = 0; p[j] != NUL; ++j)
2218 {
2219 /* Replace old sep with new sep, unless it is
2220 * escaped. */
2221 if (p[j] == old_firstc
2222 && (j == 0 || p[j - 1] != '\\'))
2223 {
2224 if (i > 0)
2225 ccline.cmdbuff[len] = firstc;
2226 }
2227 else
2228 {
2229 /* Escape new sep, unless it is already
2230 * escaped. */
2231 if (p[j] == firstc
2232 && (j == 0 || p[j - 1] != '\\'))
2233 {
2234 if (i > 0)
2235 ccline.cmdbuff[len] = '\\';
2236 ++len;
2237 }
2238 if (i > 0)
2239 ccline.cmdbuff[len] = p[j];
2240 }
2241 ++len;
2242 }
2243 if (i == 0)
2244 {
2245 alloc_cmdbuff(len);
2246 if (ccline.cmdbuff == NULL)
2247 goto returncmd;
2248 }
2249 }
2250 ccline.cmdbuff[len] = NUL;
2251 }
2252 else
2253 {
2254 alloc_cmdbuff((int)STRLEN(p));
2255 if (ccline.cmdbuff == NULL)
2256 goto returncmd;
2257 STRCPY(ccline.cmdbuff, p);
2258 }
2259
2260 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2261 redrawcmd();
2262 goto cmdline_changed;
2263 }
2264 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002265#endif
2266 goto cmdline_not_changed;
2267
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002268#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002269 case Ctrl_G: /* next match */
2270 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002271 if (may_adjust_incsearch_highlighting(
2272 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002273 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#endif
2276
2277 case Ctrl_V:
2278 case Ctrl_Q:
2279#ifdef FEAT_MOUSE
2280 ignore_drag_release = TRUE;
2281#endif
2282 putcmdline('^', TRUE);
2283 c = get_literal(); /* get next (two) character(s) */
2284 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002285 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286#ifdef FEAT_MBYTE
2287 /* may need to remove ^ when composing char was typed */
2288 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2289 {
2290 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2291 msg_putchar(' ');
2292 cursorcmd();
2293 }
2294#endif
2295 break;
2296
2297#ifdef FEAT_DIGRAPHS
2298 case Ctrl_K:
2299#ifdef FEAT_MOUSE
2300 ignore_drag_release = TRUE;
2301#endif
2302 putcmdline('?', TRUE);
2303#ifdef USE_ON_FLY_SCROLL
2304 dont_scroll = TRUE; /* disallow scrolling here */
2305#endif
2306 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002307 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (c != NUL)
2309 break;
2310
2311 redrawcmd();
2312 goto cmdline_not_changed;
2313#endif /* FEAT_DIGRAPHS */
2314
2315#ifdef FEAT_RIGHTLEFT
2316 case Ctrl__: /* CTRL-_: switch language mode */
2317 if (!p_ari)
2318 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002319# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (p_altkeymap)
2321 {
2322 cmd_fkmap = !cmd_fkmap;
2323 if (cmd_fkmap) /* in Farsi always in Insert mode */
2324 ccline.overstrike = FALSE;
2325 }
2326 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 cmd_hkmap = !cmd_hkmap;
2329 goto cmdline_not_changed;
2330#endif
2331
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002332 case K_PS:
2333 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2334 goto cmdline_changed;
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 default:
2337#ifdef UNIX
2338 if (c == intr_char)
2339 {
2340 gotesc = TRUE; /* will free ccline.cmdbuff after
2341 putting it in history */
2342 goto returncmd; /* back to Normal mode */
2343 }
2344#endif
2345 /*
2346 * Normal character with no special meaning. Just set mod_mask
2347 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2348 * the string <S-Space>. This should only happen after ^V.
2349 */
2350 if (!IS_SPECIAL(c))
2351 mod_mask = 0x0;
2352 break;
2353 }
2354 /*
2355 * End of switch on command line character.
2356 * We come here if we have a normal character.
2357 */
2358
Bram Moolenaarede3e632013-06-23 16:16:19 +02002359 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360#ifdef FEAT_MBYTE
2361 /* Add ABBR_OFF for characters above 0x100, this is
2362 * what check_abbr() expects. */
2363 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2364#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002365 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 goto cmdline_changed;
2367
2368 /*
2369 * put the character in the command line
2370 */
2371 if (IS_SPECIAL(c) || mod_mask != 0)
2372 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2373 else
2374 {
2375#ifdef FEAT_MBYTE
2376 if (has_mbyte)
2377 {
2378 j = (*mb_char2bytes)(c, IObuff);
2379 IObuff[j] = NUL; /* exclude composing chars */
2380 put_on_cmdline(IObuff, j, TRUE);
2381 }
2382 else
2383#endif
2384 {
2385 IObuff[0] = c;
2386 put_on_cmdline(IObuff, 1, TRUE);
2387 }
2388 }
2389 goto cmdline_changed;
2390
2391/*
2392 * This part implements incremental searches for "/" and "?"
2393 * Jump to cmdline_not_changed when a character has been read but the command
2394 * line did not change. Then we only search and redraw if something changed in
2395 * the past.
2396 * Jump to cmdline_changed when the command line did change.
2397 * (Sorry for the goto's, I know it is ugly).
2398 */
2399cmdline_not_changed:
2400#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002401 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 continue;
2403#endif
2404
2405cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002406 /* Trigger CmdlineChanged autocommands. */
2407 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002410 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411#endif
2412
2413#ifdef FEAT_RIGHTLEFT
2414 if (cmdmsg_rl
2415# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002416 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417# endif
2418 )
2419 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002420 * right-left typing. Not efficient, but it works.
2421 * Do it only when there are no characters left to read
2422 * to avoid useless intermediate redraws. */
2423 if (vpeekc() == NUL)
2424 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#endif
2426 }
2427
2428returncmd:
2429
2430#ifdef FEAT_RIGHTLEFT
2431 cmdmsg_rl = FALSE;
2432#endif
2433
2434#ifdef FEAT_FKMAP
2435 cmd_fkmap = 0;
2436#endif
2437
2438 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002439 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002442 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#endif
2444
2445 if (ccline.cmdbuff != NULL)
2446 {
2447 /*
2448 * Put line in history buffer (":" and "=" only when it was typed).
2449 */
2450#ifdef FEAT_CMDHIST
2451 if (ccline.cmdlen && firstc != NUL
2452 && (some_key_typed || histype == HIST_SEARCH))
2453 {
2454 add_to_history(histype, ccline.cmdbuff, TRUE,
2455 histype == HIST_SEARCH ? firstc : NUL);
2456 if (firstc == ':')
2457 {
2458 vim_free(new_last_cmdline);
2459 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2460 }
2461 }
2462#endif
2463
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002464 if (gotesc)
2465 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
2467
2468 /*
2469 * If the screen was shifted up, redraw the whole screen (later).
2470 * If the line is too long, clear it, so ruler and shown command do
2471 * not get printed in the middle of it.
2472 */
2473 msg_check();
2474 msg_scroll = save_msg_scroll;
2475 redir_off = FALSE;
2476
2477 /* When the command line was typed, no need for a wait-return prompt. */
2478 if (some_key_typed)
2479 need_wait_return = FALSE;
2480
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002481 /* Trigger CmdlineLeave autocommands. */
2482 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002483
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002485#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2487 im_save_status(b_im_ptr);
2488 im_set_active(FALSE);
2489#endif
2490#ifdef FEAT_MOUSE
2491 setmouse();
2492#endif
2493#ifdef CURSOR_SHAPE
2494 ui_cursor_shape(); /* may show different cursor shape */
2495#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002496 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar438d1762018-09-30 17:11:48 +02002498theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002499 {
2500 char_u *p = ccline.cmdbuff;
2501
Bram Moolenaar438d1762018-09-30 17:11:48 +02002502 if (did_save_ccline)
2503 restore_cmdline(&save_ccline);
2504 else
2505 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002506 return p;
2507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508}
2509
2510#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2511/*
2512 * Get a command line with a prompt.
2513 * This is prepared to be called recursively from getcmdline() (e.g. by
2514 * f_input() when evaluating an expression from CTRL-R =).
2515 * Returns the command line in allocated memory, or NULL.
2516 */
2517 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002518getcmdline_prompt(
2519 int firstc,
2520 char_u *prompt, /* command line prompt */
2521 int attr, /* attributes for prompt */
2522 int xp_context, /* type of expansion */
2523 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 char_u *s;
2526 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002527 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002529 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar438d1762018-09-30 17:11:48 +02002531 if (ccline.cmdbuff != NULL)
2532 {
2533 // Save the values of the current cmdline and restore them below.
2534 save_cmdline(&save_ccline);
2535 did_save_ccline = TRUE;
2536 }
2537
2538 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 ccline.cmdprompt = prompt;
2540 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002541# ifdef FEAT_EVAL
2542 ccline.xp_context = xp_context;
2543 ccline.xp_arg = xp_arg;
2544 ccline.input_fn = (firstc == '@');
2545# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002546 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002547 s = getcmdline_int(firstc, 1L, 0, FALSE);
2548
2549 if (did_save_ccline)
2550 restore_cmdline(&save_ccline);
2551
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002552 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002553 /* Restore msg_col, the prompt from input() may have changed it.
2554 * But only if called recursively and the commandline is therefore being
2555 * restored to an old one; if not, the input() prompt stays on the screen,
2556 * so we need its modified msg_col left intact. */
2557 if (ccline.cmdbuff != NULL)
2558 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
2560 return s;
2561}
2562#endif
2563
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002564/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002565 * Return TRUE when the text must not be changed and we can't switch to
2566 * another window or buffer. Used when editing the command line, evaluating
2567 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002568 */
2569 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002570text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002571{
2572#ifdef FEAT_CMDWIN
2573 if (cmdwin_type != 0)
2574 return TRUE;
2575#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002576 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002577}
2578
2579/*
2580 * Give an error message for a command that isn't allowed while the cmdline
2581 * window is open or editing the cmdline in another way.
2582 */
2583 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002584text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002585{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002586 EMSG(_(get_text_locked_msg()));
2587}
2588
2589 char_u *
2590get_text_locked_msg(void)
2591{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002592#ifdef FEAT_CMDWIN
2593 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002594 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002595#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002596 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002597}
2598
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002599/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002600 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2601 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002602 */
2603 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002604curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002605{
2606 if (curbuf_lock > 0)
2607 {
2608 EMSG(_("E788: Not allowed to edit another buffer now"));
2609 return TRUE;
2610 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002611 return allbuf_locked();
2612}
2613
2614/*
2615 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2616 * message.
2617 */
2618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002619allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002620{
2621 if (allbuf_lock > 0)
2622 {
2623 EMSG(_("E811: Not allowed to change buffer information now"));
2624 return TRUE;
2625 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002626 return FALSE;
2627}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002630cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2633 if (cmdline_star > 0) /* showing '*', always 1 position */
2634 return 1;
2635#endif
2636 return ptr2cells(ccline.cmdbuff + idx);
2637}
2638
2639/*
2640 * Compute the offset of the cursor on the command line for the prompt and
2641 * indent.
2642 */
2643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002644set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002646 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 ccline.cmdspos = 1 + ccline.cmdindent;
2648 else
2649 ccline.cmdspos = 0 + ccline.cmdindent;
2650}
2651
2652/*
2653 * Compute the screen position for the cursor on the command line.
2654 */
2655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002656set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
2658 int i, m, c;
2659
2660 set_cmdspos();
2661 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002664 if (m < 0) /* overflow, Columns or Rows at weird value */
2665 m = MAXCOL;
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 else
2668 m = MAXCOL;
2669 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2670 {
2671 c = cmdline_charsize(i);
2672#ifdef FEAT_MBYTE
2673 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2674 if (has_mbyte)
2675 correct_cmdspos(i, c);
2676#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002677 /* If the cmdline doesn't fit, show cursor on last visible char.
2678 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if ((ccline.cmdspos += c) >= m)
2680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 ccline.cmdspos -= c;
2682 break;
2683 }
2684#ifdef FEAT_MBYTE
2685 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002686 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#endif
2688 }
2689}
2690
2691#ifdef FEAT_MBYTE
2692/*
2693 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2694 * character that doesn't fit, so that a ">" must be displayed.
2695 */
2696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002697correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002699 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2701 && ccline.cmdspos % Columns + cells > Columns)
2702 ccline.cmdspos++;
2703}
2704#endif
2705
2706/*
2707 * Get an Ex command line for the ":" command.
2708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002710getexline(
2711 int c, /* normally ':', NUL for ":append" */
2712 void *cookie UNUSED,
2713 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
2715 /* When executing a register, remove ':' that's in front of each line. */
2716 if (exec_from_reg && vpeekc() == ':')
2717 (void)vgetc();
2718 return getcmdline(c, 1L, indent);
2719}
2720
2721/*
2722 * Get an Ex command line for Ex mode.
2723 * In Ex mode we only use the OS supplied line editing features and no
2724 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002725 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002728getexmodeline(
2729 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002731 void *cookie UNUSED,
2732 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002734 garray_T line_ga;
2735 char_u *pend;
2736 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002737 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002738 int escaped = FALSE; /* CTRL-V typed */
2739 int vcol = 0;
2740 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002741 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002742 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /* Switch cursor on now. This avoids that it happens after the "\n", which
2745 * confuses the system function that computes tabstops. */
2746 cursor_on();
2747
2748 /* always start in column 0; write a newline if necessary */
2749 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002754 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002755 if (p_prompt)
2756 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 while (indent-- > 0)
2758 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761
2762 ga_init2(&line_ga, 1, 30);
2763
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002764 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002765 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002766 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002767 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002768 while (indent >= 8)
2769 {
2770 ga_append(&line_ga, TAB);
2771 msg_puts((char_u *)" ");
2772 indent -= 8;
2773 }
2774 while (indent-- > 0)
2775 {
2776 ga_append(&line_ga, ' ');
2777 msg_putchar(' ');
2778 }
2779 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 ++no_mapping;
2781 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 /*
2784 * Get the line, one character at a time.
2785 */
2786 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002789 long sw;
2790 char_u *s;
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (ga_grow(&line_ga, 40) == FAIL)
2793 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaarba748c82017-02-26 14:00:07 +01002795 /*
2796 * Get one character at a time.
2797 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002798 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002799
2800 /* Check for a ":normal" command and no more characters left. */
2801 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2802 c1 = '\n';
2803 else
2804 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002807 * Handle line editing.
2808 * Previously this was left to the system, putting the terminal in
2809 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813 msg_putchar('\n');
2814 break;
2815 }
2816
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002817 if (c1 == K_PS)
2818 {
2819 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2820 goto redraw;
2821 }
2822
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823 if (!escaped)
2824 {
2825 /* CR typed means "enter", which is NL */
2826 if (c1 == '\r')
2827 c1 = '\n';
2828
2829 if (c1 == BS || c1 == K_BS
2830 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832 if (line_ga.ga_len > 0)
2833 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002834#ifdef FEAT_MBYTE
2835 if (has_mbyte)
2836 {
2837 p = (char_u *)line_ga.ga_data;
2838 p[line_ga.ga_len] = NUL;
2839 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2840 line_ga.ga_len -= len;
2841 }
2842 else
2843#endif
2844 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 goto redraw;
2846 }
2847 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 msg_col = startcol;
2853 msg_clr_eos();
2854 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002855 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002856 }
2857
2858 if (c1 == Ctrl_T)
2859 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002860 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861 p = (char_u *)line_ga.ga_data;
2862 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002863 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002864 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002865add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002866 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002868 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002869 p = (char_u *)line_ga.ga_data;
2870 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2872 *s = ' ';
2873 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875redraw:
2876 /* redraw the line */
2877 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002878 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002879 p = (char_u *)line_ga.ga_data;
2880 p[line_ga.ga_len] = NUL;
2881 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002885 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002887 msg_putchar(' ');
2888 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002889 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002893 len = MB_PTR2LEN(p);
2894 msg_outtrans_len(p, len);
2895 vcol += ptr2cells(p);
2896 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002899 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002900 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002901 continue;
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002904 if (c1 == Ctrl_D)
2905 {
2906 /* Delete one shiftwidth. */
2907 p = (char_u *)line_ga.ga_data;
2908 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910 if (prev_char == '^')
2911 ex_keep_indent = TRUE;
2912 indent = 0;
2913 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 else
2916 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002917 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002918 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002919 if (indent > 0)
2920 {
2921 --indent;
2922 indent -= indent % get_sw_value(curbuf);
2923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002925 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002926 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002927 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002928 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2929 --line_ga.ga_len;
2930 }
2931 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002933
2934 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2935 {
2936 escaped = TRUE;
2937 continue;
2938 }
2939
2940 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2941 if (IS_SPECIAL(c1))
2942 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944
2945 if (IS_SPECIAL(c1))
2946 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002947#ifdef FEAT_MBYTE
2948 if (has_mbyte)
2949 len = (*mb_char2bytes)(c1,
2950 (char_u *)line_ga.ga_data + line_ga.ga_len);
2951 else
2952#endif
2953 {
2954 len = 1;
2955 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2956 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002957 if (c1 == '\n')
2958 msg_putchar('\n');
2959 else if (c1 == TAB)
2960 {
2961 /* Don't use chartabsize(), 'ts' can be different */
2962 do
2963 {
2964 msg_putchar(' ');
2965 } while (++vcol % 8);
2966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002969 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002970 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002971 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002973 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002974 escaped = FALSE;
2975
2976 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002977 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002978
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002979 /* We are done when a NL is entered, but not when it comes after an
2980 * odd number of backslashes, that results in a NUL. */
2981 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002983 int bcount = 0;
2984
2985 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2986 ++bcount;
2987
2988 if (bcount > 0)
2989 {
2990 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2991 * "\NL", etc. */
2992 line_ga.ga_len -= (bcount + 1) / 2;
2993 pend -= (bcount + 1) / 2;
2994 pend[-1] = '\n';
2995 }
2996
2997 if ((bcount & 1) == 0)
2998 {
2999 --line_ga.ga_len;
3000 --pend;
3001 *pend = NUL;
3002 break;
3003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005 }
3006
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003007 --no_mapping;
3008 --allow_keys;
3009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 /* make following messages go to the next line */
3011 msg_didout = FALSE;
3012 msg_col = 0;
3013 if (msg_row < Rows - 1)
3014 ++msg_row;
3015 emsg_on_display = FALSE; /* don't want ui_delay() */
3016
3017 if (got_int)
3018 ga_clear(&line_ga);
3019
3020 return (char_u *)line_ga.ga_data;
3021}
3022
Bram Moolenaare344bea2005-09-01 20:46:49 +00003023# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3024 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025/*
3026 * Return TRUE if ccline.overstrike is on.
3027 */
3028 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003029cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 return ccline.overstrike;
3032}
3033
3034/*
3035 * Return TRUE if the cursor is at the end of the cmdline.
3036 */
3037 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003038cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 return (ccline.cmdpos >= ccline.cmdlen);
3041}
3042#endif
3043
Bram Moolenaar9372a112005-12-06 19:59:18 +00003044#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045/*
3046 * Return the virtual column number at the current cursor position.
3047 * This is used by the IM code to obtain the start of the preedit string.
3048 */
3049 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3053 return MAXCOL;
3054
3055# ifdef FEAT_MBYTE
3056 if (has_mbyte)
3057 {
3058 colnr_T col;
3059 int i = 0;
3060
3061 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003062 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
3064 return col;
3065 }
3066 else
3067# endif
3068 return ccline.cmdpos;
3069}
3070#endif
3071
3072#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3073/*
3074 * If part of the command line is an IM preedit string, redraw it with
3075 * IM feedback attributes. The cursor position is restored after drawing.
3076 */
3077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003078redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 if ((State & CMDLINE)
3081 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003082 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 && !p_imdisable
3084 && im_is_preediting())
3085 {
3086 int cmdpos = 0;
3087 int cmdspos;
3088 int old_row;
3089 int old_col;
3090 colnr_T col;
3091
3092 old_row = msg_row;
3093 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003094 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096# ifdef FEAT_MBYTE
3097 if (has_mbyte)
3098 {
3099 for (col = 0; col < preedit_start_col
3100 && cmdpos < ccline.cmdlen; ++col)
3101 {
3102 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003103 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105 }
3106 else
3107# endif
3108 {
3109 cmdspos += preedit_start_col;
3110 cmdpos += preedit_start_col;
3111 }
3112
3113 msg_row = cmdline_row + (cmdspos / (int)Columns);
3114 msg_col = cmdspos % (int)Columns;
3115 if (msg_row >= Rows)
3116 msg_row = Rows - 1;
3117
3118 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3119 {
3120 int char_len;
3121 int char_attr;
3122
3123 char_attr = im_get_feedback_attr(col);
3124 if (char_attr < 0)
3125 break; /* end of preedit string */
3126
3127# ifdef FEAT_MBYTE
3128 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003129 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 else
3131# endif
3132 char_len = 1;
3133
3134 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3135 cmdpos += char_len;
3136 }
3137
3138 msg_row = old_row;
3139 msg_col = old_col;
3140 }
3141}
3142#endif /* FEAT_XIM && FEAT_GUI_GTK */
3143
3144/*
3145 * Allocate a new command line buffer.
3146 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 */
3148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003149alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 /*
3152 * give some extra space to avoid having to allocate all the time
3153 */
3154 if (len < 80)
3155 len = 100;
3156 else
3157 len += 20;
3158
3159 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3160 ccline.cmdbufflen = len;
3161}
3162
3163/*
3164 * Re-allocate the command line to length len + something extra.
3165 * return FAIL for failure, OK otherwise
3166 */
3167 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003168realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 char_u *p;
3171
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003172 if (len < ccline.cmdbufflen)
3173 return OK; /* no need to resize */
3174
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 p = ccline.cmdbuff;
3176 alloc_cmdbuff(len); /* will get some more */
3177 if (ccline.cmdbuff == NULL) /* out of memory */
3178 {
3179 ccline.cmdbuff = p; /* keep the old one */
3180 return FAIL;
3181 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003182 /* There isn't always a NUL after the command, but it may need to be
3183 * there, thus copy up to the NUL and add a NUL. */
3184 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3185 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003187
3188 if (ccline.xpc != NULL
3189 && ccline.xpc->xp_pattern != NULL
3190 && ccline.xpc->xp_context != EXPAND_NOTHING
3191 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3192 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003193 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003194
3195 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3196 * to point into the newly allocated memory. */
3197 if (i >= 0 && i <= ccline.cmdlen)
3198 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3199 }
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 return OK;
3202}
3203
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204#if defined(FEAT_ARABIC) || defined(PROTO)
3205static char_u *arshape_buf = NULL;
3206
3207# if defined(EXITFREE) || defined(PROTO)
3208 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003209free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003210{
3211 vim_free(arshape_buf);
3212}
3213# endif
3214#endif
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216/*
3217 * Draw part of the cmdline at the current cursor position. But draw stars
3218 * when cmdline_star is TRUE.
3219 */
3220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003221draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3224 int i;
3225
3226 if (cmdline_star > 0)
3227 for (i = 0; i < len; ++i)
3228 {
3229 msg_putchar('*');
3230# ifdef FEAT_MBYTE
3231 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003232 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233# endif
3234 }
3235 else
3236#endif
3237#ifdef FEAT_ARABIC
3238 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 static int buflen = 0;
3241 char_u *p;
3242 int j;
3243 int newlen = 0;
3244 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003245 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 int prev_c = 0;
3247 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003248 int u8c;
3249 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 /*
3253 * Do arabic shaping into a temporary buffer. This is very
3254 * inefficient!
3255 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003256 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
3258 /* Re-allocate the buffer. We keep it around to avoid a lot of
3259 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003261 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 arshape_buf = alloc(buflen);
3263 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 return; /* out of memory */
3265 }
3266
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003267 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3268 {
3269 /* Prepend a space to draw the leading composing char on. */
3270 arshape_buf[0] = ' ';
3271 newlen = 1;
3272 }
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 for (j = start; j < start + len; j += mb_l)
3275 {
3276 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003277 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003278 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (ARABIC_CHAR(u8c))
3280 {
3281 /* Do Arabic shaping. */
3282 if (cmdmsg_rl)
3283 {
3284 /* displaying from right to left */
3285 pc = prev_c;
3286 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003287 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (j + mb_l >= start + len)
3289 nc = NUL;
3290 else
3291 nc = utf_ptr2char(p + mb_l);
3292 }
3293 else
3294 {
3295 /* displaying from left to right */
3296 if (j + mb_l >= start + len)
3297 pc = NUL;
3298 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003299 {
3300 int pcc[MAX_MCO];
3301
3302 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003304 pc1 = pcc[0];
3305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 nc = prev_c;
3307 }
3308 prev_c = u8c;
3309
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003310 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003312 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003313 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003315 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3316 if (u8cc[1] != 0)
3317 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003318 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320 }
3321 else
3322 {
3323 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 newlen += mb_l;
3326 }
3327 }
3328
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003329 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331 else
3332#endif
3333 msg_outtrans_len(ccline.cmdbuff + start, len);
3334}
3335
3336/*
3337 * Put a character on the command line. Shifts the following text to the
3338 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3339 * "c" must be printable (fit in one display cell)!
3340 */
3341 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003342putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 if (cmd_silent)
3345 return;
3346 msg_no_more = TRUE;
3347 msg_putchar(c);
3348 if (shift)
3349 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3350 msg_no_more = FALSE;
3351 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003352 extra_char = c;
3353 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354}
3355
3356/*
3357 * Undo a putcmdline(c, FALSE).
3358 */
3359 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003360unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 if (cmd_silent)
3363 return;
3364 msg_no_more = TRUE;
3365 if (ccline.cmdlen == ccline.cmdpos)
3366 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003367#ifdef FEAT_MBYTE
3368 else if (has_mbyte)
3369 draw_cmdline(ccline.cmdpos,
3370 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
3373 draw_cmdline(ccline.cmdpos, 1);
3374 msg_no_more = FALSE;
3375 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003376 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377}
3378
3379/*
3380 * Put the given string, of the given length, onto the command line.
3381 * If len is -1, then STRLEN() is used to calculate the length.
3382 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3383 * part will be redrawn, otherwise it will not. If this function is called
3384 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3385 * called afterwards.
3386 */
3387 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003388put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
3390 int retval;
3391 int i;
3392 int m;
3393 int c;
3394
3395 if (len < 0)
3396 len = (int)STRLEN(str);
3397
3398 /* Check if ccline.cmdbuff needs to be longer */
3399 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003400 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 else
3402 retval = OK;
3403 if (retval == OK)
3404 {
3405 if (!ccline.overstrike)
3406 {
3407 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3408 ccline.cmdbuff + ccline.cmdpos,
3409 (size_t)(ccline.cmdlen - ccline.cmdpos));
3410 ccline.cmdlen += len;
3411 }
3412 else
3413 {
3414#ifdef FEAT_MBYTE
3415 if (has_mbyte)
3416 {
3417 /* Count nr of characters in the new string. */
3418 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003419 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 ++m;
3421 /* Count nr of bytes in cmdline that are overwritten by these
3422 * characters. */
3423 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003424 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 --m;
3426 if (i < ccline.cmdlen)
3427 {
3428 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3429 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3430 ccline.cmdlen += ccline.cmdpos + len - i;
3431 }
3432 else
3433 ccline.cmdlen = ccline.cmdpos + len;
3434 }
3435 else
3436#endif
3437 if (ccline.cmdpos + len > ccline.cmdlen)
3438 ccline.cmdlen = ccline.cmdpos + len;
3439 }
3440 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3441 ccline.cmdbuff[ccline.cmdlen] = NUL;
3442
3443#ifdef FEAT_MBYTE
3444 if (enc_utf8)
3445 {
3446 /* When the inserted text starts with a composing character,
3447 * backup to the character before it. There could be two of them.
3448 */
3449 i = 0;
3450 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3451 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3452 {
3453 i = (*mb_head_off)(ccline.cmdbuff,
3454 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3455 ccline.cmdpos -= i;
3456 len += i;
3457 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3458 }
3459# ifdef FEAT_ARABIC
3460 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3461 {
3462 /* Check the previous character for Arabic combining pair. */
3463 i = (*mb_head_off)(ccline.cmdbuff,
3464 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3465 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3466 + ccline.cmdpos - i), c))
3467 {
3468 ccline.cmdpos -= i;
3469 len += i;
3470 }
3471 else
3472 i = 0;
3473 }
3474# endif
3475 if (i != 0)
3476 {
3477 /* Also backup the cursor position. */
3478 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3479 ccline.cmdspos -= i;
3480 msg_col -= i;
3481 if (msg_col < 0)
3482 {
3483 msg_col += Columns;
3484 --msg_row;
3485 }
3486 }
3487 }
3488#endif
3489
3490 if (redraw && !cmd_silent)
3491 {
3492 msg_no_more = TRUE;
3493 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003494 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3496 /* Avoid clearing the rest of the line too often. */
3497 if (cmdline_row != i || ccline.overstrike)
3498 msg_clr_eos();
3499 msg_no_more = FALSE;
3500 }
3501#ifdef FEAT_FKMAP
3502 /*
3503 * If we are in Farsi command mode, the character input must be in
3504 * Insert mode. So do not advance the cmdpos.
3505 */
3506 if (!cmd_fkmap)
3507#endif
3508 {
3509 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003512 if (m < 0) /* overflow, Columns or Rows at weird value */
3513 m = MAXCOL;
3514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 else
3516 m = MAXCOL;
3517 for (i = 0; i < len; ++i)
3518 {
3519 c = cmdline_charsize(ccline.cmdpos);
3520#ifdef FEAT_MBYTE
3521 /* count ">" for a double-wide char that doesn't fit. */
3522 if (has_mbyte)
3523 correct_cmdspos(ccline.cmdpos, c);
3524#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003525 /* Stop cursor at the end of the screen, but do increment the
3526 * insert position, so that entering a very long command
3527 * works, even though you can't see it. */
3528 if (ccline.cmdspos + c < m)
3529 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530#ifdef FEAT_MBYTE
3531 if (has_mbyte)
3532 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003533 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (c > len - i - 1)
3535 c = len - i - 1;
3536 ccline.cmdpos += c;
3537 i += c;
3538 }
3539#endif
3540 ++ccline.cmdpos;
3541 }
3542 }
3543 }
3544 if (redraw)
3545 msg_check();
3546 return retval;
3547}
3548
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003549static struct cmdline_info prev_ccline;
3550static int prev_ccline_used = FALSE;
3551
3552/*
3553 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3554 * and overwrite it. But get_cmdline_str() may need it, thus make it
3555 * available globally in prev_ccline.
3556 */
3557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003558save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003559{
3560 if (!prev_ccline_used)
3561 {
3562 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3563 prev_ccline_used = TRUE;
3564 }
3565 *ccp = prev_ccline;
3566 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003567 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003568}
3569
3570/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003571 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003572 */
3573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003574restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003575{
3576 ccline = prev_ccline;
3577 prev_ccline = *ccp;
3578}
3579
Bram Moolenaar8299df92004-07-10 09:47:34 +00003580/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003581 * Paste a yank register into the command line.
3582 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003583 * insert_reg() can't be used here, because special characters from the
3584 * register contents will be interpreted as commands.
3585 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003586 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003587 */
3588 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003589cmdline_paste(
3590 int regname,
3591 int literally, /* Insert text literally instead of "as typed" */
3592 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003593{
3594 long i;
3595 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003596 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003598
3599 /* check for valid regname; also accept special characters for CTRL-R in
3600 * the command line */
3601 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003602 && regname != Ctrl_A && regname != Ctrl_L
3603 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003604 return FAIL;
3605
3606 /* A register containing CTRL-R can cause an endless loop. Allow using
3607 * CTRL-C to break the loop. */
3608 line_breakcheck();
3609 if (got_int)
3610 return FAIL;
3611
3612#ifdef FEAT_CLIPBOARD
3613 regname = may_get_selection(regname);
3614#endif
3615
Bram Moolenaar438d1762018-09-30 17:11:48 +02003616 // Need to set "textlock" to avoid nasty things like going to another
3617 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003618 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003619 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003620 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003621
3622 if (i)
3623 {
3624 /* Got the value of a special register in "arg". */
3625 if (arg == NULL)
3626 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003627
3628 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3629 * part of the word. */
3630 p = arg;
3631 if (p_is && regname == Ctrl_W)
3632 {
3633 char_u *w;
3634 int len;
3635
3636 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003637 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003638 {
3639#ifdef FEAT_MBYTE
3640 if (has_mbyte)
3641 {
3642 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3643 if (!vim_iswordc(mb_ptr2char(w - len)))
3644 break;
3645 w -= len;
3646 }
3647 else
3648#endif
3649 {
3650 if (!vim_iswordc(w[-1]))
3651 break;
3652 --w;
3653 }
3654 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003655 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003656 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3657 p += len;
3658 }
3659
3660 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003661 if (allocated)
3662 vim_free(arg);
3663 return OK;
3664 }
3665
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003666 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003667}
3668
3669/*
3670 * Put a string on the command line.
3671 * When "literally" is TRUE, insert literally.
3672 * When "literally" is FALSE, insert as typed, but don't leave the command
3673 * line.
3674 */
3675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003676cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003677{
3678 int c, cv;
3679
3680 if (literally)
3681 put_on_cmdline(s, -1, TRUE);
3682 else
3683 while (*s != NUL)
3684 {
3685 cv = *s;
3686 if (cv == Ctrl_V && s[1])
3687 ++s;
3688#ifdef FEAT_MBYTE
3689 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003690 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003691 else
3692#endif
3693 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003694 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3695 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003696#ifdef UNIX
3697 || c == intr_char
3698#endif
3699 || (c == Ctrl_BSL && *s == Ctrl_N))
3700 stuffcharReadbuff(Ctrl_V);
3701 stuffcharReadbuff(c);
3702 }
3703}
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#ifdef FEAT_WILDMENU
3706/*
3707 * Delete characters on the command line, from "from" to the current
3708 * position.
3709 */
3710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003711cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
3713 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3714 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3715 ccline.cmdlen -= ccline.cmdpos - from;
3716 ccline.cmdpos = from;
3717}
3718#endif
3719
3720/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003721 * This function is called when the screen size changes and with incremental
3722 * search and in other situations where the command line may have been
3723 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
3725 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003726redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003728 redrawcmdline_ex(TRUE);
3729}
3730
3731 void
3732redrawcmdline_ex(int do_compute_cmdrow)
3733{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (cmd_silent)
3735 return;
3736 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003737 if (do_compute_cmdrow)
3738 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 redrawcmd();
3740 cursorcmd();
3741}
3742
3743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003744redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
3746 int i;
3747
3748 if (cmd_silent)
3749 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003750 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 msg_putchar(ccline.cmdfirstc);
3752 if (ccline.cmdprompt != NULL)
3753 {
3754 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3755 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3756 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003757 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 --ccline.cmdindent;
3759 }
3760 else
3761 for (i = ccline.cmdindent; i > 0; --i)
3762 msg_putchar(' ');
3763}
3764
3765/*
3766 * Redraw what is currently on the command line.
3767 */
3768 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003769redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 if (cmd_silent)
3772 return;
3773
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003774 /* when 'incsearch' is set there may be no command line while redrawing */
3775 if (ccline.cmdbuff == NULL)
3776 {
3777 windgoto(cmdline_row, 0);
3778 msg_clr_eos();
3779 return;
3780 }
3781
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 msg_start();
3783 redrawcmdprompt();
3784
3785 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3786 msg_no_more = TRUE;
3787 draw_cmdline(0, ccline.cmdlen);
3788 msg_clr_eos();
3789 msg_no_more = FALSE;
3790
3791 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003792 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003793 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 /*
3796 * An emsg() before may have set msg_scroll. This is used in normal mode,
3797 * in cmdline mode we can reset them now.
3798 */
3799 msg_scroll = FALSE; /* next message overwrites cmdline */
3800
3801 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3802 * in cmdline mode */
3803 skip_redraw = FALSE;
3804}
3805
3806 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003807compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003809 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 cmdline_row = Rows - 1;
3811 else
3812 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003813 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814}
3815
3816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003817cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 if (cmd_silent)
3820 return;
3821
3822#ifdef FEAT_RIGHTLEFT
3823 if (cmdmsg_rl)
3824 {
3825 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3826 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3827 if (msg_row <= 0)
3828 msg_row = Rows - 1;
3829 }
3830 else
3831#endif
3832 {
3833 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3834 msg_col = ccline.cmdspos % (int)Columns;
3835 if (msg_row >= Rows)
3836 msg_row = Rows - 1;
3837 }
3838
3839 windgoto(msg_row, msg_col);
3840#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003841 if (p_imst == IM_ON_THE_SPOT)
3842 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#endif
3844#ifdef MCH_CURSOR_SHAPE
3845 mch_update_cursor();
3846#endif
3847}
3848
3849 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003850gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 msg_start();
3853#ifdef FEAT_RIGHTLEFT
3854 if (cmdmsg_rl)
3855 msg_col = Columns - 1;
3856 else
3857#endif
3858 msg_col = 0; /* always start in column 0 */
3859 if (clr) /* clear the bottom line(s) */
3860 msg_clr_eos(); /* will reset clear_cmdline */
3861 windgoto(cmdline_row, 0);
3862}
3863
3864/*
3865 * Check the word in front of the cursor for an abbreviation.
3866 * Called when the non-id character "c" has been entered.
3867 * When an abbreviation is recognized it is removed from the text with
3868 * backspaces and the replacement string is inserted, followed by "c".
3869 */
3870 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003871ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003873 int spos = 0;
3874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3876 return FALSE;
3877
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003878 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3879 * Actually accepts any mark. */
3880 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3881 spos++;
3882 if (ccline.cmdlen - spos > 5
3883 && ccline.cmdbuff[spos] == '\''
3884 && ccline.cmdbuff[spos + 2] == ','
3885 && ccline.cmdbuff[spos + 3] == '\'')
3886 spos += 5;
3887 else
3888 /* check abbreviation from the beginning of the commandline */
3889 spos = 0;
3890
3891 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892}
3893
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003894#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3895 static int
3896#ifdef __BORLANDC__
3897_RTLENTRYF
3898#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003899sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003900{
3901 char_u *p1 = *(char_u **)s1;
3902 char_u *p2 = *(char_u **)s2;
3903
3904 if (*p1 != '<' && *p2 == '<') return -1;
3905 if (*p1 == '<' && *p2 != '<') return 1;
3906 return STRCMP(p1, p2);
3907}
3908#endif
3909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910/*
3911 * Return FAIL if this is not an appropriate context in which to do
3912 * completion of anything, return OK if it is (even if there are no matches).
3913 * For the caller, this means that the character is just passed through like a
3914 * normal character (instead of being expanded). This allows :s/^I^D etc.
3915 */
3916 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003917nextwild(
3918 expand_T *xp,
3919 int type,
3920 int options, /* extra options for ExpandOne() */
3921 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 int i, j;
3924 char_u *p1;
3925 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 int difflen;
3927 int v;
3928
3929 if (xp->xp_numfiles == -1)
3930 {
3931 set_expand_context(xp);
3932 cmd_showtail = expand_showtail(xp);
3933 }
3934
3935 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3936 {
3937 beep_flush();
3938 return OK; /* Something illegal on command line */
3939 }
3940 if (xp->xp_context == EXPAND_NOTHING)
3941 {
3942 /* Caller can use the character as a normal char instead */
3943 return FAIL;
3944 }
3945
3946 MSG_PUTS("..."); /* show that we are busy */
3947 out_flush();
3948
3949 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003950 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 if (type == WILD_NEXT || type == WILD_PREV)
3953 {
3954 /*
3955 * Get next/previous match for a previous expanded pattern.
3956 */
3957 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3958 }
3959 else
3960 {
3961 /*
3962 * Translate string into pattern and expand it.
3963 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003964 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3965 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 p2 = NULL;
3967 else
3968 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003969 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003970 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3971 if (escape)
3972 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003973
3974 if (p_wic)
3975 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003976 p2 = ExpandOne(xp, p1,
3977 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003978 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003980 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (p2 != NULL && type == WILD_LONGEST)
3982 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003983 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (ccline.cmdbuff[i + j] == '*'
3985 || ccline.cmdbuff[i + j] == '?')
3986 break;
3987 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003988 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990 }
3991 }
3992
3993 if (p2 != NULL && !got_int)
3994 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003995 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003996 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003998 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 xp->xp_pattern = ccline.cmdbuff + i;
4000 }
4001 else
4002 v = OK;
4003 if (v == OK)
4004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004005 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4006 &ccline.cmdbuff[ccline.cmdpos],
4007 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4008 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 ccline.cmdlen += difflen;
4010 ccline.cmdpos += difflen;
4011 }
4012 }
4013 vim_free(p2);
4014
4015 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004016 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 /* When expanding a ":map" command and no matches are found, assume that
4019 * the key is supposed to be inserted literally */
4020 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4021 return FAIL;
4022
4023 if (xp->xp_numfiles <= 0 && p2 == NULL)
4024 beep_flush();
4025 else if (xp->xp_numfiles == 1)
4026 /* free expanded pattern */
4027 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4028
4029 return OK;
4030}
4031
4032/*
4033 * Do wildcard expansion on the string 'str'.
4034 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004035 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Return NULL for failure.
4037 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004038 * "orig" is the originally expanded string, copied to allocated memory. It
4039 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4040 * WILD_PREV "orig" should be NULL.
4041 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004042 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4043 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 *
4045 * mode = WILD_FREE: just free previously expanded matches
4046 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4047 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4048 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4049 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4050 * mode = WILD_ALL: return all matches concatenated
4051 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004052 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 *
4054 * options = WILD_LIST_NOTFOUND: list entries without a match
4055 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4056 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4057 * options = WILD_NO_BEEP: Don't beep for multiple matches
4058 * options = WILD_ADD_SLASH: add a slash after directory names
4059 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4060 * options = WILD_SILENT: don't print warning messages
4061 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004062 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 *
4064 * The variables xp->xp_context and xp->xp_backslash must have been set!
4065 */
4066 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004067ExpandOne(
4068 expand_T *xp,
4069 char_u *str,
4070 char_u *orig, /* allocated copy of original of expanded string */
4071 int options,
4072 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
4074 char_u *ss = NULL;
4075 static int findex;
4076 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004077 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 int i;
4079 long_u len;
4080 int non_suf_match; /* number without matching suffix */
4081
4082 /*
4083 * first handle the case of using an old match
4084 */
4085 if (mode == WILD_NEXT || mode == WILD_PREV)
4086 {
4087 if (xp->xp_numfiles > 0)
4088 {
4089 if (mode == WILD_PREV)
4090 {
4091 if (findex == -1)
4092 findex = xp->xp_numfiles;
4093 --findex;
4094 }
4095 else /* mode == WILD_NEXT */
4096 ++findex;
4097
4098 /*
4099 * When wrapping around, return the original string, set findex to
4100 * -1.
4101 */
4102 if (findex < 0)
4103 {
4104 if (orig_save == NULL)
4105 findex = xp->xp_numfiles - 1;
4106 else
4107 findex = -1;
4108 }
4109 if (findex >= xp->xp_numfiles)
4110 {
4111 if (orig_save == NULL)
4112 findex = 0;
4113 else
4114 findex = -1;
4115 }
4116#ifdef FEAT_WILDMENU
4117 if (p_wmnu)
4118 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4119 findex, cmd_showtail);
4120#endif
4121 if (findex == -1)
4122 return vim_strsave(orig_save);
4123 return vim_strsave(xp->xp_files[findex]);
4124 }
4125 else
4126 return NULL;
4127 }
4128
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004129 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4131 {
4132 FreeWild(xp->xp_numfiles, xp->xp_files);
4133 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004134 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 findex = 0;
4137
4138 if (mode == WILD_FREE) /* only release file name */
4139 return NULL;
4140
4141 if (xp->xp_numfiles == -1)
4142 {
4143 vim_free(orig_save);
4144 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004145 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 /*
4148 * Do the expansion.
4149 */
4150 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4151 options) == FAIL)
4152 {
4153#ifdef FNAME_ILLEGAL
4154 /* Illegal file name has been silently skipped. But when there
4155 * are wildcards, the real problem is that there was no match,
4156 * causing the pattern to be added, which has illegal characters.
4157 */
4158 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4159 EMSG2(_(e_nomatch2), str);
4160#endif
4161 }
4162 else if (xp->xp_numfiles == 0)
4163 {
4164 if (!(options & WILD_SILENT))
4165 EMSG2(_(e_nomatch2), str);
4166 }
4167 else
4168 {
4169 /* Escape the matches for use on the command line. */
4170 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4171
4172 /*
4173 * Check for matching suffixes in file names.
4174 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004175 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4176 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 if (xp->xp_numfiles)
4179 non_suf_match = xp->xp_numfiles;
4180 else
4181 non_suf_match = 1;
4182 if ((xp->xp_context == EXPAND_FILES
4183 || xp->xp_context == EXPAND_DIRECTORIES)
4184 && xp->xp_numfiles > 1)
4185 {
4186 /*
4187 * More than one match; check suffix.
4188 * The files will have been sorted on matching suffix in
4189 * expand_wildcards, only need to check the first two.
4190 */
4191 non_suf_match = 0;
4192 for (i = 0; i < 2; ++i)
4193 if (match_suffix(xp->xp_files[i]))
4194 ++non_suf_match;
4195 }
4196 if (non_suf_match != 1)
4197 {
4198 /* Can we ever get here unless it's while expanding
4199 * interactively? If not, we can get rid of this all
4200 * together. Don't really want to wait for this message
4201 * (and possibly have to hit return to continue!).
4202 */
4203 if (!(options & WILD_SILENT))
4204 EMSG(_(e_toomany));
4205 else if (!(options & WILD_NO_BEEP))
4206 beep_flush();
4207 }
4208 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4209 ss = vim_strsave(xp->xp_files[0]);
4210 }
4211 }
4212 }
4213
4214 /* Find longest common part */
4215 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4216 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004217 int mb_len = 1;
4218 int c0, ci;
4219
4220 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004222#ifdef FEAT_MBYTE
4223 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004225 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4226 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4227 }
4228 else
4229#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004230 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004231 for (i = 1; i < xp->xp_numfiles; ++i)
4232 {
4233#ifdef FEAT_MBYTE
4234 if (has_mbyte)
4235 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4236 else
4237#endif
4238 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004239 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004241 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004242 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004244 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 break;
4246 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004247 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249 }
4250 if (i < xp->xp_numfiles)
4251 {
4252 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004253 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 break;
4255 }
4256 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 ss = alloc((unsigned)len + 1);
4259 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004260 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 findex = -1; /* next p_wc gets first one */
4262 }
4263
4264 /* Concatenate all matching names */
4265 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4266 {
4267 len = 0;
4268 for (i = 0; i < xp->xp_numfiles; ++i)
4269 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4270 ss = lalloc(len, TRUE);
4271 if (ss != NULL)
4272 {
4273 *ss = NUL;
4274 for (i = 0; i < xp->xp_numfiles; ++i)
4275 {
4276 STRCAT(ss, xp->xp_files[i]);
4277 if (i != xp->xp_numfiles - 1)
4278 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4279 }
4280 }
4281 }
4282
4283 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4284 ExpandCleanup(xp);
4285
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004286 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004287 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004288 vim_free(orig);
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 return ss;
4291}
4292
4293/*
4294 * Prepare an expand structure for use.
4295 */
4296 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004297ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004299 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004300 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004302#ifndef BACKSLASH_IN_FILENAME
4303 xp->xp_shell = FALSE;
4304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 xp->xp_numfiles = -1;
4306 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004307#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4308 xp->xp_arg = NULL;
4309#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004310 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311}
4312
4313/*
4314 * Cleanup an expand structure after use.
4315 */
4316 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004317ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
4319 if (xp->xp_numfiles >= 0)
4320 {
4321 FreeWild(xp->xp_numfiles, xp->xp_files);
4322 xp->xp_numfiles = -1;
4323 }
4324}
4325
4326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004327ExpandEscape(
4328 expand_T *xp,
4329 char_u *str,
4330 int numfiles,
4331 char_u **files,
4332 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
4334 int i;
4335 char_u *p;
4336
4337 /*
4338 * May change home directory back to "~"
4339 */
4340 if (options & WILD_HOME_REPLACE)
4341 tilde_replace(str, numfiles, files);
4342
4343 if (options & WILD_ESCAPE)
4344 {
4345 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004346 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004347 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 || xp->xp_context == EXPAND_BUFFERS
4349 || xp->xp_context == EXPAND_DIRECTORIES)
4350 {
4351 /*
4352 * Insert a backslash into a file name before a space, \, %, #
4353 * and wildmatch characters, except '~'.
4354 */
4355 for (i = 0; i < numfiles; ++i)
4356 {
4357 /* for ":set path=" we need to escape spaces twice */
4358 if (xp->xp_backslash == XP_BS_THREE)
4359 {
4360 p = vim_strsave_escaped(files[i], (char_u *)" ");
4361 if (p != NULL)
4362 {
4363 vim_free(files[i]);
4364 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004365#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 p = vim_strsave_escaped(files[i], (char_u *)" ");
4367 if (p != NULL)
4368 {
4369 vim_free(files[i]);
4370 files[i] = p;
4371 }
4372#endif
4373 }
4374 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004375#ifdef BACKSLASH_IN_FILENAME
4376 p = vim_strsave_fnameescape(files[i], FALSE);
4377#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004378 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (p != NULL)
4381 {
4382 vim_free(files[i]);
4383 files[i] = p;
4384 }
4385
4386 /* If 'str' starts with "\~", replace "~" at start of
4387 * files[i] with "\~". */
4388 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004389 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
4391 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004392
4393 /* If the first file starts with a '+' escape it. Otherwise it
4394 * could be seen as "+cmd". */
4395 if (*files[0] == '+')
4396 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 else if (xp->xp_context == EXPAND_TAGS)
4399 {
4400 /*
4401 * Insert a backslash before characters in a tag name that
4402 * would terminate the ":tag" command.
4403 */
4404 for (i = 0; i < numfiles; ++i)
4405 {
4406 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4407 if (p != NULL)
4408 {
4409 vim_free(files[i]);
4410 files[i] = p;
4411 }
4412 }
4413 }
4414 }
4415}
4416
4417/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004418 * Escape special characters in "fname" for when used as a file name argument
4419 * after a Vim command, or, when "shell" is non-zero, a shell command.
4420 * Returns the result in allocated memory.
4421 */
4422 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004423vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004424{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004425 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004426#ifdef BACKSLASH_IN_FILENAME
4427 char_u buf[20];
4428 int j = 0;
4429
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004430 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004431 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004432 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004433 buf[j++] = *p;
4434 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004435 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004436#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004437 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4438 if (shell && csh_like_shell() && p != NULL)
4439 {
4440 char_u *s;
4441
4442 /* For csh and similar shells need to put two backslashes before '!'.
4443 * One is taken by Vim, one by the shell. */
4444 s = vim_strsave_escaped(p, (char_u *)"!");
4445 vim_free(p);
4446 p = s;
4447 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004448#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004449
4450 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4451 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004452 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004453 escape_fname(&p);
4454
4455 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004456}
4457
4458/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004459 * Put a backslash before the file name in "pp", which is in allocated memory.
4460 */
4461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004462escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004463{
4464 char_u *p;
4465
4466 p = alloc((unsigned)(STRLEN(*pp) + 2));
4467 if (p != NULL)
4468 {
4469 p[0] = '\\';
4470 STRCPY(p + 1, *pp);
4471 vim_free(*pp);
4472 *pp = p;
4473 }
4474}
4475
4476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 * For each file name in files[num_files]:
4478 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4479 */
4480 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004481tilde_replace(
4482 char_u *orig_pat,
4483 int num_files,
4484 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485{
4486 int i;
4487 char_u *p;
4488
4489 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4490 {
4491 for (i = 0; i < num_files; ++i)
4492 {
4493 p = home_replace_save(NULL, files[i]);
4494 if (p != NULL)
4495 {
4496 vim_free(files[i]);
4497 files[i] = p;
4498 }
4499 }
4500 }
4501}
4502
4503/*
4504 * Show all matches for completion on the command line.
4505 * Returns EXPAND_NOTHING when the character that triggered expansion should
4506 * be inserted like a normal character.
4507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004509showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4512 int num_files;
4513 char_u **files_found;
4514 int i, j, k;
4515 int maxlen;
4516 int lines;
4517 int columns;
4518 char_u *p;
4519 int lastlen;
4520 int attr;
4521 int showtail;
4522
4523 if (xp->xp_numfiles == -1)
4524 {
4525 set_expand_context(xp);
4526 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4527 &num_files, &files_found);
4528 showtail = expand_showtail(xp);
4529 if (i != EXPAND_OK)
4530 return i;
4531
4532 }
4533 else
4534 {
4535 num_files = xp->xp_numfiles;
4536 files_found = xp->xp_files;
4537 showtail = cmd_showtail;
4538 }
4539
4540#ifdef FEAT_WILDMENU
4541 if (!wildmenu)
4542 {
4543#endif
4544 msg_didany = FALSE; /* lines_left will be set */
4545 msg_start(); /* prepare for paging */
4546 msg_putchar('\n');
4547 out_flush();
4548 cmdline_row = msg_row;
4549 msg_didany = FALSE; /* lines_left will be set again */
4550 msg_start(); /* prepare for paging */
4551#ifdef FEAT_WILDMENU
4552 }
4553#endif
4554
4555 if (got_int)
4556 got_int = FALSE; /* only int. the completion, not the cmd line */
4557#ifdef FEAT_WILDMENU
4558 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004559 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#endif
4561 else
4562 {
4563 /* find the length of the longest file name */
4564 maxlen = 0;
4565 for (i = 0; i < num_files; ++i)
4566 {
4567 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004568 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 || xp->xp_context == EXPAND_BUFFERS))
4570 {
4571 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4572 j = vim_strsize(NameBuff);
4573 }
4574 else
4575 j = vim_strsize(L_SHOWFILE(i));
4576 if (j > maxlen)
4577 maxlen = j;
4578 }
4579
4580 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4581 lines = num_files;
4582 else
4583 {
4584 /* compute the number of columns and lines for the listing */
4585 maxlen += 2; /* two spaces between file names */
4586 columns = ((int)Columns + 2) / maxlen;
4587 if (columns < 1)
4588 columns = 1;
4589 lines = (num_files + columns - 1) / columns;
4590 }
4591
Bram Moolenaar8820b482017-03-16 17:23:31 +01004592 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4595 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004596 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 msg_clr_eos();
4598 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004599 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601
4602 /* list the files line by line */
4603 for (i = 0; i < lines; ++i)
4604 {
4605 lastlen = 999;
4606 for (k = i; k < num_files; k += lines)
4607 {
4608 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4609 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004610 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 p = files_found[k] + STRLEN(files_found[k]) + 1;
4612 msg_advance(maxlen + 1);
4613 msg_puts(p);
4614 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004615 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 break;
4617 }
4618 for (j = maxlen - lastlen; --j >= 0; )
4619 msg_putchar(' ');
4620 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004621 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 || xp->xp_context == EXPAND_BUFFERS)
4623 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004624 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004625 if (xp->xp_numfiles != -1)
4626 {
4627 char_u *halved_slash;
4628 char_u *exp_path;
4629
4630 /* Expansion was done before and special characters
4631 * were escaped, need to halve backslashes. Also
4632 * $HOME has been replaced with ~/. */
4633 exp_path = expand_env_save_opt(files_found[k], TRUE);
4634 halved_slash = backslash_halve_save(
4635 exp_path != NULL ? exp_path : files_found[k]);
4636 j = mch_isdir(halved_slash != NULL ? halved_slash
4637 : files_found[k]);
4638 vim_free(exp_path);
4639 vim_free(halved_slash);
4640 }
4641 else
4642 /* Expansion was done here, file names are literal. */
4643 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (showtail)
4645 p = L_SHOWFILE(k);
4646 else
4647 {
4648 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4649 TRUE);
4650 p = NameBuff;
4651 }
4652 }
4653 else
4654 {
4655 j = FALSE;
4656 p = L_SHOWFILE(k);
4657 }
4658 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4659 }
4660 if (msg_col > 0) /* when not wrapped around */
4661 {
4662 msg_clr_eos();
4663 msg_putchar('\n');
4664 }
4665 out_flush(); /* show one line at a time */
4666 if (got_int)
4667 {
4668 got_int = FALSE;
4669 break;
4670 }
4671 }
4672
4673 /*
4674 * we redraw the command below the lines that we have just listed
4675 * This is a bit tricky, but it saves a lot of screen updating.
4676 */
4677 cmdline_row = msg_row; /* will put it back later */
4678 }
4679
4680 if (xp->xp_numfiles == -1)
4681 FreeWild(num_files, files_found);
4682
4683 return EXPAND_OK;
4684}
4685
4686/*
4687 * Private gettail for showmatches() (and win_redr_status_matches()):
4688 * Find tail of file name path, but ignore trailing "/".
4689 */
4690 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004691sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 char_u *p;
4694 char_u *t = s;
4695 int had_sep = FALSE;
4696
4697 for (p = s; *p != NUL; )
4698 {
4699 if (vim_ispathsep(*p)
4700#ifdef BACKSLASH_IN_FILENAME
4701 && !rem_backslash(p)
4702#endif
4703 )
4704 had_sep = TRUE;
4705 else if (had_sep)
4706 {
4707 t = p;
4708 had_sep = FALSE;
4709 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004710 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712 return t;
4713}
4714
4715/*
4716 * Return TRUE if we only need to show the tail of completion matches.
4717 * When not completing file names or there is a wildcard in the path FALSE is
4718 * returned.
4719 */
4720 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004721expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 char_u *s;
4724 char_u *end;
4725
4726 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004727 if (xp->xp_context != EXPAND_FILES
4728 && xp->xp_context != EXPAND_SHELLCMD
4729 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 return FALSE;
4731
4732 end = gettail(xp->xp_pattern);
4733 if (end == xp->xp_pattern) /* there is no path separator */
4734 return FALSE;
4735
4736 for (s = xp->xp_pattern; s < end; s++)
4737 {
4738 /* Skip escaped wildcards. Only when the backslash is not a path
4739 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4740 if (rem_backslash(s))
4741 ++s;
4742 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4743 return FALSE;
4744 }
4745 return TRUE;
4746}
4747
4748/*
4749 * Prepare a string for expansion.
4750 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004751 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 * When expanding other names: The string will be used with regcomp(). Copy
4753 * the name into allocated memory and prepend "^".
4754 */
4755 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756addstar(
4757 char_u *fname,
4758 int len,
4759 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 char_u *retval;
4762 int i, j;
4763 int new_len;
4764 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004765 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004767 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004768 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004769 && context != EXPAND_SHELLCMD
4770 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
4772 /*
4773 * Matching will be done internally (on something other than files).
4774 * So we convert the file-matching-type wildcards into our kind for
4775 * use with vim_regcomp(). First work out how long it will be:
4776 */
4777
4778 /* For help tags the translation is done in find_help_tags().
4779 * For a tag pattern starting with "/" no translation is needed. */
4780 if (context == EXPAND_HELP
4781 || context == EXPAND_COLORS
4782 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004783 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004784 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004785 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004786 || ((context == EXPAND_TAGS_LISTFILES
4787 || context == EXPAND_TAGS)
4788 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 retval = vim_strnsave(fname, len);
4790 else
4791 {
4792 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4793 for (i = 0; i < len; i++)
4794 {
4795 if (fname[i] == '*' || fname[i] == '~')
4796 new_len++; /* '*' needs to be replaced by ".*"
4797 '~' needs to be replaced by "\~" */
4798
4799 /* Buffer names are like file names. "." should be literal */
4800 if (context == EXPAND_BUFFERS && fname[i] == '.')
4801 new_len++; /* "." becomes "\." */
4802
4803 /* Custom expansion takes care of special things, match
4804 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004805 if ((context == EXPAND_USER_DEFINED
4806 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 new_len++; /* '\' becomes "\\" */
4808 }
4809 retval = alloc(new_len);
4810 if (retval != NULL)
4811 {
4812 retval[0] = '^';
4813 j = 1;
4814 for (i = 0; i < len; i++, j++)
4815 {
4816 /* Skip backslash. But why? At least keep it for custom
4817 * expansion. */
4818 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004819 && context != EXPAND_USER_LIST
4820 && fname[i] == '\\'
4821 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 break;
4823
4824 switch (fname[i])
4825 {
4826 case '*': retval[j++] = '.';
4827 break;
4828 case '~': retval[j++] = '\\';
4829 break;
4830 case '?': retval[j] = '.';
4831 continue;
4832 case '.': if (context == EXPAND_BUFFERS)
4833 retval[j++] = '\\';
4834 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004835 case '\\': if (context == EXPAND_USER_DEFINED
4836 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 retval[j++] = '\\';
4838 break;
4839 }
4840 retval[j] = fname[i];
4841 }
4842 retval[j] = NUL;
4843 }
4844 }
4845 }
4846 else
4847 {
4848 retval = alloc(len + 4);
4849 if (retval != NULL)
4850 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004851 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
4853 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004854 * Don't add a star to *, ~, ~user, $var or `cmd`.
4855 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 * ~ would be at the start of the file name, but not the tail.
4857 * $ could be anywhere in the tail.
4858 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004859 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 */
4861 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004862 ends_in_star = (len > 0 && retval[len - 1] == '*');
4863#ifndef BACKSLASH_IN_FILENAME
4864 for (i = len - 2; i >= 0; --i)
4865 {
4866 if (retval[i] != '\\')
4867 break;
4868 ends_in_star = !ends_in_star;
4869 }
4870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004872 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 && vim_strchr(tail, '$') == NULL
4874 && vim_strchr(retval, '`') == NULL)
4875 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004876 else if (len > 0 && retval[len - 1] == '$')
4877 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 retval[len] = NUL;
4879 }
4880 }
4881 return retval;
4882}
4883
4884/*
4885 * Must parse the command line so far to work out what context we are in.
4886 * Completion can then be done based on that context.
4887 * This routine sets the variables:
4888 * xp->xp_pattern The start of the pattern to be expanded within
4889 * the command line (ends at the cursor).
4890 * xp->xp_context The type of thing to expand. Will be one of:
4891 *
4892 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4893 * the command line, like an unknown command. Caller
4894 * should beep.
4895 * EXPAND_NOTHING Unrecognised context for completion, use char like
4896 * a normal char, rather than for completion. eg
4897 * :s/^I/
4898 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4899 * it.
4900 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4901 * EXPAND_FILES After command with XFILE set, or after setting
4902 * with P_EXPAND set. eg :e ^I, :w>>^I
4903 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4904 * when we know only directories are of interest. eg
4905 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004906 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4908 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4909 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4910 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4911 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4912 * EXPAND_EVENTS Complete event names
4913 * EXPAND_SYNTAX Complete :syntax command arguments
4914 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4915 * EXPAND_AUGROUP Complete autocommand group names
4916 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4917 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4918 * eg :unmap a^I , :cunab x^I
4919 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4920 * eg :call sub^I
4921 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4922 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4923 * names in expressions, eg :while s^I
4924 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004925 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 */
4927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004928set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004930 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (ccline.cmdfirstc != ':'
4932#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004933 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004934 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935#endif
4936 )
4937 {
4938 xp->xp_context = EXPAND_NOTHING;
4939 return;
4940 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004941 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942}
4943
4944 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004945set_cmd_context(
4946 expand_T *xp,
4947 char_u *str, /* start of command line */
4948 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004949 int col, /* position of cursor */
4950 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 int old_char = NUL;
4953 char_u *nextcomm;
4954
4955 /*
4956 * Avoid a UMR warning from Purify, only save the character if it has been
4957 * written before.
4958 */
4959 if (col < len)
4960 old_char = str[col];
4961 str[col] = NUL;
4962 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004963
4964#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004965 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004966 {
4967# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004968 /* pass CMD_SIZE because there is no real command */
4969 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004970# endif
4971 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004972 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004973 {
4974 xp->xp_context = ccline.xp_context;
4975 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004976# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004977 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004978# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004979 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004980 else
4981#endif
4982 while (nextcomm != NULL)
4983 nextcomm = set_one_cmd_context(xp, nextcomm);
4984
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004985 /* Store the string here so that call_user_expand_func() can get to them
4986 * easily. */
4987 xp->xp_line = str;
4988 xp->xp_col = col;
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 str[col] = old_char;
4991}
4992
4993/*
4994 * Expand the command line "str" from context "xp".
4995 * "xp" must have been set by set_cmd_context().
4996 * xp->xp_pattern points into "str", to where the text that is to be expanded
4997 * starts.
4998 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4999 * cursor.
5000 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
5001 * key that triggered expansion literally.
5002 * Returns EXPAND_OK otherwise.
5003 */
5004 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005005expand_cmdline(
5006 expand_T *xp,
5007 char_u *str, /* start of command line */
5008 int col, /* position of cursor */
5009 int *matchcount, /* return: nr of matches */
5010 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005013 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
5015 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5016 {
5017 beep_flush();
5018 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5019 }
5020 if (xp->xp_context == EXPAND_NOTHING)
5021 {
5022 /* Caller can use the character as a normal char instead */
5023 return EXPAND_NOTHING;
5024 }
5025
5026 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005027 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5028 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (file_str == NULL)
5030 return EXPAND_UNSUCCESSFUL;
5031
Bram Moolenaar94950a92010-12-02 16:01:29 +01005032 if (p_wic)
5033 options += WILD_ICASE;
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005036 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
5038 *matchcount = 0;
5039 *matches = NULL;
5040 }
5041 vim_free(file_str);
5042
5043 return EXPAND_OK;
5044}
5045
5046#ifdef FEAT_MULTI_LANG
5047/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005048 * Cleanup matches for help tags:
5049 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5050 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005052static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005055cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 int i, j;
5058 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005059 char_u buf[4];
5060 char_u *p = buf;
5061
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005062 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005063 {
5064 *p++ = '@';
5065 *p++ = p_hlg[0];
5066 *p++ = p_hlg[1];
5067 }
5068 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 for (i = 0; i < num_file; ++i)
5071 {
5072 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005073 if (len <= 0)
5074 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005075 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 /* Sorting on priority means the same item in another language may
5078 * be anywhere. Search all items for a match up to the "@en". */
5079 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005080 if (j != i && (int)STRLEN(file[j]) == len + 3
5081 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 break;
5083 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005084 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 file[i][len] = NUL;
5086 }
5087 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005088
5089 if (*buf != NUL)
5090 for (i = 0; i < num_file; ++i)
5091 {
5092 len = (int)STRLEN(file[i]) - 3;
5093 if (len <= 0)
5094 continue;
5095 if (STRCMP(file[i] + len, buf) == 0)
5096 {
5097 /* remove the default language */
5098 file[i][len] = NUL;
5099 }
5100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101}
5102#endif
5103
5104/*
5105 * Do the expansion based on xp->xp_context and "pat".
5106 */
5107 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005108ExpandFromContext(
5109 expand_T *xp,
5110 char_u *pat,
5111 int *num_file,
5112 char_u ***file,
5113 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114{
5115#ifdef FEAT_CMDL_COMPL
5116 regmatch_T regmatch;
5117#endif
5118 int ret;
5119 int flags;
5120
5121 flags = EW_DIR; /* include directories */
5122 if (options & WILD_LIST_NOTFOUND)
5123 flags |= EW_NOTFOUND;
5124 if (options & WILD_ADD_SLASH)
5125 flags |= EW_ADDSLASH;
5126 if (options & WILD_KEEP_ALL)
5127 flags |= EW_KEEPALL;
5128 if (options & WILD_SILENT)
5129 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005130 if (options & WILD_ALLLINKS)
5131 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005133 if (xp->xp_context == EXPAND_FILES
5134 || xp->xp_context == EXPAND_DIRECTORIES
5135 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
5137 /*
5138 * Expand file or directory names.
5139 */
5140 int free_pat = FALSE;
5141 int i;
5142
5143 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5144 * space */
5145 if (xp->xp_backslash != XP_BS_NONE)
5146 {
5147 free_pat = TRUE;
5148 pat = vim_strsave(pat);
5149 for (i = 0; pat[i]; ++i)
5150 if (pat[i] == '\\')
5151 {
5152 if (xp->xp_backslash == XP_BS_THREE
5153 && pat[i + 1] == '\\'
5154 && pat[i + 2] == '\\'
5155 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005156 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (xp->xp_backslash == XP_BS_ONE
5158 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005159 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 }
5162
5163 if (xp->xp_context == EXPAND_FILES)
5164 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005165 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5166 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 else
5168 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005169 if (options & WILD_ICASE)
5170 flags |= EW_ICASE;
5171
Bram Moolenaard7834d32009-12-02 16:14:36 +00005172 /* Expand wildcards, supporting %:h and the like. */
5173 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (free_pat)
5175 vim_free(pat);
5176 return ret;
5177 }
5178
5179 *file = (char_u **)"";
5180 *num_file = 0;
5181 if (xp->xp_context == EXPAND_HELP)
5182 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005183 /* With an empty argument we would get all the help tags, which is
5184 * very slow. Get matches for "help" instead. */
5185 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5186 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
5188#ifdef FEAT_MULTI_LANG
5189 cleanup_help_tags(*num_file, *file);
5190#endif
5191 return OK;
5192 }
5193 return FAIL;
5194 }
5195
5196#ifndef FEAT_CMDL_COMPL
5197 return FAIL;
5198#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005199 if (xp->xp_context == EXPAND_SHELLCMD)
5200 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (xp->xp_context == EXPAND_OLD_SETTING)
5202 return ExpandOldSetting(num_file, file);
5203 if (xp->xp_context == EXPAND_BUFFERS)
5204 return ExpandBufnames(pat, num_file, file, options);
5205 if (xp->xp_context == EXPAND_TAGS
5206 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5207 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5208 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005209 {
5210 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005211 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5212 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005215 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005216 char *directories[] = {"compiler", 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 Moolenaar1587a1e2010-07-29 20:59:59 +02005219 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005220 {
5221 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005222 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005223 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005224 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005225 {
5226 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005227 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005228 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005229# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5230 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005231 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005232# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005233 if (xp->xp_context == EXPAND_PACKADD)
5234 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5237 if (regmatch.regprog == NULL)
5238 return FAIL;
5239
5240 /* set ignore-case according to p_ic, p_scs and pat */
5241 regmatch.rm_ic = ignorecase(pat);
5242
5243 if (xp->xp_context == EXPAND_SETTINGS
5244 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5245 ret = ExpandSettings(xp, &regmatch, num_file, file);
5246 else if (xp->xp_context == EXPAND_MAPPINGS)
5247 ret = ExpandMappings(&regmatch, num_file, file);
5248# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5249 else if (xp->xp_context == EXPAND_USER_DEFINED)
5250 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5251# endif
5252 else
5253 {
5254 static struct expgen
5255 {
5256 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005257 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005259 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 } tab[] =
5261 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005262 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5263 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005264 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005265 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005266#ifdef FEAT_CMDHIST
5267 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005270 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005271 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005272 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5273 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5274 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275#endif
5276#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005277 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5278 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5279 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5280 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281#endif
5282#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005283 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5284 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
5286#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005287 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005289#ifdef FEAT_PROFILE
5290 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5291#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005292 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005293 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5294 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005295#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005296 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005297#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005298#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005299 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005300#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005301#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005302 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5305 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005306 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5307 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005309 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005310 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005311 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 };
5313 int i;
5314
5315 /*
5316 * Find a context in the table and call the ExpandGeneric() with the
5317 * right function to do the expansion.
5318 */
5319 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005320 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (xp->xp_context == tab[i].context)
5322 {
5323 if (tab[i].ic)
5324 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005325 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005326 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 break;
5328 }
5329 }
5330
Bram Moolenaar473de612013-06-08 18:19:48 +02005331 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332
5333 return ret;
5334#endif /* FEAT_CMDL_COMPL */
5335}
5336
5337#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5338/*
5339 * Expand a list of names.
5340 *
5341 * Generic function for command line completion. It calls a function to
5342 * obtain strings, one by one. The strings are matched against a regexp
5343 * program. Matching strings are copied into an array, which is returned.
5344 *
5345 * Returns OK when no problems encountered, FAIL for error (out of memory).
5346 */
5347 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005348ExpandGeneric(
5349 expand_T *xp,
5350 regmatch_T *regmatch,
5351 int *num_file,
5352 char_u ***file,
5353 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005355 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 int i;
5358 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005359 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 char_u *str;
5361
5362 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005363 * round == 0: count the number of matching names
5364 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005366 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
5368 for (i = 0; ; ++i)
5369 {
5370 str = (*func)(xp, i);
5371 if (str == NULL) /* end of list */
5372 break;
5373 if (*str == NUL) /* skip empty strings */
5374 continue;
5375
5376 if (vim_regexec(regmatch, str, (colnr_T)0))
5377 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005378 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005380 if (escaped)
5381 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5382 else
5383 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 (*file)[count] = str;
5385#ifdef FEAT_MENU
5386 if (func == get_menu_names && str != NULL)
5387 {
5388 /* test for separator added by get_menu_names() */
5389 str += STRLEN(str) - 1;
5390 if (*str == '\001')
5391 *str = '.';
5392 }
5393#endif
5394 }
5395 ++count;
5396 }
5397 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005398 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
5400 if (count == 0)
5401 return OK;
5402 *num_file = count;
5403 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5404 if (*file == NULL)
5405 {
5406 *file = (char_u **)"";
5407 return FAIL;
5408 }
5409 count = 0;
5410 }
5411 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005412
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005413 /* Sort the results. Keep menu's in the specified order. */
5414 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005415 {
5416 if (xp->xp_context == EXPAND_EXPRESSION
5417 || xp->xp_context == EXPAND_FUNCTIONS
5418 || xp->xp_context == EXPAND_USER_FUNC)
5419 /* <SNR> functions should be sorted to the end. */
5420 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5421 sort_func_compare);
5422 else
5423 sort_strings(*file, *num_file);
5424 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005425
Bram Moolenaar4f688582007-07-24 12:34:30 +00005426#ifdef FEAT_CMDL_COMPL
5427 /* Reset the variables used for special highlight names expansion, so that
5428 * they don't show up when getting normal highlight names by ID. */
5429 reset_expand_highlight();
5430#endif
5431
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 return OK;
5433}
5434
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005435/*
5436 * Complete a shell command.
5437 * Returns FAIL or OK;
5438 */
5439 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005440expand_shellcmd(
5441 char_u *filepat, /* pattern to match with command names */
5442 int *num_file, /* return: number of matches */
5443 char_u ***file, /* return: array with matches */
5444 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005445{
5446 char_u *pat;
5447 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005448 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005449 int mustfree = FALSE;
5450 garray_T ga;
5451 char_u *buf = alloc(MAXPATHL);
5452 size_t l;
5453 char_u *s, *e;
5454 int flags = flagsarg;
5455 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005456 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005457 hashtab_T found_ht;
5458 hashitem_T *hi;
5459 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005460
5461 if (buf == NULL)
5462 return FAIL;
5463
5464 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5465 * space */
5466 pat = vim_strsave(filepat);
5467 for (i = 0; pat[i]; ++i)
5468 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005469 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005470
Bram Moolenaarb5971142015-03-21 17:32:19 +01005471 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005472
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005473 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5474 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005475 path = (char_u *)".";
5476 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005477 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005478 /* For an absolute name we don't use $PATH. */
5479 if (!mch_isFullName(pat))
5480 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005481 if (path == NULL)
5482 path = (char_u *)"";
5483 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005484
5485 /*
5486 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005487 * collect them in "ga". When "." is not in $PATH also expand for the
5488 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005489 */
5490 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005491 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005492 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005493 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005494#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005495 e = vim_strchr(s, ';');
5496#else
5497 e = vim_strchr(s, ':');
5498#endif
5499 if (e == NULL)
5500 e = s + STRLEN(s);
5501
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005502 if (*s == NUL)
5503 {
5504 if (did_curdir)
5505 break;
5506 // Find directories in the current directory, path is empty.
5507 did_curdir = TRUE;
5508 flags |= EW_DIR;
5509 }
5510 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5511 {
5512 did_curdir = TRUE;
5513 flags |= EW_DIR;
5514 }
5515 else
5516 // Do not match directories inside a $PATH item.
5517 flags &= ~EW_DIR;
5518
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005519 l = e - s;
5520 if (l > MAXPATHL - 5)
5521 break;
5522 vim_strncpy(buf, s, l);
5523 add_pathsep(buf);
5524 l = STRLEN(buf);
5525 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5526
5527 /* Expand matches in one directory of $PATH. */
5528 ret = expand_wildcards(1, &buf, num_file, file, flags);
5529 if (ret == OK)
5530 {
5531 if (ga_grow(&ga, *num_file) == FAIL)
5532 FreeWild(*num_file, *file);
5533 else
5534 {
5535 for (i = 0; i < *num_file; ++i)
5536 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005537 char_u *name = (*file)[i];
5538
5539 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005540 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005541 // Check if this name was already found.
5542 hash = hash_hash(name + l);
5543 hi = hash_lookup(&found_ht, name + l, hash);
5544 if (HASHITEM_EMPTY(hi))
5545 {
5546 // Remove the path that was prepended.
5547 STRMOVE(name, name + l);
5548 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5549 hash_add_item(&found_ht, hi, name, hash);
5550 name = NULL;
5551 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005552 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005553 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005554 }
5555 vim_free(*file);
5556 }
5557 }
5558 if (*e != NUL)
5559 ++e;
5560 }
5561 *file = ga.ga_data;
5562 *num_file = ga.ga_len;
5563
5564 vim_free(buf);
5565 vim_free(pat);
5566 if (mustfree)
5567 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005568 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005569 return OK;
5570}
5571
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5574/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005575 * Call "user_expand_func()" to invoke a user defined Vim script function and
5576 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005578 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005579call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005580 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581 expand_T *xp,
5582 int *num_file,
5583 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005585 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005586 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005587 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005588 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005589 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaarb7515462013-06-29 12:58:33 +02005591 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005592 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 *num_file = 0;
5594 *file = NULL;
5595
Bram Moolenaarb7515462013-06-29 12:58:33 +02005596 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005597 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005598 keep = ccline.cmdbuff[ccline.cmdlen];
5599 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005600 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005601
Bram Moolenaarffa96842018-06-12 22:05:14 +02005602 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5603
5604 args[0].v_type = VAR_STRING;
5605 args[0].vval.v_string = pat;
5606 args[1].v_type = VAR_STRING;
5607 args[1].vval.v_string = xp->xp_line;
5608 args[2].v_type = VAR_NUMBER;
5609 args[2].vval.v_number = xp->xp_col;
5610 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005612 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005613
Bram Moolenaarded27a12018-08-01 19:06:03 +02005614 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005615
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005616 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005617 if (ccline.cmdbuff != NULL)
5618 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005619
Bram Moolenaarffa96842018-06-12 22:05:14 +02005620 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005621 return ret;
5622}
5623
5624/*
5625 * Expand names with a function defined by the user.
5626 */
5627 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628ExpandUserDefined(
5629 expand_T *xp,
5630 regmatch_T *regmatch,
5631 int *num_file,
5632 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005633{
5634 char_u *retstr;
5635 char_u *s;
5636 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005637 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005638 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005639 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005640
5641 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5642 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 return FAIL;
5644
5645 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005646 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
5648 e = vim_strchr(s, '\n');
5649 if (e == NULL)
5650 e = s + STRLEN(s);
5651 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005652 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005654 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5655 *e = keep;
5656
5657 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005659 if (ga_grow(&ga, 1) == FAIL)
5660 break;
5661 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5662 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 if (*e != NUL)
5666 ++e;
5667 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005668 vim_free(retstr);
5669 *file = ga.ga_data;
5670 *num_file = ga.ga_len;
5671 return OK;
5672}
5673
5674/*
5675 * Expand names with a list returned by a function defined by the user.
5676 */
5677 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678ExpandUserList(
5679 expand_T *xp,
5680 int *num_file,
5681 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005682{
5683 list_T *retlist;
5684 listitem_T *li;
5685 garray_T ga;
5686
5687 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5688 if (retlist == NULL)
5689 return FAIL;
5690
5691 ga_init2(&ga, (int)sizeof(char *), 3);
5692 /* Loop over the items in the list. */
5693 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5694 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005695 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5696 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005697
5698 if (ga_grow(&ga, 1) == FAIL)
5699 break;
5700
5701 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005702 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005703 ++ga.ga_len;
5704 }
5705 list_unref(retlist);
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 *file = ga.ga_data;
5708 *num_file = ga.ga_len;
5709 return OK;
5710}
5711#endif
5712
5713/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005714 * Expand color scheme, compiler or filetype names.
5715 * Search from 'runtimepath':
5716 * 'runtimepath'/{dirnames}/{pat}.vim
5717 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5718 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5719 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5720 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005721 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 */
5723 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005724ExpandRTDir(
5725 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005726 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005727 int *num_file,
5728 char_u ***file,
5729 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 char_u *s;
5732 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005733 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005735 int i;
5736 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 *num_file = 0;
5739 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005740 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005741 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005743 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005745 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5746 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005748 ga_clear_strings(&ga);
5749 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005751 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005752 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005753 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005755
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005756 if (flags & DIP_START) {
5757 for (i = 0; dirnames[i] != NULL; ++i)
5758 {
5759 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5760 if (s == NULL)
5761 {
5762 ga_clear_strings(&ga);
5763 return FAIL;
5764 }
5765 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5766 globpath(p_pp, s, &ga, 0);
5767 vim_free(s);
5768 }
5769 }
5770
5771 if (flags & DIP_OPT) {
5772 for (i = 0; dirnames[i] != NULL; ++i)
5773 {
5774 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5775 if (s == NULL)
5776 {
5777 ga_clear_strings(&ga);
5778 return FAIL;
5779 }
5780 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5781 globpath(p_pp, s, &ga, 0);
5782 vim_free(s);
5783 }
5784 }
5785
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005786 for (i = 0; i < ga.ga_len; ++i)
5787 {
5788 match = ((char_u **)ga.ga_data)[i];
5789 s = match;
5790 e = s + STRLEN(s);
5791 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5792 {
5793 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005794 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005795 if (s < match || vim_ispathsep(*s))
5796 break;
5797 ++s;
5798 *e = NUL;
5799 mch_memmove(match, s, e - s + 1);
5800 }
5801 }
5802
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005803 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005804 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005805
5806 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005807 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005808 remove_duplicates(&ga);
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 *file = ga.ga_data;
5811 *num_file = ga.ga_len;
5812 return OK;
5813}
5814
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005815/*
5816 * Expand loadplugin names:
5817 * 'packpath'/pack/ * /opt/{pat}
5818 */
5819 static int
5820ExpandPackAddDir(
5821 char_u *pat,
5822 int *num_file,
5823 char_u ***file)
5824{
5825 char_u *s;
5826 char_u *e;
5827 char_u *match;
5828 garray_T ga;
5829 int i;
5830 int pat_len;
5831
5832 *num_file = 0;
5833 *file = NULL;
5834 pat_len = (int)STRLEN(pat);
5835 ga_init2(&ga, (int)sizeof(char *), 10);
5836
5837 s = alloc((unsigned)(pat_len + 26));
5838 if (s == NULL)
5839 {
5840 ga_clear_strings(&ga);
5841 return FAIL;
5842 }
5843 sprintf((char *)s, "pack/*/opt/%s*", pat);
5844 globpath(p_pp, s, &ga, 0);
5845 vim_free(s);
5846
5847 for (i = 0; i < ga.ga_len; ++i)
5848 {
5849 match = ((char_u **)ga.ga_data)[i];
5850 s = gettail(match);
5851 e = s + STRLEN(s);
5852 mch_memmove(match, s, e - s + 1);
5853 }
5854
5855 if (ga.ga_len == 0)
5856 return FAIL;
5857
5858 /* Sort and remove duplicates which can happen when specifying multiple
5859 * directories in dirnames. */
5860 remove_duplicates(&ga);
5861
5862 *file = ga.ga_data;
5863 *num_file = ga.ga_len;
5864 return OK;
5865}
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#endif
5868
5869#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5870/*
5871 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005872 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005874 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005875globpath(
5876 char_u *path,
5877 char_u *file,
5878 garray_T *ga,
5879 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 expand_T xpc;
5882 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 int num_p;
5885 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
5887 buf = alloc(MAXPATHL);
5888 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005889 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005891 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005893
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 /* Loop over all entries in {path}. */
5895 while (*path != NUL)
5896 {
5897 /* Copy one item of the path to buf[] and concatenate the file name. */
5898 copy_option_part(&path, buf, MAXPATHL, ",");
5899 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5900 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005901# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005902 /* Using the platform's path separator (\) makes vim incorrectly
5903 * treat it as an escape character, use '/' instead. */
5904 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5905 STRCAT(buf, "/");
5906# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005910 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5911 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005913 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005915 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 for (i = 0; i < num_p; ++i)
5918 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005919 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005920 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005921 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005924
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 FreeWild(num_p, p);
5926 }
5927 }
5928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
5930 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931}
5932
5933#endif
5934
5935#if defined(FEAT_CMDHIST) || defined(PROTO)
5936
5937/*********************************
5938 * Command line history stuff *
5939 *********************************/
5940
5941/*
5942 * Translate a history character to the associated type number.
5943 */
5944 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005945hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
5947 if (c == ':')
5948 return HIST_CMD;
5949 if (c == '=')
5950 return HIST_EXPR;
5951 if (c == '@')
5952 return HIST_INPUT;
5953 if (c == '>')
5954 return HIST_DEBUG;
5955 return HIST_SEARCH; /* must be '?' or '/' */
5956}
5957
5958/*
5959 * Table of history names.
5960 * These names are used in :history and various hist...() functions.
5961 * It is sufficient to give the significant prefix of a history name.
5962 */
5963
5964static char *(history_names[]) =
5965{
5966 "cmd",
5967 "search",
5968 "expr",
5969 "input",
5970 "debug",
5971 NULL
5972};
5973
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005974#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5975/*
5976 * Function given to ExpandGeneric() to obtain the possible first
5977 * arguments of the ":history command.
5978 */
5979 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005980get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005981{
5982 static char_u compl[2] = { NUL, NUL };
5983 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005984 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005985 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5986
5987 if (idx < short_names_count)
5988 {
5989 compl[0] = (char_u)short_names[idx];
5990 return compl;
5991 }
5992 if (idx < short_names_count + history_name_count)
5993 return (char_u *)history_names[idx - short_names_count];
5994 if (idx == short_names_count + history_name_count)
5995 return (char_u *)"all";
5996 return NULL;
5997}
5998#endif
5999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000/*
6001 * init_history() - Initialize the command line history.
6002 * Also used to re-allocate the history when the size changes.
6003 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006004 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006{
6007 int newlen; /* new length of history table */
6008 histentry_T *temp;
6009 int i;
6010 int j;
6011 int type;
6012
6013 /*
6014 * If size of history table changed, reallocate it
6015 */
6016 newlen = (int)p_hi;
6017 if (newlen != hislen) /* history length changed */
6018 {
6019 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6020 {
6021 if (newlen)
6022 {
6023 temp = (histentry_T *)lalloc(
6024 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6025 if (temp == NULL) /* out of memory! */
6026 {
6027 if (type == 0) /* first one: just keep the old length */
6028 {
6029 newlen = hislen;
6030 break;
6031 }
6032 /* Already changed one table, now we can only have zero
6033 * length for all tables. */
6034 newlen = 0;
6035 type = -1;
6036 continue;
6037 }
6038 }
6039 else
6040 temp = NULL;
6041 if (newlen == 0 || temp != NULL)
6042 {
6043 if (hisidx[type] < 0) /* there are no entries yet */
6044 {
6045 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006046 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 }
6048 else if (newlen > hislen) /* array becomes bigger */
6049 {
6050 for (i = 0; i <= hisidx[type]; ++i)
6051 temp[i] = history[type][i];
6052 j = i;
6053 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006054 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 for ( ; j < hislen; ++i, ++j)
6056 temp[i] = history[type][j];
6057 }
6058 else /* array becomes smaller or 0 */
6059 {
6060 j = hisidx[type];
6061 for (i = newlen - 1; ; --i)
6062 {
6063 if (i >= 0) /* copy newest entries */
6064 temp[i] = history[type][j];
6065 else /* remove older entries */
6066 vim_free(history[type][j].hisstr);
6067 if (--j < 0)
6068 j = hislen - 1;
6069 if (j == hisidx[type])
6070 break;
6071 }
6072 hisidx[type] = newlen - 1;
6073 }
6074 vim_free(history[type]);
6075 history[type] = temp;
6076 }
6077 }
6078 hislen = newlen;
6079 }
6080}
6081
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006083clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006084{
6085 hisptr->hisnum = 0;
6086 hisptr->viminfo = FALSE;
6087 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006088 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006089}
6090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091/*
6092 * Check if command line 'str' is already in history.
6093 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6094 */
6095 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006096in_history(
6097 int type,
6098 char_u *str,
6099 int move_to_front, /* Move the entry to the front if it exists */
6100 int sep,
6101 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 int i;
6104 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006105 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 if (hisidx[type] < 0)
6108 return FALSE;
6109 i = hisidx[type];
6110 do
6111 {
6112 if (history[type][i].hisstr == NULL)
6113 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006114
6115 /* For search history, check that the separator character matches as
6116 * well. */
6117 p = history[type][i].hisstr;
6118 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006119 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006120 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {
6122 if (!move_to_front)
6123 return TRUE;
6124 last_i = i;
6125 break;
6126 }
6127 if (--i < 0)
6128 i = hislen - 1;
6129 } while (i != hisidx[type]);
6130
6131 if (last_i >= 0)
6132 {
6133 str = history[type][i].hisstr;
6134 while (i != hisidx[type])
6135 {
6136 if (++i >= hislen)
6137 i = 0;
6138 history[type][last_i] = history[type][i];
6139 last_i = i;
6140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006142 history[type][i].viminfo = FALSE;
6143 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006144 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 return TRUE;
6146 }
6147 return FALSE;
6148}
6149
6150/*
6151 * Convert history name (from table above) to its HIST_ equivalent.
6152 * When "name" is empty, return "cmd" history.
6153 * Returns -1 for unknown history name.
6154 */
6155 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006156get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 int i;
6159 int len = (int)STRLEN(name);
6160
6161 /* No argument: use current history. */
6162 if (len == 0)
6163 return hist_char2type(ccline.cmdfirstc);
6164
6165 for (i = 0; history_names[i] != NULL; ++i)
6166 if (STRNICMP(name, history_names[i], len) == 0)
6167 return i;
6168
6169 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6170 return hist_char2type(name[0]);
6171
6172 return -1;
6173}
6174
6175static int last_maptick = -1; /* last seen maptick */
6176
6177/*
6178 * Add the given string to the given history. If the string is already in the
6179 * history then it is moved to the front. "histype" may be one of he HIST_
6180 * values.
6181 */
6182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006183add_to_history(
6184 int histype,
6185 char_u *new_entry,
6186 int in_map, /* consider maptick when inside a mapping */
6187 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189 histentry_T *hisptr;
6190 int len;
6191
6192 if (hislen == 0) /* no history */
6193 return;
6194
Bram Moolenaara939e432013-11-09 05:30:26 +01006195 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6196 return;
6197
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 /*
6199 * Searches inside the same mapping overwrite each other, so that only
6200 * the last line is kept. Be careful not to remove a line that was moved
6201 * down, only lines that were added.
6202 */
6203 if (histype == HIST_SEARCH && in_map)
6204 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006205 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
6207 /* Current line is from the same mapping, remove it */
6208 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6209 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006210 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 --hisnum[histype];
6212 if (--hisidx[HIST_SEARCH] < 0)
6213 hisidx[HIST_SEARCH] = hislen - 1;
6214 }
6215 last_maptick = -1;
6216 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006217 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
6219 if (++hisidx[histype] == hislen)
6220 hisidx[histype] = 0;
6221 hisptr = &history[histype][hisidx[histype]];
6222 vim_free(hisptr->hisstr);
6223
6224 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006225 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6227 if (hisptr->hisstr != NULL)
6228 hisptr->hisstr[len + 1] = sep;
6229
6230 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006231 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006232 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (histype == HIST_SEARCH && in_map)
6234 last_maptick = maptick;
6235 }
6236}
6237
6238#if defined(FEAT_EVAL) || defined(PROTO)
6239
6240/*
6241 * Get identifier of newest history entry.
6242 * "histype" may be one of the HIST_ values.
6243 */
6244 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
6247 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6248 || hisidx[histype] < 0)
6249 return -1;
6250
6251 return history[histype][hisidx[histype]].hisnum;
6252}
6253
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 * Calculate history index from a number:
6256 * num > 0: seen as identifying number of a history entry
6257 * num < 0: relative position in history wrt newest entry
6258 * "histype" may be one of the HIST_ values.
6259 */
6260 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 int i;
6264 histentry_T *hist;
6265 int wrapped = FALSE;
6266
6267 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6268 || (i = hisidx[histype]) < 0 || num == 0)
6269 return -1;
6270
6271 hist = history[histype];
6272 if (num > 0)
6273 {
6274 while (hist[i].hisnum > num)
6275 if (--i < 0)
6276 {
6277 if (wrapped)
6278 break;
6279 i += hislen;
6280 wrapped = TRUE;
6281 }
6282 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6283 return i;
6284 }
6285 else if (-num <= hislen)
6286 {
6287 i += num + 1;
6288 if (i < 0)
6289 i += hislen;
6290 if (hist[i].hisstr != NULL)
6291 return i;
6292 }
6293 return -1;
6294}
6295
6296/*
6297 * Get a history entry by its index.
6298 * "histype" may be one of the HIST_ values.
6299 */
6300 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006301get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 idx = calc_hist_idx(histype, idx);
6304 if (idx >= 0)
6305 return history[histype][idx].hisstr;
6306 else
6307 return (char_u *)"";
6308}
6309
6310/*
6311 * Clear all entries of a history.
6312 * "histype" may be one of the HIST_ values.
6313 */
6314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 int i;
6318 histentry_T *hisptr;
6319
6320 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6321 {
6322 hisptr = history[histype];
6323 for (i = hislen; i--;)
6324 {
6325 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006326 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006327 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 }
6329 hisidx[histype] = -1; /* mark history as cleared */
6330 hisnum[histype] = 0; /* reset identifier counter */
6331 return OK;
6332 }
6333 return FAIL;
6334}
6335
6336/*
6337 * Remove all entries matching {str} from a history.
6338 * "histype" may be one of the HIST_ values.
6339 */
6340 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006341del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 regmatch_T regmatch;
6344 histentry_T *hisptr;
6345 int idx;
6346 int i;
6347 int last;
6348 int found = FALSE;
6349
6350 regmatch.regprog = NULL;
6351 regmatch.rm_ic = FALSE; /* always match case */
6352 if (hislen != 0
6353 && histype >= 0
6354 && histype < HIST_COUNT
6355 && *str != NUL
6356 && (idx = hisidx[histype]) >= 0
6357 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6358 != NULL)
6359 {
6360 i = last = idx;
6361 do
6362 {
6363 hisptr = &history[histype][i];
6364 if (hisptr->hisstr == NULL)
6365 break;
6366 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6367 {
6368 found = TRUE;
6369 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006370 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
6372 else
6373 {
6374 if (i != last)
6375 {
6376 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006377 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 }
6379 if (--last < 0)
6380 last += hislen;
6381 }
6382 if (--i < 0)
6383 i += hislen;
6384 } while (i != idx);
6385 if (history[histype][idx].hisstr == NULL)
6386 hisidx[histype] = -1;
6387 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006388 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 return found;
6390}
6391
6392/*
6393 * Remove an indexed entry from a history.
6394 * "histype" may be one of the HIST_ values.
6395 */
6396 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006397del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
6399 int i, j;
6400
6401 i = calc_hist_idx(histype, idx);
6402 if (i < 0)
6403 return FALSE;
6404 idx = hisidx[histype];
6405 vim_free(history[histype][i].hisstr);
6406
6407 /* When deleting the last added search string in a mapping, reset
6408 * last_maptick, so that the last added search string isn't deleted again.
6409 */
6410 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6411 last_maptick = -1;
6412
6413 while (i != idx)
6414 {
6415 j = (i + 1) % hislen;
6416 history[histype][i] = history[histype][j];
6417 i = j;
6418 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006419 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (--i < 0)
6421 i += hislen;
6422 hisidx[histype] = i;
6423 return TRUE;
6424}
6425
6426#endif /* FEAT_EVAL */
6427
6428#if defined(FEAT_CRYPT) || defined(PROTO)
6429/*
6430 * Very specific function to remove the value in ":set key=val" from the
6431 * history.
6432 */
6433 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006434remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 char_u *p;
6437 int i;
6438
6439 i = hisidx[HIST_CMD];
6440 if (i < 0)
6441 return;
6442 p = history[HIST_CMD][i].hisstr;
6443 if (p != NULL)
6444 for ( ; *p; ++p)
6445 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6446 {
6447 p = vim_strchr(p + 3, '=');
6448 if (p == NULL)
6449 break;
6450 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006451 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (p[i] == '\\' && p[i + 1])
6453 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006454 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 --p;
6456 }
6457}
6458#endif
6459
6460#endif /* FEAT_CMDHIST */
6461
Bram Moolenaar064154c2016-03-19 22:50:43 +01006462#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006463/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006464 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006465 * ccline and put the previous value in prev_ccline.
6466 */
6467 static struct cmdline_info *
6468get_ccline_ptr(void)
6469{
6470 if ((State & CMDLINE) == 0)
6471 return NULL;
6472 if (ccline.cmdbuff != NULL)
6473 return &ccline;
6474 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6475 return &prev_ccline;
6476 return NULL;
6477}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006478#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006479
Bram Moolenaar064154c2016-03-19 22:50:43 +01006480#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006481/*
6482 * Get the current command line in allocated memory.
6483 * Only works when the command line is being edited.
6484 * Returns NULL when something is wrong.
6485 */
6486 char_u *
6487get_cmdline_str(void)
6488{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006489 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006490
Bram Moolenaaree91c332018-09-25 22:27:35 +02006491 if (cmdline_star > 0)
6492 return NULL;
6493 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006494 if (p == NULL)
6495 return NULL;
6496 return vim_strnsave(p->cmdbuff, p->cmdlen);
6497}
6498
6499/*
6500 * Get the current command line position, counted in bytes.
6501 * Zero is the first position.
6502 * Only works when the command line is being edited.
6503 * Returns -1 when something is wrong.
6504 */
6505 int
6506get_cmdline_pos(void)
6507{
6508 struct cmdline_info *p = get_ccline_ptr();
6509
6510 if (p == NULL)
6511 return -1;
6512 return p->cmdpos;
6513}
6514
6515/*
6516 * Set the command line byte position to "pos". Zero is the first position.
6517 * Only works when the command line is being edited.
6518 * Returns 1 when failed, 0 when OK.
6519 */
6520 int
6521set_cmdline_pos(
6522 int pos)
6523{
6524 struct cmdline_info *p = get_ccline_ptr();
6525
6526 if (p == NULL)
6527 return 1;
6528
6529 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6530 * changed the command line. */
6531 if (pos < 0)
6532 new_cmdpos = 0;
6533 else
6534 new_cmdpos = pos;
6535 return 0;
6536}
6537#endif
6538
6539#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6540/*
6541 * Get the current command-line type.
6542 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6543 * Only works when the command line is being edited.
6544 * Returns NUL when something is wrong.
6545 */
6546 int
6547get_cmdline_type(void)
6548{
6549 struct cmdline_info *p = get_ccline_ptr();
6550
6551 if (p == NULL)
6552 return NUL;
6553 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006554 return
6555# ifdef FEAT_EVAL
6556 (p->input_fn) ? '@' :
6557# endif
6558 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006559 return p->cmdfirstc;
6560}
6561#endif
6562
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6564/*
6565 * Get indices "num1,num2" that specify a range within a list (not a range of
6566 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6567 * Returns OK if parsed successfully, otherwise FAIL.
6568 */
6569 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006570get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 int len;
6573 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006574 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
6576 *str = skipwhite(*str);
6577 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6578 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006579 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 *str += len;
6581 *num1 = (int)num;
6582 first = TRUE;
6583 }
6584 *str = skipwhite(*str);
6585 if (**str == ',') /* parse "to" part of range */
6586 {
6587 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006588 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (len > 0)
6590 {
6591 *num2 = (int)num;
6592 *str = skipwhite(*str + len);
6593 }
6594 else if (!first) /* no number given at all */
6595 return FAIL;
6596 }
6597 else if (first) /* only one number given */
6598 *num2 = *num1;
6599 return OK;
6600}
6601#endif
6602
6603#if defined(FEAT_CMDHIST) || defined(PROTO)
6604/*
6605 * :history command - print a history
6606 */
6607 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006608ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
6610 histentry_T *hist;
6611 int histype1 = HIST_CMD;
6612 int histype2 = HIST_CMD;
6613 int hisidx1 = 1;
6614 int hisidx2 = -1;
6615 int idx;
6616 int i, j, k;
6617 char_u *end;
6618 char_u *arg = eap->arg;
6619
6620 if (hislen == 0)
6621 {
6622 MSG(_("'history' option is zero"));
6623 return;
6624 }
6625
6626 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6627 {
6628 end = arg;
6629 while (ASCII_ISALPHA(*end)
6630 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6631 end++;
6632 i = *end;
6633 *end = NUL;
6634 histype1 = get_histtype(arg);
6635 if (histype1 == -1)
6636 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006637 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 {
6639 histype1 = 0;
6640 histype2 = HIST_COUNT-1;
6641 }
6642 else
6643 {
6644 *end = i;
6645 EMSG(_(e_trailing));
6646 return;
6647 }
6648 }
6649 else
6650 histype2 = histype1;
6651 *end = i;
6652 }
6653 else
6654 end = arg;
6655 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6656 {
6657 EMSG(_(e_trailing));
6658 return;
6659 }
6660
6661 for (; !got_int && histype1 <= histype2; ++histype1)
6662 {
6663 STRCPY(IObuff, "\n # ");
6664 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6665 MSG_PUTS_TITLE(IObuff);
6666 idx = hisidx[histype1];
6667 hist = history[histype1];
6668 j = hisidx1;
6669 k = hisidx2;
6670 if (j < 0)
6671 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6672 if (k < 0)
6673 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6674 if (idx >= 0 && j <= k)
6675 for (i = idx + 1; !got_int; ++i)
6676 {
6677 if (i == hislen)
6678 i = 0;
6679 if (hist[i].hisstr != NULL
6680 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6681 {
6682 msg_putchar('\n');
6683 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6684 hist[i].hisnum);
6685 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6686 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006687 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 else
6689 STRCAT(IObuff, hist[i].hisstr);
6690 msg_outtrans(IObuff);
6691 out_flush();
6692 }
6693 if (i == idx)
6694 break;
6695 }
6696 }
6697}
6698#endif
6699
6700#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006701/*
6702 * Buffers for history read from a viminfo file. Only valid while reading.
6703 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006704static histentry_T *viminfo_history[HIST_COUNT] =
6705 {NULL, NULL, NULL, NULL, NULL};
6706static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6707static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708static int viminfo_add_at_front = FALSE;
6709
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006710static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712/*
6713 * Translate a history type number to the associated character.
6714 */
6715 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006716hist_type2char(
6717 int type,
6718 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 if (type == HIST_CMD)
6721 return ':';
6722 if (type == HIST_SEARCH)
6723 {
6724 if (use_question)
6725 return '?';
6726 else
6727 return '/';
6728 }
6729 if (type == HIST_EXPR)
6730 return '=';
6731 return '@';
6732}
6733
6734/*
6735 * Prepare for reading the history from the viminfo file.
6736 * This allocates history arrays to store the read history lines.
6737 */
6738 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006739prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 int i;
6742 int num;
6743 int type;
6744 int len;
6745
6746 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006747 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (asklen > hislen)
6749 asklen = hislen;
6750
6751 for (type = 0; type < HIST_COUNT; ++type)
6752 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006753 /* Count the number of empty spaces in the history list. Entries read
6754 * from viminfo previously are also considered empty. If there are
6755 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006757 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 num++;
6759 len = asklen;
6760 if (num > len)
6761 len = num;
6762 if (len <= 0)
6763 viminfo_history[type] = NULL;
6764 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006765 viminfo_history[type] = (histentry_T *)lalloc(
6766 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (viminfo_history[type] == NULL)
6768 len = 0;
6769 viminfo_hislen[type] = len;
6770 viminfo_hisidx[type] = 0;
6771 }
6772}
6773
6774/*
6775 * Accept a line from the viminfo, store it in the history array when it's
6776 * new.
6777 */
6778 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006779read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 int type;
6782 long_u len;
6783 char_u *val;
6784 char_u *p;
6785
6786 type = hist_char2type(virp->vir_line[0]);
6787 if (viminfo_hisidx[type] < viminfo_hislen[type])
6788 {
6789 val = viminfo_readstring(virp, 1, TRUE);
6790 if (val != NULL && *val != NUL)
6791 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006792 int sep = (*val == ' ' ? NUL : *val);
6793
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006795 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
6797 /* Need to re-allocate to append the separator byte. */
6798 len = STRLEN(val);
6799 p = lalloc(len + 2, TRUE);
6800 if (p != NULL)
6801 {
6802 if (type == HIST_SEARCH)
6803 {
6804 /* Search entry: Move the separator from the first
6805 * column to after the NUL. */
6806 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006807 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 }
6809 else
6810 {
6811 /* Not a search entry: No separator in the viminfo
6812 * file, add a NUL separator. */
6813 mch_memmove(p, val, (size_t)len + 1);
6814 p[len + 1] = NUL;
6815 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006816 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6817 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006818 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6819 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006820 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822 }
6823 }
6824 vim_free(val);
6825 }
6826 return viminfo_readline(virp);
6827}
6828
Bram Moolenaar07219f92013-04-14 23:19:36 +02006829/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006830 * Accept a new style history line from the viminfo, store it in the history
6831 * array when it's new.
6832 */
6833 void
6834handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006835 garray_T *values,
6836 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006837{
6838 int type;
6839 long_u len;
6840 char_u *val;
6841 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006842 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006843
6844 /* Check the format:
6845 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006846 if (values->ga_len < 4
6847 || vp[0].bv_type != BVAL_NR
6848 || vp[1].bv_type != BVAL_NR
6849 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6850 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006851 return;
6852
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006853 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006854 if (type >= HIST_COUNT)
6855 return;
6856 if (viminfo_hisidx[type] < viminfo_hislen[type])
6857 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006858 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006859 if (val != NULL && *val != NUL)
6860 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006861 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6862 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006863 int idx;
6864 int overwrite = FALSE;
6865
6866 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6867 {
6868 /* If lines were written by an older Vim we need to avoid
6869 * getting duplicates. See if the entry already exists. */
6870 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6871 {
6872 p = viminfo_history[type][idx].hisstr;
6873 if (STRCMP(val, p) == 0
6874 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6875 {
6876 overwrite = TRUE;
6877 break;
6878 }
6879 }
6880
6881 if (!overwrite)
6882 {
6883 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006884 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006885 p = lalloc(len + 2, TRUE);
6886 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006887 else
6888 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006889 if (p != NULL)
6890 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006891 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006892 if (!overwrite)
6893 {
6894 mch_memmove(p, val, (size_t)len + 1);
6895 /* Put the separator after the NUL. */
6896 p[len + 1] = sep;
6897 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006898 viminfo_history[type][idx].hisnum = 0;
6899 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006900 viminfo_hisidx[type]++;
6901 }
6902 }
6903 }
6904 }
6905 }
6906}
6907
6908/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006909 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006910 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006911 static void
6912concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 int idx;
6915 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006916
6917 idx = hisidx[type] + viminfo_hisidx[type];
6918 if (idx >= hislen)
6919 idx -= hislen;
6920 else if (idx < 0)
6921 idx = hislen - 1;
6922 if (viminfo_add_at_front)
6923 hisidx[type] = idx;
6924 else
6925 {
6926 if (hisidx[type] == -1)
6927 hisidx[type] = hislen - 1;
6928 do
6929 {
6930 if (history[type][idx].hisstr != NULL
6931 || history[type][idx].viminfo)
6932 break;
6933 if (++idx == hislen)
6934 idx = 0;
6935 } while (idx != hisidx[type]);
6936 if (idx != hisidx[type] && --idx < 0)
6937 idx = hislen - 1;
6938 }
6939 for (i = 0; i < viminfo_hisidx[type]; i++)
6940 {
6941 vim_free(history[type][idx].hisstr);
6942 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6943 history[type][idx].viminfo = TRUE;
6944 history[type][idx].time_set = viminfo_history[type][i].time_set;
6945 if (--idx < 0)
6946 idx = hislen - 1;
6947 }
6948 idx += 1;
6949 idx %= hislen;
6950 for (i = 0; i < viminfo_hisidx[type]; i++)
6951 {
6952 history[type][idx++].hisnum = ++hisnum[type];
6953 idx %= hislen;
6954 }
6955}
6956
6957#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6958 static int
6959#ifdef __BORLANDC__
6960_RTLENTRYF
6961#endif
6962sort_hist(const void *s1, const void *s2)
6963{
6964 histentry_T *p1 = *(histentry_T **)s1;
6965 histentry_T *p2 = *(histentry_T **)s2;
6966
6967 if (p1->time_set < p2->time_set) return -1;
6968 if (p1->time_set > p2->time_set) return 1;
6969 return 0;
6970}
6971#endif
6972
6973/*
6974 * Merge history lines from viminfo and lines typed in this Vim based on the
6975 * timestamp;
6976 */
6977 static void
6978merge_history(int type)
6979{
6980 int max_len;
6981 histentry_T **tot_hist;
6982 histentry_T *new_hist;
6983 int i;
6984 int len;
6985
6986 /* Make one long list with all entries. */
6987 max_len = hislen + viminfo_hisidx[type];
6988 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6989 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6990 if (tot_hist == NULL || new_hist == NULL)
6991 {
6992 vim_free(tot_hist);
6993 vim_free(new_hist);
6994 return;
6995 }
6996 for (i = 0; i < viminfo_hisidx[type]; i++)
6997 tot_hist[i] = &viminfo_history[type][i];
6998 len = i;
6999 for (i = 0; i < hislen; i++)
7000 if (history[type][i].hisstr != NULL)
7001 tot_hist[len++] = &history[type][i];
7002
7003 /* Sort the list on timestamp. */
7004 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
7005
7006 /* Keep the newest ones. */
7007 for (i = 0; i < hislen; i++)
7008 {
7009 if (i < len)
7010 {
7011 new_hist[i] = *tot_hist[i];
7012 tot_hist[i]->hisstr = NULL;
7013 if (new_hist[i].hisnum == 0)
7014 new_hist[i].hisnum = ++hisnum[type];
7015 }
7016 else
7017 clear_hist_entry(&new_hist[i]);
7018 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007019 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007020
7021 /* Free what is not kept. */
7022 for (i = 0; i < viminfo_hisidx[type]; i++)
7023 vim_free(viminfo_history[type][i].hisstr);
7024 for (i = 0; i < hislen; i++)
7025 vim_free(history[type][i].hisstr);
7026 vim_free(history[type]);
7027 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007028 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007029}
7030
7031/*
7032 * Finish reading history lines from viminfo. Not used when writing viminfo.
7033 */
7034 void
7035finish_viminfo_history(vir_T *virp)
7036{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007038 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
7040 for (type = 0; type < HIST_COUNT; ++type)
7041 {
7042 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007043 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007044
7045 if (merge)
7046 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007048 concat_history(type);
7049
Bram Moolenaard23a8232018-02-10 18:45:26 +01007050 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007051 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
7053}
7054
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007055/*
7056 * Write history to viminfo file in "fp".
7057 * When "merge" is TRUE merge history lines with a previously read viminfo
7058 * file, data is in viminfo_history[].
7059 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007062write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 int i;
7065 int type;
7066 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007067 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
7069 init_history();
7070 if (hislen == 0)
7071 return;
7072 for (type = 0; type < HIST_COUNT; ++type)
7073 {
7074 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7075 if (num_saved == 0)
7076 continue;
7077 if (num_saved < 0) /* Use default */
7078 num_saved = hislen;
7079 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7080 type == HIST_CMD ? _("Command Line") :
7081 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007082 type == HIST_EXPR ? _("Expression") :
7083 type == HIST_INPUT ? _("Input Line") :
7084 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (num_saved > hislen)
7086 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007087
7088 /*
7089 * Merge typed and viminfo history:
7090 * round 1: history of typed commands.
7091 * round 2: history from recently read viminfo.
7092 */
7093 for (round = 1; round <= 2; ++round)
7094 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007095 if (round == 1)
7096 /* start at newest entry, somewhere in the list */
7097 i = hisidx[type];
7098 else if (viminfo_hisidx[type] > 0)
7099 /* start at newest entry, first in the list */
7100 i = 0;
7101 else
7102 /* empty list */
7103 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007104 if (i >= 0)
7105 while (num_saved > 0
7106 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007108 char_u *p;
7109 time_t timestamp;
7110 int c = NUL;
7111
7112 if (round == 1)
7113 {
7114 p = history[type][i].hisstr;
7115 timestamp = history[type][i].time_set;
7116 }
7117 else
7118 {
7119 p = viminfo_history[type] == NULL ? NULL
7120 : viminfo_history[type][i].hisstr;
7121 timestamp = viminfo_history[type] == NULL ? 0
7122 : viminfo_history[type][i].time_set;
7123 }
7124
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007125 if (p != NULL && (round == 2
7126 || !merge
7127 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007129 --num_saved;
7130 fputc(hist_type2char(type, TRUE), fp);
7131 /* For the search history: put the separator in the
7132 * second column; use a space if there isn't one. */
7133 if (type == HIST_SEARCH)
7134 {
7135 c = p[STRLEN(p) + 1];
7136 putc(c == NUL ? ' ' : c, fp);
7137 }
7138 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007139
7140 {
7141 char cbuf[NUMBUFLEN];
7142
7143 /* New style history with a bar line. Format:
7144 * |{bartype},{histtype},{timestamp},{separator},"text" */
7145 if (c == NUL)
7146 cbuf[0] = NUL;
7147 else
7148 sprintf(cbuf, "%d", c);
7149 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7150 type, (long)timestamp, cbuf);
7151 barline_writestring(fp, p, LSIZE - 20);
7152 putc('\n', fp);
7153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007155 if (round == 1)
7156 {
7157 /* Decrement index, loop around and stop when back at
7158 * the start. */
7159 if (--i < 0)
7160 i = hislen - 1;
7161 if (i == hisidx[type])
7162 break;
7163 }
7164 else
7165 {
7166 /* Increment index. Stop at the end in the while. */
7167 ++i;
7168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007170 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007171 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007172 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007173 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007174 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007175 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
7177}
7178#endif /* FEAT_VIMINFO */
7179
7180#if defined(FEAT_FKMAP) || defined(PROTO)
7181/*
7182 * Write a character at the current cursor+offset position.
7183 * It is directly written into the command buffer block.
7184 */
7185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7189 {
7190 EMSG(_("E198: cmd_pchar beyond the command length"));
7191 return;
7192 }
7193 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7194 ccline.cmdbuff[ccline.cmdlen] = NUL;
7195}
7196
7197 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007198cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7201 {
7202 /* EMSG(_("cmd_gchar beyond the command length")); */
7203 return NUL;
7204 }
7205 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7206}
7207#endif
7208
7209#if defined(FEAT_CMDWIN) || defined(PROTO)
7210/*
7211 * Open a window on the current command line and history. Allow editing in
7212 * the window. Returns when the window is closed.
7213 * Returns:
7214 * CR if the command is to be executed
7215 * Ctrl_C if it is to be abandoned
7216 * K_IGNORE if editing continues
7217 */
7218 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007219open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007221 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007223 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 win_T *wp;
7225 int i;
7226 linenr_T lnum;
7227 int histtype;
7228 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 int save_restart_edit = restart_edit;
7230 int save_State = State;
7231 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007232#ifdef FEAT_RIGHTLEFT
7233 int save_cmdmsg_rl = cmdmsg_rl;
7234#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007235#ifdef FEAT_FOLDING
7236 int save_KeyTyped;
7237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
7239 /* Can't do this recursively. Can't do it when typing a password. */
7240 if (cmdwin_type != 0
7241# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7242 || cmdline_star > 0
7243# endif
7244 )
7245 {
7246 beep_flush();
7247 return K_IGNORE;
7248 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007249 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251 /* Save current window sizes. */
7252 win_size_save(&winsizes);
7253
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007255 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007256
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007257 /* don't use a new tab page */
7258 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007259 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 /* Create a window for the command-line buffer. */
7262 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7263 {
7264 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007265 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 return K_IGNORE;
7267 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007268 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007271 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007272 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007275#ifdef FEAT_FOLDING
7276 curwin->w_p_fen = FALSE;
7277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007279 curwin->w_p_rl = cmdmsg_rl;
7280 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007282 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007285 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007286 /* But don't allow switching to another buffer. */
7287 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288
Bram Moolenaar46152342005-09-07 21:18:43 +00007289 /* Showing the prompt may have set need_wait_return, reset it. */
7290 need_wait_return = FALSE;
7291
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007292 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7294 {
7295 if (p_wc == TAB)
7296 {
7297 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7298 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7299 }
7300 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7301 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007302 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007304 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7305 * sets 'textwidth' to 78). */
7306 curbuf->b_p_tw = 0;
7307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 /* Fill the buffer with the history. */
7309 init_history();
7310 if (hislen > 0)
7311 {
7312 i = hisidx[histtype];
7313 if (i >= 0)
7314 {
7315 lnum = 0;
7316 do
7317 {
7318 if (++i == hislen)
7319 i = 0;
7320 if (history[histtype][i].hisstr != NULL)
7321 ml_append(lnum++, history[histtype][i].hisstr,
7322 (colnr_T)0, FALSE);
7323 }
7324 while (i != hisidx[histtype]);
7325 }
7326 }
7327
7328 /* Replace the empty last line with the current command-line and put the
7329 * cursor there. */
7330 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7331 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7332 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007333 changed_line_abv_curs();
7334 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007335 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 /* No Ex mode here! */
7338 exmode_active = 0;
7339
7340 State = NORMAL;
7341# ifdef FEAT_MOUSE
7342 setmouse();
7343# endif
7344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007346 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007347 if (restart_edit != 0) /* autocmd with ":startinsert" */
7348 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
7350 i = RedrawingDisabled;
7351 RedrawingDisabled = 0;
7352
7353 /*
7354 * Call the main loop until <CR> or CTRL-C is typed.
7355 */
7356 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007357 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
7359 RedrawingDisabled = i;
7360
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007361# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007362 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007363# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007364
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007366 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007367
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007368# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007369 /* Restore KeyTyped in case it is modified by autocommands */
7370 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371# endif
7372
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 exmode_active = save_exmode;
7375
Bram Moolenaarf9821062008-06-20 16:31:07 +00007376 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007378 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 {
7380 cmdwin_result = Ctrl_C;
7381 EMSG(_("E199: Active window or buffer deleted"));
7382 }
7383 else
7384 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007385# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 /* autocmds may abort script processing */
7387 if (aborting() && cmdwin_result != K_IGNORE)
7388 cmdwin_result = Ctrl_C;
7389# endif
7390 /* Set the new command line from the cmdline buffer. */
7391 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007392 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007394 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7395
7396 if (histtype == HIST_CMD)
7397 {
7398 /* Execute the command directly. */
7399 ccline.cmdbuff = vim_strsave((char_u *)p);
7400 cmdwin_result = CAR;
7401 }
7402 else
7403 {
7404 /* First need to cancel what we were doing. */
7405 ccline.cmdbuff = NULL;
7406 stuffcharReadbuff(':');
7407 stuffReadbuff((char_u *)p);
7408 stuffcharReadbuff(CAR);
7409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 }
7411 else if (cmdwin_result == K_XF2) /* :qa typed */
7412 {
7413 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7414 cmdwin_result = CAR;
7415 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007416 else if (cmdwin_result == Ctrl_C)
7417 {
7418 /* :q or :close, don't execute any command
7419 * and don't modify the cmd window. */
7420 ccline.cmdbuff = NULL;
7421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 else
7423 ccline.cmdbuff = vim_strsave(ml_get_curline());
7424 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007425 {
7426 ccline.cmdbuff = vim_strsave((char_u *)"");
7427 ccline.cmdlen = 0;
7428 ccline.cmdbufflen = 1;
7429 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 else
7433 {
7434 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7435 ccline.cmdbufflen = ccline.cmdlen + 1;
7436 ccline.cmdpos = curwin->w_cursor.col;
7437 if (ccline.cmdpos > ccline.cmdlen)
7438 ccline.cmdpos = ccline.cmdlen;
7439 if (cmdwin_result == K_IGNORE)
7440 {
7441 set_cmdspos_cursor();
7442 redrawcmd();
7443 }
7444 }
7445
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007447 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007448# ifdef FEAT_CONCEAL
7449 /* Avoid command-line window first character being concealed. */
7450 curwin->w_p_cole = 0;
7451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007453 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 win_goto(old_curwin);
7455 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007456
7457 /* win_close() may have already wiped the buffer when 'bh' is
7458 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007459 if (bufref_valid(&bufref))
7460 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 /* Restore window sizes. */
7463 win_size_restore(&winsizes);
7464
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007465 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467
7468 ga_clear(&winsizes);
7469 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007470# ifdef FEAT_RIGHTLEFT
7471 cmdmsg_rl = save_cmdmsg_rl;
7472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
7474 State = save_State;
7475# ifdef FEAT_MOUSE
7476 setmouse();
7477# endif
7478
7479 return cmdwin_result;
7480}
7481#endif /* FEAT_CMDWIN */
7482
7483/*
7484 * Used for commands that either take a simple command string argument, or:
7485 * cmd << endmarker
7486 * {script}
7487 * endmarker
7488 * Returns a pointer to allocated memory with {script} or NULL.
7489 */
7490 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007491script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
7493 char_u *theline;
7494 char *end_pattern = NULL;
7495 char dot[] = ".";
7496 garray_T ga;
7497
7498 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7499 return NULL;
7500
7501 ga_init2(&ga, 1, 0x400);
7502
7503 if (cmd[2] != NUL)
7504 end_pattern = (char *)skipwhite(cmd + 2);
7505 else
7506 end_pattern = dot;
7507
7508 for (;;)
7509 {
7510 theline = eap->getline(
7511#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007512 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513#endif
7514 NUL, eap->cookie, 0);
7515
7516 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007517 {
7518 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
7522 ga_concat(&ga, theline);
7523 ga_append(&ga, '\n');
7524 vim_free(theline);
7525 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007526 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527
7528 return (char_u *)ga.ga_data;
7529}