blob: e1642d1d480a08bc8f5c1e70f0d0df95d06193a0 [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 Moolenaar66216052017-12-16 16:33:44 +0100778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 * getcmdline() - accept a command line starting with firstc.
780 *
781 * firstc == ':' get ":" command line.
782 * firstc == '/' or '?' get search pattern
783 * firstc == '=' get expression
784 * firstc == '@' get text for input() function
785 * firstc == '>' get text for debug mode
786 * firstc == NUL get text for :insert command
787 * firstc == -1 like NUL, and break on CTRL-C
788 *
789 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
790 * command line.
791 *
792 * Careful: getcmdline() can be called recursively!
793 *
794 * Return pointer to allocated string if there is a commandline, NULL
795 * otherwise.
796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100798getcmdline(
799 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200800 long count, // only used for incremental search
801 int indent) // indent for inside conditionals
802{
803 return getcmdline_int(firstc, count, indent, TRUE);
804}
805
806 static char_u *
807getcmdline_int(
808 int firstc,
809 long count UNUSED, // only used for incremental search
810 int indent, // indent for inside conditionals
811 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 int c;
814 int i;
815 int j;
816 int gotesc = FALSE; /* TRUE when <ESC> just typed */
817 int do_abbr; /* when TRUE check for abbr. */
818#ifdef FEAT_CMDHIST
819 char_u *lookfor = NULL; /* string to match */
820 int hiscnt; /* current history line in use */
821 int histype; /* history type to be used */
822#endif
823#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200824 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#endif
826 int did_wild_list = FALSE; /* did wild_list() recently */
827 int wim_index = 0; /* index in wim_flags[] */
828 int res;
829 int save_msg_scroll = msg_scroll;
830 int save_State = State; /* remember State when called */
831 int some_key_typed = FALSE; /* one of the keys was typed */
832#ifdef FEAT_MOUSE
833 /* mouse drag and release events are ignored, unless they are
834 * preceded with a mouse down event */
835 int ignore_drag_release = TRUE;
836#endif
837#ifdef FEAT_EVAL
838 int break_ctrl_c = FALSE;
839#endif
840 expand_T xpc;
841 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000842 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200843 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200844 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
Bram Moolenaar438d1762018-09-30 17:11:48 +0200846 if (ccline.cmdbuff != NULL)
847 {
848 // Being called recursively. Since ccline is global, we need to save
849 // the current buffer and restore it when returning.
850 save_cmdline(&save_ccline);
851 did_save_ccline = TRUE;
852 }
853 if (init_ccline)
854 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
855
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#ifdef FEAT_EVAL
857 if (firstc == -1)
858 {
859 firstc = NUL;
860 break_ctrl_c = TRUE;
861 }
862#endif
863#ifdef FEAT_RIGHTLEFT
864 /* start without Hebrew mapping for a command line */
865 if (firstc == ':' || firstc == '=' || firstc == '>')
866 cmd_hkmap = 0;
867#endif
868
869 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200872 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874
875 /*
876 * set some variables for redrawcmd()
877 */
878 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000879 ccline.cmdindent = (firstc > 0 ? indent : 0);
880
881 /* alloc initial ccline.cmdbuff */
882 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200884 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 ccline.cmdlen = ccline.cmdpos = 0;
886 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100887 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000889 /* autoindent for :insert and :append */
890 if (firstc <= 0)
891 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200892 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000893 ccline.cmdbuff[indent] = NUL;
894 ccline.cmdpos = indent;
895 ccline.cmdspos = indent;
896 ccline.cmdlen = indent;
897 }
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000900 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902#ifdef FEAT_RIGHTLEFT
903 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
904 && (firstc == '/' || firstc == '?'))
905 cmdmsg_rl = TRUE;
906 else
907 cmdmsg_rl = FALSE;
908#endif
909
910 redir_off = TRUE; /* don't redirect the typed command */
911 if (!cmd_silent)
912 {
913 i = msg_scrolled;
914 msg_scrolled = 0; /* avoid wait_return message */
915 gotocmdline(TRUE);
916 msg_scrolled += i;
917 redrawcmdprompt(); /* draw prompt or indent */
918 set_cmdspos();
919 }
920 xpc.xp_context = EXPAND_NOTHING;
921 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000922#ifndef BACKSLASH_IN_FILENAME
923 xpc.xp_shell = FALSE;
924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000926#if defined(FEAT_EVAL)
927 if (ccline.input_fn)
928 {
929 xpc.xp_context = ccline.xp_context;
930 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000931# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000932 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000933# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000934 }
935#endif
936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 /*
938 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
939 * doing ":@0" when register 0 doesn't contain a CR.
940 */
941 msg_scroll = FALSE;
942
943 State = CMDLINE;
944
945 if (firstc == '/' || firstc == '?' || firstc == '@')
946 {
947 /* Use ":lmap" mappings for search pattern and input(). */
948 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
949 b_im_ptr = &curbuf->b_p_iminsert;
950 else
951 b_im_ptr = &curbuf->b_p_imsearch;
952 if (*b_im_ptr == B_IMODE_LMAP)
953 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100954#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 im_set_active(*b_im_ptr == B_IMODE_IM);
956#endif
957 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100958#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 else if (p_imcmdline)
960 im_set_active(TRUE);
961#endif
962
963#ifdef FEAT_MOUSE
964 setmouse();
965#endif
966#ifdef CURSOR_SHAPE
967 ui_cursor_shape(); /* may show different cursor shape */
968#endif
969
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000970 /* When inside an autocommand for writing "exiting" may be set and
971 * terminal mode set to cooked. Need to set raw mode here then. */
972 settmode(TMODE_RAW);
973
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200974 /* Trigger CmdlineEnter autocommands. */
975 cmdline_type = firstc == NUL ? '-' : firstc;
976 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#ifdef FEAT_CMDHIST
979 init_history();
980 hiscnt = hislen; /* set hiscnt to impossible history value */
981 histype = hist_char2type(firstc);
982#endif
983
984#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000985 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#endif
987
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200988 /* If something above caused an error, reset the flags, we do want to type
989 * and execute commands. Display may be messed up a bit. */
990 if (did_emsg)
991 redrawcmd();
992 did_emsg = FALSE;
993 got_int = FALSE;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 /*
996 * Collect the command string, handling editing keys.
997 */
998 for (;;)
999 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001000 redir_off = TRUE; /* Don't redirect the typed command.
1001 Repeated, because a ":redir" inside
1002 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#ifdef USE_ON_FLY_SCROLL
1004 dont_scroll = FALSE; /* allow scrolling here */
1005#endif
1006 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1007
Bram Moolenaar72532d32018-04-07 19:09:09 +02001008 did_emsg = FALSE; /* There can't really be a reason why an error
1009 that occurs while typing a command should
1010 cause the command not to be executed. */
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001013
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001014 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1015 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001016 do
1017 {
1018 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001019 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (KeyTyped)
1022 {
1023 some_key_typed = TRUE;
1024#ifdef FEAT_RIGHTLEFT
1025 if (cmd_hkmap)
1026 c = hkmap(c);
1027# ifdef FEAT_FKMAP
1028 if (cmd_fkmap)
1029 c = cmdl_fkmap(c);
1030# endif
1031 if (cmdmsg_rl && !KeyStuffed)
1032 {
1033 /* Invert horizontal movements and operations. Only when
1034 * typed by the user directly, not when the result of a
1035 * mapping. */
1036 switch (c)
1037 {
1038 case K_RIGHT: c = K_LEFT; break;
1039 case K_S_RIGHT: c = K_S_LEFT; break;
1040 case K_C_RIGHT: c = K_C_LEFT; break;
1041 case K_LEFT: c = K_RIGHT; break;
1042 case K_S_LEFT: c = K_S_RIGHT; break;
1043 case K_C_LEFT: c = K_C_RIGHT; break;
1044 }
1045 }
1046#endif
1047 }
1048
1049 /*
1050 * Ignore got_int when CTRL-C was typed here.
1051 * Don't ignore it in :global, we really need to break then, e.g., for
1052 * ":g/pat/normal /pat" (without the <CR>).
1053 * Don't ignore it for the input() function.
1054 */
1055 if ((c == Ctrl_C
1056#ifdef UNIX
1057 || c == intr_char
1058#endif
1059 )
1060#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1061 && firstc != '@'
1062#endif
1063#ifdef FEAT_EVAL
1064 && !break_ctrl_c
1065#endif
1066 && !global_busy)
1067 got_int = FALSE;
1068
1069#ifdef FEAT_CMDHIST
1070 /* free old command line when finished moving around in the history
1071 * list */
1072 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001073 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001074 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 && c != K_PAGEDOWN && c != K_PAGEUP
1076 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001077 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001079 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080#endif
1081
1082 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001083 * When there are matching completions to select <S-Tab> works like
1084 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001086 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 c = Ctrl_P;
1088
1089#ifdef FEAT_WILDMENU
1090 /* Special translations for 'wildmenu' */
1091 if (did_wild_list && p_wmnu)
1092 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001093 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001095 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 c = Ctrl_N;
1097 }
1098 /* Hitting CR after "emenu Name.": complete submenu */
1099 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1100 && ccline.cmdpos > 1
1101 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1102 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1103 && (c == '\n' || c == '\r' || c == K_KENTER))
1104 c = K_DOWN;
1105#endif
1106
1107 /* free expanded names when finished walking through matches */
1108 if (xpc.xp_numfiles != -1
1109 && !(c == p_wc && KeyTyped) && c != p_wcm
1110 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1111 && c != Ctrl_L)
1112 {
1113 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1114 did_wild_list = FALSE;
1115#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001116 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117#endif
1118 xpc.xp_context = EXPAND_NOTHING;
1119 wim_index = 0;
1120#ifdef FEAT_WILDMENU
1121 if (p_wmnu && wild_menu_showing != 0)
1122 {
1123 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001124 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001125
1126 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001127 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 if (wild_menu_showing == WM_SCROLLED)
1130 {
1131 /* Entered command line, move it up */
1132 cmdline_row--;
1133 redrawcmd();
1134 }
1135 else if (save_p_ls != -1)
1136 {
1137 /* restore 'laststatus' and 'winminheight' */
1138 p_ls = save_p_ls;
1139 p_wmh = save_p_wmh;
1140 last_status(FALSE);
1141 update_screen(VALID); /* redraw the screen NOW */
1142 redrawcmd();
1143 save_p_ls = -1;
1144 }
1145 else
1146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 redraw_statuslines();
1149 }
1150 KeyTyped = skt;
1151 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001152 if (ccline.input_fn)
1153 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155#endif
1156 }
1157
1158#ifdef FEAT_WILDMENU
1159 /* Special translations for 'wildmenu' */
1160 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1161 {
1162 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001163 if (c == K_DOWN && ccline.cmdpos > 0
1164 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001166 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168 /* Hitting <Up>: Remove one submenu name in front of the
1169 * cursor */
1170 int found = FALSE;
1171
1172 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1173 i = 0;
1174 while (--j > 0)
1175 {
1176 /* check for start of menu name */
1177 if (ccline.cmdbuff[j] == ' '
1178 && ccline.cmdbuff[j - 1] != '\\')
1179 {
1180 i = j + 1;
1181 break;
1182 }
1183 /* check for start of submenu name */
1184 if (ccline.cmdbuff[j] == '.'
1185 && ccline.cmdbuff[j - 1] != '\\')
1186 {
1187 if (found)
1188 {
1189 i = j + 1;
1190 break;
1191 }
1192 else
1193 found = TRUE;
1194 }
1195 }
1196 if (i > 0)
1197 cmdline_del(i);
1198 c = p_wc;
1199 xpc.xp_context = EXPAND_NOTHING;
1200 }
1201 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001202 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001203 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001204 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
1206 char_u upseg[5];
1207
1208 upseg[0] = PATHSEP;
1209 upseg[1] = '.';
1210 upseg[2] = '.';
1211 upseg[3] = PATHSEP;
1212 upseg[4] = NUL;
1213
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001214 if (c == K_DOWN
1215 && ccline.cmdpos > 0
1216 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1217 && (ccline.cmdpos < 3
1218 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1220 {
1221 /* go down a directory */
1222 c = p_wc;
1223 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001224 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 /* If in a direct ancestor, strip off one ../ to go down */
1227 int found = FALSE;
1228
1229 j = ccline.cmdpos;
1230 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1231 while (--j > i)
1232 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001233#ifdef FEAT_MBYTE
1234 if (has_mbyte)
1235 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (vim_ispathsep(ccline.cmdbuff[j]))
1238 {
1239 found = TRUE;
1240 break;
1241 }
1242 }
1243 if (found
1244 && ccline.cmdbuff[j - 1] == '.'
1245 && ccline.cmdbuff[j - 2] == '.'
1246 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1247 {
1248 cmdline_del(j - 2);
1249 c = p_wc;
1250 }
1251 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001252 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
1254 /* go up a directory */
1255 int found = FALSE;
1256
1257 j = ccline.cmdpos - 1;
1258 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1259 while (--j > i)
1260 {
1261#ifdef FEAT_MBYTE
1262 if (has_mbyte)
1263 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1264#endif
1265 if (vim_ispathsep(ccline.cmdbuff[j])
1266#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001267 && vim_strchr((char_u *)" *?[{`$%#",
1268 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269#endif
1270 )
1271 {
1272 if (found)
1273 {
1274 i = j + 1;
1275 break;
1276 }
1277 else
1278 found = TRUE;
1279 }
1280 }
1281
1282 if (!found)
1283 j = i;
1284 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1285 j += 4;
1286 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1287 && j == i)
1288 j += 3;
1289 else
1290 j = 0;
1291 if (j > 0)
1292 {
1293 /* TODO this is only for DOS/UNIX systems - need to put in
1294 * machine-specific stuff here and in upseg init */
1295 cmdline_del(j);
1296 put_on_cmdline(upseg + 1, 3, FALSE);
1297 }
1298 else if (ccline.cmdpos > i)
1299 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001300
1301 /* Now complete in the new directory. Set KeyTyped in case the
1302 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001304 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307
1308#endif /* FEAT_WILDMENU */
1309
1310 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1311 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1312 if (c == Ctrl_BSL)
1313 {
1314 ++no_mapping;
1315 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001316 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 --no_mapping;
1318 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001319 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1320 * is in a mapping. */
1321 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001322 || (ccline.cmdfirstc == '=' && KeyTyped)
1323#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001324 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001325#endif
1326 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
1328 vungetc(c);
1329 c = Ctrl_BSL;
1330 }
1331#ifdef FEAT_EVAL
1332 else if (c == 'e')
1333 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001334 char_u *p = NULL;
1335 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
1337 /*
1338 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001339 * Need to save and restore the current command line, to be
1340 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
1342 if (ccline.cmdpos == ccline.cmdlen)
1343 new_cmdpos = 99999; /* keep it at the end */
1344 else
1345 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 if (c == '=')
1349 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001350 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001351 * to avoid nasty things like going to another buffer when
1352 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001353 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001355 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001356
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001357 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001359 len = (int)STRLEN(p);
1360 if (realloc_cmdbuff(len + 1) == OK)
1361 {
1362 ccline.cmdlen = len;
1363 STRCPY(ccline.cmdbuff, p);
1364 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001366 /* Restore the cursor or use the position set with
1367 * set_cmdline_pos(). */
1368 if (new_cmdpos > ccline.cmdlen)
1369 ccline.cmdpos = ccline.cmdlen;
1370 else
1371 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001373 KeyTyped = FALSE; /* Don't do p_wc completion. */
1374 redrawcmd();
1375 goto cmdline_changed;
1376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 }
1379 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001380 got_int = FALSE; /* don't abandon the command line */
1381 did_emsg = FALSE;
1382 emsg_on_display = FALSE;
1383 redrawcmd();
1384 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386#endif
1387 else
1388 {
1389 if (c == Ctrl_G && p_im && restart_edit == 0)
1390 restart_edit = 'a';
1391 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1392 in history */
1393 goto returncmd; /* back to Normal mode */
1394 }
1395 }
1396
1397#ifdef FEAT_CMDWIN
1398 if (c == cedit_key || c == K_CMDWIN)
1399 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001400 if (ex_normal_busy == 0 && got_int == FALSE)
1401 {
1402 /*
1403 * Open a window to edit the command line (and history).
1404 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001405 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001406 some_key_typed = TRUE;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409# ifdef FEAT_DIGRAPHS
1410 else
1411# endif
1412#endif
1413#ifdef FEAT_DIGRAPHS
1414 c = do_digraph(c);
1415#endif
1416
1417 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1418 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1419 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001420 /* In Ex mode a backslash escapes a newline. */
1421 if (exmode_active
1422 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001423 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001424 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001425 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001427 if (c == K_KENTER)
1428 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001430 else
1431 {
1432 gotesc = FALSE; /* Might have typed ESC previously, don't
1433 truncate the cmdline now. */
1434 if (ccheck_abbr(c + ABBR_OFF))
1435 goto cmdline_changed;
1436 if (!cmd_silent)
1437 {
1438 windgoto(msg_row, 0);
1439 out_flush();
1440 }
1441 break;
1442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444
1445 /*
1446 * Completion for 'wildchar' or 'wildcharm' key.
1447 * - hitting <ESC> twice means: abandon command line.
1448 * - wildcard expansion is only done when the 'wildchar' key is really
1449 * typed, not when it comes from a macro
1450 */
1451 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1452 {
1453 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1454 {
1455 /* if 'wildmode' contains "list" may still need to list */
1456 if (xpc.xp_numfiles > 1
1457 && !did_wild_list
1458 && (wim_flags[wim_index] & WIM_LIST))
1459 {
1460 (void)showmatches(&xpc, FALSE);
1461 redrawcmd();
1462 did_wild_list = TRUE;
1463 }
1464 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001465 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1466 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001468 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1469 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else
1471 res = OK; /* don't insert 'wildchar' now */
1472 }
1473 else /* typed p_wc first time */
1474 {
1475 wim_index = 0;
1476 j = ccline.cmdpos;
1477 /* if 'wildmode' first contains "longest", get longest
1478 * common part */
1479 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001480 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1481 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001483 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1484 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 /* if interrupted while completing, behave like it failed */
1487 if (got_int)
1488 {
1489 (void)vpeekc(); /* remove <C-C> from input stream */
1490 got_int = FALSE; /* don't abandon the command line */
1491 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1492#ifdef FEAT_WILDMENU
1493 xpc.xp_context = EXPAND_NOTHING;
1494#endif
1495 goto cmdline_changed;
1496 }
1497
1498 /* when more than one match, and 'wildmode' first contains
1499 * "list", or no change and 'wildmode' contains "longest,list",
1500 * list all matches */
1501 if (res == OK && xpc.xp_numfiles > 1)
1502 {
1503 /* a "longest" that didn't do anything is skipped (but not
1504 * "list:longest") */
1505 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1506 wim_index = 1;
1507 if ((wim_flags[wim_index] & WIM_LIST)
1508#ifdef FEAT_WILDMENU
1509 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1510#endif
1511 )
1512 {
1513 if (!(wim_flags[0] & WIM_LONGEST))
1514 {
1515#ifdef FEAT_WILDMENU
1516 int p_wmnu_save = p_wmnu;
1517 p_wmnu = 0;
1518#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001519 /* remove match */
1520 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521#ifdef FEAT_WILDMENU
1522 p_wmnu = p_wmnu_save;
1523#endif
1524 }
1525#ifdef FEAT_WILDMENU
1526 (void)showmatches(&xpc, p_wmnu
1527 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1528#else
1529 (void)showmatches(&xpc, FALSE);
1530#endif
1531 redrawcmd();
1532 did_wild_list = TRUE;
1533 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001534 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1535 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001537 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1538 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 }
1540 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001541 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 }
1543#ifdef FEAT_WILDMENU
1544 else if (xpc.xp_numfiles == -1)
1545 xpc.xp_context = EXPAND_NOTHING;
1546#endif
1547 }
1548 if (wim_index < 3)
1549 ++wim_index;
1550 if (c == ESC)
1551 gotesc = TRUE;
1552 if (res == OK)
1553 goto cmdline_changed;
1554 }
1555
1556 gotesc = FALSE;
1557
1558 /* <S-Tab> goes to last match, in a clumsy way */
1559 if (c == K_S_TAB && KeyTyped)
1560 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001561 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1562 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1563 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 goto cmdline_changed;
1565 }
1566
1567 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1568 c = NL;
1569
1570 do_abbr = TRUE; /* default: check for abbreviation */
1571
1572 /*
1573 * Big switch for a typed command line character.
1574 */
1575 switch (c)
1576 {
1577 case K_BS:
1578 case Ctrl_H:
1579 case K_DEL:
1580 case K_KDEL:
1581 case Ctrl_W:
1582#ifdef FEAT_FKMAP
1583 if (cmd_fkmap && c == K_BS)
1584 c = K_DEL;
1585#endif
1586 if (c == K_KDEL)
1587 c = K_DEL;
1588
1589 /*
1590 * delete current character is the same as backspace on next
1591 * character, except at end of line
1592 */
1593 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1594 ++ccline.cmdpos;
1595#ifdef FEAT_MBYTE
1596 if (has_mbyte && c == K_DEL)
1597 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1598 ccline.cmdbuff + ccline.cmdpos);
1599#endif
1600 if (ccline.cmdpos > 0)
1601 {
1602 char_u *p;
1603
1604 j = ccline.cmdpos;
1605 p = ccline.cmdbuff + j;
1606#ifdef FEAT_MBYTE
1607 if (has_mbyte)
1608 {
1609 p = mb_prevptr(ccline.cmdbuff, p);
1610 if (c == Ctrl_W)
1611 {
1612 while (p > ccline.cmdbuff && vim_isspace(*p))
1613 p = mb_prevptr(ccline.cmdbuff, p);
1614 i = mb_get_class(p);
1615 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1616 p = mb_prevptr(ccline.cmdbuff, p);
1617 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001618 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620 }
1621 else
1622#endif
1623 if (c == Ctrl_W)
1624 {
1625 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1626 --p;
1627 i = vim_iswordc(p[-1]);
1628 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1629 && vim_iswordc(p[-1]) == i)
1630 --p;
1631 }
1632 else
1633 --p;
1634 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1635 ccline.cmdlen -= j - ccline.cmdpos;
1636 i = ccline.cmdpos;
1637 while (i < ccline.cmdlen)
1638 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1639
1640 /* Truncate at the end, required for multi-byte chars. */
1641 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001642#ifdef FEAT_SEARCH_EXTRA
1643 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001644 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001645 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001646 /* save view settings, so that the screen
1647 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001648 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001649 }
1650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 redrawcmd();
1652 }
1653 else if (ccline.cmdlen == 0 && c != Ctrl_W
1654 && ccline.cmdprompt == NULL && indent == 0)
1655 {
1656 /* In ex and debug mode it doesn't make sense to return. */
1657 if (exmode_active
1658#ifdef FEAT_EVAL
1659 || ccline.cmdfirstc == '>'
1660#endif
1661 )
1662 goto cmdline_not_changed;
1663
Bram Moolenaard23a8232018-02-10 18:45:26 +01001664 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (!cmd_silent)
1666 {
1667#ifdef FEAT_RIGHTLEFT
1668 if (cmdmsg_rl)
1669 msg_col = Columns;
1670 else
1671#endif
1672 msg_col = 0;
1673 msg_putchar(' '); /* delete ':' */
1674 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001675#ifdef FEAT_SEARCH_EXTRA
1676 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001677 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 redraw_cmdline = TRUE;
1680 goto returncmd; /* back to cmd mode */
1681 }
1682 goto cmdline_changed;
1683
1684 case K_INS:
1685 case K_KINS:
1686#ifdef FEAT_FKMAP
1687 /* if Farsi mode set, we are in reverse insert mode -
1688 Do not change the mode */
1689 if (cmd_fkmap)
1690 beep_flush();
1691 else
1692#endif
1693 ccline.overstrike = !ccline.overstrike;
1694#ifdef CURSOR_SHAPE
1695 ui_cursor_shape(); /* may show different cursor shape */
1696#endif
1697 goto cmdline_not_changed;
1698
1699 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001700 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
1702 /* ":lmap" mappings exists, toggle use of mappings. */
1703 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001704#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 im_set_active(FALSE); /* Disable input method */
1706#endif
1707 if (b_im_ptr != NULL)
1708 {
1709 if (State & LANGMAP)
1710 *b_im_ptr = B_IMODE_LMAP;
1711 else
1712 *b_im_ptr = B_IMODE_NONE;
1713 }
1714 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001715#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 else
1717 {
1718 /* There are no ":lmap" mappings, toggle IM. When
1719 * 'imdisable' is set don't try getting the status, it's
1720 * always off. */
1721 if ((p_imdisable && b_im_ptr != NULL)
1722 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1723 {
1724 im_set_active(FALSE); /* Disable input method */
1725 if (b_im_ptr != NULL)
1726 *b_im_ptr = B_IMODE_NONE;
1727 }
1728 else
1729 {
1730 im_set_active(TRUE); /* Enable input method */
1731 if (b_im_ptr != NULL)
1732 *b_im_ptr = B_IMODE_IM;
1733 }
1734 }
1735#endif
1736 if (b_im_ptr != NULL)
1737 {
1738 if (b_im_ptr == &curbuf->b_p_iminsert)
1739 set_iminsert_global();
1740 else
1741 set_imsearch_global();
1742 }
1743#ifdef CURSOR_SHAPE
1744 ui_cursor_shape(); /* may show different cursor shape */
1745#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001746#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 /* Show/unshow value of 'keymap' in status lines later. */
1748 status_redraw_curbuf();
1749#endif
1750 goto cmdline_not_changed;
1751
1752/* case '@': only in very old vi */
1753 case Ctrl_U:
1754 /* delete all characters left of the cursor */
1755 j = ccline.cmdpos;
1756 ccline.cmdlen -= j;
1757 i = ccline.cmdpos = 0;
1758 while (i < ccline.cmdlen)
1759 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1760 /* Truncate at the end, required for multi-byte chars. */
1761 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001762#ifdef FEAT_SEARCH_EXTRA
1763 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001764 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 redrawcmd();
1767 goto cmdline_changed;
1768
1769#ifdef FEAT_CLIPBOARD
1770 case Ctrl_Y:
1771 /* Copy the modeless selection, if there is one. */
1772 if (clip_star.state != SELECT_CLEARED)
1773 {
1774 if (clip_star.state == SELECT_DONE)
1775 clip_copy_modeless_selection(TRUE);
1776 goto cmdline_not_changed;
1777 }
1778 break;
1779#endif
1780
1781 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1782 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001783 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001784 * ":normal" runs out of characters. */
1785 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001786 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 goto cmdline_not_changed;
1788
1789 gotesc = TRUE; /* will free ccline.cmdbuff after
1790 putting it in history */
1791 goto returncmd; /* back to cmd mode */
1792
1793 case Ctrl_R: /* insert register */
1794#ifdef USE_ON_FLY_SCROLL
1795 dont_scroll = TRUE; /* disallow scrolling here */
1796#endif
1797 putcmdline('"', TRUE);
1798 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001799 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (i == Ctrl_O)
1801 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1802 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001803 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001804 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 --no_mapping;
1806#ifdef FEAT_EVAL
1807 /*
1808 * Insert the result of an expression.
1809 * Need to save the current command line, to be able to enter
1810 * a new one...
1811 */
1812 new_cmdpos = -1;
1813 if (c == '=')
1814 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001815 if (ccline.cmdfirstc == '=' // can't do this recursively
1816 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 beep_flush();
1819 c = ESC;
1820 }
1821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824#endif
1825 if (c != ESC) /* use ESC to cancel inserting register */
1826 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001827 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001828
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001829#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001830 /* When there was a serious error abort getting the
1831 * command line. */
1832 if (aborting())
1833 {
1834 gotesc = TRUE; /* will free ccline.cmdbuff after
1835 putting it in history */
1836 goto returncmd; /* back to cmd mode */
1837 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 KeyTyped = FALSE; /* Don't do p_wc completion. */
1840#ifdef FEAT_EVAL
1841 if (new_cmdpos >= 0)
1842 {
1843 /* set_cmdline_pos() was used */
1844 if (new_cmdpos > ccline.cmdlen)
1845 ccline.cmdpos = ccline.cmdlen;
1846 else
1847 ccline.cmdpos = new_cmdpos;
1848 }
1849#endif
1850 }
1851 redrawcmd();
1852 goto cmdline_changed;
1853
1854 case Ctrl_D:
1855 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1856 break; /* Use ^D as normal char instead */
1857
1858 redrawcmd();
1859 continue; /* don't do incremental search now */
1860
1861 case K_RIGHT:
1862 case K_S_RIGHT:
1863 case K_C_RIGHT:
1864 do
1865 {
1866 if (ccline.cmdpos >= ccline.cmdlen)
1867 break;
1868 i = cmdline_charsize(ccline.cmdpos);
1869 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1870 break;
1871 ccline.cmdspos += i;
1872#ifdef FEAT_MBYTE
1873 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001874 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 + ccline.cmdpos);
1876 else
1877#endif
1878 ++ccline.cmdpos;
1879 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001880 while ((c == K_S_RIGHT || c == K_C_RIGHT
1881 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1883#ifdef FEAT_MBYTE
1884 if (has_mbyte)
1885 set_cmdspos_cursor();
1886#endif
1887 goto cmdline_not_changed;
1888
1889 case K_LEFT:
1890 case K_S_LEFT:
1891 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001892 if (ccline.cmdpos == 0)
1893 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 do
1895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 --ccline.cmdpos;
1897#ifdef FEAT_MBYTE
1898 if (has_mbyte) /* move to first byte of char */
1899 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1900 ccline.cmdbuff + ccline.cmdpos);
1901#endif
1902 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1903 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001904 while (ccline.cmdpos > 0
1905 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001906 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1908#ifdef FEAT_MBYTE
1909 if (has_mbyte)
1910 set_cmdspos_cursor();
1911#endif
1912 goto cmdline_not_changed;
1913
1914 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001915 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001916 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
Bram Moolenaar4770d092006-01-12 23:22:24 +00001918#ifdef FEAT_GUI_W32
1919 /* On Win32 ignore <M-F4>, we get it when closing the window was
1920 * cancelled. */
1921 case K_F4:
1922 if (mod_mask == MOD_MASK_ALT)
1923 {
1924 redrawcmd(); /* somehow the cmdline is cleared */
1925 goto cmdline_not_changed;
1926 }
1927 break;
1928#endif
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930#ifdef FEAT_MOUSE
1931 case K_MIDDLEDRAG:
1932 case K_MIDDLERELEASE:
1933 goto cmdline_not_changed; /* Ignore mouse */
1934
1935 case K_MIDDLEMOUSE:
1936# ifdef FEAT_GUI
1937 /* When GUI is active, also paste when 'mouse' is empty */
1938 if (!gui.in_use)
1939# endif
1940 if (!mouse_has(MOUSE_COMMAND))
1941 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001942# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001944 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001946# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001947 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 redrawcmd();
1949 goto cmdline_changed;
1950
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001951# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001953 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 redrawcmd();
1955 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 case K_LEFTDRAG:
1959 case K_LEFTRELEASE:
1960 case K_RIGHTDRAG:
1961 case K_RIGHTRELEASE:
1962 /* Ignore drag and release events when the button-down wasn't
1963 * seen before. */
1964 if (ignore_drag_release)
1965 goto cmdline_not_changed;
1966 /* FALLTHROUGH */
1967 case K_LEFTMOUSE:
1968 case K_RIGHTMOUSE:
1969 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1970 ignore_drag_release = TRUE;
1971 else
1972 ignore_drag_release = FALSE;
1973# ifdef FEAT_GUI
1974 /* When GUI is active, also move when 'mouse' is empty */
1975 if (!gui.in_use)
1976# endif
1977 if (!mouse_has(MOUSE_COMMAND))
1978 goto cmdline_not_changed; /* Ignore mouse */
1979# ifdef FEAT_CLIPBOARD
1980 if (mouse_row < cmdline_row && clip_star.available)
1981 {
1982 int button, is_click, is_drag;
1983
1984 /*
1985 * Handle modeless selection.
1986 */
1987 button = get_mouse_button(KEY2TERMCAP1(c),
1988 &is_click, &is_drag);
1989 if (mouse_model_popup() && button == MOUSE_LEFT
1990 && (mod_mask & MOD_MASK_SHIFT))
1991 {
1992 /* Translate shift-left to right button. */
1993 button = MOUSE_RIGHT;
1994 mod_mask &= ~MOD_MASK_SHIFT;
1995 }
1996 clip_modeless(button, is_click, is_drag);
1997 goto cmdline_not_changed;
1998 }
1999# endif
2000
2001 set_cmdspos();
2002 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2003 ++ccline.cmdpos)
2004 {
2005 i = cmdline_charsize(ccline.cmdpos);
2006 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2007 && mouse_col < ccline.cmdspos % Columns + i)
2008 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002009# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (has_mbyte)
2011 {
2012 /* Count ">" for double-wide char that doesn't fit. */
2013 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002014 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 + ccline.cmdpos) - 1;
2016 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 ccline.cmdspos += i;
2019 }
2020 goto cmdline_not_changed;
2021
2022 /* Mouse scroll wheel: ignored here */
2023 case K_MOUSEDOWN:
2024 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002025 case K_MOUSELEFT:
2026 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 /* Alternate buttons ignored here */
2028 case K_X1MOUSE:
2029 case K_X1DRAG:
2030 case K_X1RELEASE:
2031 case K_X2MOUSE:
2032 case K_X2DRAG:
2033 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002034 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 goto cmdline_not_changed;
2036
2037#endif /* FEAT_MOUSE */
2038
2039#ifdef FEAT_GUI
2040 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2041 case K_LEFTRELEASE_NM:
2042 goto cmdline_not_changed;
2043
2044 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002045 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
2047 gui_do_scroll();
2048 redrawcmd();
2049 }
2050 goto cmdline_not_changed;
2051
2052 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002053 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002055 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 redrawcmd();
2057 }
2058 goto cmdline_not_changed;
2059#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002060#ifdef FEAT_GUI_TABLINE
2061 case K_TABLINE:
2062 case K_TABMENU:
2063 /* Don't want to change any tabs here. Make sure the same tab
2064 * is still selected. */
2065 if (gui_use_tabline())
2066 gui_mch_set_curtab(tabpage_index(curtab));
2067 goto cmdline_not_changed;
2068#endif
2069
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 case K_SELECT: /* end of Select mode mapping - ignore */
2071 goto cmdline_not_changed;
2072
2073 case Ctrl_B: /* begin of command line */
2074 case K_HOME:
2075 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 case K_S_HOME:
2077 case K_C_HOME:
2078 ccline.cmdpos = 0;
2079 set_cmdspos();
2080 goto cmdline_not_changed;
2081
2082 case Ctrl_E: /* end of command line */
2083 case K_END:
2084 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 case K_S_END:
2086 case K_C_END:
2087 ccline.cmdpos = ccline.cmdlen;
2088 set_cmdspos_cursor();
2089 goto cmdline_not_changed;
2090
2091 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002092 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 break;
2094 goto cmdline_changed;
2095
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002096 case Ctrl_L:
2097#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002098 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002099 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002100#endif
2101
2102 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002103 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 break;
2105 goto cmdline_changed;
2106
2107 case Ctrl_N: /* next match */
2108 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002109 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002111 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2112 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002114 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002117 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 case K_UP:
2119 case K_DOWN:
2120 case K_S_UP:
2121 case K_S_DOWN:
2122 case K_PAGEUP:
2123 case K_KPAGEUP:
2124 case K_PAGEDOWN:
2125 case K_KPAGEDOWN:
2126 if (hislen == 0 || firstc == NUL) /* no history */
2127 goto cmdline_not_changed;
2128
2129 i = hiscnt;
2130
2131 /* save current command string so it can be restored later */
2132 if (lookfor == NULL)
2133 {
2134 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2135 goto cmdline_not_changed;
2136 lookfor[ccline.cmdpos] = NUL;
2137 }
2138
2139 j = (int)STRLEN(lookfor);
2140 for (;;)
2141 {
2142 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002143 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002144 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 if (hiscnt == hislen) /* first time */
2147 hiscnt = hisidx[histype];
2148 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2149 hiscnt = hislen - 1;
2150 else if (hiscnt != hisidx[histype] + 1)
2151 --hiscnt;
2152 else /* at top of list */
2153 {
2154 hiscnt = i;
2155 break;
2156 }
2157 }
2158 else /* one step forwards */
2159 {
2160 /* on last entry, clear the line */
2161 if (hiscnt == hisidx[histype])
2162 {
2163 hiscnt = hislen;
2164 break;
2165 }
2166
2167 /* not on a history line, nothing to do */
2168 if (hiscnt == hislen)
2169 break;
2170 if (hiscnt == hislen - 1) /* wrap around */
2171 hiscnt = 0;
2172 else
2173 ++hiscnt;
2174 }
2175 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2176 {
2177 hiscnt = i;
2178 break;
2179 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002180 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002181 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 || STRNCMP(history[histype][hiscnt].hisstr,
2183 lookfor, (size_t)j) == 0)
2184 break;
2185 }
2186
2187 if (hiscnt != i) /* jumped to other entry */
2188 {
2189 char_u *p;
2190 int len;
2191 int old_firstc;
2192
Bram Moolenaar438d1762018-09-30 17:11:48 +02002193 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002194 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (hiscnt == hislen)
2196 p = lookfor; /* back to the old one */
2197 else
2198 p = history[histype][hiscnt].hisstr;
2199
2200 if (histype == HIST_SEARCH
2201 && p != lookfor
2202 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2203 {
2204 /* Correct for the separator character used when
2205 * adding the history entry vs the one used now.
2206 * First loop: count length.
2207 * Second loop: copy the characters. */
2208 for (i = 0; i <= 1; ++i)
2209 {
2210 len = 0;
2211 for (j = 0; p[j] != NUL; ++j)
2212 {
2213 /* Replace old sep with new sep, unless it is
2214 * escaped. */
2215 if (p[j] == old_firstc
2216 && (j == 0 || p[j - 1] != '\\'))
2217 {
2218 if (i > 0)
2219 ccline.cmdbuff[len] = firstc;
2220 }
2221 else
2222 {
2223 /* Escape new sep, unless it is already
2224 * escaped. */
2225 if (p[j] == firstc
2226 && (j == 0 || p[j - 1] != '\\'))
2227 {
2228 if (i > 0)
2229 ccline.cmdbuff[len] = '\\';
2230 ++len;
2231 }
2232 if (i > 0)
2233 ccline.cmdbuff[len] = p[j];
2234 }
2235 ++len;
2236 }
2237 if (i == 0)
2238 {
2239 alloc_cmdbuff(len);
2240 if (ccline.cmdbuff == NULL)
2241 goto returncmd;
2242 }
2243 }
2244 ccline.cmdbuff[len] = NUL;
2245 }
2246 else
2247 {
2248 alloc_cmdbuff((int)STRLEN(p));
2249 if (ccline.cmdbuff == NULL)
2250 goto returncmd;
2251 STRCPY(ccline.cmdbuff, p);
2252 }
2253
2254 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2255 redrawcmd();
2256 goto cmdline_changed;
2257 }
2258 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002259#endif
2260 goto cmdline_not_changed;
2261
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002262#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002263 case Ctrl_G: /* next match */
2264 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002265 if (may_adjust_incsearch_highlighting(
2266 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002267 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#endif
2270
2271 case Ctrl_V:
2272 case Ctrl_Q:
2273#ifdef FEAT_MOUSE
2274 ignore_drag_release = TRUE;
2275#endif
2276 putcmdline('^', TRUE);
2277 c = get_literal(); /* get next (two) character(s) */
2278 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002279 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280#ifdef FEAT_MBYTE
2281 /* may need to remove ^ when composing char was typed */
2282 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2283 {
2284 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2285 msg_putchar(' ');
2286 cursorcmd();
2287 }
2288#endif
2289 break;
2290
2291#ifdef FEAT_DIGRAPHS
2292 case Ctrl_K:
2293#ifdef FEAT_MOUSE
2294 ignore_drag_release = TRUE;
2295#endif
2296 putcmdline('?', TRUE);
2297#ifdef USE_ON_FLY_SCROLL
2298 dont_scroll = TRUE; /* disallow scrolling here */
2299#endif
2300 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002301 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (c != NUL)
2303 break;
2304
2305 redrawcmd();
2306 goto cmdline_not_changed;
2307#endif /* FEAT_DIGRAPHS */
2308
2309#ifdef FEAT_RIGHTLEFT
2310 case Ctrl__: /* CTRL-_: switch language mode */
2311 if (!p_ari)
2312 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002313# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (p_altkeymap)
2315 {
2316 cmd_fkmap = !cmd_fkmap;
2317 if (cmd_fkmap) /* in Farsi always in Insert mode */
2318 ccline.overstrike = FALSE;
2319 }
2320 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 cmd_hkmap = !cmd_hkmap;
2323 goto cmdline_not_changed;
2324#endif
2325
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002326 case K_PS:
2327 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2328 goto cmdline_changed;
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 default:
2331#ifdef UNIX
2332 if (c == intr_char)
2333 {
2334 gotesc = TRUE; /* will free ccline.cmdbuff after
2335 putting it in history */
2336 goto returncmd; /* back to Normal mode */
2337 }
2338#endif
2339 /*
2340 * Normal character with no special meaning. Just set mod_mask
2341 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2342 * the string <S-Space>. This should only happen after ^V.
2343 */
2344 if (!IS_SPECIAL(c))
2345 mod_mask = 0x0;
2346 break;
2347 }
2348 /*
2349 * End of switch on command line character.
2350 * We come here if we have a normal character.
2351 */
2352
Bram Moolenaarede3e632013-06-23 16:16:19 +02002353 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354#ifdef FEAT_MBYTE
2355 /* Add ABBR_OFF for characters above 0x100, this is
2356 * what check_abbr() expects. */
2357 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2358#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002359 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 goto cmdline_changed;
2361
2362 /*
2363 * put the character in the command line
2364 */
2365 if (IS_SPECIAL(c) || mod_mask != 0)
2366 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2367 else
2368 {
2369#ifdef FEAT_MBYTE
2370 if (has_mbyte)
2371 {
2372 j = (*mb_char2bytes)(c, IObuff);
2373 IObuff[j] = NUL; /* exclude composing chars */
2374 put_on_cmdline(IObuff, j, TRUE);
2375 }
2376 else
2377#endif
2378 {
2379 IObuff[0] = c;
2380 put_on_cmdline(IObuff, 1, TRUE);
2381 }
2382 }
2383 goto cmdline_changed;
2384
2385/*
2386 * This part implements incremental searches for "/" and "?"
2387 * Jump to cmdline_not_changed when a character has been read but the command
2388 * line did not change. Then we only search and redraw if something changed in
2389 * the past.
2390 * Jump to cmdline_changed when the command line did change.
2391 * (Sorry for the goto's, I know it is ugly).
2392 */
2393cmdline_not_changed:
2394#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002395 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 continue;
2397#endif
2398
2399cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002400 /* Trigger CmdlineChanged autocommands. */
2401 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002402
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002404 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405#endif
2406
2407#ifdef FEAT_RIGHTLEFT
2408 if (cmdmsg_rl
2409# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002410 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411# endif
2412 )
2413 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002414 * right-left typing. Not efficient, but it works.
2415 * Do it only when there are no characters left to read
2416 * to avoid useless intermediate redraws. */
2417 if (vpeekc() == NUL)
2418 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419#endif
2420 }
2421
2422returncmd:
2423
2424#ifdef FEAT_RIGHTLEFT
2425 cmdmsg_rl = FALSE;
2426#endif
2427
2428#ifdef FEAT_FKMAP
2429 cmd_fkmap = 0;
2430#endif
2431
2432 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002433 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002436 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437#endif
2438
2439 if (ccline.cmdbuff != NULL)
2440 {
2441 /*
2442 * Put line in history buffer (":" and "=" only when it was typed).
2443 */
2444#ifdef FEAT_CMDHIST
2445 if (ccline.cmdlen && firstc != NUL
2446 && (some_key_typed || histype == HIST_SEARCH))
2447 {
2448 add_to_history(histype, ccline.cmdbuff, TRUE,
2449 histype == HIST_SEARCH ? firstc : NUL);
2450 if (firstc == ':')
2451 {
2452 vim_free(new_last_cmdline);
2453 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2454 }
2455 }
2456#endif
2457
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002458 if (gotesc)
2459 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461
2462 /*
2463 * If the screen was shifted up, redraw the whole screen (later).
2464 * If the line is too long, clear it, so ruler and shown command do
2465 * not get printed in the middle of it.
2466 */
2467 msg_check();
2468 msg_scroll = save_msg_scroll;
2469 redir_off = FALSE;
2470
2471 /* When the command line was typed, no need for a wait-return prompt. */
2472 if (some_key_typed)
2473 need_wait_return = FALSE;
2474
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002475 /* Trigger CmdlineLeave autocommands. */
2476 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002479#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2481 im_save_status(b_im_ptr);
2482 im_set_active(FALSE);
2483#endif
2484#ifdef FEAT_MOUSE
2485 setmouse();
2486#endif
2487#ifdef CURSOR_SHAPE
2488 ui_cursor_shape(); /* may show different cursor shape */
2489#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002490 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar438d1762018-09-30 17:11:48 +02002492theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002493 {
2494 char_u *p = ccline.cmdbuff;
2495
Bram Moolenaar438d1762018-09-30 17:11:48 +02002496 if (did_save_ccline)
2497 restore_cmdline(&save_ccline);
2498 else
2499 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002500 return p;
2501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502}
2503
2504#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2505/*
2506 * Get a command line with a prompt.
2507 * This is prepared to be called recursively from getcmdline() (e.g. by
2508 * f_input() when evaluating an expression from CTRL-R =).
2509 * Returns the command line in allocated memory, or NULL.
2510 */
2511 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002512getcmdline_prompt(
2513 int firstc,
2514 char_u *prompt, /* command line prompt */
2515 int attr, /* attributes for prompt */
2516 int xp_context, /* type of expansion */
2517 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 char_u *s;
2520 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002521 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002523 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar438d1762018-09-30 17:11:48 +02002525 if (ccline.cmdbuff != NULL)
2526 {
2527 // Save the values of the current cmdline and restore them below.
2528 save_cmdline(&save_ccline);
2529 did_save_ccline = TRUE;
2530 }
2531
2532 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 ccline.cmdprompt = prompt;
2534 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002535# ifdef FEAT_EVAL
2536 ccline.xp_context = xp_context;
2537 ccline.xp_arg = xp_arg;
2538 ccline.input_fn = (firstc == '@');
2539# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002540 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002541 s = getcmdline_int(firstc, 1L, 0, FALSE);
2542
2543 if (did_save_ccline)
2544 restore_cmdline(&save_ccline);
2545
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002546 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002547 /* Restore msg_col, the prompt from input() may have changed it.
2548 * But only if called recursively and the commandline is therefore being
2549 * restored to an old one; if not, the input() prompt stays on the screen,
2550 * so we need its modified msg_col left intact. */
2551 if (ccline.cmdbuff != NULL)
2552 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 return s;
2555}
2556#endif
2557
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002558/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002559 * Return TRUE when the text must not be changed and we can't switch to
2560 * another window or buffer. Used when editing the command line, evaluating
2561 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002562 */
2563 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002564text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002565{
2566#ifdef FEAT_CMDWIN
2567 if (cmdwin_type != 0)
2568 return TRUE;
2569#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002570 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002571}
2572
2573/*
2574 * Give an error message for a command that isn't allowed while the cmdline
2575 * window is open or editing the cmdline in another way.
2576 */
2577 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002578text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002579{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002580 EMSG(_(get_text_locked_msg()));
2581}
2582
2583 char_u *
2584get_text_locked_msg(void)
2585{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002586#ifdef FEAT_CMDWIN
2587 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002588 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002589#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002590 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002591}
2592
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002593/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002594 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2595 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002596 */
2597 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002598curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002599{
2600 if (curbuf_lock > 0)
2601 {
2602 EMSG(_("E788: Not allowed to edit another buffer now"));
2603 return TRUE;
2604 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002605 return allbuf_locked();
2606}
2607
2608/*
2609 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2610 * message.
2611 */
2612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002613allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002614{
2615 if (allbuf_lock > 0)
2616 {
2617 EMSG(_("E811: Not allowed to change buffer information now"));
2618 return TRUE;
2619 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002620 return FALSE;
2621}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002624cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2627 if (cmdline_star > 0) /* showing '*', always 1 position */
2628 return 1;
2629#endif
2630 return ptr2cells(ccline.cmdbuff + idx);
2631}
2632
2633/*
2634 * Compute the offset of the cursor on the command line for the prompt and
2635 * indent.
2636 */
2637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002638set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002640 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 ccline.cmdspos = 1 + ccline.cmdindent;
2642 else
2643 ccline.cmdspos = 0 + ccline.cmdindent;
2644}
2645
2646/*
2647 * Compute the screen position for the cursor on the command line.
2648 */
2649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002650set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 int i, m, c;
2653
2654 set_cmdspos();
2655 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002658 if (m < 0) /* overflow, Columns or Rows at weird value */
2659 m = MAXCOL;
2660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 else
2662 m = MAXCOL;
2663 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2664 {
2665 c = cmdline_charsize(i);
2666#ifdef FEAT_MBYTE
2667 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2668 if (has_mbyte)
2669 correct_cmdspos(i, c);
2670#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002671 /* If the cmdline doesn't fit, show cursor on last visible char.
2672 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if ((ccline.cmdspos += c) >= m)
2674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 ccline.cmdspos -= c;
2676 break;
2677 }
2678#ifdef FEAT_MBYTE
2679 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002680 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681#endif
2682 }
2683}
2684
2685#ifdef FEAT_MBYTE
2686/*
2687 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2688 * character that doesn't fit, so that a ">" must be displayed.
2689 */
2690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002691correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002693 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2695 && ccline.cmdspos % Columns + cells > Columns)
2696 ccline.cmdspos++;
2697}
2698#endif
2699
2700/*
2701 * Get an Ex command line for the ":" command.
2702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002704getexline(
2705 int c, /* normally ':', NUL for ":append" */
2706 void *cookie UNUSED,
2707 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 /* When executing a register, remove ':' that's in front of each line. */
2710 if (exec_from_reg && vpeekc() == ':')
2711 (void)vgetc();
2712 return getcmdline(c, 1L, indent);
2713}
2714
2715/*
2716 * Get an Ex command line for Ex mode.
2717 * In Ex mode we only use the OS supplied line editing features and no
2718 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002719 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002722getexmodeline(
2723 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002725 void *cookie UNUSED,
2726 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 garray_T line_ga;
2729 char_u *pend;
2730 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002731 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732 int escaped = FALSE; /* CTRL-V typed */
2733 int vcol = 0;
2734 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002735 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002736 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 /* Switch cursor on now. This avoids that it happens after the "\n", which
2739 * confuses the system function that computes tabstops. */
2740 cursor_on();
2741
2742 /* always start in column 0; write a newline if necessary */
2743 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002746 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002748 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 if (p_prompt)
2750 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 while (indent-- > 0)
2752 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755
2756 ga_init2(&line_ga, 1, 30);
2757
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002758 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002760 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002761 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002762 while (indent >= 8)
2763 {
2764 ga_append(&line_ga, TAB);
2765 msg_puts((char_u *)" ");
2766 indent -= 8;
2767 }
2768 while (indent-- > 0)
2769 {
2770 ga_append(&line_ga, ' ');
2771 msg_putchar(' ');
2772 }
2773 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 ++no_mapping;
2775 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 /*
2778 * Get the line, one character at a time.
2779 */
2780 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002783 long sw;
2784 char_u *s;
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (ga_grow(&line_ga, 40) == FAIL)
2787 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
Bram Moolenaarba748c82017-02-26 14:00:07 +01002789 /*
2790 * Get one character at a time.
2791 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002792 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002793
2794 /* Check for a ":normal" command and no more characters left. */
2795 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2796 c1 = '\n';
2797 else
2798 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801 * Handle line editing.
2802 * Previously this was left to the system, putting the terminal in
2803 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002807 msg_putchar('\n');
2808 break;
2809 }
2810
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002811 if (c1 == K_PS)
2812 {
2813 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2814 goto redraw;
2815 }
2816
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 if (!escaped)
2818 {
2819 /* CR typed means "enter", which is NL */
2820 if (c1 == '\r')
2821 c1 = '\n';
2822
2823 if (c1 == BS || c1 == K_BS
2824 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 if (line_ga.ga_len > 0)
2827 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002828#ifdef FEAT_MBYTE
2829 if (has_mbyte)
2830 {
2831 p = (char_u *)line_ga.ga_data;
2832 p[line_ga.ga_len] = NUL;
2833 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2834 line_ga.ga_len -= len;
2835 }
2836 else
2837#endif
2838 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839 goto redraw;
2840 }
2841 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 msg_col = startcol;
2847 msg_clr_eos();
2848 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002849 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 }
2851
2852 if (c1 == Ctrl_T)
2853 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002854 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 p = (char_u *)line_ga.ga_data;
2856 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002857 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002858 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002860 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002862 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002863 p = (char_u *)line_ga.ga_data;
2864 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002865 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2866 *s = ' ';
2867 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869redraw:
2870 /* redraw the line */
2871 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002872 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002873 p = (char_u *)line_ga.ga_data;
2874 p[line_ga.ga_len] = NUL;
2875 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002879 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002881 msg_putchar(' ');
2882 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002883 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002887 len = MB_PTR2LEN(p);
2888 msg_outtrans_len(p, len);
2889 vcol += ptr2cells(p);
2890 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002893 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002894 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 continue;
2896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898 if (c1 == Ctrl_D)
2899 {
2900 /* Delete one shiftwidth. */
2901 p = (char_u *)line_ga.ga_data;
2902 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002904 if (prev_char == '^')
2905 ex_keep_indent = TRUE;
2906 indent = 0;
2907 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909 else
2910 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002912 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002913 if (indent > 0)
2914 {
2915 --indent;
2916 indent -= indent % get_sw_value(curbuf);
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002919 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002921 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002922 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2923 --line_ga.ga_len;
2924 }
2925 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002927
2928 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2929 {
2930 escaped = TRUE;
2931 continue;
2932 }
2933
2934 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2935 if (IS_SPECIAL(c1))
2936 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002938
2939 if (IS_SPECIAL(c1))
2940 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002941#ifdef FEAT_MBYTE
2942 if (has_mbyte)
2943 len = (*mb_char2bytes)(c1,
2944 (char_u *)line_ga.ga_data + line_ga.ga_len);
2945 else
2946#endif
2947 {
2948 len = 1;
2949 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2950 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002951 if (c1 == '\n')
2952 msg_putchar('\n');
2953 else if (c1 == TAB)
2954 {
2955 /* Don't use chartabsize(), 'ts' can be different */
2956 do
2957 {
2958 msg_putchar(' ');
2959 } while (++vcol % 8);
2960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002963 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002964 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002965 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002967 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002968 escaped = FALSE;
2969
2970 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002971 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002972
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002973 /* We are done when a NL is entered, but not when it comes after an
2974 * odd number of backslashes, that results in a NUL. */
2975 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002977 int bcount = 0;
2978
2979 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2980 ++bcount;
2981
2982 if (bcount > 0)
2983 {
2984 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2985 * "\NL", etc. */
2986 line_ga.ga_len -= (bcount + 1) / 2;
2987 pend -= (bcount + 1) / 2;
2988 pend[-1] = '\n';
2989 }
2990
2991 if ((bcount & 1) == 0)
2992 {
2993 --line_ga.ga_len;
2994 --pend;
2995 *pend = NUL;
2996 break;
2997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
2999 }
3000
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003001 --no_mapping;
3002 --allow_keys;
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /* make following messages go to the next line */
3005 msg_didout = FALSE;
3006 msg_col = 0;
3007 if (msg_row < Rows - 1)
3008 ++msg_row;
3009 emsg_on_display = FALSE; /* don't want ui_delay() */
3010
3011 if (got_int)
3012 ga_clear(&line_ga);
3013
3014 return (char_u *)line_ga.ga_data;
3015}
3016
Bram Moolenaare344bea2005-09-01 20:46:49 +00003017# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3018 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019/*
3020 * Return TRUE if ccline.overstrike is on.
3021 */
3022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003023cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 return ccline.overstrike;
3026}
3027
3028/*
3029 * Return TRUE if the cursor is at the end of the cmdline.
3030 */
3031 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003032cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 return (ccline.cmdpos >= ccline.cmdlen);
3035}
3036#endif
3037
Bram Moolenaar9372a112005-12-06 19:59:18 +00003038#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039/*
3040 * Return the virtual column number at the current cursor position.
3041 * This is used by the IM code to obtain the start of the preedit string.
3042 */
3043 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003044cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3047 return MAXCOL;
3048
3049# ifdef FEAT_MBYTE
3050 if (has_mbyte)
3051 {
3052 colnr_T col;
3053 int i = 0;
3054
3055 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003056 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
3058 return col;
3059 }
3060 else
3061# endif
3062 return ccline.cmdpos;
3063}
3064#endif
3065
3066#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3067/*
3068 * If part of the command line is an IM preedit string, redraw it with
3069 * IM feedback attributes. The cursor position is restored after drawing.
3070 */
3071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003072redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 if ((State & CMDLINE)
3075 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003076 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 && !p_imdisable
3078 && im_is_preediting())
3079 {
3080 int cmdpos = 0;
3081 int cmdspos;
3082 int old_row;
3083 int old_col;
3084 colnr_T col;
3085
3086 old_row = msg_row;
3087 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003088 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090# ifdef FEAT_MBYTE
3091 if (has_mbyte)
3092 {
3093 for (col = 0; col < preedit_start_col
3094 && cmdpos < ccline.cmdlen; ++col)
3095 {
3096 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003097 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
3099 }
3100 else
3101# endif
3102 {
3103 cmdspos += preedit_start_col;
3104 cmdpos += preedit_start_col;
3105 }
3106
3107 msg_row = cmdline_row + (cmdspos / (int)Columns);
3108 msg_col = cmdspos % (int)Columns;
3109 if (msg_row >= Rows)
3110 msg_row = Rows - 1;
3111
3112 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3113 {
3114 int char_len;
3115 int char_attr;
3116
3117 char_attr = im_get_feedback_attr(col);
3118 if (char_attr < 0)
3119 break; /* end of preedit string */
3120
3121# ifdef FEAT_MBYTE
3122 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003123 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 else
3125# endif
3126 char_len = 1;
3127
3128 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3129 cmdpos += char_len;
3130 }
3131
3132 msg_row = old_row;
3133 msg_col = old_col;
3134 }
3135}
3136#endif /* FEAT_XIM && FEAT_GUI_GTK */
3137
3138/*
3139 * Allocate a new command line buffer.
3140 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 */
3142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003143alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 /*
3146 * give some extra space to avoid having to allocate all the time
3147 */
3148 if (len < 80)
3149 len = 100;
3150 else
3151 len += 20;
3152
3153 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3154 ccline.cmdbufflen = len;
3155}
3156
3157/*
3158 * Re-allocate the command line to length len + something extra.
3159 * return FAIL for failure, OK otherwise
3160 */
3161 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003162realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 char_u *p;
3165
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003166 if (len < ccline.cmdbufflen)
3167 return OK; /* no need to resize */
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 p = ccline.cmdbuff;
3170 alloc_cmdbuff(len); /* will get some more */
3171 if (ccline.cmdbuff == NULL) /* out of memory */
3172 {
3173 ccline.cmdbuff = p; /* keep the old one */
3174 return FAIL;
3175 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003176 /* There isn't always a NUL after the command, but it may need to be
3177 * there, thus copy up to the NUL and add a NUL. */
3178 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3179 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003181
3182 if (ccline.xpc != NULL
3183 && ccline.xpc->xp_pattern != NULL
3184 && ccline.xpc->xp_context != EXPAND_NOTHING
3185 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3186 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003187 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003188
3189 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3190 * to point into the newly allocated memory. */
3191 if (i >= 0 && i <= ccline.cmdlen)
3192 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3193 }
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 return OK;
3196}
3197
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003198#if defined(FEAT_ARABIC) || defined(PROTO)
3199static char_u *arshape_buf = NULL;
3200
3201# if defined(EXITFREE) || defined(PROTO)
3202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003203free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204{
3205 vim_free(arshape_buf);
3206}
3207# endif
3208#endif
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210/*
3211 * Draw part of the cmdline at the current cursor position. But draw stars
3212 * when cmdline_star is TRUE.
3213 */
3214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003215draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216{
3217#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3218 int i;
3219
3220 if (cmdline_star > 0)
3221 for (i = 0; i < len; ++i)
3222 {
3223 msg_putchar('*');
3224# ifdef FEAT_MBYTE
3225 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003226 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227# endif
3228 }
3229 else
3230#endif
3231#ifdef FEAT_ARABIC
3232 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 static int buflen = 0;
3235 char_u *p;
3236 int j;
3237 int newlen = 0;
3238 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003239 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 int prev_c = 0;
3241 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003242 int u8c;
3243 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246 /*
3247 * Do arabic shaping into a temporary buffer. This is very
3248 * inefficient!
3249 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003250 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
3252 /* Re-allocate the buffer. We keep it around to avoid a lot of
3253 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003255 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 arshape_buf = alloc(buflen);
3257 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 return; /* out of memory */
3259 }
3260
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003261 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3262 {
3263 /* Prepend a space to draw the leading composing char on. */
3264 arshape_buf[0] = ' ';
3265 newlen = 1;
3266 }
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 for (j = start; j < start + len; j += mb_l)
3269 {
3270 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003271 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003272 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (ARABIC_CHAR(u8c))
3274 {
3275 /* Do Arabic shaping. */
3276 if (cmdmsg_rl)
3277 {
3278 /* displaying from right to left */
3279 pc = prev_c;
3280 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003281 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (j + mb_l >= start + len)
3283 nc = NUL;
3284 else
3285 nc = utf_ptr2char(p + mb_l);
3286 }
3287 else
3288 {
3289 /* displaying from left to right */
3290 if (j + mb_l >= start + len)
3291 pc = NUL;
3292 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003293 {
3294 int pcc[MAX_MCO];
3295
3296 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003298 pc1 = pcc[0];
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 nc = prev_c;
3301 }
3302 prev_c = u8c;
3303
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003304 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003306 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003307 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003309 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3310 if (u8cc[1] != 0)
3311 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003312 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 }
3315 else
3316 {
3317 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003318 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 newlen += mb_l;
3320 }
3321 }
3322
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325 else
3326#endif
3327 msg_outtrans_len(ccline.cmdbuff + start, len);
3328}
3329
3330/*
3331 * Put a character on the command line. Shifts the following text to the
3332 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3333 * "c" must be printable (fit in one display cell)!
3334 */
3335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003336putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 if (cmd_silent)
3339 return;
3340 msg_no_more = TRUE;
3341 msg_putchar(c);
3342 if (shift)
3343 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3344 msg_no_more = FALSE;
3345 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003346 extra_char = c;
3347 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348}
3349
3350/*
3351 * Undo a putcmdline(c, FALSE).
3352 */
3353 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003354unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 if (cmd_silent)
3357 return;
3358 msg_no_more = TRUE;
3359 if (ccline.cmdlen == ccline.cmdpos)
3360 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003361#ifdef FEAT_MBYTE
3362 else if (has_mbyte)
3363 draw_cmdline(ccline.cmdpos,
3364 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 else
3367 draw_cmdline(ccline.cmdpos, 1);
3368 msg_no_more = FALSE;
3369 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003370 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373/*
3374 * Put the given string, of the given length, onto the command line.
3375 * If len is -1, then STRLEN() is used to calculate the length.
3376 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3377 * part will be redrawn, otherwise it will not. If this function is called
3378 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3379 * called afterwards.
3380 */
3381 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003382put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 int retval;
3385 int i;
3386 int m;
3387 int c;
3388
3389 if (len < 0)
3390 len = (int)STRLEN(str);
3391
3392 /* Check if ccline.cmdbuff needs to be longer */
3393 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003394 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 else
3396 retval = OK;
3397 if (retval == OK)
3398 {
3399 if (!ccline.overstrike)
3400 {
3401 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3402 ccline.cmdbuff + ccline.cmdpos,
3403 (size_t)(ccline.cmdlen - ccline.cmdpos));
3404 ccline.cmdlen += len;
3405 }
3406 else
3407 {
3408#ifdef FEAT_MBYTE
3409 if (has_mbyte)
3410 {
3411 /* Count nr of characters in the new string. */
3412 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003413 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 ++m;
3415 /* Count nr of bytes in cmdline that are overwritten by these
3416 * characters. */
3417 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003418 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 --m;
3420 if (i < ccline.cmdlen)
3421 {
3422 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3423 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3424 ccline.cmdlen += ccline.cmdpos + len - i;
3425 }
3426 else
3427 ccline.cmdlen = ccline.cmdpos + len;
3428 }
3429 else
3430#endif
3431 if (ccline.cmdpos + len > ccline.cmdlen)
3432 ccline.cmdlen = ccline.cmdpos + len;
3433 }
3434 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3435 ccline.cmdbuff[ccline.cmdlen] = NUL;
3436
3437#ifdef FEAT_MBYTE
3438 if (enc_utf8)
3439 {
3440 /* When the inserted text starts with a composing character,
3441 * backup to the character before it. There could be two of them.
3442 */
3443 i = 0;
3444 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3445 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3446 {
3447 i = (*mb_head_off)(ccline.cmdbuff,
3448 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3449 ccline.cmdpos -= i;
3450 len += i;
3451 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3452 }
3453# ifdef FEAT_ARABIC
3454 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3455 {
3456 /* Check the previous character for Arabic combining pair. */
3457 i = (*mb_head_off)(ccline.cmdbuff,
3458 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3459 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3460 + ccline.cmdpos - i), c))
3461 {
3462 ccline.cmdpos -= i;
3463 len += i;
3464 }
3465 else
3466 i = 0;
3467 }
3468# endif
3469 if (i != 0)
3470 {
3471 /* Also backup the cursor position. */
3472 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3473 ccline.cmdspos -= i;
3474 msg_col -= i;
3475 if (msg_col < 0)
3476 {
3477 msg_col += Columns;
3478 --msg_row;
3479 }
3480 }
3481 }
3482#endif
3483
3484 if (redraw && !cmd_silent)
3485 {
3486 msg_no_more = TRUE;
3487 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003488 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3490 /* Avoid clearing the rest of the line too often. */
3491 if (cmdline_row != i || ccline.overstrike)
3492 msg_clr_eos();
3493 msg_no_more = FALSE;
3494 }
3495#ifdef FEAT_FKMAP
3496 /*
3497 * If we are in Farsi command mode, the character input must be in
3498 * Insert mode. So do not advance the cmdpos.
3499 */
3500 if (!cmd_fkmap)
3501#endif
3502 {
3503 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003506 if (m < 0) /* overflow, Columns or Rows at weird value */
3507 m = MAXCOL;
3508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 else
3510 m = MAXCOL;
3511 for (i = 0; i < len; ++i)
3512 {
3513 c = cmdline_charsize(ccline.cmdpos);
3514#ifdef FEAT_MBYTE
3515 /* count ">" for a double-wide char that doesn't fit. */
3516 if (has_mbyte)
3517 correct_cmdspos(ccline.cmdpos, c);
3518#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003519 /* Stop cursor at the end of the screen, but do increment the
3520 * insert position, so that entering a very long command
3521 * works, even though you can't see it. */
3522 if (ccline.cmdspos + c < m)
3523 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#ifdef FEAT_MBYTE
3525 if (has_mbyte)
3526 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003527 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (c > len - i - 1)
3529 c = len - i - 1;
3530 ccline.cmdpos += c;
3531 i += c;
3532 }
3533#endif
3534 ++ccline.cmdpos;
3535 }
3536 }
3537 }
3538 if (redraw)
3539 msg_check();
3540 return retval;
3541}
3542
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003543static struct cmdline_info prev_ccline;
3544static int prev_ccline_used = FALSE;
3545
3546/*
3547 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3548 * and overwrite it. But get_cmdline_str() may need it, thus make it
3549 * available globally in prev_ccline.
3550 */
3551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003552save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003553{
3554 if (!prev_ccline_used)
3555 {
3556 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3557 prev_ccline_used = TRUE;
3558 }
3559 *ccp = prev_ccline;
3560 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003561 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003562}
3563
3564/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003565 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003566 */
3567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003568restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003569{
3570 ccline = prev_ccline;
3571 prev_ccline = *ccp;
3572}
3573
Bram Moolenaar8299df92004-07-10 09:47:34 +00003574/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003575 * Paste a yank register into the command line.
3576 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003577 * insert_reg() can't be used here, because special characters from the
3578 * register contents will be interpreted as commands.
3579 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003580 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581 */
3582 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003583cmdline_paste(
3584 int regname,
3585 int literally, /* Insert text literally instead of "as typed" */
3586 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003587{
3588 long i;
3589 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003590 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003591 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003592
3593 /* check for valid regname; also accept special characters for CTRL-R in
3594 * the command line */
3595 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003596 && regname != Ctrl_A && regname != Ctrl_L
3597 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003598 return FAIL;
3599
3600 /* A register containing CTRL-R can cause an endless loop. Allow using
3601 * CTRL-C to break the loop. */
3602 line_breakcheck();
3603 if (got_int)
3604 return FAIL;
3605
3606#ifdef FEAT_CLIPBOARD
3607 regname = may_get_selection(regname);
3608#endif
3609
Bram Moolenaar438d1762018-09-30 17:11:48 +02003610 // Need to set "textlock" to avoid nasty things like going to another
3611 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003612 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003613 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003614 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003615
3616 if (i)
3617 {
3618 /* Got the value of a special register in "arg". */
3619 if (arg == NULL)
3620 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003621
3622 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3623 * part of the word. */
3624 p = arg;
3625 if (p_is && regname == Ctrl_W)
3626 {
3627 char_u *w;
3628 int len;
3629
3630 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003631 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003632 {
3633#ifdef FEAT_MBYTE
3634 if (has_mbyte)
3635 {
3636 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3637 if (!vim_iswordc(mb_ptr2char(w - len)))
3638 break;
3639 w -= len;
3640 }
3641 else
3642#endif
3643 {
3644 if (!vim_iswordc(w[-1]))
3645 break;
3646 --w;
3647 }
3648 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003649 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003650 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3651 p += len;
3652 }
3653
3654 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003655 if (allocated)
3656 vim_free(arg);
3657 return OK;
3658 }
3659
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003660 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003661}
3662
3663/*
3664 * Put a string on the command line.
3665 * When "literally" is TRUE, insert literally.
3666 * When "literally" is FALSE, insert as typed, but don't leave the command
3667 * line.
3668 */
3669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003670cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003671{
3672 int c, cv;
3673
3674 if (literally)
3675 put_on_cmdline(s, -1, TRUE);
3676 else
3677 while (*s != NUL)
3678 {
3679 cv = *s;
3680 if (cv == Ctrl_V && s[1])
3681 ++s;
3682#ifdef FEAT_MBYTE
3683 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003684 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003685 else
3686#endif
3687 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003688 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3689 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003690#ifdef UNIX
3691 || c == intr_char
3692#endif
3693 || (c == Ctrl_BSL && *s == Ctrl_N))
3694 stuffcharReadbuff(Ctrl_V);
3695 stuffcharReadbuff(c);
3696 }
3697}
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699#ifdef FEAT_WILDMENU
3700/*
3701 * Delete characters on the command line, from "from" to the current
3702 * position.
3703 */
3704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003705cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3708 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3709 ccline.cmdlen -= ccline.cmdpos - from;
3710 ccline.cmdpos = from;
3711}
3712#endif
3713
3714/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003715 * This function is called when the screen size changes and with incremental
3716 * search and in other situations where the command line may have been
3717 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 */
3719 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003720redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003722 redrawcmdline_ex(TRUE);
3723}
3724
3725 void
3726redrawcmdline_ex(int do_compute_cmdrow)
3727{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (cmd_silent)
3729 return;
3730 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003731 if (do_compute_cmdrow)
3732 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 redrawcmd();
3734 cursorcmd();
3735}
3736
3737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003738redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 int i;
3741
3742 if (cmd_silent)
3743 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003744 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 msg_putchar(ccline.cmdfirstc);
3746 if (ccline.cmdprompt != NULL)
3747 {
3748 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3749 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3750 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003751 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 --ccline.cmdindent;
3753 }
3754 else
3755 for (i = ccline.cmdindent; i > 0; --i)
3756 msg_putchar(' ');
3757}
3758
3759/*
3760 * Redraw what is currently on the command line.
3761 */
3762 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003763redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 if (cmd_silent)
3766 return;
3767
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003768 /* when 'incsearch' is set there may be no command line while redrawing */
3769 if (ccline.cmdbuff == NULL)
3770 {
3771 windgoto(cmdline_row, 0);
3772 msg_clr_eos();
3773 return;
3774 }
3775
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 msg_start();
3777 redrawcmdprompt();
3778
3779 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3780 msg_no_more = TRUE;
3781 draw_cmdline(0, ccline.cmdlen);
3782 msg_clr_eos();
3783 msg_no_more = FALSE;
3784
3785 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003786 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003787 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 /*
3790 * An emsg() before may have set msg_scroll. This is used in normal mode,
3791 * in cmdline mode we can reset them now.
3792 */
3793 msg_scroll = FALSE; /* next message overwrites cmdline */
3794
3795 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3796 * in cmdline mode */
3797 skip_redraw = FALSE;
3798}
3799
3800 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003801compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003803 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 cmdline_row = Rows - 1;
3805 else
3806 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003807 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808}
3809
3810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003811cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
3813 if (cmd_silent)
3814 return;
3815
3816#ifdef FEAT_RIGHTLEFT
3817 if (cmdmsg_rl)
3818 {
3819 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3820 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3821 if (msg_row <= 0)
3822 msg_row = Rows - 1;
3823 }
3824 else
3825#endif
3826 {
3827 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3828 msg_col = ccline.cmdspos % (int)Columns;
3829 if (msg_row >= Rows)
3830 msg_row = Rows - 1;
3831 }
3832
3833 windgoto(msg_row, msg_col);
3834#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003835 if (p_imst == IM_ON_THE_SPOT)
3836 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#endif
3838#ifdef MCH_CURSOR_SHAPE
3839 mch_update_cursor();
3840#endif
3841}
3842
3843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003844gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
3846 msg_start();
3847#ifdef FEAT_RIGHTLEFT
3848 if (cmdmsg_rl)
3849 msg_col = Columns - 1;
3850 else
3851#endif
3852 msg_col = 0; /* always start in column 0 */
3853 if (clr) /* clear the bottom line(s) */
3854 msg_clr_eos(); /* will reset clear_cmdline */
3855 windgoto(cmdline_row, 0);
3856}
3857
3858/*
3859 * Check the word in front of the cursor for an abbreviation.
3860 * Called when the non-id character "c" has been entered.
3861 * When an abbreviation is recognized it is removed from the text with
3862 * backspaces and the replacement string is inserted, followed by "c".
3863 */
3864 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003865ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003867 int spos = 0;
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3870 return FALSE;
3871
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003872 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3873 * Actually accepts any mark. */
3874 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3875 spos++;
3876 if (ccline.cmdlen - spos > 5
3877 && ccline.cmdbuff[spos] == '\''
3878 && ccline.cmdbuff[spos + 2] == ','
3879 && ccline.cmdbuff[spos + 3] == '\'')
3880 spos += 5;
3881 else
3882 /* check abbreviation from the beginning of the commandline */
3883 spos = 0;
3884
3885 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886}
3887
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003888#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3889 static int
3890#ifdef __BORLANDC__
3891_RTLENTRYF
3892#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003893sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003894{
3895 char_u *p1 = *(char_u **)s1;
3896 char_u *p2 = *(char_u **)s2;
3897
3898 if (*p1 != '<' && *p2 == '<') return -1;
3899 if (*p1 == '<' && *p2 != '<') return 1;
3900 return STRCMP(p1, p2);
3901}
3902#endif
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904/*
3905 * Return FAIL if this is not an appropriate context in which to do
3906 * completion of anything, return OK if it is (even if there are no matches).
3907 * For the caller, this means that the character is just passed through like a
3908 * normal character (instead of being expanded). This allows :s/^I^D etc.
3909 */
3910 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003911nextwild(
3912 expand_T *xp,
3913 int type,
3914 int options, /* extra options for ExpandOne() */
3915 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
3917 int i, j;
3918 char_u *p1;
3919 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 int difflen;
3921 int v;
3922
3923 if (xp->xp_numfiles == -1)
3924 {
3925 set_expand_context(xp);
3926 cmd_showtail = expand_showtail(xp);
3927 }
3928
3929 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3930 {
3931 beep_flush();
3932 return OK; /* Something illegal on command line */
3933 }
3934 if (xp->xp_context == EXPAND_NOTHING)
3935 {
3936 /* Caller can use the character as a normal char instead */
3937 return FAIL;
3938 }
3939
3940 MSG_PUTS("..."); /* show that we are busy */
3941 out_flush();
3942
3943 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003944 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
3946 if (type == WILD_NEXT || type == WILD_PREV)
3947 {
3948 /*
3949 * Get next/previous match for a previous expanded pattern.
3950 */
3951 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3952 }
3953 else
3954 {
3955 /*
3956 * Translate string into pattern and expand it.
3957 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003958 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3959 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 p2 = NULL;
3961 else
3962 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003963 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003964 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3965 if (escape)
3966 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003967
3968 if (p_wic)
3969 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003970 p2 = ExpandOne(xp, p1,
3971 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003972 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003974 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (p2 != NULL && type == WILD_LONGEST)
3976 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003977 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (ccline.cmdbuff[i + j] == '*'
3979 || ccline.cmdbuff[i + j] == '?')
3980 break;
3981 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003982 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984 }
3985 }
3986
3987 if (p2 != NULL && !got_int)
3988 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003989 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003990 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003992 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 xp->xp_pattern = ccline.cmdbuff + i;
3994 }
3995 else
3996 v = OK;
3997 if (v == OK)
3998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003999 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4000 &ccline.cmdbuff[ccline.cmdpos],
4001 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4002 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 ccline.cmdlen += difflen;
4004 ccline.cmdpos += difflen;
4005 }
4006 }
4007 vim_free(p2);
4008
4009 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004010 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
4012 /* When expanding a ":map" command and no matches are found, assume that
4013 * the key is supposed to be inserted literally */
4014 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4015 return FAIL;
4016
4017 if (xp->xp_numfiles <= 0 && p2 == NULL)
4018 beep_flush();
4019 else if (xp->xp_numfiles == 1)
4020 /* free expanded pattern */
4021 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4022
4023 return OK;
4024}
4025
4026/*
4027 * Do wildcard expansion on the string 'str'.
4028 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004029 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 * Return NULL for failure.
4031 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004032 * "orig" is the originally expanded string, copied to allocated memory. It
4033 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4034 * WILD_PREV "orig" should be NULL.
4035 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004036 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4037 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 *
4039 * mode = WILD_FREE: just free previously expanded matches
4040 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4041 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4042 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4043 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4044 * mode = WILD_ALL: return all matches concatenated
4045 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004046 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 *
4048 * options = WILD_LIST_NOTFOUND: list entries without a match
4049 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4050 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4051 * options = WILD_NO_BEEP: Don't beep for multiple matches
4052 * options = WILD_ADD_SLASH: add a slash after directory names
4053 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4054 * options = WILD_SILENT: don't print warning messages
4055 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004056 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 *
4058 * The variables xp->xp_context and xp->xp_backslash must have been set!
4059 */
4060 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004061ExpandOne(
4062 expand_T *xp,
4063 char_u *str,
4064 char_u *orig, /* allocated copy of original of expanded string */
4065 int options,
4066 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
4068 char_u *ss = NULL;
4069 static int findex;
4070 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004071 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 int i;
4073 long_u len;
4074 int non_suf_match; /* number without matching suffix */
4075
4076 /*
4077 * first handle the case of using an old match
4078 */
4079 if (mode == WILD_NEXT || mode == WILD_PREV)
4080 {
4081 if (xp->xp_numfiles > 0)
4082 {
4083 if (mode == WILD_PREV)
4084 {
4085 if (findex == -1)
4086 findex = xp->xp_numfiles;
4087 --findex;
4088 }
4089 else /* mode == WILD_NEXT */
4090 ++findex;
4091
4092 /*
4093 * When wrapping around, return the original string, set findex to
4094 * -1.
4095 */
4096 if (findex < 0)
4097 {
4098 if (orig_save == NULL)
4099 findex = xp->xp_numfiles - 1;
4100 else
4101 findex = -1;
4102 }
4103 if (findex >= xp->xp_numfiles)
4104 {
4105 if (orig_save == NULL)
4106 findex = 0;
4107 else
4108 findex = -1;
4109 }
4110#ifdef FEAT_WILDMENU
4111 if (p_wmnu)
4112 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4113 findex, cmd_showtail);
4114#endif
4115 if (findex == -1)
4116 return vim_strsave(orig_save);
4117 return vim_strsave(xp->xp_files[findex]);
4118 }
4119 else
4120 return NULL;
4121 }
4122
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004123 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4125 {
4126 FreeWild(xp->xp_numfiles, xp->xp_files);
4127 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004128 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 findex = 0;
4131
4132 if (mode == WILD_FREE) /* only release file name */
4133 return NULL;
4134
4135 if (xp->xp_numfiles == -1)
4136 {
4137 vim_free(orig_save);
4138 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004139 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 /*
4142 * Do the expansion.
4143 */
4144 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4145 options) == FAIL)
4146 {
4147#ifdef FNAME_ILLEGAL
4148 /* Illegal file name has been silently skipped. But when there
4149 * are wildcards, the real problem is that there was no match,
4150 * causing the pattern to be added, which has illegal characters.
4151 */
4152 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4153 EMSG2(_(e_nomatch2), str);
4154#endif
4155 }
4156 else if (xp->xp_numfiles == 0)
4157 {
4158 if (!(options & WILD_SILENT))
4159 EMSG2(_(e_nomatch2), str);
4160 }
4161 else
4162 {
4163 /* Escape the matches for use on the command line. */
4164 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4165
4166 /*
4167 * Check for matching suffixes in file names.
4168 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004169 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4170 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
4172 if (xp->xp_numfiles)
4173 non_suf_match = xp->xp_numfiles;
4174 else
4175 non_suf_match = 1;
4176 if ((xp->xp_context == EXPAND_FILES
4177 || xp->xp_context == EXPAND_DIRECTORIES)
4178 && xp->xp_numfiles > 1)
4179 {
4180 /*
4181 * More than one match; check suffix.
4182 * The files will have been sorted on matching suffix in
4183 * expand_wildcards, only need to check the first two.
4184 */
4185 non_suf_match = 0;
4186 for (i = 0; i < 2; ++i)
4187 if (match_suffix(xp->xp_files[i]))
4188 ++non_suf_match;
4189 }
4190 if (non_suf_match != 1)
4191 {
4192 /* Can we ever get here unless it's while expanding
4193 * interactively? If not, we can get rid of this all
4194 * together. Don't really want to wait for this message
4195 * (and possibly have to hit return to continue!).
4196 */
4197 if (!(options & WILD_SILENT))
4198 EMSG(_(e_toomany));
4199 else if (!(options & WILD_NO_BEEP))
4200 beep_flush();
4201 }
4202 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4203 ss = vim_strsave(xp->xp_files[0]);
4204 }
4205 }
4206 }
4207
4208 /* Find longest common part */
4209 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4210 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004211 int mb_len = 1;
4212 int c0, ci;
4213
4214 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004216#ifdef FEAT_MBYTE
4217 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004219 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4220 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4221 }
4222 else
4223#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004224 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004225 for (i = 1; i < xp->xp_numfiles; ++i)
4226 {
4227#ifdef FEAT_MBYTE
4228 if (has_mbyte)
4229 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4230 else
4231#endif
4232 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004233 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004235 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004236 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004238 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 break;
4240 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004241 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 break;
4243 }
4244 if (i < xp->xp_numfiles)
4245 {
4246 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004247 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249 }
4250 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 ss = alloc((unsigned)len + 1);
4253 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004254 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 findex = -1; /* next p_wc gets first one */
4256 }
4257
4258 /* Concatenate all matching names */
4259 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4260 {
4261 len = 0;
4262 for (i = 0; i < xp->xp_numfiles; ++i)
4263 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4264 ss = lalloc(len, TRUE);
4265 if (ss != NULL)
4266 {
4267 *ss = NUL;
4268 for (i = 0; i < xp->xp_numfiles; ++i)
4269 {
4270 STRCAT(ss, xp->xp_files[i]);
4271 if (i != xp->xp_numfiles - 1)
4272 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4273 }
4274 }
4275 }
4276
4277 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4278 ExpandCleanup(xp);
4279
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004280 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004281 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004282 vim_free(orig);
4283
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return ss;
4285}
4286
4287/*
4288 * Prepare an expand structure for use.
4289 */
4290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004291ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004293 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004294 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004296#ifndef BACKSLASH_IN_FILENAME
4297 xp->xp_shell = FALSE;
4298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 xp->xp_numfiles = -1;
4300 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004301#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4302 xp->xp_arg = NULL;
4303#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004304 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305}
4306
4307/*
4308 * Cleanup an expand structure after use.
4309 */
4310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004311ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312{
4313 if (xp->xp_numfiles >= 0)
4314 {
4315 FreeWild(xp->xp_numfiles, xp->xp_files);
4316 xp->xp_numfiles = -1;
4317 }
4318}
4319
4320 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004321ExpandEscape(
4322 expand_T *xp,
4323 char_u *str,
4324 int numfiles,
4325 char_u **files,
4326 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 int i;
4329 char_u *p;
4330
4331 /*
4332 * May change home directory back to "~"
4333 */
4334 if (options & WILD_HOME_REPLACE)
4335 tilde_replace(str, numfiles, files);
4336
4337 if (options & WILD_ESCAPE)
4338 {
4339 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004340 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004341 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 || xp->xp_context == EXPAND_BUFFERS
4343 || xp->xp_context == EXPAND_DIRECTORIES)
4344 {
4345 /*
4346 * Insert a backslash into a file name before a space, \, %, #
4347 * and wildmatch characters, except '~'.
4348 */
4349 for (i = 0; i < numfiles; ++i)
4350 {
4351 /* for ":set path=" we need to escape spaces twice */
4352 if (xp->xp_backslash == XP_BS_THREE)
4353 {
4354 p = vim_strsave_escaped(files[i], (char_u *)" ");
4355 if (p != NULL)
4356 {
4357 vim_free(files[i]);
4358 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004359#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 p = vim_strsave_escaped(files[i], (char_u *)" ");
4361 if (p != NULL)
4362 {
4363 vim_free(files[i]);
4364 files[i] = p;
4365 }
4366#endif
4367 }
4368 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004369#ifdef BACKSLASH_IN_FILENAME
4370 p = vim_strsave_fnameescape(files[i], FALSE);
4371#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004372 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 if (p != NULL)
4375 {
4376 vim_free(files[i]);
4377 files[i] = p;
4378 }
4379
4380 /* If 'str' starts with "\~", replace "~" at start of
4381 * files[i] with "\~". */
4382 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004383 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004386
4387 /* If the first file starts with a '+' escape it. Otherwise it
4388 * could be seen as "+cmd". */
4389 if (*files[0] == '+')
4390 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392 else if (xp->xp_context == EXPAND_TAGS)
4393 {
4394 /*
4395 * Insert a backslash before characters in a tag name that
4396 * would terminate the ":tag" command.
4397 */
4398 for (i = 0; i < numfiles; ++i)
4399 {
4400 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4401 if (p != NULL)
4402 {
4403 vim_free(files[i]);
4404 files[i] = p;
4405 }
4406 }
4407 }
4408 }
4409}
4410
4411/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004412 * Escape special characters in "fname" for when used as a file name argument
4413 * after a Vim command, or, when "shell" is non-zero, a shell command.
4414 * Returns the result in allocated memory.
4415 */
4416 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004417vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004418{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004419 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004420#ifdef BACKSLASH_IN_FILENAME
4421 char_u buf[20];
4422 int j = 0;
4423
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004424 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004425 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004426 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004427 buf[j++] = *p;
4428 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004429 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004430#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004431 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4432 if (shell && csh_like_shell() && p != NULL)
4433 {
4434 char_u *s;
4435
4436 /* For csh and similar shells need to put two backslashes before '!'.
4437 * One is taken by Vim, one by the shell. */
4438 s = vim_strsave_escaped(p, (char_u *)"!");
4439 vim_free(p);
4440 p = s;
4441 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004442#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004443
4444 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4445 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004446 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004447 escape_fname(&p);
4448
4449 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004450}
4451
4452/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004453 * Put a backslash before the file name in "pp", which is in allocated memory.
4454 */
4455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004456escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004457{
4458 char_u *p;
4459
4460 p = alloc((unsigned)(STRLEN(*pp) + 2));
4461 if (p != NULL)
4462 {
4463 p[0] = '\\';
4464 STRCPY(p + 1, *pp);
4465 vim_free(*pp);
4466 *pp = p;
4467 }
4468}
4469
4470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 * For each file name in files[num_files]:
4472 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4473 */
4474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004475tilde_replace(
4476 char_u *orig_pat,
4477 int num_files,
4478 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479{
4480 int i;
4481 char_u *p;
4482
4483 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4484 {
4485 for (i = 0; i < num_files; ++i)
4486 {
4487 p = home_replace_save(NULL, files[i]);
4488 if (p != NULL)
4489 {
4490 vim_free(files[i]);
4491 files[i] = p;
4492 }
4493 }
4494 }
4495}
4496
4497/*
4498 * Show all matches for completion on the command line.
4499 * Returns EXPAND_NOTHING when the character that triggered expansion should
4500 * be inserted like a normal character.
4501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004503showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504{
4505#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4506 int num_files;
4507 char_u **files_found;
4508 int i, j, k;
4509 int maxlen;
4510 int lines;
4511 int columns;
4512 char_u *p;
4513 int lastlen;
4514 int attr;
4515 int showtail;
4516
4517 if (xp->xp_numfiles == -1)
4518 {
4519 set_expand_context(xp);
4520 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4521 &num_files, &files_found);
4522 showtail = expand_showtail(xp);
4523 if (i != EXPAND_OK)
4524 return i;
4525
4526 }
4527 else
4528 {
4529 num_files = xp->xp_numfiles;
4530 files_found = xp->xp_files;
4531 showtail = cmd_showtail;
4532 }
4533
4534#ifdef FEAT_WILDMENU
4535 if (!wildmenu)
4536 {
4537#endif
4538 msg_didany = FALSE; /* lines_left will be set */
4539 msg_start(); /* prepare for paging */
4540 msg_putchar('\n');
4541 out_flush();
4542 cmdline_row = msg_row;
4543 msg_didany = FALSE; /* lines_left will be set again */
4544 msg_start(); /* prepare for paging */
4545#ifdef FEAT_WILDMENU
4546 }
4547#endif
4548
4549 if (got_int)
4550 got_int = FALSE; /* only int. the completion, not the cmd line */
4551#ifdef FEAT_WILDMENU
4552 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004553 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#endif
4555 else
4556 {
4557 /* find the length of the longest file name */
4558 maxlen = 0;
4559 for (i = 0; i < num_files; ++i)
4560 {
4561 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004562 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 || xp->xp_context == EXPAND_BUFFERS))
4564 {
4565 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4566 j = vim_strsize(NameBuff);
4567 }
4568 else
4569 j = vim_strsize(L_SHOWFILE(i));
4570 if (j > maxlen)
4571 maxlen = j;
4572 }
4573
4574 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4575 lines = num_files;
4576 else
4577 {
4578 /* compute the number of columns and lines for the listing */
4579 maxlen += 2; /* two spaces between file names */
4580 columns = ((int)Columns + 2) / maxlen;
4581 if (columns < 1)
4582 columns = 1;
4583 lines = (num_files + columns - 1) / columns;
4584 }
4585
Bram Moolenaar8820b482017-03-16 17:23:31 +01004586 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4589 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004590 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 msg_clr_eos();
4592 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004593 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595
4596 /* list the files line by line */
4597 for (i = 0; i < lines; ++i)
4598 {
4599 lastlen = 999;
4600 for (k = i; k < num_files; k += lines)
4601 {
4602 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4603 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004604 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 p = files_found[k] + STRLEN(files_found[k]) + 1;
4606 msg_advance(maxlen + 1);
4607 msg_puts(p);
4608 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004609 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 break;
4611 }
4612 for (j = maxlen - lastlen; --j >= 0; )
4613 msg_putchar(' ');
4614 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004615 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 || xp->xp_context == EXPAND_BUFFERS)
4617 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004618 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004619 if (xp->xp_numfiles != -1)
4620 {
4621 char_u *halved_slash;
4622 char_u *exp_path;
4623
4624 /* Expansion was done before and special characters
4625 * were escaped, need to halve backslashes. Also
4626 * $HOME has been replaced with ~/. */
4627 exp_path = expand_env_save_opt(files_found[k], TRUE);
4628 halved_slash = backslash_halve_save(
4629 exp_path != NULL ? exp_path : files_found[k]);
4630 j = mch_isdir(halved_slash != NULL ? halved_slash
4631 : files_found[k]);
4632 vim_free(exp_path);
4633 vim_free(halved_slash);
4634 }
4635 else
4636 /* Expansion was done here, file names are literal. */
4637 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (showtail)
4639 p = L_SHOWFILE(k);
4640 else
4641 {
4642 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4643 TRUE);
4644 p = NameBuff;
4645 }
4646 }
4647 else
4648 {
4649 j = FALSE;
4650 p = L_SHOWFILE(k);
4651 }
4652 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4653 }
4654 if (msg_col > 0) /* when not wrapped around */
4655 {
4656 msg_clr_eos();
4657 msg_putchar('\n');
4658 }
4659 out_flush(); /* show one line at a time */
4660 if (got_int)
4661 {
4662 got_int = FALSE;
4663 break;
4664 }
4665 }
4666
4667 /*
4668 * we redraw the command below the lines that we have just listed
4669 * This is a bit tricky, but it saves a lot of screen updating.
4670 */
4671 cmdline_row = msg_row; /* will put it back later */
4672 }
4673
4674 if (xp->xp_numfiles == -1)
4675 FreeWild(num_files, files_found);
4676
4677 return EXPAND_OK;
4678}
4679
4680/*
4681 * Private gettail for showmatches() (and win_redr_status_matches()):
4682 * Find tail of file name path, but ignore trailing "/".
4683 */
4684 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004685sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 char_u *p;
4688 char_u *t = s;
4689 int had_sep = FALSE;
4690
4691 for (p = s; *p != NUL; )
4692 {
4693 if (vim_ispathsep(*p)
4694#ifdef BACKSLASH_IN_FILENAME
4695 && !rem_backslash(p)
4696#endif
4697 )
4698 had_sep = TRUE;
4699 else if (had_sep)
4700 {
4701 t = p;
4702 had_sep = FALSE;
4703 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004704 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 }
4706 return t;
4707}
4708
4709/*
4710 * Return TRUE if we only need to show the tail of completion matches.
4711 * When not completing file names or there is a wildcard in the path FALSE is
4712 * returned.
4713 */
4714 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 char_u *s;
4718 char_u *end;
4719
4720 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004721 if (xp->xp_context != EXPAND_FILES
4722 && xp->xp_context != EXPAND_SHELLCMD
4723 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return FALSE;
4725
4726 end = gettail(xp->xp_pattern);
4727 if (end == xp->xp_pattern) /* there is no path separator */
4728 return FALSE;
4729
4730 for (s = xp->xp_pattern; s < end; s++)
4731 {
4732 /* Skip escaped wildcards. Only when the backslash is not a path
4733 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4734 if (rem_backslash(s))
4735 ++s;
4736 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4737 return FALSE;
4738 }
4739 return TRUE;
4740}
4741
4742/*
4743 * Prepare a string for expansion.
4744 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004745 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 * When expanding other names: The string will be used with regcomp(). Copy
4747 * the name into allocated memory and prepend "^".
4748 */
4749 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004750addstar(
4751 char_u *fname,
4752 int len,
4753 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 char_u *retval;
4756 int i, j;
4757 int new_len;
4758 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004759 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004761 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004762 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004763 && context != EXPAND_SHELLCMD
4764 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 /*
4767 * Matching will be done internally (on something other than files).
4768 * So we convert the file-matching-type wildcards into our kind for
4769 * use with vim_regcomp(). First work out how long it will be:
4770 */
4771
4772 /* For help tags the translation is done in find_help_tags().
4773 * For a tag pattern starting with "/" no translation is needed. */
4774 if (context == EXPAND_HELP
4775 || context == EXPAND_COLORS
4776 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004777 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004778 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004779 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004780 || ((context == EXPAND_TAGS_LISTFILES
4781 || context == EXPAND_TAGS)
4782 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 retval = vim_strnsave(fname, len);
4784 else
4785 {
4786 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4787 for (i = 0; i < len; i++)
4788 {
4789 if (fname[i] == '*' || fname[i] == '~')
4790 new_len++; /* '*' needs to be replaced by ".*"
4791 '~' needs to be replaced by "\~" */
4792
4793 /* Buffer names are like file names. "." should be literal */
4794 if (context == EXPAND_BUFFERS && fname[i] == '.')
4795 new_len++; /* "." becomes "\." */
4796
4797 /* Custom expansion takes care of special things, match
4798 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004799 if ((context == EXPAND_USER_DEFINED
4800 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 new_len++; /* '\' becomes "\\" */
4802 }
4803 retval = alloc(new_len);
4804 if (retval != NULL)
4805 {
4806 retval[0] = '^';
4807 j = 1;
4808 for (i = 0; i < len; i++, j++)
4809 {
4810 /* Skip backslash. But why? At least keep it for custom
4811 * expansion. */
4812 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004813 && context != EXPAND_USER_LIST
4814 && fname[i] == '\\'
4815 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 break;
4817
4818 switch (fname[i])
4819 {
4820 case '*': retval[j++] = '.';
4821 break;
4822 case '~': retval[j++] = '\\';
4823 break;
4824 case '?': retval[j] = '.';
4825 continue;
4826 case '.': if (context == EXPAND_BUFFERS)
4827 retval[j++] = '\\';
4828 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004829 case '\\': if (context == EXPAND_USER_DEFINED
4830 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 retval[j++] = '\\';
4832 break;
4833 }
4834 retval[j] = fname[i];
4835 }
4836 retval[j] = NUL;
4837 }
4838 }
4839 }
4840 else
4841 {
4842 retval = alloc(len + 4);
4843 if (retval != NULL)
4844 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004845 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004848 * Don't add a star to *, ~, ~user, $var or `cmd`.
4849 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 * ~ would be at the start of the file name, but not the tail.
4851 * $ could be anywhere in the tail.
4852 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004853 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 */
4855 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004856 ends_in_star = (len > 0 && retval[len - 1] == '*');
4857#ifndef BACKSLASH_IN_FILENAME
4858 for (i = len - 2; i >= 0; --i)
4859 {
4860 if (retval[i] != '\\')
4861 break;
4862 ends_in_star = !ends_in_star;
4863 }
4864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004866 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 && vim_strchr(tail, '$') == NULL
4868 && vim_strchr(retval, '`') == NULL)
4869 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004870 else if (len > 0 && retval[len - 1] == '$')
4871 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 retval[len] = NUL;
4873 }
4874 }
4875 return retval;
4876}
4877
4878/*
4879 * Must parse the command line so far to work out what context we are in.
4880 * Completion can then be done based on that context.
4881 * This routine sets the variables:
4882 * xp->xp_pattern The start of the pattern to be expanded within
4883 * the command line (ends at the cursor).
4884 * xp->xp_context The type of thing to expand. Will be one of:
4885 *
4886 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4887 * the command line, like an unknown command. Caller
4888 * should beep.
4889 * EXPAND_NOTHING Unrecognised context for completion, use char like
4890 * a normal char, rather than for completion. eg
4891 * :s/^I/
4892 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4893 * it.
4894 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4895 * EXPAND_FILES After command with XFILE set, or after setting
4896 * with P_EXPAND set. eg :e ^I, :w>>^I
4897 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4898 * when we know only directories are of interest. eg
4899 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004900 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4902 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4903 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4904 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4905 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4906 * EXPAND_EVENTS Complete event names
4907 * EXPAND_SYNTAX Complete :syntax command arguments
4908 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4909 * EXPAND_AUGROUP Complete autocommand group names
4910 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4911 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4912 * eg :unmap a^I , :cunab x^I
4913 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4914 * eg :call sub^I
4915 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4916 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4917 * names in expressions, eg :while s^I
4918 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004919 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 */
4921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004922set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004924 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 if (ccline.cmdfirstc != ':'
4926#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004927 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004928 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
4930 )
4931 {
4932 xp->xp_context = EXPAND_NOTHING;
4933 return;
4934 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004935 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936}
4937
4938 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004939set_cmd_context(
4940 expand_T *xp,
4941 char_u *str, /* start of command line */
4942 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004943 int col, /* position of cursor */
4944 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 int old_char = NUL;
4947 char_u *nextcomm;
4948
4949 /*
4950 * Avoid a UMR warning from Purify, only save the character if it has been
4951 * written before.
4952 */
4953 if (col < len)
4954 old_char = str[col];
4955 str[col] = NUL;
4956 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004957
4958#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004959 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004960 {
4961# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004962 /* pass CMD_SIZE because there is no real command */
4963 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004964# endif
4965 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004966 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004967 {
4968 xp->xp_context = ccline.xp_context;
4969 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004970# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004971 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004972# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004973 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004974 else
4975#endif
4976 while (nextcomm != NULL)
4977 nextcomm = set_one_cmd_context(xp, nextcomm);
4978
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004979 /* Store the string here so that call_user_expand_func() can get to them
4980 * easily. */
4981 xp->xp_line = str;
4982 xp->xp_col = col;
4983
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 str[col] = old_char;
4985}
4986
4987/*
4988 * Expand the command line "str" from context "xp".
4989 * "xp" must have been set by set_cmd_context().
4990 * xp->xp_pattern points into "str", to where the text that is to be expanded
4991 * starts.
4992 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4993 * cursor.
4994 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4995 * key that triggered expansion literally.
4996 * Returns EXPAND_OK otherwise.
4997 */
4998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999expand_cmdline(
5000 expand_T *xp,
5001 char_u *str, /* start of command line */
5002 int col, /* position of cursor */
5003 int *matchcount, /* return: nr of matches */
5004 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005007 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5010 {
5011 beep_flush();
5012 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5013 }
5014 if (xp->xp_context == EXPAND_NOTHING)
5015 {
5016 /* Caller can use the character as a normal char instead */
5017 return EXPAND_NOTHING;
5018 }
5019
5020 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005021 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5022 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (file_str == NULL)
5024 return EXPAND_UNSUCCESSFUL;
5025
Bram Moolenaar94950a92010-12-02 16:01:29 +01005026 if (p_wic)
5027 options += WILD_ICASE;
5028
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005030 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 *matchcount = 0;
5033 *matches = NULL;
5034 }
5035 vim_free(file_str);
5036
5037 return EXPAND_OK;
5038}
5039
5040#ifdef FEAT_MULTI_LANG
5041/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005042 * Cleanup matches for help tags:
5043 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5044 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005046static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005049cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051 int i, j;
5052 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005053 char_u buf[4];
5054 char_u *p = buf;
5055
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005056 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005057 {
5058 *p++ = '@';
5059 *p++ = p_hlg[0];
5060 *p++ = p_hlg[1];
5061 }
5062 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 for (i = 0; i < num_file; ++i)
5065 {
5066 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005067 if (len <= 0)
5068 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005069 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 /* Sorting on priority means the same item in another language may
5072 * be anywhere. Search all items for a match up to the "@en". */
5073 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005074 if (j != i && (int)STRLEN(file[j]) == len + 3
5075 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005078 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 file[i][len] = NUL;
5080 }
5081 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005082
5083 if (*buf != NUL)
5084 for (i = 0; i < num_file; ++i)
5085 {
5086 len = (int)STRLEN(file[i]) - 3;
5087 if (len <= 0)
5088 continue;
5089 if (STRCMP(file[i] + len, buf) == 0)
5090 {
5091 /* remove the default language */
5092 file[i][len] = NUL;
5093 }
5094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095}
5096#endif
5097
5098/*
5099 * Do the expansion based on xp->xp_context and "pat".
5100 */
5101 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005102ExpandFromContext(
5103 expand_T *xp,
5104 char_u *pat,
5105 int *num_file,
5106 char_u ***file,
5107 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108{
5109#ifdef FEAT_CMDL_COMPL
5110 regmatch_T regmatch;
5111#endif
5112 int ret;
5113 int flags;
5114
5115 flags = EW_DIR; /* include directories */
5116 if (options & WILD_LIST_NOTFOUND)
5117 flags |= EW_NOTFOUND;
5118 if (options & WILD_ADD_SLASH)
5119 flags |= EW_ADDSLASH;
5120 if (options & WILD_KEEP_ALL)
5121 flags |= EW_KEEPALL;
5122 if (options & WILD_SILENT)
5123 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005124 if (options & WILD_ALLLINKS)
5125 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005127 if (xp->xp_context == EXPAND_FILES
5128 || xp->xp_context == EXPAND_DIRECTORIES
5129 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 /*
5132 * Expand file or directory names.
5133 */
5134 int free_pat = FALSE;
5135 int i;
5136
5137 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5138 * space */
5139 if (xp->xp_backslash != XP_BS_NONE)
5140 {
5141 free_pat = TRUE;
5142 pat = vim_strsave(pat);
5143 for (i = 0; pat[i]; ++i)
5144 if (pat[i] == '\\')
5145 {
5146 if (xp->xp_backslash == XP_BS_THREE
5147 && pat[i + 1] == '\\'
5148 && pat[i + 2] == '\\'
5149 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005150 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (xp->xp_backslash == XP_BS_ONE
5152 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005153 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155 }
5156
5157 if (xp->xp_context == EXPAND_FILES)
5158 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005159 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5160 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 else
5162 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005163 if (options & WILD_ICASE)
5164 flags |= EW_ICASE;
5165
Bram Moolenaard7834d32009-12-02 16:14:36 +00005166 /* Expand wildcards, supporting %:h and the like. */
5167 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (free_pat)
5169 vim_free(pat);
5170 return ret;
5171 }
5172
5173 *file = (char_u **)"";
5174 *num_file = 0;
5175 if (xp->xp_context == EXPAND_HELP)
5176 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005177 /* With an empty argument we would get all the help tags, which is
5178 * very slow. Get matches for "help" instead. */
5179 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5180 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182#ifdef FEAT_MULTI_LANG
5183 cleanup_help_tags(*num_file, *file);
5184#endif
5185 return OK;
5186 }
5187 return FAIL;
5188 }
5189
5190#ifndef FEAT_CMDL_COMPL
5191 return FAIL;
5192#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005193 if (xp->xp_context == EXPAND_SHELLCMD)
5194 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if (xp->xp_context == EXPAND_OLD_SETTING)
5196 return ExpandOldSetting(num_file, file);
5197 if (xp->xp_context == EXPAND_BUFFERS)
5198 return ExpandBufnames(pat, num_file, file, options);
5199 if (xp->xp_context == EXPAND_TAGS
5200 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5201 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5202 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005203 {
5204 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005205 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5206 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005209 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005210 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005211 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005212 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005213 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005214 {
5215 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005216 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005217 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005218 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005219 {
5220 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005221 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005222 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005223# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5224 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005225 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005226# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005227 if (xp->xp_context == EXPAND_PACKADD)
5228 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5231 if (regmatch.regprog == NULL)
5232 return FAIL;
5233
5234 /* set ignore-case according to p_ic, p_scs and pat */
5235 regmatch.rm_ic = ignorecase(pat);
5236
5237 if (xp->xp_context == EXPAND_SETTINGS
5238 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5239 ret = ExpandSettings(xp, &regmatch, num_file, file);
5240 else if (xp->xp_context == EXPAND_MAPPINGS)
5241 ret = ExpandMappings(&regmatch, num_file, file);
5242# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5243 else if (xp->xp_context == EXPAND_USER_DEFINED)
5244 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5245# endif
5246 else
5247 {
5248 static struct expgen
5249 {
5250 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005251 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005253 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 } tab[] =
5255 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005256 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5257 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005258 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005259 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005260#ifdef FEAT_CMDHIST
5261 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005264 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005265 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005266 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5267 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5268 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#endif
5270#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005271 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5272 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5273 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5274 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275#endif
5276#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005277 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5278 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
5280#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005281 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005283#ifdef FEAT_PROFILE
5284 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5285#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005286 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005287 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5288 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005289#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005290 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005291#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005292#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005293 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005294#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005295#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005296 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5299 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005300 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5301 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005303 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005304 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005305 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 };
5307 int i;
5308
5309 /*
5310 * Find a context in the table and call the ExpandGeneric() with the
5311 * right function to do the expansion.
5312 */
5313 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005314 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (xp->xp_context == tab[i].context)
5316 {
5317 if (tab[i].ic)
5318 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005319 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005320 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 break;
5322 }
5323 }
5324
Bram Moolenaar473de612013-06-08 18:19:48 +02005325 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 return ret;
5328#endif /* FEAT_CMDL_COMPL */
5329}
5330
5331#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5332/*
5333 * Expand a list of names.
5334 *
5335 * Generic function for command line completion. It calls a function to
5336 * obtain strings, one by one. The strings are matched against a regexp
5337 * program. Matching strings are copied into an array, which is returned.
5338 *
5339 * Returns OK when no problems encountered, FAIL for error (out of memory).
5340 */
5341 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005342ExpandGeneric(
5343 expand_T *xp,
5344 regmatch_T *regmatch,
5345 int *num_file,
5346 char_u ***file,
5347 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005349 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 int i;
5352 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005353 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 char_u *str;
5355
5356 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005357 * round == 0: count the number of matching names
5358 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005360 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
5362 for (i = 0; ; ++i)
5363 {
5364 str = (*func)(xp, i);
5365 if (str == NULL) /* end of list */
5366 break;
5367 if (*str == NUL) /* skip empty strings */
5368 continue;
5369
5370 if (vim_regexec(regmatch, str, (colnr_T)0))
5371 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005372 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005374 if (escaped)
5375 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5376 else
5377 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 (*file)[count] = str;
5379#ifdef FEAT_MENU
5380 if (func == get_menu_names && str != NULL)
5381 {
5382 /* test for separator added by get_menu_names() */
5383 str += STRLEN(str) - 1;
5384 if (*str == '\001')
5385 *str = '.';
5386 }
5387#endif
5388 }
5389 ++count;
5390 }
5391 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005392 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
5394 if (count == 0)
5395 return OK;
5396 *num_file = count;
5397 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5398 if (*file == NULL)
5399 {
5400 *file = (char_u **)"";
5401 return FAIL;
5402 }
5403 count = 0;
5404 }
5405 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005406
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005407 /* Sort the results. Keep menu's in the specified order. */
5408 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005409 {
5410 if (xp->xp_context == EXPAND_EXPRESSION
5411 || xp->xp_context == EXPAND_FUNCTIONS
5412 || xp->xp_context == EXPAND_USER_FUNC)
5413 /* <SNR> functions should be sorted to the end. */
5414 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5415 sort_func_compare);
5416 else
5417 sort_strings(*file, *num_file);
5418 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005419
Bram Moolenaar4f688582007-07-24 12:34:30 +00005420#ifdef FEAT_CMDL_COMPL
5421 /* Reset the variables used for special highlight names expansion, so that
5422 * they don't show up when getting normal highlight names by ID. */
5423 reset_expand_highlight();
5424#endif
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return OK;
5427}
5428
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005429/*
5430 * Complete a shell command.
5431 * Returns FAIL or OK;
5432 */
5433 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434expand_shellcmd(
5435 char_u *filepat, /* pattern to match with command names */
5436 int *num_file, /* return: number of matches */
5437 char_u ***file, /* return: array with matches */
5438 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005439{
5440 char_u *pat;
5441 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005442 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005443 int mustfree = FALSE;
5444 garray_T ga;
5445 char_u *buf = alloc(MAXPATHL);
5446 size_t l;
5447 char_u *s, *e;
5448 int flags = flagsarg;
5449 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005450 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005451 hashtab_T found_ht;
5452 hashitem_T *hi;
5453 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005454
5455 if (buf == NULL)
5456 return FAIL;
5457
5458 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5459 * space */
5460 pat = vim_strsave(filepat);
5461 for (i = 0; pat[i]; ++i)
5462 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005463 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005464
Bram Moolenaarb5971142015-03-21 17:32:19 +01005465 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005466
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005467 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5468 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005469 path = (char_u *)".";
5470 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005471 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005472 /* For an absolute name we don't use $PATH. */
5473 if (!mch_isFullName(pat))
5474 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005475 if (path == NULL)
5476 path = (char_u *)"";
5477 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005478
5479 /*
5480 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005481 * collect them in "ga". When "." is not in $PATH also expand for the
5482 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005483 */
5484 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005485 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005486 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005487 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005488#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005489 e = vim_strchr(s, ';');
5490#else
5491 e = vim_strchr(s, ':');
5492#endif
5493 if (e == NULL)
5494 e = s + STRLEN(s);
5495
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005496 if (*s == NUL)
5497 {
5498 if (did_curdir)
5499 break;
5500 // Find directories in the current directory, path is empty.
5501 did_curdir = TRUE;
5502 flags |= EW_DIR;
5503 }
5504 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5505 {
5506 did_curdir = TRUE;
5507 flags |= EW_DIR;
5508 }
5509 else
5510 // Do not match directories inside a $PATH item.
5511 flags &= ~EW_DIR;
5512
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005513 l = e - s;
5514 if (l > MAXPATHL - 5)
5515 break;
5516 vim_strncpy(buf, s, l);
5517 add_pathsep(buf);
5518 l = STRLEN(buf);
5519 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5520
5521 /* Expand matches in one directory of $PATH. */
5522 ret = expand_wildcards(1, &buf, num_file, file, flags);
5523 if (ret == OK)
5524 {
5525 if (ga_grow(&ga, *num_file) == FAIL)
5526 FreeWild(*num_file, *file);
5527 else
5528 {
5529 for (i = 0; i < *num_file; ++i)
5530 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005531 char_u *name = (*file)[i];
5532
5533 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005534 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005535 // Check if this name was already found.
5536 hash = hash_hash(name + l);
5537 hi = hash_lookup(&found_ht, name + l, hash);
5538 if (HASHITEM_EMPTY(hi))
5539 {
5540 // Remove the path that was prepended.
5541 STRMOVE(name, name + l);
5542 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5543 hash_add_item(&found_ht, hi, name, hash);
5544 name = NULL;
5545 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005546 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005547 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005548 }
5549 vim_free(*file);
5550 }
5551 }
5552 if (*e != NUL)
5553 ++e;
5554 }
5555 *file = ga.ga_data;
5556 *num_file = ga.ga_len;
5557
5558 vim_free(buf);
5559 vim_free(pat);
5560 if (mustfree)
5561 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005562 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005563 return OK;
5564}
5565
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5568/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005569 * Call "user_expand_func()" to invoke a user defined Vim script function and
5570 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005572 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005573call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005574 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005575 expand_T *xp,
5576 int *num_file,
5577 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005579 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005580 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005581 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005582 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005583 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
Bram Moolenaarb7515462013-06-29 12:58:33 +02005585 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005586 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 *num_file = 0;
5588 *file = NULL;
5589
Bram Moolenaarb7515462013-06-29 12:58:33 +02005590 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005591 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005592 keep = ccline.cmdbuff[ccline.cmdlen];
5593 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005594 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005595
Bram Moolenaarffa96842018-06-12 22:05:14 +02005596 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5597
5598 args[0].v_type = VAR_STRING;
5599 args[0].vval.v_string = pat;
5600 args[1].v_type = VAR_STRING;
5601 args[1].vval.v_string = xp->xp_line;
5602 args[2].v_type = VAR_NUMBER;
5603 args[2].vval.v_number = xp->xp_col;
5604 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005606 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005607
Bram Moolenaarded27a12018-08-01 19:06:03 +02005608 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005609
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005610 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005611 if (ccline.cmdbuff != NULL)
5612 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005613
Bram Moolenaarffa96842018-06-12 22:05:14 +02005614 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005615 return ret;
5616}
5617
5618/*
5619 * Expand names with a function defined by the user.
5620 */
5621 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622ExpandUserDefined(
5623 expand_T *xp,
5624 regmatch_T *regmatch,
5625 int *num_file,
5626 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005627{
5628 char_u *retstr;
5629 char_u *s;
5630 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005631 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005632 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005633 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005634
5635 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5636 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 return FAIL;
5638
5639 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005640 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 e = vim_strchr(s, '\n');
5643 if (e == NULL)
5644 e = s + STRLEN(s);
5645 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005646 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005648 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5649 *e = keep;
5650
5651 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005653 if (ga_grow(&ga, 1) == FAIL)
5654 break;
5655 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5656 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (*e != NUL)
5660 ++e;
5661 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005662 vim_free(retstr);
5663 *file = ga.ga_data;
5664 *num_file = ga.ga_len;
5665 return OK;
5666}
5667
5668/*
5669 * Expand names with a list returned by a function defined by the user.
5670 */
5671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672ExpandUserList(
5673 expand_T *xp,
5674 int *num_file,
5675 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005676{
5677 list_T *retlist;
5678 listitem_T *li;
5679 garray_T ga;
5680
5681 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5682 if (retlist == NULL)
5683 return FAIL;
5684
5685 ga_init2(&ga, (int)sizeof(char *), 3);
5686 /* Loop over the items in the list. */
5687 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5688 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005689 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5690 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005691
5692 if (ga_grow(&ga, 1) == FAIL)
5693 break;
5694
5695 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005696 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005697 ++ga.ga_len;
5698 }
5699 list_unref(retlist);
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 *file = ga.ga_data;
5702 *num_file = ga.ga_len;
5703 return OK;
5704}
5705#endif
5706
5707/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005708 * Expand color scheme, compiler or filetype names.
5709 * Search from 'runtimepath':
5710 * 'runtimepath'/{dirnames}/{pat}.vim
5711 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5712 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5713 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5714 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005715 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 */
5717 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718ExpandRTDir(
5719 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005720 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005721 int *num_file,
5722 char_u ***file,
5723 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 char_u *s;
5726 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005727 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005729 int i;
5730 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 *num_file = 0;
5733 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005734 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005735 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005737 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005739 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5740 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005742 ga_clear_strings(&ga);
5743 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005745 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005746 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005747 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005749
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005750 if (flags & DIP_START) {
5751 for (i = 0; dirnames[i] != NULL; ++i)
5752 {
5753 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5754 if (s == NULL)
5755 {
5756 ga_clear_strings(&ga);
5757 return FAIL;
5758 }
5759 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5760 globpath(p_pp, s, &ga, 0);
5761 vim_free(s);
5762 }
5763 }
5764
5765 if (flags & DIP_OPT) {
5766 for (i = 0; dirnames[i] != NULL; ++i)
5767 {
5768 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5769 if (s == NULL)
5770 {
5771 ga_clear_strings(&ga);
5772 return FAIL;
5773 }
5774 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5775 globpath(p_pp, s, &ga, 0);
5776 vim_free(s);
5777 }
5778 }
5779
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005780 for (i = 0; i < ga.ga_len; ++i)
5781 {
5782 match = ((char_u **)ga.ga_data)[i];
5783 s = match;
5784 e = s + STRLEN(s);
5785 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5786 {
5787 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005788 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005789 if (s < match || vim_ispathsep(*s))
5790 break;
5791 ++s;
5792 *e = NUL;
5793 mch_memmove(match, s, e - s + 1);
5794 }
5795 }
5796
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005797 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005798 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005799
5800 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005801 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005802 remove_duplicates(&ga);
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 *file = ga.ga_data;
5805 *num_file = ga.ga_len;
5806 return OK;
5807}
5808
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005809/*
5810 * Expand loadplugin names:
5811 * 'packpath'/pack/ * /opt/{pat}
5812 */
5813 static int
5814ExpandPackAddDir(
5815 char_u *pat,
5816 int *num_file,
5817 char_u ***file)
5818{
5819 char_u *s;
5820 char_u *e;
5821 char_u *match;
5822 garray_T ga;
5823 int i;
5824 int pat_len;
5825
5826 *num_file = 0;
5827 *file = NULL;
5828 pat_len = (int)STRLEN(pat);
5829 ga_init2(&ga, (int)sizeof(char *), 10);
5830
5831 s = alloc((unsigned)(pat_len + 26));
5832 if (s == NULL)
5833 {
5834 ga_clear_strings(&ga);
5835 return FAIL;
5836 }
5837 sprintf((char *)s, "pack/*/opt/%s*", pat);
5838 globpath(p_pp, s, &ga, 0);
5839 vim_free(s);
5840
5841 for (i = 0; i < ga.ga_len; ++i)
5842 {
5843 match = ((char_u **)ga.ga_data)[i];
5844 s = gettail(match);
5845 e = s + STRLEN(s);
5846 mch_memmove(match, s, e - s + 1);
5847 }
5848
5849 if (ga.ga_len == 0)
5850 return FAIL;
5851
5852 /* Sort and remove duplicates which can happen when specifying multiple
5853 * directories in dirnames. */
5854 remove_duplicates(&ga);
5855
5856 *file = ga.ga_data;
5857 *num_file = ga.ga_len;
5858 return OK;
5859}
5860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861#endif
5862
5863#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5864/*
5865 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005866 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005868 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005869globpath(
5870 char_u *path,
5871 char_u *file,
5872 garray_T *ga,
5873 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 expand_T xpc;
5876 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 int num_p;
5879 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 buf = alloc(MAXPATHL);
5882 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005883 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005885 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 /* Loop over all entries in {path}. */
5889 while (*path != NUL)
5890 {
5891 /* Copy one item of the path to buf[] and concatenate the file name. */
5892 copy_option_part(&path, buf, MAXPATHL, ",");
5893 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5894 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005895# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005896 /* Using the platform's path separator (\) makes vim incorrectly
5897 * treat it as an escape character, use '/' instead. */
5898 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5899 STRCAT(buf, "/");
5900# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005904 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5905 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005907 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005909 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 for (i = 0; i < num_p; ++i)
5912 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005913 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005914 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005915 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 FreeWild(num_p, p);
5920 }
5921 }
5922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
5924 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925}
5926
5927#endif
5928
5929#if defined(FEAT_CMDHIST) || defined(PROTO)
5930
5931/*********************************
5932 * Command line history stuff *
5933 *********************************/
5934
5935/*
5936 * Translate a history character to the associated type number.
5937 */
5938 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005939hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 if (c == ':')
5942 return HIST_CMD;
5943 if (c == '=')
5944 return HIST_EXPR;
5945 if (c == '@')
5946 return HIST_INPUT;
5947 if (c == '>')
5948 return HIST_DEBUG;
5949 return HIST_SEARCH; /* must be '?' or '/' */
5950}
5951
5952/*
5953 * Table of history names.
5954 * These names are used in :history and various hist...() functions.
5955 * It is sufficient to give the significant prefix of a history name.
5956 */
5957
5958static char *(history_names[]) =
5959{
5960 "cmd",
5961 "search",
5962 "expr",
5963 "input",
5964 "debug",
5965 NULL
5966};
5967
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005968#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5969/*
5970 * Function given to ExpandGeneric() to obtain the possible first
5971 * arguments of the ":history command.
5972 */
5973 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005975{
5976 static char_u compl[2] = { NUL, NUL };
5977 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005978 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005979 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5980
5981 if (idx < short_names_count)
5982 {
5983 compl[0] = (char_u)short_names[idx];
5984 return compl;
5985 }
5986 if (idx < short_names_count + history_name_count)
5987 return (char_u *)history_names[idx - short_names_count];
5988 if (idx == short_names_count + history_name_count)
5989 return (char_u *)"all";
5990 return NULL;
5991}
5992#endif
5993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994/*
5995 * init_history() - Initialize the command line history.
5996 * Also used to re-allocate the history when the size changes.
5997 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005998 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005999init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 int newlen; /* new length of history table */
6002 histentry_T *temp;
6003 int i;
6004 int j;
6005 int type;
6006
6007 /*
6008 * If size of history table changed, reallocate it
6009 */
6010 newlen = (int)p_hi;
6011 if (newlen != hislen) /* history length changed */
6012 {
6013 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6014 {
6015 if (newlen)
6016 {
6017 temp = (histentry_T *)lalloc(
6018 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6019 if (temp == NULL) /* out of memory! */
6020 {
6021 if (type == 0) /* first one: just keep the old length */
6022 {
6023 newlen = hislen;
6024 break;
6025 }
6026 /* Already changed one table, now we can only have zero
6027 * length for all tables. */
6028 newlen = 0;
6029 type = -1;
6030 continue;
6031 }
6032 }
6033 else
6034 temp = NULL;
6035 if (newlen == 0 || temp != NULL)
6036 {
6037 if (hisidx[type] < 0) /* there are no entries yet */
6038 {
6039 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006040 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 }
6042 else if (newlen > hislen) /* array becomes bigger */
6043 {
6044 for (i = 0; i <= hisidx[type]; ++i)
6045 temp[i] = history[type][i];
6046 j = i;
6047 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006048 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 for ( ; j < hislen; ++i, ++j)
6050 temp[i] = history[type][j];
6051 }
6052 else /* array becomes smaller or 0 */
6053 {
6054 j = hisidx[type];
6055 for (i = newlen - 1; ; --i)
6056 {
6057 if (i >= 0) /* copy newest entries */
6058 temp[i] = history[type][j];
6059 else /* remove older entries */
6060 vim_free(history[type][j].hisstr);
6061 if (--j < 0)
6062 j = hislen - 1;
6063 if (j == hisidx[type])
6064 break;
6065 }
6066 hisidx[type] = newlen - 1;
6067 }
6068 vim_free(history[type]);
6069 history[type] = temp;
6070 }
6071 }
6072 hislen = newlen;
6073 }
6074}
6075
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006077clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006078{
6079 hisptr->hisnum = 0;
6080 hisptr->viminfo = FALSE;
6081 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006082 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006083}
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085/*
6086 * Check if command line 'str' is already in history.
6087 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6088 */
6089 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006090in_history(
6091 int type,
6092 char_u *str,
6093 int move_to_front, /* Move the entry to the front if it exists */
6094 int sep,
6095 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 int i;
6098 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006099 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 if (hisidx[type] < 0)
6102 return FALSE;
6103 i = hisidx[type];
6104 do
6105 {
6106 if (history[type][i].hisstr == NULL)
6107 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006108
6109 /* For search history, check that the separator character matches as
6110 * well. */
6111 p = history[type][i].hisstr;
6112 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006113 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006114 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
6116 if (!move_to_front)
6117 return TRUE;
6118 last_i = i;
6119 break;
6120 }
6121 if (--i < 0)
6122 i = hislen - 1;
6123 } while (i != hisidx[type]);
6124
6125 if (last_i >= 0)
6126 {
6127 str = history[type][i].hisstr;
6128 while (i != hisidx[type])
6129 {
6130 if (++i >= hislen)
6131 i = 0;
6132 history[type][last_i] = history[type][i];
6133 last_i = i;
6134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006136 history[type][i].viminfo = FALSE;
6137 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006138 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 return TRUE;
6140 }
6141 return FALSE;
6142}
6143
6144/*
6145 * Convert history name (from table above) to its HIST_ equivalent.
6146 * When "name" is empty, return "cmd" history.
6147 * Returns -1 for unknown history name.
6148 */
6149 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006150get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 int i;
6153 int len = (int)STRLEN(name);
6154
6155 /* No argument: use current history. */
6156 if (len == 0)
6157 return hist_char2type(ccline.cmdfirstc);
6158
6159 for (i = 0; history_names[i] != NULL; ++i)
6160 if (STRNICMP(name, history_names[i], len) == 0)
6161 return i;
6162
6163 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6164 return hist_char2type(name[0]);
6165
6166 return -1;
6167}
6168
6169static int last_maptick = -1; /* last seen maptick */
6170
6171/*
6172 * Add the given string to the given history. If the string is already in the
6173 * history then it is moved to the front. "histype" may be one of he HIST_
6174 * values.
6175 */
6176 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006177add_to_history(
6178 int histype,
6179 char_u *new_entry,
6180 int in_map, /* consider maptick when inside a mapping */
6181 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183 histentry_T *hisptr;
6184 int len;
6185
6186 if (hislen == 0) /* no history */
6187 return;
6188
Bram Moolenaara939e432013-11-09 05:30:26 +01006189 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6190 return;
6191
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 /*
6193 * Searches inside the same mapping overwrite each other, so that only
6194 * the last line is kept. Be careful not to remove a line that was moved
6195 * down, only lines that were added.
6196 */
6197 if (histype == HIST_SEARCH && in_map)
6198 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006199 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
6201 /* Current line is from the same mapping, remove it */
6202 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6203 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006204 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 --hisnum[histype];
6206 if (--hisidx[HIST_SEARCH] < 0)
6207 hisidx[HIST_SEARCH] = hislen - 1;
6208 }
6209 last_maptick = -1;
6210 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006211 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
6213 if (++hisidx[histype] == hislen)
6214 hisidx[histype] = 0;
6215 hisptr = &history[histype][hisidx[histype]];
6216 vim_free(hisptr->hisstr);
6217
6218 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006219 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6221 if (hisptr->hisstr != NULL)
6222 hisptr->hisstr[len + 1] = sep;
6223
6224 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006225 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006226 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (histype == HIST_SEARCH && in_map)
6228 last_maptick = maptick;
6229 }
6230}
6231
6232#if defined(FEAT_EVAL) || defined(PROTO)
6233
6234/*
6235 * Get identifier of newest history entry.
6236 * "histype" may be one of the HIST_ values.
6237 */
6238 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006239get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6242 || hisidx[histype] < 0)
6243 return -1;
6244
6245 return history[histype][hisidx[histype]].hisnum;
6246}
6247
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 * Calculate history index from a number:
6250 * num > 0: seen as identifying number of a history entry
6251 * num < 0: relative position in history wrt newest entry
6252 * "histype" may be one of the HIST_ values.
6253 */
6254 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006255calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 int i;
6258 histentry_T *hist;
6259 int wrapped = FALSE;
6260
6261 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6262 || (i = hisidx[histype]) < 0 || num == 0)
6263 return -1;
6264
6265 hist = history[histype];
6266 if (num > 0)
6267 {
6268 while (hist[i].hisnum > num)
6269 if (--i < 0)
6270 {
6271 if (wrapped)
6272 break;
6273 i += hislen;
6274 wrapped = TRUE;
6275 }
6276 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6277 return i;
6278 }
6279 else if (-num <= hislen)
6280 {
6281 i += num + 1;
6282 if (i < 0)
6283 i += hislen;
6284 if (hist[i].hisstr != NULL)
6285 return i;
6286 }
6287 return -1;
6288}
6289
6290/*
6291 * Get a history entry by its index.
6292 * "histype" may be one of the HIST_ values.
6293 */
6294 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006295get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297 idx = calc_hist_idx(histype, idx);
6298 if (idx >= 0)
6299 return history[histype][idx].hisstr;
6300 else
6301 return (char_u *)"";
6302}
6303
6304/*
6305 * Clear all entries of a history.
6306 * "histype" may be one of the HIST_ values.
6307 */
6308 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006309clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 int i;
6312 histentry_T *hisptr;
6313
6314 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6315 {
6316 hisptr = history[histype];
6317 for (i = hislen; i--;)
6318 {
6319 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006320 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006321 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 }
6323 hisidx[histype] = -1; /* mark history as cleared */
6324 hisnum[histype] = 0; /* reset identifier counter */
6325 return OK;
6326 }
6327 return FAIL;
6328}
6329
6330/*
6331 * Remove all entries matching {str} from a history.
6332 * "histype" may be one of the HIST_ values.
6333 */
6334 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006335del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 regmatch_T regmatch;
6338 histentry_T *hisptr;
6339 int idx;
6340 int i;
6341 int last;
6342 int found = FALSE;
6343
6344 regmatch.regprog = NULL;
6345 regmatch.rm_ic = FALSE; /* always match case */
6346 if (hislen != 0
6347 && histype >= 0
6348 && histype < HIST_COUNT
6349 && *str != NUL
6350 && (idx = hisidx[histype]) >= 0
6351 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6352 != NULL)
6353 {
6354 i = last = idx;
6355 do
6356 {
6357 hisptr = &history[histype][i];
6358 if (hisptr->hisstr == NULL)
6359 break;
6360 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6361 {
6362 found = TRUE;
6363 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006364 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366 else
6367 {
6368 if (i != last)
6369 {
6370 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006371 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
6373 if (--last < 0)
6374 last += hislen;
6375 }
6376 if (--i < 0)
6377 i += hislen;
6378 } while (i != idx);
6379 if (history[histype][idx].hisstr == NULL)
6380 hisidx[histype] = -1;
6381 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006382 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 return found;
6384}
6385
6386/*
6387 * Remove an indexed entry from a history.
6388 * "histype" may be one of the HIST_ values.
6389 */
6390 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006391del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 int i, j;
6394
6395 i = calc_hist_idx(histype, idx);
6396 if (i < 0)
6397 return FALSE;
6398 idx = hisidx[histype];
6399 vim_free(history[histype][i].hisstr);
6400
6401 /* When deleting the last added search string in a mapping, reset
6402 * last_maptick, so that the last added search string isn't deleted again.
6403 */
6404 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6405 last_maptick = -1;
6406
6407 while (i != idx)
6408 {
6409 j = (i + 1) % hislen;
6410 history[histype][i] = history[histype][j];
6411 i = j;
6412 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006413 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (--i < 0)
6415 i += hislen;
6416 hisidx[histype] = i;
6417 return TRUE;
6418}
6419
6420#endif /* FEAT_EVAL */
6421
6422#if defined(FEAT_CRYPT) || defined(PROTO)
6423/*
6424 * Very specific function to remove the value in ":set key=val" from the
6425 * history.
6426 */
6427 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006428remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430 char_u *p;
6431 int i;
6432
6433 i = hisidx[HIST_CMD];
6434 if (i < 0)
6435 return;
6436 p = history[HIST_CMD][i].hisstr;
6437 if (p != NULL)
6438 for ( ; *p; ++p)
6439 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6440 {
6441 p = vim_strchr(p + 3, '=');
6442 if (p == NULL)
6443 break;
6444 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006445 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (p[i] == '\\' && p[i + 1])
6447 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006448 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 --p;
6450 }
6451}
6452#endif
6453
6454#endif /* FEAT_CMDHIST */
6455
Bram Moolenaar064154c2016-03-19 22:50:43 +01006456#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006457/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006458 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006459 * ccline and put the previous value in prev_ccline.
6460 */
6461 static struct cmdline_info *
6462get_ccline_ptr(void)
6463{
6464 if ((State & CMDLINE) == 0)
6465 return NULL;
6466 if (ccline.cmdbuff != NULL)
6467 return &ccline;
6468 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6469 return &prev_ccline;
6470 return NULL;
6471}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006472#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006473
Bram Moolenaar064154c2016-03-19 22:50:43 +01006474#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006475/*
6476 * Get the current command line in allocated memory.
6477 * Only works when the command line is being edited.
6478 * Returns NULL when something is wrong.
6479 */
6480 char_u *
6481get_cmdline_str(void)
6482{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006483 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006484
Bram Moolenaaree91c332018-09-25 22:27:35 +02006485 if (cmdline_star > 0)
6486 return NULL;
6487 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006488 if (p == NULL)
6489 return NULL;
6490 return vim_strnsave(p->cmdbuff, p->cmdlen);
6491}
6492
6493/*
6494 * Get the current command line position, counted in bytes.
6495 * Zero is the first position.
6496 * Only works when the command line is being edited.
6497 * Returns -1 when something is wrong.
6498 */
6499 int
6500get_cmdline_pos(void)
6501{
6502 struct cmdline_info *p = get_ccline_ptr();
6503
6504 if (p == NULL)
6505 return -1;
6506 return p->cmdpos;
6507}
6508
6509/*
6510 * Set the command line byte position to "pos". Zero is the first position.
6511 * Only works when the command line is being edited.
6512 * Returns 1 when failed, 0 when OK.
6513 */
6514 int
6515set_cmdline_pos(
6516 int pos)
6517{
6518 struct cmdline_info *p = get_ccline_ptr();
6519
6520 if (p == NULL)
6521 return 1;
6522
6523 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6524 * changed the command line. */
6525 if (pos < 0)
6526 new_cmdpos = 0;
6527 else
6528 new_cmdpos = pos;
6529 return 0;
6530}
6531#endif
6532
6533#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6534/*
6535 * Get the current command-line type.
6536 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6537 * Only works when the command line is being edited.
6538 * Returns NUL when something is wrong.
6539 */
6540 int
6541get_cmdline_type(void)
6542{
6543 struct cmdline_info *p = get_ccline_ptr();
6544
6545 if (p == NULL)
6546 return NUL;
6547 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006548 return
6549# ifdef FEAT_EVAL
6550 (p->input_fn) ? '@' :
6551# endif
6552 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006553 return p->cmdfirstc;
6554}
6555#endif
6556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6558/*
6559 * Get indices "num1,num2" that specify a range within a list (not a range of
6560 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6561 * Returns OK if parsed successfully, otherwise FAIL.
6562 */
6563 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006564get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
6566 int len;
6567 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006568 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 *str = skipwhite(*str);
6571 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6572 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006573 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 *str += len;
6575 *num1 = (int)num;
6576 first = TRUE;
6577 }
6578 *str = skipwhite(*str);
6579 if (**str == ',') /* parse "to" part of range */
6580 {
6581 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006582 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (len > 0)
6584 {
6585 *num2 = (int)num;
6586 *str = skipwhite(*str + len);
6587 }
6588 else if (!first) /* no number given at all */
6589 return FAIL;
6590 }
6591 else if (first) /* only one number given */
6592 *num2 = *num1;
6593 return OK;
6594}
6595#endif
6596
6597#if defined(FEAT_CMDHIST) || defined(PROTO)
6598/*
6599 * :history command - print a history
6600 */
6601 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006602ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 histentry_T *hist;
6605 int histype1 = HIST_CMD;
6606 int histype2 = HIST_CMD;
6607 int hisidx1 = 1;
6608 int hisidx2 = -1;
6609 int idx;
6610 int i, j, k;
6611 char_u *end;
6612 char_u *arg = eap->arg;
6613
6614 if (hislen == 0)
6615 {
6616 MSG(_("'history' option is zero"));
6617 return;
6618 }
6619
6620 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6621 {
6622 end = arg;
6623 while (ASCII_ISALPHA(*end)
6624 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6625 end++;
6626 i = *end;
6627 *end = NUL;
6628 histype1 = get_histtype(arg);
6629 if (histype1 == -1)
6630 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006631 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 {
6633 histype1 = 0;
6634 histype2 = HIST_COUNT-1;
6635 }
6636 else
6637 {
6638 *end = i;
6639 EMSG(_(e_trailing));
6640 return;
6641 }
6642 }
6643 else
6644 histype2 = histype1;
6645 *end = i;
6646 }
6647 else
6648 end = arg;
6649 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6650 {
6651 EMSG(_(e_trailing));
6652 return;
6653 }
6654
6655 for (; !got_int && histype1 <= histype2; ++histype1)
6656 {
6657 STRCPY(IObuff, "\n # ");
6658 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6659 MSG_PUTS_TITLE(IObuff);
6660 idx = hisidx[histype1];
6661 hist = history[histype1];
6662 j = hisidx1;
6663 k = hisidx2;
6664 if (j < 0)
6665 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6666 if (k < 0)
6667 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6668 if (idx >= 0 && j <= k)
6669 for (i = idx + 1; !got_int; ++i)
6670 {
6671 if (i == hislen)
6672 i = 0;
6673 if (hist[i].hisstr != NULL
6674 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6675 {
6676 msg_putchar('\n');
6677 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6678 hist[i].hisnum);
6679 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6680 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006681 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 else
6683 STRCAT(IObuff, hist[i].hisstr);
6684 msg_outtrans(IObuff);
6685 out_flush();
6686 }
6687 if (i == idx)
6688 break;
6689 }
6690 }
6691}
6692#endif
6693
6694#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006695/*
6696 * Buffers for history read from a viminfo file. Only valid while reading.
6697 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006698static histentry_T *viminfo_history[HIST_COUNT] =
6699 {NULL, NULL, NULL, NULL, NULL};
6700static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6701static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702static int viminfo_add_at_front = FALSE;
6703
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006704static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706/*
6707 * Translate a history type number to the associated character.
6708 */
6709 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006710hist_type2char(
6711 int type,
6712 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 if (type == HIST_CMD)
6715 return ':';
6716 if (type == HIST_SEARCH)
6717 {
6718 if (use_question)
6719 return '?';
6720 else
6721 return '/';
6722 }
6723 if (type == HIST_EXPR)
6724 return '=';
6725 return '@';
6726}
6727
6728/*
6729 * Prepare for reading the history from the viminfo file.
6730 * This allocates history arrays to store the read history lines.
6731 */
6732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 int i;
6736 int num;
6737 int type;
6738 int len;
6739
6740 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006741 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (asklen > hislen)
6743 asklen = hislen;
6744
6745 for (type = 0; type < HIST_COUNT; ++type)
6746 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006747 /* Count the number of empty spaces in the history list. Entries read
6748 * from viminfo previously are also considered empty. If there are
6749 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006751 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 num++;
6753 len = asklen;
6754 if (num > len)
6755 len = num;
6756 if (len <= 0)
6757 viminfo_history[type] = NULL;
6758 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006759 viminfo_history[type] = (histentry_T *)lalloc(
6760 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if (viminfo_history[type] == NULL)
6762 len = 0;
6763 viminfo_hislen[type] = len;
6764 viminfo_hisidx[type] = 0;
6765 }
6766}
6767
6768/*
6769 * Accept a line from the viminfo, store it in the history array when it's
6770 * new.
6771 */
6772 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006773read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 int type;
6776 long_u len;
6777 char_u *val;
6778 char_u *p;
6779
6780 type = hist_char2type(virp->vir_line[0]);
6781 if (viminfo_hisidx[type] < viminfo_hislen[type])
6782 {
6783 val = viminfo_readstring(virp, 1, TRUE);
6784 if (val != NULL && *val != NUL)
6785 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006786 int sep = (*val == ' ' ? NUL : *val);
6787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006789 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 /* Need to re-allocate to append the separator byte. */
6792 len = STRLEN(val);
6793 p = lalloc(len + 2, TRUE);
6794 if (p != NULL)
6795 {
6796 if (type == HIST_SEARCH)
6797 {
6798 /* Search entry: Move the separator from the first
6799 * column to after the NUL. */
6800 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006801 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803 else
6804 {
6805 /* Not a search entry: No separator in the viminfo
6806 * file, add a NUL separator. */
6807 mch_memmove(p, val, (size_t)len + 1);
6808 p[len + 1] = NUL;
6809 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006810 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6811 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006812 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6813 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006814 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816 }
6817 }
6818 vim_free(val);
6819 }
6820 return viminfo_readline(virp);
6821}
6822
Bram Moolenaar07219f92013-04-14 23:19:36 +02006823/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006824 * Accept a new style history line from the viminfo, store it in the history
6825 * array when it's new.
6826 */
6827 void
6828handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006829 garray_T *values,
6830 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006831{
6832 int type;
6833 long_u len;
6834 char_u *val;
6835 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006836 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006837
6838 /* Check the format:
6839 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006840 if (values->ga_len < 4
6841 || vp[0].bv_type != BVAL_NR
6842 || vp[1].bv_type != BVAL_NR
6843 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6844 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006845 return;
6846
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006847 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006848 if (type >= HIST_COUNT)
6849 return;
6850 if (viminfo_hisidx[type] < viminfo_hislen[type])
6851 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006852 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006853 if (val != NULL && *val != NUL)
6854 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006855 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6856 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006857 int idx;
6858 int overwrite = FALSE;
6859
6860 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6861 {
6862 /* If lines were written by an older Vim we need to avoid
6863 * getting duplicates. See if the entry already exists. */
6864 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6865 {
6866 p = viminfo_history[type][idx].hisstr;
6867 if (STRCMP(val, p) == 0
6868 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6869 {
6870 overwrite = TRUE;
6871 break;
6872 }
6873 }
6874
6875 if (!overwrite)
6876 {
6877 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006878 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006879 p = lalloc(len + 2, TRUE);
6880 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006881 else
6882 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006883 if (p != NULL)
6884 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006885 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006886 if (!overwrite)
6887 {
6888 mch_memmove(p, val, (size_t)len + 1);
6889 /* Put the separator after the NUL. */
6890 p[len + 1] = sep;
6891 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006892 viminfo_history[type][idx].hisnum = 0;
6893 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006894 viminfo_hisidx[type]++;
6895 }
6896 }
6897 }
6898 }
6899 }
6900}
6901
6902/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006903 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006904 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006905 static void
6906concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
6908 int idx;
6909 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006910
6911 idx = hisidx[type] + viminfo_hisidx[type];
6912 if (idx >= hislen)
6913 idx -= hislen;
6914 else if (idx < 0)
6915 idx = hislen - 1;
6916 if (viminfo_add_at_front)
6917 hisidx[type] = idx;
6918 else
6919 {
6920 if (hisidx[type] == -1)
6921 hisidx[type] = hislen - 1;
6922 do
6923 {
6924 if (history[type][idx].hisstr != NULL
6925 || history[type][idx].viminfo)
6926 break;
6927 if (++idx == hislen)
6928 idx = 0;
6929 } while (idx != hisidx[type]);
6930 if (idx != hisidx[type] && --idx < 0)
6931 idx = hislen - 1;
6932 }
6933 for (i = 0; i < viminfo_hisidx[type]; i++)
6934 {
6935 vim_free(history[type][idx].hisstr);
6936 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6937 history[type][idx].viminfo = TRUE;
6938 history[type][idx].time_set = viminfo_history[type][i].time_set;
6939 if (--idx < 0)
6940 idx = hislen - 1;
6941 }
6942 idx += 1;
6943 idx %= hislen;
6944 for (i = 0; i < viminfo_hisidx[type]; i++)
6945 {
6946 history[type][idx++].hisnum = ++hisnum[type];
6947 idx %= hislen;
6948 }
6949}
6950
6951#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6952 static int
6953#ifdef __BORLANDC__
6954_RTLENTRYF
6955#endif
6956sort_hist(const void *s1, const void *s2)
6957{
6958 histentry_T *p1 = *(histentry_T **)s1;
6959 histentry_T *p2 = *(histentry_T **)s2;
6960
6961 if (p1->time_set < p2->time_set) return -1;
6962 if (p1->time_set > p2->time_set) return 1;
6963 return 0;
6964}
6965#endif
6966
6967/*
6968 * Merge history lines from viminfo and lines typed in this Vim based on the
6969 * timestamp;
6970 */
6971 static void
6972merge_history(int type)
6973{
6974 int max_len;
6975 histentry_T **tot_hist;
6976 histentry_T *new_hist;
6977 int i;
6978 int len;
6979
6980 /* Make one long list with all entries. */
6981 max_len = hislen + viminfo_hisidx[type];
6982 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6983 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6984 if (tot_hist == NULL || new_hist == NULL)
6985 {
6986 vim_free(tot_hist);
6987 vim_free(new_hist);
6988 return;
6989 }
6990 for (i = 0; i < viminfo_hisidx[type]; i++)
6991 tot_hist[i] = &viminfo_history[type][i];
6992 len = i;
6993 for (i = 0; i < hislen; i++)
6994 if (history[type][i].hisstr != NULL)
6995 tot_hist[len++] = &history[type][i];
6996
6997 /* Sort the list on timestamp. */
6998 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6999
7000 /* Keep the newest ones. */
7001 for (i = 0; i < hislen; i++)
7002 {
7003 if (i < len)
7004 {
7005 new_hist[i] = *tot_hist[i];
7006 tot_hist[i]->hisstr = NULL;
7007 if (new_hist[i].hisnum == 0)
7008 new_hist[i].hisnum = ++hisnum[type];
7009 }
7010 else
7011 clear_hist_entry(&new_hist[i]);
7012 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007013 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007014
7015 /* Free what is not kept. */
7016 for (i = 0; i < viminfo_hisidx[type]; i++)
7017 vim_free(viminfo_history[type][i].hisstr);
7018 for (i = 0; i < hislen; i++)
7019 vim_free(history[type][i].hisstr);
7020 vim_free(history[type]);
7021 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007022 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007023}
7024
7025/*
7026 * Finish reading history lines from viminfo. Not used when writing viminfo.
7027 */
7028 void
7029finish_viminfo_history(vir_T *virp)
7030{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007032 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 for (type = 0; type < HIST_COUNT; ++type)
7035 {
7036 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007037 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007038
7039 if (merge)
7040 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007042 concat_history(type);
7043
Bram Moolenaard23a8232018-02-10 18:45:26 +01007044 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007045 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047}
7048
Bram Moolenaar438d1762018-09-30 17:11:48 +02007049 void
7050cmdline_init(void)
7051{
7052 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
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}