blob: 02c3e3009544d0b6d31ad20bd6dfd658dacf14f7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020/*
21 * Variables shared between getcmdline(), redrawcmdline() and others.
22 * These need to be saved when using CTRL-R |, that's why they are in a
23 * structure.
24 */
25struct cmdline_info
26{
27 char_u *cmdbuff; /* pointer to command line buffer */
28 int cmdbufflen; /* length of cmdbuff */
29 int cmdlen; /* number of chars in command line */
30 int cmdpos; /* current cursor position */
31 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020032 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 int cmdindent; /* number of spaces before cmdline */
34 char_u *cmdprompt; /* message in front of cmdline */
35 int cmdattr; /* attributes for prompt */
36 int overstrike; /* Typing mode on the command line. Shared by
37 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000038 expand_T *xpc; /* struct being used for expansion, xp_pattern
39 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040 int xp_context; /* type of expansion */
41# ifdef FEAT_EVAL
42 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000043 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000047/* The current cmdline_info. It is initialized in getcmdline() and after that
48 * used by other functions. When invoking getcmdline() recursively it needs
49 * to be saved with save_cmdline() and restored with restore_cmdline().
50 * TODO: make it local to getcmdline() and pass it around. */
51static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53static int cmd_showtail; /* Only show path tail in lists ? */
54
55#ifdef FEAT_EVAL
56static int new_cmdpos; /* position set by set_cmdline_pos() */
57#endif
58
Bram Moolenaara92522f2017-07-15 15:21:38 +020059static int extra_char = NUL; /* extra character to display when redrawing
60 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020061static int extra_char_shift;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#ifdef FEAT_CMDHIST
64typedef struct hist_entry
65{
66 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020067 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020069 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} histentry_T;
71
72static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
73static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
74static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
75 /* identifying (unique) number of newest history entry */
76static int hislen = 0; /* actual length of history tables */
77
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaard25c16e2016-01-29 22:13:30 +010080static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010082static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083# endif
84#endif
85
86#ifdef FEAT_RIGHTLEFT
87static int cmd_hkmap = 0; /* Hebrew mapping during command line */
88#endif
89
90#ifdef FEAT_FKMAP
91static int cmd_fkmap = 0; /* Farsi mapping during command line */
92#endif
93
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static int cmdline_charsize(int idx);
95static void set_cmdspos(void);
96static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void alloc_cmdbuff(int len);
101static int realloc_cmdbuff(int len);
102static void draw_cmdline(int start, int len);
103static void save_cmdline(struct cmdline_info *ccp);
104static void restore_cmdline(struct cmdline_info *ccp);
105static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100107static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100110static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static void redrawcmdprompt(void);
113static void cursorcmd(void);
114static int ccheck_abbr(int);
115static int nextwild(expand_T *xp, int type, int options, int escape);
116static void escape_fname(char_u **pp);
117static int showmatches(expand_T *xp, int wildmenu);
118static void set_expand_context(expand_T *xp);
119static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
120static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100123static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100124static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200125# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100129static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
130static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# endif
132#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200133#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100134static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200138static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
142static int
143#ifdef __BORLANDC__
144_RTLENTRYF
145#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100146sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147#endif
148
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200149
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200161 * Abandon the command line.
162 */
163 static void
164abandon_cmdline(void)
165{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100166 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200167 if (msg_scrolled == 0)
168 compute_cmdrow();
169 MSG("");
170 redraw_cmdline = TRUE;
171}
172
Bram Moolenaaree219b02017-12-17 14:55:01 +0100173#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200174/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100175 * Guess that the pattern matches everything. Only finds specific cases, such
176 * as a trailing \|, which can happen while typing a pattern.
177 */
178 static int
179empty_pattern(char_u *p)
180{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100181 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100182
183 /* remove trailing \v and the like */
184 while (n >= 2 && p[n - 2] == '\\'
185 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
186 n -= 2;
187 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
188}
189
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200190// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200191typedef struct {
192 colnr_T vs_curswant;
193 colnr_T vs_leftcol;
194 linenr_T vs_topline;
195# ifdef FEAT_DIFF
196 int vs_topfill;
197# endif
198 linenr_T vs_botline;
199 linenr_T vs_empty_rows;
200} viewstate_T;
201
202 static void
203save_viewstate(viewstate_T *vs)
204{
205 vs->vs_curswant = curwin->w_curswant;
206 vs->vs_leftcol = curwin->w_leftcol;
207 vs->vs_topline = curwin->w_topline;
208# ifdef FEAT_DIFF
209 vs->vs_topfill = curwin->w_topfill;
210# endif
211 vs->vs_botline = curwin->w_botline;
212 vs->vs_empty_rows = curwin->w_empty_rows;
213}
214
215 static void
216restore_viewstate(viewstate_T *vs)
217{
218 curwin->w_curswant = vs->vs_curswant;
219 curwin->w_leftcol = vs->vs_leftcol;
220 curwin->w_topline = vs->vs_topline;
221# ifdef FEAT_DIFF
222 curwin->w_topfill = vs->vs_topfill;
223# endif
224 curwin->w_botline = vs->vs_botline;
225 curwin->w_empty_rows = vs->vs_empty_rows;
226}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200227
228// Struct to store the state of 'incsearch' highlighting.
229typedef struct {
230 pos_T search_start; // where 'incsearch' starts searching
231 pos_T save_cursor;
232 viewstate_T init_viewstate;
233 viewstate_T old_viewstate;
234 pos_T match_start;
235 pos_T match_end;
236 int did_incsearch;
237 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200238 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200239} incsearch_state_T;
240
241 static void
242init_incsearch_state(incsearch_state_T *is_state)
243{
244 is_state->match_start = curwin->w_cursor;
245 is_state->did_incsearch = FALSE;
246 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200247 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200248 CLEAR_POS(&is_state->match_end);
249 is_state->save_cursor = curwin->w_cursor; // may be restored later
250 is_state->search_start = curwin->w_cursor;
251 save_viewstate(&is_state->init_viewstate);
252 save_viewstate(&is_state->old_viewstate);
253}
254
255/*
256 * First move cursor to end of match, then to the start. This
257 * moves the whole match onto the screen when 'nowrap' is set.
258 */
259 static void
260set_search_match(pos_T *t)
261{
262 t->lnum += search_match_lines;
263 t->col = search_match_endcol;
264 if (t->lnum > curbuf->b_ml.ml_line_count)
265 {
266 t->lnum = curbuf->b_ml.ml_line_count;
267 coladvance((colnr_T)MAXCOL);
268 }
269}
270
271/*
272 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200274 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200275 */
276 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200277do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
278 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200279{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200280 char_u *cmd;
281 cmdmod_T save_cmdmod = cmdmod;
282 char_u *p;
283 int delim_optional = FALSE;
284 int delim;
285 char_u *end;
286 char_u *dummy;
287 exarg_T ea;
288 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200289 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200290
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200291 *skiplen = 0;
292 *patlen = ccline.cmdlen;
293
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200294 if (!p_is || cmd_silent)
295 return FALSE;
296
297 // by default search all lines
298 search_first_line = 0;
299 search_last_line = MAXLNUM;
300
301 if (firstc == '/' || firstc == '?')
302 return TRUE;
303 if (firstc != ':')
304 return FALSE;
305
306 vim_memset(&ea, 0, sizeof(ea));
307 ea.line1 = 1;
308 ea.line2 = 1;
309 ea.cmd = ccline.cmdbuff;
310 ea.addr_type = ADDR_LINES;
311
312 parse_command_modifiers(&ea, &dummy, TRUE);
313 cmdmod = save_cmdmod;
314
315 cmd = skip_range(ea.cmd, NULL);
316 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
317 return FALSE;
318
319 // Skip over "substitute" to find the pattern separator.
320 for (p = cmd; ASCII_ISALPHA(*p); ++p)
321 ;
322 if (*skipwhite(p) == NUL)
323 return FALSE;
324
325 if (STRNCMP(cmd, "substitute", p - cmd) == 0
326 || STRNCMP(cmd, "smagic", p - cmd) == 0
327 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
328 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200329 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200330 if (*cmd == 's' && cmd[1] == 'm')
331 p_magic = TRUE;
332 else if (*cmd == 's' && cmd[1] == 'n')
333 p_magic = FALSE;
334 }
335 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
336 {
337 // skip over flags
338 while (ASCII_ISALPHA(*(p = skipwhite(p))))
339 ++p;
340 if (*p == NUL)
341 return FALSE;
342 }
343 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
344 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
345 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
346 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
347 || STRNCMP(cmd, "global", p - cmd) == 0)
348 {
349 // skip over "!"
350 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200351 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200352 p++;
353 if (*skipwhite(p) == NUL)
354 return FALSE;
355 }
356 if (*cmd != 'g')
357 delim_optional = TRUE;
358 }
359 else
360 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200361
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200362 p = skipwhite(p);
363 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
364 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200365
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200366 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200367
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200368 if (end == p && !use_last_pat)
369 return FALSE;
370
371 // Don't do 'hlsearch' highlighting if the pattern matches everything.
372 if (!use_last_pat)
373 {
374 char c = *end;
375 int empty;
376
377 *end = NUL;
378 empty = empty_pattern(p);
379 *end = c;
380 if (empty)
381 return FALSE;
382 }
383
384 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200385 *skiplen = (int)(p - ccline.cmdbuff);
386 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200387
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200388 // parse the address range
389 save_cursor = curwin->w_cursor;
390 curwin->w_cursor = is_state->search_start;
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;
466 struct cmdline_info save_ccline;
467#ifdef FEAT_RELTIME
468 proftime_T tm;
469#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200470 int next_char;
471 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472
Bram Moolenaar198cb662018-09-06 21:44:17 +0200473 // Parsing range may already set the last search pattern.
474 save_last_search_pattern();
475
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200476 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200477 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200478 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200479 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200480 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200481 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200482
483 // If there is a character waiting, search and redraw later.
484 if (char_avail())
485 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200486 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200487 is_state->incsearch_postponed = TRUE;
488 return;
489 }
490 is_state->incsearch_postponed = FALSE;
491
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200492 if (search_first_line == 0)
493 // start at the original cursor position
494 curwin->w_cursor = is_state->search_start;
495 else
496 {
497 // start at the first line in the range
498 curwin->w_cursor.lnum = search_first_line;
499 curwin->w_cursor.col = 0;
500 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200501
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200502 // Use the previous pattern for ":s//".
503 next_char = ccline.cmdbuff[skiplen + patlen];
504 use_last_pat = patlen == 0 && skiplen > 0
505 && ccline.cmdbuff[skiplen - 1] == next_char;
506
507 // If there is no pattern, don't do anything.
508 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200509 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200510 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200511 set_no_hlsearch(TRUE); // turn off previous highlight
512 redraw_all_later(SOME_VALID);
513 }
514 else
515 {
516 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
517
518 cursor_off(); // so the user knows we're busy
519 out_flush();
520 ++emsg_off; // so it doesn't beep if bad expr
521#ifdef FEAT_RELTIME
522 // Set the time limit to half a second.
523 profile_setlimit(500L, &tm);
524#endif
525 if (!p_hls)
526 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200527 if (search_first_line != 0)
528 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200529 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200530 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200531 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200532#ifdef FEAT_RELTIME
533 &tm, NULL
534#else
535 NULL, NULL
536#endif
537 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200538 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200539 --emsg_off;
540
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200541 if (curwin->w_cursor.lnum < search_first_line
542 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200543 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200544 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200545 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200546 curwin->w_cursor = is_state->search_start;
547 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200548
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549 // if interrupted while searching, behave like it failed
550 if (got_int)
551 {
552 (void)vpeekc(); // remove <C-C> from input stream
553 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200554 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200555 }
556 else if (char_avail())
557 // cancelled searching because a char was typed
558 is_state->incsearch_postponed = TRUE;
559 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200560 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561 highlight_match = TRUE; // highlight position
562 else
563 highlight_match = FALSE; // remove highlight
564
565 // First restore the old curwin values, so the screen is positioned in the
566 // same way as the actual search command.
567 restore_viewstate(&is_state->old_viewstate);
568 changed_cline_bef_curs();
569 update_topline();
570
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200571 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200572 {
573 pos_T save_pos = curwin->w_cursor;
574
575 is_state->match_start = curwin->w_cursor;
576 set_search_match(&curwin->w_cursor);
577 validate_cursor();
578 end_pos = curwin->w_cursor;
579 is_state->match_end = end_pos;
580 curwin->w_cursor = save_pos;
581 }
582 else
583 end_pos = curwin->w_cursor; // shutup gcc 4
584
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200585 // Disable 'hlsearch' highlighting if the pattern matches everything.
586 // Avoids a flash when typing "foo\|".
587 if (!use_last_pat)
588 {
589 next_char = ccline.cmdbuff[skiplen + patlen];
590 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200591 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
592 {
593 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200594 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200595 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200596 ccline.cmdbuff[skiplen + patlen] = next_char;
597 }
598
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599 validate_cursor();
600 // May redraw the status line to show the cursor position.
601 if (p_ru && curwin->w_status_height > 0)
602 curwin->w_redr_status = TRUE;
603
604 save_cmdline(&save_ccline);
605 update_screen(SOME_VALID);
606 restore_cmdline(&save_ccline);
607 restore_last_search_pattern();
608
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200609 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
610 // end of the pattern, e.g. for ":s/pat/".
611 if (ccline.cmdbuff[skiplen + patlen] != NUL)
612 curwin->w_cursor = is_state->search_start;
613 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200614 curwin->w_cursor = end_pos;
615
616 msg_starthere();
617 redrawcmdline();
618 is_state->did_incsearch = TRUE;
619}
620
621/*
622 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
623 * or previous match.
624 * Returns FAIL when jumping to cmdline_not_changed;
625 */
626 static int
627may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int firstc,
629 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200630 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200631 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200632{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200633 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200634 pos_T t;
635 char_u *pat;
636 int search_flags = SEARCH_NOOF;
637 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200638 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200639
Bram Moolenaar198cb662018-09-06 21:44:17 +0200640 // Parsing range may already set the last search pattern.
641 save_last_search_pattern();
642
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200643 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200644 {
645 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200646 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200647 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200648 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200649 {
650 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200652 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200654 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200655 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200656 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200657 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200658 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200659 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200661 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200662
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200663 cursor_off();
664 out_flush();
665 if (c == Ctrl_G)
666 {
667 t = is_state->match_end;
668 if (LT_POS(is_state->match_start, is_state->match_end))
669 // Start searching at the end of the match not at the beginning of
670 // the next column.
671 (void)decl(&t);
672 search_flags += SEARCH_COL;
673 }
674 else
675 t = is_state->match_start;
676 if (!p_hls)
677 search_flags += SEARCH_KEEP;
678 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200679 save = pat[patlen];
680 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200681 i = searchit(curwin, curbuf, &t,
682 c == Ctrl_G ? FORWARD : BACKWARD,
683 pat, count, search_flags,
684 RE_SEARCH, 0, NULL, NULL);
685 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200686 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200687 if (i)
688 {
689 is_state->search_start = is_state->match_start;
690 is_state->match_end = t;
691 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200692 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200693 {
694 // Move just before the current match, so that when nv_search
695 // finishes the cursor will be put back on the match.
696 is_state->search_start = t;
697 (void)decl(&is_state->search_start);
698 }
699 else if (c == Ctrl_G && firstc == '?')
700 {
701 // Move just after the current match, so that when nv_search
702 // finishes the cursor will be put back on the match.
703 is_state->search_start = t;
704 (void)incl(&is_state->search_start);
705 }
706 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
707 {
708 // wrap around
709 is_state->search_start = t;
710 if (firstc == '?')
711 (void)incl(&is_state->search_start);
712 else
713 (void)decl(&is_state->search_start);
714 }
715
716 set_search_match(&is_state->match_end);
717 curwin->w_cursor = is_state->match_start;
718 changed_cline_bef_curs();
719 update_topline();
720 validate_cursor();
721 highlight_match = TRUE;
722 save_viewstate(&is_state->old_viewstate);
723 update_screen(NOT_VALID);
724 redrawcmdline();
725 }
726 else
727 vim_beep(BO_ERROR);
728 restore_last_search_pattern();
729 return FAIL;
730}
731
732/*
733 * When CTRL-L typed: add character from the match to the pattern.
734 * May set "*c" to the added character.
735 * Return OK when jumping to cmdline_not_changed.
736 */
737 static int
738may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
739{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200740 int skiplen, patlen;
741
Bram Moolenaar198cb662018-09-06 21:44:17 +0200742 // Parsing range may already set the last search pattern.
743 save_last_search_pattern();
744
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200745 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200746 {
747 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200748 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200749 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200750
751 // Add a character from under the cursor for 'incsearch'.
752 if (is_state->did_incsearch)
753 {
754 curwin->w_cursor = is_state->match_end;
755 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
756 {
757 *c = gchar_cursor();
758
759 // If 'ignorecase' and 'smartcase' are set and the
760 // command line has no uppercase characters, convert
761 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200762 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200763 *c = MB_TOLOWER(*c);
764 if (*c != NUL)
765 {
766 if (*c == firstc || vim_strchr((char_u *)(
767 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
768 {
769 // put a backslash before special characters
770 stuffcharReadbuff(*c);
771 *c = '\\';
772 }
773 return FAIL;
774 }
775 }
776 }
777 return OK;
778}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200779#endif
780
Bram Moolenaar66216052017-12-16 16:33:44 +0100781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * getcmdline() - accept a command line starting with firstc.
783 *
784 * firstc == ':' get ":" command line.
785 * firstc == '/' or '?' get search pattern
786 * firstc == '=' get expression
787 * firstc == '@' get text for input() function
788 * firstc == '>' get text for debug mode
789 * firstc == NUL get text for :insert command
790 * firstc == -1 like NUL, and break on CTRL-C
791 *
792 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
793 * command line.
794 *
795 * Careful: getcmdline() can be called recursively!
796 *
797 * Return pointer to allocated string if there is a commandline, NULL
798 * otherwise.
799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100801getcmdline(
802 int firstc,
803 long count UNUSED, /* only used for incremental search */
804 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 int c;
807 int i;
808 int j;
809 int gotesc = FALSE; /* TRUE when <ESC> just typed */
810 int do_abbr; /* when TRUE check for abbr. */
811#ifdef FEAT_CMDHIST
812 char_u *lookfor = NULL; /* string to match */
813 int hiscnt; /* current history line in use */
814 int histype; /* history type to be used */
815#endif
816#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200817 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#endif
819 int did_wild_list = FALSE; /* did wild_list() recently */
820 int wim_index = 0; /* index in wim_flags[] */
821 int res;
822 int save_msg_scroll = msg_scroll;
823 int save_State = State; /* remember State when called */
824 int some_key_typed = FALSE; /* one of the keys was typed */
825#ifdef FEAT_MOUSE
826 /* mouse drag and release events are ignored, unless they are
827 * preceded with a mouse down event */
828 int ignore_drag_release = TRUE;
829#endif
830#ifdef FEAT_EVAL
831 int break_ctrl_c = FALSE;
832#endif
833 expand_T xpc;
834 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200835#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000836 /* Everything that may work recursively should save and restore the
837 * current command line in save_ccline. That includes update_screen(), a
838 * custom status line may invoke ":normal". */
839 struct cmdline_info save_ccline;
840#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200841 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843#ifdef FEAT_EVAL
844 if (firstc == -1)
845 {
846 firstc = NUL;
847 break_ctrl_c = TRUE;
848 }
849#endif
850#ifdef FEAT_RIGHTLEFT
851 /* start without Hebrew mapping for a command line */
852 if (firstc == ':' || firstc == '=' || firstc == '>')
853 cmd_hkmap = 0;
854#endif
855
856 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200859 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif
861
862 /*
863 * set some variables for redrawcmd()
864 */
865 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000866 ccline.cmdindent = (firstc > 0 ? indent : 0);
867
868 /* alloc initial ccline.cmdbuff */
869 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (ccline.cmdbuff == NULL)
871 return NULL; /* out of memory */
872 ccline.cmdlen = ccline.cmdpos = 0;
873 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100874 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000876 /* autoindent for :insert and :append */
877 if (firstc <= 0)
878 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200879 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000880 ccline.cmdbuff[indent] = NUL;
881 ccline.cmdpos = indent;
882 ccline.cmdspos = indent;
883 ccline.cmdlen = indent;
884 }
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000887 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889#ifdef FEAT_RIGHTLEFT
890 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
891 && (firstc == '/' || firstc == '?'))
892 cmdmsg_rl = TRUE;
893 else
894 cmdmsg_rl = FALSE;
895#endif
896
897 redir_off = TRUE; /* don't redirect the typed command */
898 if (!cmd_silent)
899 {
900 i = msg_scrolled;
901 msg_scrolled = 0; /* avoid wait_return message */
902 gotocmdline(TRUE);
903 msg_scrolled += i;
904 redrawcmdprompt(); /* draw prompt or indent */
905 set_cmdspos();
906 }
907 xpc.xp_context = EXPAND_NOTHING;
908 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000909#ifndef BACKSLASH_IN_FILENAME
910 xpc.xp_shell = FALSE;
911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000913#if defined(FEAT_EVAL)
914 if (ccline.input_fn)
915 {
916 xpc.xp_context = ccline.xp_context;
917 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000918# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000919 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000920# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000921 }
922#endif
923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 /*
925 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
926 * doing ":@0" when register 0 doesn't contain a CR.
927 */
928 msg_scroll = FALSE;
929
930 State = CMDLINE;
931
932 if (firstc == '/' || firstc == '?' || firstc == '@')
933 {
934 /* Use ":lmap" mappings for search pattern and input(). */
935 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
936 b_im_ptr = &curbuf->b_p_iminsert;
937 else
938 b_im_ptr = &curbuf->b_p_imsearch;
939 if (*b_im_ptr == B_IMODE_LMAP)
940 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100941#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 im_set_active(*b_im_ptr == B_IMODE_IM);
943#endif
944 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100945#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 else if (p_imcmdline)
947 im_set_active(TRUE);
948#endif
949
950#ifdef FEAT_MOUSE
951 setmouse();
952#endif
953#ifdef CURSOR_SHAPE
954 ui_cursor_shape(); /* may show different cursor shape */
955#endif
956
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000957 /* When inside an autocommand for writing "exiting" may be set and
958 * terminal mode set to cooked. Need to set raw mode here then. */
959 settmode(TMODE_RAW);
960
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200961 /* Trigger CmdlineEnter autocommands. */
962 cmdline_type = firstc == NUL ? '-' : firstc;
963 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200964
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#ifdef FEAT_CMDHIST
966 init_history();
967 hiscnt = hislen; /* set hiscnt to impossible history value */
968 histype = hist_char2type(firstc);
969#endif
970
971#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000972 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#endif
974
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200975 /* If something above caused an error, reset the flags, we do want to type
976 * and execute commands. Display may be messed up a bit. */
977 if (did_emsg)
978 redrawcmd();
979 did_emsg = FALSE;
980 got_int = FALSE;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 /*
983 * Collect the command string, handling editing keys.
984 */
985 for (;;)
986 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000987 redir_off = TRUE; /* Don't redirect the typed command.
988 Repeated, because a ":redir" inside
989 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#ifdef USE_ON_FLY_SCROLL
991 dont_scroll = FALSE; /* allow scrolling here */
992#endif
993 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
994
Bram Moolenaar72532d32018-04-07 19:09:09 +0200995 did_emsg = FALSE; /* There can't really be a reason why an error
996 that occurs while typing a command should
997 cause the command not to be executed. */
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001000
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001001 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1002 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001003 do
1004 {
1005 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001006 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (KeyTyped)
1009 {
1010 some_key_typed = TRUE;
1011#ifdef FEAT_RIGHTLEFT
1012 if (cmd_hkmap)
1013 c = hkmap(c);
1014# ifdef FEAT_FKMAP
1015 if (cmd_fkmap)
1016 c = cmdl_fkmap(c);
1017# endif
1018 if (cmdmsg_rl && !KeyStuffed)
1019 {
1020 /* Invert horizontal movements and operations. Only when
1021 * typed by the user directly, not when the result of a
1022 * mapping. */
1023 switch (c)
1024 {
1025 case K_RIGHT: c = K_LEFT; break;
1026 case K_S_RIGHT: c = K_S_LEFT; break;
1027 case K_C_RIGHT: c = K_C_LEFT; break;
1028 case K_LEFT: c = K_RIGHT; break;
1029 case K_S_LEFT: c = K_S_RIGHT; break;
1030 case K_C_LEFT: c = K_C_RIGHT; break;
1031 }
1032 }
1033#endif
1034 }
1035
1036 /*
1037 * Ignore got_int when CTRL-C was typed here.
1038 * Don't ignore it in :global, we really need to break then, e.g., for
1039 * ":g/pat/normal /pat" (without the <CR>).
1040 * Don't ignore it for the input() function.
1041 */
1042 if ((c == Ctrl_C
1043#ifdef UNIX
1044 || c == intr_char
1045#endif
1046 )
1047#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1048 && firstc != '@'
1049#endif
1050#ifdef FEAT_EVAL
1051 && !break_ctrl_c
1052#endif
1053 && !global_busy)
1054 got_int = FALSE;
1055
1056#ifdef FEAT_CMDHIST
1057 /* free old command line when finished moving around in the history
1058 * list */
1059 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001060 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001061 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 && c != K_PAGEDOWN && c != K_PAGEUP
1063 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001064 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001066 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
1068
1069 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001070 * When there are matching completions to select <S-Tab> works like
1071 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001073 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 c = Ctrl_P;
1075
1076#ifdef FEAT_WILDMENU
1077 /* Special translations for 'wildmenu' */
1078 if (did_wild_list && p_wmnu)
1079 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001080 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001082 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 c = Ctrl_N;
1084 }
1085 /* Hitting CR after "emenu Name.": complete submenu */
1086 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1087 && ccline.cmdpos > 1
1088 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1089 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1090 && (c == '\n' || c == '\r' || c == K_KENTER))
1091 c = K_DOWN;
1092#endif
1093
1094 /* free expanded names when finished walking through matches */
1095 if (xpc.xp_numfiles != -1
1096 && !(c == p_wc && KeyTyped) && c != p_wcm
1097 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1098 && c != Ctrl_L)
1099 {
1100 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1101 did_wild_list = FALSE;
1102#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001103 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105 xpc.xp_context = EXPAND_NOTHING;
1106 wim_index = 0;
1107#ifdef FEAT_WILDMENU
1108 if (p_wmnu && wild_menu_showing != 0)
1109 {
1110 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001111 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001112
1113 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001114 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
1116 if (wild_menu_showing == WM_SCROLLED)
1117 {
1118 /* Entered command line, move it up */
1119 cmdline_row--;
1120 redrawcmd();
1121 }
1122 else if (save_p_ls != -1)
1123 {
1124 /* restore 'laststatus' and 'winminheight' */
1125 p_ls = save_p_ls;
1126 p_wmh = save_p_wmh;
1127 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001128 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001130 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 redrawcmd();
1132 save_p_ls = -1;
1133 }
1134 else
1135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 redraw_statuslines();
1138 }
1139 KeyTyped = skt;
1140 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001141 if (ccline.input_fn)
1142 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144#endif
1145 }
1146
1147#ifdef FEAT_WILDMENU
1148 /* Special translations for 'wildmenu' */
1149 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1150 {
1151 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001152 if (c == K_DOWN && ccline.cmdpos > 0
1153 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001155 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 /* Hitting <Up>: Remove one submenu name in front of the
1158 * cursor */
1159 int found = FALSE;
1160
1161 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1162 i = 0;
1163 while (--j > 0)
1164 {
1165 /* check for start of menu name */
1166 if (ccline.cmdbuff[j] == ' '
1167 && ccline.cmdbuff[j - 1] != '\\')
1168 {
1169 i = j + 1;
1170 break;
1171 }
1172 /* check for start of submenu name */
1173 if (ccline.cmdbuff[j] == '.'
1174 && ccline.cmdbuff[j - 1] != '\\')
1175 {
1176 if (found)
1177 {
1178 i = j + 1;
1179 break;
1180 }
1181 else
1182 found = TRUE;
1183 }
1184 }
1185 if (i > 0)
1186 cmdline_del(i);
1187 c = p_wc;
1188 xpc.xp_context = EXPAND_NOTHING;
1189 }
1190 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001191 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001192 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001193 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
1195 char_u upseg[5];
1196
1197 upseg[0] = PATHSEP;
1198 upseg[1] = '.';
1199 upseg[2] = '.';
1200 upseg[3] = PATHSEP;
1201 upseg[4] = NUL;
1202
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001203 if (c == K_DOWN
1204 && ccline.cmdpos > 0
1205 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1206 && (ccline.cmdpos < 3
1207 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1209 {
1210 /* go down a directory */
1211 c = p_wc;
1212 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001213 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
1215 /* If in a direct ancestor, strip off one ../ to go down */
1216 int found = FALSE;
1217
1218 j = ccline.cmdpos;
1219 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1220 while (--j > i)
1221 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001222#ifdef FEAT_MBYTE
1223 if (has_mbyte)
1224 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (vim_ispathsep(ccline.cmdbuff[j]))
1227 {
1228 found = TRUE;
1229 break;
1230 }
1231 }
1232 if (found
1233 && ccline.cmdbuff[j - 1] == '.'
1234 && ccline.cmdbuff[j - 2] == '.'
1235 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1236 {
1237 cmdline_del(j - 2);
1238 c = p_wc;
1239 }
1240 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001241 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 /* go up a directory */
1244 int found = FALSE;
1245
1246 j = ccline.cmdpos - 1;
1247 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1248 while (--j > i)
1249 {
1250#ifdef FEAT_MBYTE
1251 if (has_mbyte)
1252 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1253#endif
1254 if (vim_ispathsep(ccline.cmdbuff[j])
1255#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001256 && vim_strchr((char_u *)" *?[{`$%#",
1257 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
1259 )
1260 {
1261 if (found)
1262 {
1263 i = j + 1;
1264 break;
1265 }
1266 else
1267 found = TRUE;
1268 }
1269 }
1270
1271 if (!found)
1272 j = i;
1273 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1274 j += 4;
1275 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1276 && j == i)
1277 j += 3;
1278 else
1279 j = 0;
1280 if (j > 0)
1281 {
1282 /* TODO this is only for DOS/UNIX systems - need to put in
1283 * machine-specific stuff here and in upseg init */
1284 cmdline_del(j);
1285 put_on_cmdline(upseg + 1, 3, FALSE);
1286 }
1287 else if (ccline.cmdpos > i)
1288 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001289
1290 /* Now complete in the new directory. Set KeyTyped in case the
1291 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001293 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297#endif /* FEAT_WILDMENU */
1298
1299 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1300 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1301 if (c == Ctrl_BSL)
1302 {
1303 ++no_mapping;
1304 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001305 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 --no_mapping;
1307 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001308 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1309 * is in a mapping. */
1310 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001311 || (ccline.cmdfirstc == '=' && KeyTyped)
1312#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001313 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001314#endif
1315 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 vungetc(c);
1318 c = Ctrl_BSL;
1319 }
1320#ifdef FEAT_EVAL
1321 else if (c == 'e')
1322 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001323 char_u *p = NULL;
1324 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326 /*
1327 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001328 * Need to save and restore the current command line, to be
1329 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 */
1331 if (ccline.cmdpos == ccline.cmdlen)
1332 new_cmdpos = 99999; /* keep it at the end */
1333 else
1334 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001335
1336 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001338 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (c == '=')
1340 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001341 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001342 * to avoid nasty things like going to another buffer when
1343 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001344 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001345 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001347 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001348 restore_cmdline(&save_ccline);
1349
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001350 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001352 len = (int)STRLEN(p);
1353 if (realloc_cmdbuff(len + 1) == OK)
1354 {
1355 ccline.cmdlen = len;
1356 STRCPY(ccline.cmdbuff, p);
1357 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001359 /* Restore the cursor or use the position set with
1360 * set_cmdline_pos(). */
1361 if (new_cmdpos > ccline.cmdlen)
1362 ccline.cmdpos = ccline.cmdlen;
1363 else
1364 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001366 KeyTyped = FALSE; /* Don't do p_wc completion. */
1367 redrawcmd();
1368 goto cmdline_changed;
1369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371 }
1372 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001373 got_int = FALSE; /* don't abandon the command line */
1374 did_emsg = FALSE;
1375 emsg_on_display = FALSE;
1376 redrawcmd();
1377 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379#endif
1380 else
1381 {
1382 if (c == Ctrl_G && p_im && restart_edit == 0)
1383 restart_edit = 'a';
1384 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1385 in history */
1386 goto returncmd; /* back to Normal mode */
1387 }
1388 }
1389
1390#ifdef FEAT_CMDWIN
1391 if (c == cedit_key || c == K_CMDWIN)
1392 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001393 if (ex_normal_busy == 0 && got_int == FALSE)
1394 {
1395 /*
1396 * Open a window to edit the command line (and history).
1397 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001398 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001399 some_key_typed = TRUE;
1400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402# ifdef FEAT_DIGRAPHS
1403 else
1404# endif
1405#endif
1406#ifdef FEAT_DIGRAPHS
1407 c = do_digraph(c);
1408#endif
1409
1410 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1411 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1412 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001413 /* In Ex mode a backslash escapes a newline. */
1414 if (exmode_active
1415 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001416 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001417 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001418 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001420 if (c == K_KENTER)
1421 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001423 else
1424 {
1425 gotesc = FALSE; /* Might have typed ESC previously, don't
1426 truncate the cmdline now. */
1427 if (ccheck_abbr(c + ABBR_OFF))
1428 goto cmdline_changed;
1429 if (!cmd_silent)
1430 {
1431 windgoto(msg_row, 0);
1432 out_flush();
1433 }
1434 break;
1435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437
1438 /*
1439 * Completion for 'wildchar' or 'wildcharm' key.
1440 * - hitting <ESC> twice means: abandon command line.
1441 * - wildcard expansion is only done when the 'wildchar' key is really
1442 * typed, not when it comes from a macro
1443 */
1444 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1445 {
1446 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1447 {
1448 /* if 'wildmode' contains "list" may still need to list */
1449 if (xpc.xp_numfiles > 1
1450 && !did_wild_list
1451 && (wim_flags[wim_index] & WIM_LIST))
1452 {
1453 (void)showmatches(&xpc, FALSE);
1454 redrawcmd();
1455 did_wild_list = TRUE;
1456 }
1457 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001458 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1459 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001461 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1462 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 else
1464 res = OK; /* don't insert 'wildchar' now */
1465 }
1466 else /* typed p_wc first time */
1467 {
1468 wim_index = 0;
1469 j = ccline.cmdpos;
1470 /* if 'wildmode' first contains "longest", get longest
1471 * common part */
1472 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001473 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1474 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001476 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1477 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479 /* if interrupted while completing, behave like it failed */
1480 if (got_int)
1481 {
1482 (void)vpeekc(); /* remove <C-C> from input stream */
1483 got_int = FALSE; /* don't abandon the command line */
1484 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1485#ifdef FEAT_WILDMENU
1486 xpc.xp_context = EXPAND_NOTHING;
1487#endif
1488 goto cmdline_changed;
1489 }
1490
1491 /* when more than one match, and 'wildmode' first contains
1492 * "list", or no change and 'wildmode' contains "longest,list",
1493 * list all matches */
1494 if (res == OK && xpc.xp_numfiles > 1)
1495 {
1496 /* a "longest" that didn't do anything is skipped (but not
1497 * "list:longest") */
1498 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1499 wim_index = 1;
1500 if ((wim_flags[wim_index] & WIM_LIST)
1501#ifdef FEAT_WILDMENU
1502 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1503#endif
1504 )
1505 {
1506 if (!(wim_flags[0] & WIM_LONGEST))
1507 {
1508#ifdef FEAT_WILDMENU
1509 int p_wmnu_save = p_wmnu;
1510 p_wmnu = 0;
1511#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001512 /* remove match */
1513 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#ifdef FEAT_WILDMENU
1515 p_wmnu = p_wmnu_save;
1516#endif
1517 }
1518#ifdef FEAT_WILDMENU
1519 (void)showmatches(&xpc, p_wmnu
1520 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1521#else
1522 (void)showmatches(&xpc, FALSE);
1523#endif
1524 redrawcmd();
1525 did_wild_list = TRUE;
1526 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001527 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1528 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001530 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1531 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001534 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536#ifdef FEAT_WILDMENU
1537 else if (xpc.xp_numfiles == -1)
1538 xpc.xp_context = EXPAND_NOTHING;
1539#endif
1540 }
1541 if (wim_index < 3)
1542 ++wim_index;
1543 if (c == ESC)
1544 gotesc = TRUE;
1545 if (res == OK)
1546 goto cmdline_changed;
1547 }
1548
1549 gotesc = FALSE;
1550
1551 /* <S-Tab> goes to last match, in a clumsy way */
1552 if (c == K_S_TAB && KeyTyped)
1553 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001554 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1555 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1556 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 goto cmdline_changed;
1558 }
1559
1560 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1561 c = NL;
1562
1563 do_abbr = TRUE; /* default: check for abbreviation */
1564
1565 /*
1566 * Big switch for a typed command line character.
1567 */
1568 switch (c)
1569 {
1570 case K_BS:
1571 case Ctrl_H:
1572 case K_DEL:
1573 case K_KDEL:
1574 case Ctrl_W:
1575#ifdef FEAT_FKMAP
1576 if (cmd_fkmap && c == K_BS)
1577 c = K_DEL;
1578#endif
1579 if (c == K_KDEL)
1580 c = K_DEL;
1581
1582 /*
1583 * delete current character is the same as backspace on next
1584 * character, except at end of line
1585 */
1586 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1587 ++ccline.cmdpos;
1588#ifdef FEAT_MBYTE
1589 if (has_mbyte && c == K_DEL)
1590 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1591 ccline.cmdbuff + ccline.cmdpos);
1592#endif
1593 if (ccline.cmdpos > 0)
1594 {
1595 char_u *p;
1596
1597 j = ccline.cmdpos;
1598 p = ccline.cmdbuff + j;
1599#ifdef FEAT_MBYTE
1600 if (has_mbyte)
1601 {
1602 p = mb_prevptr(ccline.cmdbuff, p);
1603 if (c == Ctrl_W)
1604 {
1605 while (p > ccline.cmdbuff && vim_isspace(*p))
1606 p = mb_prevptr(ccline.cmdbuff, p);
1607 i = mb_get_class(p);
1608 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1609 p = mb_prevptr(ccline.cmdbuff, p);
1610 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001611 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614 else
1615#endif
1616 if (c == Ctrl_W)
1617 {
1618 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1619 --p;
1620 i = vim_iswordc(p[-1]);
1621 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1622 && vim_iswordc(p[-1]) == i)
1623 --p;
1624 }
1625 else
1626 --p;
1627 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1628 ccline.cmdlen -= j - ccline.cmdpos;
1629 i = ccline.cmdpos;
1630 while (i < ccline.cmdlen)
1631 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1632
1633 /* Truncate at the end, required for multi-byte chars. */
1634 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001635#ifdef FEAT_SEARCH_EXTRA
1636 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001637 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001638 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001639 /* save view settings, so that the screen
1640 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001641 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001642 }
1643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 redrawcmd();
1645 }
1646 else if (ccline.cmdlen == 0 && c != Ctrl_W
1647 && ccline.cmdprompt == NULL && indent == 0)
1648 {
1649 /* In ex and debug mode it doesn't make sense to return. */
1650 if (exmode_active
1651#ifdef FEAT_EVAL
1652 || ccline.cmdfirstc == '>'
1653#endif
1654 )
1655 goto cmdline_not_changed;
1656
Bram Moolenaard23a8232018-02-10 18:45:26 +01001657 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (!cmd_silent)
1659 {
1660#ifdef FEAT_RIGHTLEFT
1661 if (cmdmsg_rl)
1662 msg_col = Columns;
1663 else
1664#endif
1665 msg_col = 0;
1666 msg_putchar(' '); /* delete ':' */
1667 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001668#ifdef FEAT_SEARCH_EXTRA
1669 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001670 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 redraw_cmdline = TRUE;
1673 goto returncmd; /* back to cmd mode */
1674 }
1675 goto cmdline_changed;
1676
1677 case K_INS:
1678 case K_KINS:
1679#ifdef FEAT_FKMAP
1680 /* if Farsi mode set, we are in reverse insert mode -
1681 Do not change the mode */
1682 if (cmd_fkmap)
1683 beep_flush();
1684 else
1685#endif
1686 ccline.overstrike = !ccline.overstrike;
1687#ifdef CURSOR_SHAPE
1688 ui_cursor_shape(); /* may show different cursor shape */
1689#endif
1690 goto cmdline_not_changed;
1691
1692 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001693 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 /* ":lmap" mappings exists, toggle use of mappings. */
1696 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001697#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 im_set_active(FALSE); /* Disable input method */
1699#endif
1700 if (b_im_ptr != NULL)
1701 {
1702 if (State & LANGMAP)
1703 *b_im_ptr = B_IMODE_LMAP;
1704 else
1705 *b_im_ptr = B_IMODE_NONE;
1706 }
1707 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001708#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 else
1710 {
1711 /* There are no ":lmap" mappings, toggle IM. When
1712 * 'imdisable' is set don't try getting the status, it's
1713 * always off. */
1714 if ((p_imdisable && b_im_ptr != NULL)
1715 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1716 {
1717 im_set_active(FALSE); /* Disable input method */
1718 if (b_im_ptr != NULL)
1719 *b_im_ptr = B_IMODE_NONE;
1720 }
1721 else
1722 {
1723 im_set_active(TRUE); /* Enable input method */
1724 if (b_im_ptr != NULL)
1725 *b_im_ptr = B_IMODE_IM;
1726 }
1727 }
1728#endif
1729 if (b_im_ptr != NULL)
1730 {
1731 if (b_im_ptr == &curbuf->b_p_iminsert)
1732 set_iminsert_global();
1733 else
1734 set_imsearch_global();
1735 }
1736#ifdef CURSOR_SHAPE
1737 ui_cursor_shape(); /* may show different cursor shape */
1738#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001739#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 /* Show/unshow value of 'keymap' in status lines later. */
1741 status_redraw_curbuf();
1742#endif
1743 goto cmdline_not_changed;
1744
1745/* case '@': only in very old vi */
1746 case Ctrl_U:
1747 /* delete all characters left of the cursor */
1748 j = ccline.cmdpos;
1749 ccline.cmdlen -= j;
1750 i = ccline.cmdpos = 0;
1751 while (i < ccline.cmdlen)
1752 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1753 /* Truncate at the end, required for multi-byte chars. */
1754 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001755#ifdef FEAT_SEARCH_EXTRA
1756 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001757 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 redrawcmd();
1760 goto cmdline_changed;
1761
1762#ifdef FEAT_CLIPBOARD
1763 case Ctrl_Y:
1764 /* Copy the modeless selection, if there is one. */
1765 if (clip_star.state != SELECT_CLEARED)
1766 {
1767 if (clip_star.state == SELECT_DONE)
1768 clip_copy_modeless_selection(TRUE);
1769 goto cmdline_not_changed;
1770 }
1771 break;
1772#endif
1773
1774 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1775 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001776 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001777 * ":normal" runs out of characters. */
1778 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001779 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 goto cmdline_not_changed;
1781
1782 gotesc = TRUE; /* will free ccline.cmdbuff after
1783 putting it in history */
1784 goto returncmd; /* back to cmd mode */
1785
1786 case Ctrl_R: /* insert register */
1787#ifdef USE_ON_FLY_SCROLL
1788 dont_scroll = TRUE; /* disallow scrolling here */
1789#endif
1790 putcmdline('"', TRUE);
1791 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001792 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 if (i == Ctrl_O)
1794 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1795 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001796 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001797 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 --no_mapping;
1799#ifdef FEAT_EVAL
1800 /*
1801 * Insert the result of an expression.
1802 * Need to save the current command line, to be able to enter
1803 * a new one...
1804 */
1805 new_cmdpos = -1;
1806 if (c == '=')
1807 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001808 if (ccline.cmdfirstc == '=' // can't do this recursively
1809 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
1811 beep_flush();
1812 c = ESC;
1813 }
1814 else
1815 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001816 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001818 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 }
1821#endif
1822 if (c != ESC) /* use ESC to cancel inserting register */
1823 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001824 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001825
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001826#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001827 /* When there was a serious error abort getting the
1828 * command line. */
1829 if (aborting())
1830 {
1831 gotesc = TRUE; /* will free ccline.cmdbuff after
1832 putting it in history */
1833 goto returncmd; /* back to cmd mode */
1834 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 KeyTyped = FALSE; /* Don't do p_wc completion. */
1837#ifdef FEAT_EVAL
1838 if (new_cmdpos >= 0)
1839 {
1840 /* set_cmdline_pos() was used */
1841 if (new_cmdpos > ccline.cmdlen)
1842 ccline.cmdpos = ccline.cmdlen;
1843 else
1844 ccline.cmdpos = new_cmdpos;
1845 }
1846#endif
1847 }
1848 redrawcmd();
1849 goto cmdline_changed;
1850
1851 case Ctrl_D:
1852 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1853 break; /* Use ^D as normal char instead */
1854
1855 redrawcmd();
1856 continue; /* don't do incremental search now */
1857
1858 case K_RIGHT:
1859 case K_S_RIGHT:
1860 case K_C_RIGHT:
1861 do
1862 {
1863 if (ccline.cmdpos >= ccline.cmdlen)
1864 break;
1865 i = cmdline_charsize(ccline.cmdpos);
1866 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1867 break;
1868 ccline.cmdspos += i;
1869#ifdef FEAT_MBYTE
1870 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001871 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 + ccline.cmdpos);
1873 else
1874#endif
1875 ++ccline.cmdpos;
1876 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001877 while ((c == K_S_RIGHT || c == K_C_RIGHT
1878 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1880#ifdef FEAT_MBYTE
1881 if (has_mbyte)
1882 set_cmdspos_cursor();
1883#endif
1884 goto cmdline_not_changed;
1885
1886 case K_LEFT:
1887 case K_S_LEFT:
1888 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001889 if (ccline.cmdpos == 0)
1890 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 do
1892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 --ccline.cmdpos;
1894#ifdef FEAT_MBYTE
1895 if (has_mbyte) /* move to first byte of char */
1896 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1897 ccline.cmdbuff + ccline.cmdpos);
1898#endif
1899 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1900 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001901 while (ccline.cmdpos > 0
1902 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001903 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1905#ifdef FEAT_MBYTE
1906 if (has_mbyte)
1907 set_cmdspos_cursor();
1908#endif
1909 goto cmdline_not_changed;
1910
1911 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001912 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001913 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
Bram Moolenaar4770d092006-01-12 23:22:24 +00001915#ifdef FEAT_GUI_W32
1916 /* On Win32 ignore <M-F4>, we get it when closing the window was
1917 * cancelled. */
1918 case K_F4:
1919 if (mod_mask == MOD_MASK_ALT)
1920 {
1921 redrawcmd(); /* somehow the cmdline is cleared */
1922 goto cmdline_not_changed;
1923 }
1924 break;
1925#endif
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef FEAT_MOUSE
1928 case K_MIDDLEDRAG:
1929 case K_MIDDLERELEASE:
1930 goto cmdline_not_changed; /* Ignore mouse */
1931
1932 case K_MIDDLEMOUSE:
1933# ifdef FEAT_GUI
1934 /* When GUI is active, also paste when 'mouse' is empty */
1935 if (!gui.in_use)
1936# endif
1937 if (!mouse_has(MOUSE_COMMAND))
1938 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001939# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001941 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001943# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001944 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 redrawcmd();
1946 goto cmdline_changed;
1947
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001948# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001950 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 redrawcmd();
1952 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 case K_LEFTDRAG:
1956 case K_LEFTRELEASE:
1957 case K_RIGHTDRAG:
1958 case K_RIGHTRELEASE:
1959 /* Ignore drag and release events when the button-down wasn't
1960 * seen before. */
1961 if (ignore_drag_release)
1962 goto cmdline_not_changed;
1963 /* FALLTHROUGH */
1964 case K_LEFTMOUSE:
1965 case K_RIGHTMOUSE:
1966 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1967 ignore_drag_release = TRUE;
1968 else
1969 ignore_drag_release = FALSE;
1970# ifdef FEAT_GUI
1971 /* When GUI is active, also move when 'mouse' is empty */
1972 if (!gui.in_use)
1973# endif
1974 if (!mouse_has(MOUSE_COMMAND))
1975 goto cmdline_not_changed; /* Ignore mouse */
1976# ifdef FEAT_CLIPBOARD
1977 if (mouse_row < cmdline_row && clip_star.available)
1978 {
1979 int button, is_click, is_drag;
1980
1981 /*
1982 * Handle modeless selection.
1983 */
1984 button = get_mouse_button(KEY2TERMCAP1(c),
1985 &is_click, &is_drag);
1986 if (mouse_model_popup() && button == MOUSE_LEFT
1987 && (mod_mask & MOD_MASK_SHIFT))
1988 {
1989 /* Translate shift-left to right button. */
1990 button = MOUSE_RIGHT;
1991 mod_mask &= ~MOD_MASK_SHIFT;
1992 }
1993 clip_modeless(button, is_click, is_drag);
1994 goto cmdline_not_changed;
1995 }
1996# endif
1997
1998 set_cmdspos();
1999 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2000 ++ccline.cmdpos)
2001 {
2002 i = cmdline_charsize(ccline.cmdpos);
2003 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2004 && mouse_col < ccline.cmdspos % Columns + i)
2005 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002006# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (has_mbyte)
2008 {
2009 /* Count ">" for double-wide char that doesn't fit. */
2010 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002011 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 + ccline.cmdpos) - 1;
2013 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 ccline.cmdspos += i;
2016 }
2017 goto cmdline_not_changed;
2018
2019 /* Mouse scroll wheel: ignored here */
2020 case K_MOUSEDOWN:
2021 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002022 case K_MOUSELEFT:
2023 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 /* Alternate buttons ignored here */
2025 case K_X1MOUSE:
2026 case K_X1DRAG:
2027 case K_X1RELEASE:
2028 case K_X2MOUSE:
2029 case K_X2DRAG:
2030 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002031 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 goto cmdline_not_changed;
2033
2034#endif /* FEAT_MOUSE */
2035
2036#ifdef FEAT_GUI
2037 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2038 case K_LEFTRELEASE_NM:
2039 goto cmdline_not_changed;
2040
2041 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002042 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
2044 gui_do_scroll();
2045 redrawcmd();
2046 }
2047 goto cmdline_not_changed;
2048
2049 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002050 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002052 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 redrawcmd();
2054 }
2055 goto cmdline_not_changed;
2056#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002057#ifdef FEAT_GUI_TABLINE
2058 case K_TABLINE:
2059 case K_TABMENU:
2060 /* Don't want to change any tabs here. Make sure the same tab
2061 * is still selected. */
2062 if (gui_use_tabline())
2063 gui_mch_set_curtab(tabpage_index(curtab));
2064 goto cmdline_not_changed;
2065#endif
2066
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 case K_SELECT: /* end of Select mode mapping - ignore */
2068 goto cmdline_not_changed;
2069
2070 case Ctrl_B: /* begin of command line */
2071 case K_HOME:
2072 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 case K_S_HOME:
2074 case K_C_HOME:
2075 ccline.cmdpos = 0;
2076 set_cmdspos();
2077 goto cmdline_not_changed;
2078
2079 case Ctrl_E: /* end of command line */
2080 case K_END:
2081 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 case K_S_END:
2083 case K_C_END:
2084 ccline.cmdpos = ccline.cmdlen;
2085 set_cmdspos_cursor();
2086 goto cmdline_not_changed;
2087
2088 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002089 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 break;
2091 goto cmdline_changed;
2092
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002093 case Ctrl_L:
2094#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002095 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002096 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002097#endif
2098
2099 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002100 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 break;
2102 goto cmdline_changed;
2103
2104 case Ctrl_N: /* next match */
2105 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002106 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002108 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2109 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002111 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002114 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 case K_UP:
2116 case K_DOWN:
2117 case K_S_UP:
2118 case K_S_DOWN:
2119 case K_PAGEUP:
2120 case K_KPAGEUP:
2121 case K_PAGEDOWN:
2122 case K_KPAGEDOWN:
2123 if (hislen == 0 || firstc == NUL) /* no history */
2124 goto cmdline_not_changed;
2125
2126 i = hiscnt;
2127
2128 /* save current command string so it can be restored later */
2129 if (lookfor == NULL)
2130 {
2131 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2132 goto cmdline_not_changed;
2133 lookfor[ccline.cmdpos] = NUL;
2134 }
2135
2136 j = (int)STRLEN(lookfor);
2137 for (;;)
2138 {
2139 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002140 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002141 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
2143 if (hiscnt == hislen) /* first time */
2144 hiscnt = hisidx[histype];
2145 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2146 hiscnt = hislen - 1;
2147 else if (hiscnt != hisidx[histype] + 1)
2148 --hiscnt;
2149 else /* at top of list */
2150 {
2151 hiscnt = i;
2152 break;
2153 }
2154 }
2155 else /* one step forwards */
2156 {
2157 /* on last entry, clear the line */
2158 if (hiscnt == hisidx[histype])
2159 {
2160 hiscnt = hislen;
2161 break;
2162 }
2163
2164 /* not on a history line, nothing to do */
2165 if (hiscnt == hislen)
2166 break;
2167 if (hiscnt == hislen - 1) /* wrap around */
2168 hiscnt = 0;
2169 else
2170 ++hiscnt;
2171 }
2172 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2173 {
2174 hiscnt = i;
2175 break;
2176 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002177 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002178 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 || STRNCMP(history[histype][hiscnt].hisstr,
2180 lookfor, (size_t)j) == 0)
2181 break;
2182 }
2183
2184 if (hiscnt != i) /* jumped to other entry */
2185 {
2186 char_u *p;
2187 int len;
2188 int old_firstc;
2189
2190 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002191 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if (hiscnt == hislen)
2193 p = lookfor; /* back to the old one */
2194 else
2195 p = history[histype][hiscnt].hisstr;
2196
2197 if (histype == HIST_SEARCH
2198 && p != lookfor
2199 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2200 {
2201 /* Correct for the separator character used when
2202 * adding the history entry vs the one used now.
2203 * First loop: count length.
2204 * Second loop: copy the characters. */
2205 for (i = 0; i <= 1; ++i)
2206 {
2207 len = 0;
2208 for (j = 0; p[j] != NUL; ++j)
2209 {
2210 /* Replace old sep with new sep, unless it is
2211 * escaped. */
2212 if (p[j] == old_firstc
2213 && (j == 0 || p[j - 1] != '\\'))
2214 {
2215 if (i > 0)
2216 ccline.cmdbuff[len] = firstc;
2217 }
2218 else
2219 {
2220 /* Escape new sep, unless it is already
2221 * escaped. */
2222 if (p[j] == firstc
2223 && (j == 0 || p[j - 1] != '\\'))
2224 {
2225 if (i > 0)
2226 ccline.cmdbuff[len] = '\\';
2227 ++len;
2228 }
2229 if (i > 0)
2230 ccline.cmdbuff[len] = p[j];
2231 }
2232 ++len;
2233 }
2234 if (i == 0)
2235 {
2236 alloc_cmdbuff(len);
2237 if (ccline.cmdbuff == NULL)
2238 goto returncmd;
2239 }
2240 }
2241 ccline.cmdbuff[len] = NUL;
2242 }
2243 else
2244 {
2245 alloc_cmdbuff((int)STRLEN(p));
2246 if (ccline.cmdbuff == NULL)
2247 goto returncmd;
2248 STRCPY(ccline.cmdbuff, p);
2249 }
2250
2251 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2252 redrawcmd();
2253 goto cmdline_changed;
2254 }
2255 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002256#endif
2257 goto cmdline_not_changed;
2258
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002259#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002260 case Ctrl_G: /* next match */
2261 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002262 if (may_adjust_incsearch_highlighting(
2263 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002264 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#endif
2267
2268 case Ctrl_V:
2269 case Ctrl_Q:
2270#ifdef FEAT_MOUSE
2271 ignore_drag_release = TRUE;
2272#endif
2273 putcmdline('^', TRUE);
2274 c = get_literal(); /* get next (two) character(s) */
2275 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002276 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#ifdef FEAT_MBYTE
2278 /* may need to remove ^ when composing char was typed */
2279 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2280 {
2281 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2282 msg_putchar(' ');
2283 cursorcmd();
2284 }
2285#endif
2286 break;
2287
2288#ifdef FEAT_DIGRAPHS
2289 case Ctrl_K:
2290#ifdef FEAT_MOUSE
2291 ignore_drag_release = TRUE;
2292#endif
2293 putcmdline('?', TRUE);
2294#ifdef USE_ON_FLY_SCROLL
2295 dont_scroll = TRUE; /* disallow scrolling here */
2296#endif
2297 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002298 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (c != NUL)
2300 break;
2301
2302 redrawcmd();
2303 goto cmdline_not_changed;
2304#endif /* FEAT_DIGRAPHS */
2305
2306#ifdef FEAT_RIGHTLEFT
2307 case Ctrl__: /* CTRL-_: switch language mode */
2308 if (!p_ari)
2309 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002310# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (p_altkeymap)
2312 {
2313 cmd_fkmap = !cmd_fkmap;
2314 if (cmd_fkmap) /* in Farsi always in Insert mode */
2315 ccline.overstrike = FALSE;
2316 }
2317 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 cmd_hkmap = !cmd_hkmap;
2320 goto cmdline_not_changed;
2321#endif
2322
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002323 case K_PS:
2324 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2325 goto cmdline_changed;
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 default:
2328#ifdef UNIX
2329 if (c == intr_char)
2330 {
2331 gotesc = TRUE; /* will free ccline.cmdbuff after
2332 putting it in history */
2333 goto returncmd; /* back to Normal mode */
2334 }
2335#endif
2336 /*
2337 * Normal character with no special meaning. Just set mod_mask
2338 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2339 * the string <S-Space>. This should only happen after ^V.
2340 */
2341 if (!IS_SPECIAL(c))
2342 mod_mask = 0x0;
2343 break;
2344 }
2345 /*
2346 * End of switch on command line character.
2347 * We come here if we have a normal character.
2348 */
2349
Bram Moolenaarede3e632013-06-23 16:16:19 +02002350 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#ifdef FEAT_MBYTE
2352 /* Add ABBR_OFF for characters above 0x100, this is
2353 * what check_abbr() expects. */
2354 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2355#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002356 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 goto cmdline_changed;
2358
2359 /*
2360 * put the character in the command line
2361 */
2362 if (IS_SPECIAL(c) || mod_mask != 0)
2363 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2364 else
2365 {
2366#ifdef FEAT_MBYTE
2367 if (has_mbyte)
2368 {
2369 j = (*mb_char2bytes)(c, IObuff);
2370 IObuff[j] = NUL; /* exclude composing chars */
2371 put_on_cmdline(IObuff, j, TRUE);
2372 }
2373 else
2374#endif
2375 {
2376 IObuff[0] = c;
2377 put_on_cmdline(IObuff, 1, TRUE);
2378 }
2379 }
2380 goto cmdline_changed;
2381
2382/*
2383 * This part implements incremental searches for "/" and "?"
2384 * Jump to cmdline_not_changed when a character has been read but the command
2385 * line did not change. Then we only search and redraw if something changed in
2386 * the past.
2387 * Jump to cmdline_changed when the command line did change.
2388 * (Sorry for the goto's, I know it is ugly).
2389 */
2390cmdline_not_changed:
2391#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002392 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 continue;
2394#endif
2395
2396cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002397 /* Trigger CmdlineChanged autocommands. */
2398 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002401 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402#endif
2403
2404#ifdef FEAT_RIGHTLEFT
2405 if (cmdmsg_rl
2406# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002407 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408# endif
2409 )
2410 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002411 * right-left typing. Not efficient, but it works.
2412 * Do it only when there are no characters left to read
2413 * to avoid useless intermediate redraws. */
2414 if (vpeekc() == NUL)
2415 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#endif
2417 }
2418
2419returncmd:
2420
2421#ifdef FEAT_RIGHTLEFT
2422 cmdmsg_rl = FALSE;
2423#endif
2424
2425#ifdef FEAT_FKMAP
2426 cmd_fkmap = 0;
2427#endif
2428
2429 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002430 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002433 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
2435
2436 if (ccline.cmdbuff != NULL)
2437 {
2438 /*
2439 * Put line in history buffer (":" and "=" only when it was typed).
2440 */
2441#ifdef FEAT_CMDHIST
2442 if (ccline.cmdlen && firstc != NUL
2443 && (some_key_typed || histype == HIST_SEARCH))
2444 {
2445 add_to_history(histype, ccline.cmdbuff, TRUE,
2446 histype == HIST_SEARCH ? firstc : NUL);
2447 if (firstc == ':')
2448 {
2449 vim_free(new_last_cmdline);
2450 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2451 }
2452 }
2453#endif
2454
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002455 if (gotesc)
2456 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458
2459 /*
2460 * If the screen was shifted up, redraw the whole screen (later).
2461 * If the line is too long, clear it, so ruler and shown command do
2462 * not get printed in the middle of it.
2463 */
2464 msg_check();
2465 msg_scroll = save_msg_scroll;
2466 redir_off = FALSE;
2467
2468 /* When the command line was typed, no need for a wait-return prompt. */
2469 if (some_key_typed)
2470 need_wait_return = FALSE;
2471
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002472 /* Trigger CmdlineLeave autocommands. */
2473 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002476#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2478 im_save_status(b_im_ptr);
2479 im_set_active(FALSE);
2480#endif
2481#ifdef FEAT_MOUSE
2482 setmouse();
2483#endif
2484#ifdef CURSOR_SHAPE
2485 ui_cursor_shape(); /* may show different cursor shape */
2486#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002487 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002489 {
2490 char_u *p = ccline.cmdbuff;
2491
2492 /* Make ccline empty, getcmdline() may try to use it. */
2493 ccline.cmdbuff = NULL;
2494 return p;
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496}
2497
2498#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2499/*
2500 * Get a command line with a prompt.
2501 * This is prepared to be called recursively from getcmdline() (e.g. by
2502 * f_input() when evaluating an expression from CTRL-R =).
2503 * Returns the command line in allocated memory, or NULL.
2504 */
2505 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002506getcmdline_prompt(
2507 int firstc,
2508 char_u *prompt, /* command line prompt */
2509 int attr, /* attributes for prompt */
2510 int xp_context, /* type of expansion */
2511 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 char_u *s;
2514 struct cmdline_info save_ccline;
2515 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002516 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002518 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 ccline.cmdprompt = prompt;
2520 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002521# ifdef FEAT_EVAL
2522 ccline.xp_context = xp_context;
2523 ccline.xp_arg = xp_arg;
2524 ccline.input_fn = (firstc == '@');
2525# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002526 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002528 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002529 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002530 /* Restore msg_col, the prompt from input() may have changed it.
2531 * But only if called recursively and the commandline is therefore being
2532 * restored to an old one; if not, the input() prompt stays on the screen,
2533 * so we need its modified msg_col left intact. */
2534 if (ccline.cmdbuff != NULL)
2535 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
2537 return s;
2538}
2539#endif
2540
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002541/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002542 * Return TRUE when the text must not be changed and we can't switch to
2543 * another window or buffer. Used when editing the command line, evaluating
2544 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002545 */
2546 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002547text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002548{
2549#ifdef FEAT_CMDWIN
2550 if (cmdwin_type != 0)
2551 return TRUE;
2552#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002553 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002554}
2555
2556/*
2557 * Give an error message for a command that isn't allowed while the cmdline
2558 * window is open or editing the cmdline in another way.
2559 */
2560 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002561text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002562{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002563 EMSG(_(get_text_locked_msg()));
2564}
2565
2566 char_u *
2567get_text_locked_msg(void)
2568{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002569#ifdef FEAT_CMDWIN
2570 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002571 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002572#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002573 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002574}
2575
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002576/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002577 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2578 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002579 */
2580 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002581curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002582{
2583 if (curbuf_lock > 0)
2584 {
2585 EMSG(_("E788: Not allowed to edit another buffer now"));
2586 return TRUE;
2587 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002588 return allbuf_locked();
2589}
2590
2591/*
2592 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2593 * message.
2594 */
2595 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002596allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002597{
2598 if (allbuf_lock > 0)
2599 {
2600 EMSG(_("E811: Not allowed to change buffer information now"));
2601 return TRUE;
2602 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002603 return FALSE;
2604}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002607cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2610 if (cmdline_star > 0) /* showing '*', always 1 position */
2611 return 1;
2612#endif
2613 return ptr2cells(ccline.cmdbuff + idx);
2614}
2615
2616/*
2617 * Compute the offset of the cursor on the command line for the prompt and
2618 * indent.
2619 */
2620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002621set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002623 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 ccline.cmdspos = 1 + ccline.cmdindent;
2625 else
2626 ccline.cmdspos = 0 + ccline.cmdindent;
2627}
2628
2629/*
2630 * Compute the screen position for the cursor on the command line.
2631 */
2632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002633set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635 int i, m, c;
2636
2637 set_cmdspos();
2638 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002641 if (m < 0) /* overflow, Columns or Rows at weird value */
2642 m = MAXCOL;
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 else
2645 m = MAXCOL;
2646 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2647 {
2648 c = cmdline_charsize(i);
2649#ifdef FEAT_MBYTE
2650 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2651 if (has_mbyte)
2652 correct_cmdspos(i, c);
2653#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002654 /* If the cmdline doesn't fit, show cursor on last visible char.
2655 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if ((ccline.cmdspos += c) >= m)
2657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 ccline.cmdspos -= c;
2659 break;
2660 }
2661#ifdef FEAT_MBYTE
2662 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002663 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665 }
2666}
2667
2668#ifdef FEAT_MBYTE
2669/*
2670 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2671 * character that doesn't fit, so that a ">" must be displayed.
2672 */
2673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002674correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002676 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2678 && ccline.cmdspos % Columns + cells > Columns)
2679 ccline.cmdspos++;
2680}
2681#endif
2682
2683/*
2684 * Get an Ex command line for the ":" command.
2685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002687getexline(
2688 int c, /* normally ':', NUL for ":append" */
2689 void *cookie UNUSED,
2690 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 /* When executing a register, remove ':' that's in front of each line. */
2693 if (exec_from_reg && vpeekc() == ':')
2694 (void)vgetc();
2695 return getcmdline(c, 1L, indent);
2696}
2697
2698/*
2699 * Get an Ex command line for Ex mode.
2700 * In Ex mode we only use the OS supplied line editing features and no
2701 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002705getexmodeline(
2706 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002708 void *cookie UNUSED,
2709 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002711 garray_T line_ga;
2712 char_u *pend;
2713 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002714 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 int escaped = FALSE; /* CTRL-V typed */
2716 int vcol = 0;
2717 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002718 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002719 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 /* Switch cursor on now. This avoids that it happens after the "\n", which
2722 * confuses the system function that computes tabstops. */
2723 cursor_on();
2724
2725 /* always start in column 0; write a newline if necessary */
2726 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002731 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732 if (p_prompt)
2733 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 while (indent-- > 0)
2735 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
2738
2739 ga_init2(&line_ga, 1, 30);
2740
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002741 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002742 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002743 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002744 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002745 while (indent >= 8)
2746 {
2747 ga_append(&line_ga, TAB);
2748 msg_puts((char_u *)" ");
2749 indent -= 8;
2750 }
2751 while (indent-- > 0)
2752 {
2753 ga_append(&line_ga, ' ');
2754 msg_putchar(' ');
2755 }
2756 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757 ++no_mapping;
2758 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 /*
2761 * Get the line, one character at a time.
2762 */
2763 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002766 long sw;
2767 char_u *s;
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (ga_grow(&line_ga, 40) == FAIL)
2770 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
Bram Moolenaarba748c82017-02-26 14:00:07 +01002772 /*
2773 * Get one character at a time.
2774 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002775 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002776
2777 /* Check for a ":normal" command and no more characters left. */
2778 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2779 c1 = '\n';
2780 else
2781 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002784 * Handle line editing.
2785 * Previously this was left to the system, putting the terminal in
2786 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002788 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790 msg_putchar('\n');
2791 break;
2792 }
2793
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002794 if (c1 == K_PS)
2795 {
2796 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2797 goto redraw;
2798 }
2799
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 if (!escaped)
2801 {
2802 /* CR typed means "enter", which is NL */
2803 if (c1 == '\r')
2804 c1 = '\n';
2805
2806 if (c1 == BS || c1 == K_BS
2807 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 if (line_ga.ga_len > 0)
2810 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002811#ifdef FEAT_MBYTE
2812 if (has_mbyte)
2813 {
2814 p = (char_u *)line_ga.ga_data;
2815 p[line_ga.ga_len] = NUL;
2816 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2817 line_ga.ga_len -= len;
2818 }
2819 else
2820#endif
2821 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 goto redraw;
2823 }
2824 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002829 msg_col = startcol;
2830 msg_clr_eos();
2831 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002832 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 }
2834
2835 if (c1 == Ctrl_T)
2836 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002837 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002838 p = (char_u *)line_ga.ga_data;
2839 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002840 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002841 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002842add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002843 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002845 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002846 p = (char_u *)line_ga.ga_data;
2847 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2849 *s = ' ';
2850 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852redraw:
2853 /* redraw the line */
2854 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002856 p = (char_u *)line_ga.ga_data;
2857 p[line_ga.ga_len] = NUL;
2858 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002862 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 msg_putchar(' ');
2865 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002866 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002870 len = MB_PTR2LEN(p);
2871 msg_outtrans_len(p, len);
2872 vcol += ptr2cells(p);
2873 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002877 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002878 continue;
2879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002881 if (c1 == Ctrl_D)
2882 {
2883 /* Delete one shiftwidth. */
2884 p = (char_u *)line_ga.ga_data;
2885 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002887 if (prev_char == '^')
2888 ex_keep_indent = TRUE;
2889 indent = 0;
2890 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892 else
2893 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002894 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002895 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002896 if (indent > 0)
2897 {
2898 --indent;
2899 indent -= indent % get_sw_value(curbuf);
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002902 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002903 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002904 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002905 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2906 --line_ga.ga_len;
2907 }
2908 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910
2911 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2912 {
2913 escaped = TRUE;
2914 continue;
2915 }
2916
2917 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2918 if (IS_SPECIAL(c1))
2919 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002921
2922 if (IS_SPECIAL(c1))
2923 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002924#ifdef FEAT_MBYTE
2925 if (has_mbyte)
2926 len = (*mb_char2bytes)(c1,
2927 (char_u *)line_ga.ga_data + line_ga.ga_len);
2928 else
2929#endif
2930 {
2931 len = 1;
2932 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2933 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002934 if (c1 == '\n')
2935 msg_putchar('\n');
2936 else if (c1 == TAB)
2937 {
2938 /* Don't use chartabsize(), 'ts' can be different */
2939 do
2940 {
2941 msg_putchar(' ');
2942 } while (++vcol % 8);
2943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002946 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002947 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002948 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002950 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002951 escaped = FALSE;
2952
2953 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002954 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002955
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002956 /* We are done when a NL is entered, but not when it comes after an
2957 * odd number of backslashes, that results in a NUL. */
2958 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002960 int bcount = 0;
2961
2962 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2963 ++bcount;
2964
2965 if (bcount > 0)
2966 {
2967 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2968 * "\NL", etc. */
2969 line_ga.ga_len -= (bcount + 1) / 2;
2970 pend -= (bcount + 1) / 2;
2971 pend[-1] = '\n';
2972 }
2973
2974 if ((bcount & 1) == 0)
2975 {
2976 --line_ga.ga_len;
2977 --pend;
2978 *pend = NUL;
2979 break;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 }
2983
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002984 --no_mapping;
2985 --allow_keys;
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 /* make following messages go to the next line */
2988 msg_didout = FALSE;
2989 msg_col = 0;
2990 if (msg_row < Rows - 1)
2991 ++msg_row;
2992 emsg_on_display = FALSE; /* don't want ui_delay() */
2993
2994 if (got_int)
2995 ga_clear(&line_ga);
2996
2997 return (char_u *)line_ga.ga_data;
2998}
2999
Bram Moolenaare344bea2005-09-01 20:46:49 +00003000# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3001 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002/*
3003 * Return TRUE if ccline.overstrike is on.
3004 */
3005 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003006cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
3008 return ccline.overstrike;
3009}
3010
3011/*
3012 * Return TRUE if the cursor is at the end of the cmdline.
3013 */
3014 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003015cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 return (ccline.cmdpos >= ccline.cmdlen);
3018}
3019#endif
3020
Bram Moolenaar9372a112005-12-06 19:59:18 +00003021#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022/*
3023 * Return the virtual column number at the current cursor position.
3024 * This is used by the IM code to obtain the start of the preedit string.
3025 */
3026 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003027cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3030 return MAXCOL;
3031
3032# ifdef FEAT_MBYTE
3033 if (has_mbyte)
3034 {
3035 colnr_T col;
3036 int i = 0;
3037
3038 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003039 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 return col;
3042 }
3043 else
3044# endif
3045 return ccline.cmdpos;
3046}
3047#endif
3048
3049#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3050/*
3051 * If part of the command line is an IM preedit string, redraw it with
3052 * IM feedback attributes. The cursor position is restored after drawing.
3053 */
3054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003055redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 if ((State & CMDLINE)
3058 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003059 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 && !p_imdisable
3061 && im_is_preediting())
3062 {
3063 int cmdpos = 0;
3064 int cmdspos;
3065 int old_row;
3066 int old_col;
3067 colnr_T col;
3068
3069 old_row = msg_row;
3070 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003071 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
3073# ifdef FEAT_MBYTE
3074 if (has_mbyte)
3075 {
3076 for (col = 0; col < preedit_start_col
3077 && cmdpos < ccline.cmdlen; ++col)
3078 {
3079 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003080 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
3082 }
3083 else
3084# endif
3085 {
3086 cmdspos += preedit_start_col;
3087 cmdpos += preedit_start_col;
3088 }
3089
3090 msg_row = cmdline_row + (cmdspos / (int)Columns);
3091 msg_col = cmdspos % (int)Columns;
3092 if (msg_row >= Rows)
3093 msg_row = Rows - 1;
3094
3095 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3096 {
3097 int char_len;
3098 int char_attr;
3099
3100 char_attr = im_get_feedback_attr(col);
3101 if (char_attr < 0)
3102 break; /* end of preedit string */
3103
3104# ifdef FEAT_MBYTE
3105 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003106 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 else
3108# endif
3109 char_len = 1;
3110
3111 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3112 cmdpos += char_len;
3113 }
3114
3115 msg_row = old_row;
3116 msg_col = old_col;
3117 }
3118}
3119#endif /* FEAT_XIM && FEAT_GUI_GTK */
3120
3121/*
3122 * Allocate a new command line buffer.
3123 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3124 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3125 */
3126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003127alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128{
3129 /*
3130 * give some extra space to avoid having to allocate all the time
3131 */
3132 if (len < 80)
3133 len = 100;
3134 else
3135 len += 20;
3136
3137 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3138 ccline.cmdbufflen = len;
3139}
3140
3141/*
3142 * Re-allocate the command line to length len + something extra.
3143 * return FAIL for failure, OK otherwise
3144 */
3145 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003146realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
3148 char_u *p;
3149
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003150 if (len < ccline.cmdbufflen)
3151 return OK; /* no need to resize */
3152
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 p = ccline.cmdbuff;
3154 alloc_cmdbuff(len); /* will get some more */
3155 if (ccline.cmdbuff == NULL) /* out of memory */
3156 {
3157 ccline.cmdbuff = p; /* keep the old one */
3158 return FAIL;
3159 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003160 /* There isn't always a NUL after the command, but it may need to be
3161 * there, thus copy up to the NUL and add a NUL. */
3162 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3163 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003165
3166 if (ccline.xpc != NULL
3167 && ccline.xpc->xp_pattern != NULL
3168 && ccline.xpc->xp_context != EXPAND_NOTHING
3169 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3170 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003171 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003172
3173 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3174 * to point into the newly allocated memory. */
3175 if (i >= 0 && i <= ccline.cmdlen)
3176 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3177 }
3178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 return OK;
3180}
3181
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003182#if defined(FEAT_ARABIC) || defined(PROTO)
3183static char_u *arshape_buf = NULL;
3184
3185# if defined(EXITFREE) || defined(PROTO)
3186 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003187free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003188{
3189 vim_free(arshape_buf);
3190}
3191# endif
3192#endif
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194/*
3195 * Draw part of the cmdline at the current cursor position. But draw stars
3196 * when cmdline_star is TRUE.
3197 */
3198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003199draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3202 int i;
3203
3204 if (cmdline_star > 0)
3205 for (i = 0; i < len; ++i)
3206 {
3207 msg_putchar('*');
3208# ifdef FEAT_MBYTE
3209 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003210 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211# endif
3212 }
3213 else
3214#endif
3215#ifdef FEAT_ARABIC
3216 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 static int buflen = 0;
3219 char_u *p;
3220 int j;
3221 int newlen = 0;
3222 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003223 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 int prev_c = 0;
3225 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003226 int u8c;
3227 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230 /*
3231 * Do arabic shaping into a temporary buffer. This is very
3232 * inefficient!
3233 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003234 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 /* Re-allocate the buffer. We keep it around to avoid a lot of
3237 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003238 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003239 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003240 arshape_buf = alloc(buflen);
3241 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 return; /* out of memory */
3243 }
3244
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003245 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3246 {
3247 /* Prepend a space to draw the leading composing char on. */
3248 arshape_buf[0] = ' ';
3249 newlen = 1;
3250 }
3251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 for (j = start; j < start + len; j += mb_l)
3253 {
3254 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003255 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003256 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (ARABIC_CHAR(u8c))
3258 {
3259 /* Do Arabic shaping. */
3260 if (cmdmsg_rl)
3261 {
3262 /* displaying from right to left */
3263 pc = prev_c;
3264 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003265 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (j + mb_l >= start + len)
3267 nc = NUL;
3268 else
3269 nc = utf_ptr2char(p + mb_l);
3270 }
3271 else
3272 {
3273 /* displaying from left to right */
3274 if (j + mb_l >= start + len)
3275 pc = NUL;
3276 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003277 {
3278 int pcc[MAX_MCO];
3279
3280 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003282 pc1 = pcc[0];
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 nc = prev_c;
3285 }
3286 prev_c = u8c;
3287
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003288 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003290 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003291 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003293 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3294 if (u8cc[1] != 0)
3295 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
3298 }
3299 else
3300 {
3301 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003302 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 newlen += mb_l;
3304 }
3305 }
3306
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309 else
3310#endif
3311 msg_outtrans_len(ccline.cmdbuff + start, len);
3312}
3313
3314/*
3315 * Put a character on the command line. Shifts the following text to the
3316 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3317 * "c" must be printable (fit in one display cell)!
3318 */
3319 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003320putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 if (cmd_silent)
3323 return;
3324 msg_no_more = TRUE;
3325 msg_putchar(c);
3326 if (shift)
3327 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3328 msg_no_more = FALSE;
3329 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003330 extra_char = c;
3331 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332}
3333
3334/*
3335 * Undo a putcmdline(c, FALSE).
3336 */
3337 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003338unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 if (cmd_silent)
3341 return;
3342 msg_no_more = TRUE;
3343 if (ccline.cmdlen == ccline.cmdpos)
3344 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003345#ifdef FEAT_MBYTE
3346 else if (has_mbyte)
3347 draw_cmdline(ccline.cmdpos,
3348 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 else
3351 draw_cmdline(ccline.cmdpos, 1);
3352 msg_no_more = FALSE;
3353 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003354 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355}
3356
3357/*
3358 * Put the given string, of the given length, onto the command line.
3359 * If len is -1, then STRLEN() is used to calculate the length.
3360 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3361 * part will be redrawn, otherwise it will not. If this function is called
3362 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3363 * called afterwards.
3364 */
3365 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003366put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 int retval;
3369 int i;
3370 int m;
3371 int c;
3372
3373 if (len < 0)
3374 len = (int)STRLEN(str);
3375
3376 /* Check if ccline.cmdbuff needs to be longer */
3377 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003378 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 else
3380 retval = OK;
3381 if (retval == OK)
3382 {
3383 if (!ccline.overstrike)
3384 {
3385 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3386 ccline.cmdbuff + ccline.cmdpos,
3387 (size_t)(ccline.cmdlen - ccline.cmdpos));
3388 ccline.cmdlen += len;
3389 }
3390 else
3391 {
3392#ifdef FEAT_MBYTE
3393 if (has_mbyte)
3394 {
3395 /* Count nr of characters in the new string. */
3396 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003397 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 ++m;
3399 /* Count nr of bytes in cmdline that are overwritten by these
3400 * characters. */
3401 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003402 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 --m;
3404 if (i < ccline.cmdlen)
3405 {
3406 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3407 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3408 ccline.cmdlen += ccline.cmdpos + len - i;
3409 }
3410 else
3411 ccline.cmdlen = ccline.cmdpos + len;
3412 }
3413 else
3414#endif
3415 if (ccline.cmdpos + len > ccline.cmdlen)
3416 ccline.cmdlen = ccline.cmdpos + len;
3417 }
3418 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3419 ccline.cmdbuff[ccline.cmdlen] = NUL;
3420
3421#ifdef FEAT_MBYTE
3422 if (enc_utf8)
3423 {
3424 /* When the inserted text starts with a composing character,
3425 * backup to the character before it. There could be two of them.
3426 */
3427 i = 0;
3428 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3429 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3430 {
3431 i = (*mb_head_off)(ccline.cmdbuff,
3432 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3433 ccline.cmdpos -= i;
3434 len += i;
3435 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3436 }
3437# ifdef FEAT_ARABIC
3438 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3439 {
3440 /* Check the previous character for Arabic combining pair. */
3441 i = (*mb_head_off)(ccline.cmdbuff,
3442 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3443 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3444 + ccline.cmdpos - i), c))
3445 {
3446 ccline.cmdpos -= i;
3447 len += i;
3448 }
3449 else
3450 i = 0;
3451 }
3452# endif
3453 if (i != 0)
3454 {
3455 /* Also backup the cursor position. */
3456 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3457 ccline.cmdspos -= i;
3458 msg_col -= i;
3459 if (msg_col < 0)
3460 {
3461 msg_col += Columns;
3462 --msg_row;
3463 }
3464 }
3465 }
3466#endif
3467
3468 if (redraw && !cmd_silent)
3469 {
3470 msg_no_more = TRUE;
3471 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003472 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3474 /* Avoid clearing the rest of the line too often. */
3475 if (cmdline_row != i || ccline.overstrike)
3476 msg_clr_eos();
3477 msg_no_more = FALSE;
3478 }
3479#ifdef FEAT_FKMAP
3480 /*
3481 * If we are in Farsi command mode, the character input must be in
3482 * Insert mode. So do not advance the cmdpos.
3483 */
3484 if (!cmd_fkmap)
3485#endif
3486 {
3487 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003490 if (m < 0) /* overflow, Columns or Rows at weird value */
3491 m = MAXCOL;
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 else
3494 m = MAXCOL;
3495 for (i = 0; i < len; ++i)
3496 {
3497 c = cmdline_charsize(ccline.cmdpos);
3498#ifdef FEAT_MBYTE
3499 /* count ">" for a double-wide char that doesn't fit. */
3500 if (has_mbyte)
3501 correct_cmdspos(ccline.cmdpos, c);
3502#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003503 /* Stop cursor at the end of the screen, but do increment the
3504 * insert position, so that entering a very long command
3505 * works, even though you can't see it. */
3506 if (ccline.cmdspos + c < m)
3507 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#ifdef FEAT_MBYTE
3509 if (has_mbyte)
3510 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003511 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (c > len - i - 1)
3513 c = len - i - 1;
3514 ccline.cmdpos += c;
3515 i += c;
3516 }
3517#endif
3518 ++ccline.cmdpos;
3519 }
3520 }
3521 }
3522 if (redraw)
3523 msg_check();
3524 return retval;
3525}
3526
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003527static struct cmdline_info prev_ccline;
3528static int prev_ccline_used = FALSE;
3529
3530/*
3531 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3532 * and overwrite it. But get_cmdline_str() may need it, thus make it
3533 * available globally in prev_ccline.
3534 */
3535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003536save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003537{
3538 if (!prev_ccline_used)
3539 {
3540 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3541 prev_ccline_used = TRUE;
3542 }
3543 *ccp = prev_ccline;
3544 prev_ccline = ccline;
3545 ccline.cmdbuff = NULL;
3546 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003547 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003548}
3549
3550/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003551 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003552 */
3553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003554restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003555{
3556 ccline = prev_ccline;
3557 prev_ccline = *ccp;
3558}
3559
Bram Moolenaar5a305422006-04-28 22:38:25 +00003560#if defined(FEAT_EVAL) || defined(PROTO)
3561/*
3562 * Save the command line into allocated memory. Returns a pointer to be
3563 * passed to restore_cmdline_alloc() later.
3564 * Returns NULL when failed.
3565 */
3566 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003567save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003568{
3569 struct cmdline_info *p;
3570
3571 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3572 if (p != NULL)
3573 save_cmdline(p);
3574 return (char_u *)p;
3575}
3576
3577/*
3578 * Restore the command line from the return value of save_cmdline_alloc().
3579 */
3580 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003581restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003582{
3583 if (p != NULL)
3584 {
3585 restore_cmdline((struct cmdline_info *)p);
3586 vim_free(p);
3587 }
3588}
3589#endif
3590
Bram Moolenaar8299df92004-07-10 09:47:34 +00003591/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003592 * Paste a yank register into the command line.
3593 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003594 * insert_reg() can't be used here, because special characters from the
3595 * register contents will be interpreted as commands.
3596 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003597 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003598 */
3599 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003600cmdline_paste(
3601 int regname,
3602 int literally, /* Insert text literally instead of "as typed" */
3603 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003604{
3605 long i;
3606 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003607 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003608 int allocated;
3609 struct cmdline_info save_ccline;
3610
3611 /* check for valid regname; also accept special characters for CTRL-R in
3612 * the command line */
3613 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003614 && regname != Ctrl_A && regname != Ctrl_L
3615 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003616 return FAIL;
3617
3618 /* A register containing CTRL-R can cause an endless loop. Allow using
3619 * CTRL-C to break the loop. */
3620 line_breakcheck();
3621 if (got_int)
3622 return FAIL;
3623
3624#ifdef FEAT_CLIPBOARD
3625 regname = may_get_selection(regname);
3626#endif
3627
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003628 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003629 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003630 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003631 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003632 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003633 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003634 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003635
3636 if (i)
3637 {
3638 /* Got the value of a special register in "arg". */
3639 if (arg == NULL)
3640 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003641
3642 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3643 * part of the word. */
3644 p = arg;
3645 if (p_is && regname == Ctrl_W)
3646 {
3647 char_u *w;
3648 int len;
3649
3650 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003651 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003652 {
3653#ifdef FEAT_MBYTE
3654 if (has_mbyte)
3655 {
3656 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3657 if (!vim_iswordc(mb_ptr2char(w - len)))
3658 break;
3659 w -= len;
3660 }
3661 else
3662#endif
3663 {
3664 if (!vim_iswordc(w[-1]))
3665 break;
3666 --w;
3667 }
3668 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003669 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003670 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3671 p += len;
3672 }
3673
3674 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003675 if (allocated)
3676 vim_free(arg);
3677 return OK;
3678 }
3679
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003680 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003681}
3682
3683/*
3684 * Put a string on the command line.
3685 * When "literally" is TRUE, insert literally.
3686 * When "literally" is FALSE, insert as typed, but don't leave the command
3687 * line.
3688 */
3689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003690cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003691{
3692 int c, cv;
3693
3694 if (literally)
3695 put_on_cmdline(s, -1, TRUE);
3696 else
3697 while (*s != NUL)
3698 {
3699 cv = *s;
3700 if (cv == Ctrl_V && s[1])
3701 ++s;
3702#ifdef FEAT_MBYTE
3703 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003704 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003705 else
3706#endif
3707 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003708 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3709 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003710#ifdef UNIX
3711 || c == intr_char
3712#endif
3713 || (c == Ctrl_BSL && *s == Ctrl_N))
3714 stuffcharReadbuff(Ctrl_V);
3715 stuffcharReadbuff(c);
3716 }
3717}
3718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#ifdef FEAT_WILDMENU
3720/*
3721 * Delete characters on the command line, from "from" to the current
3722 * position.
3723 */
3724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003725cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3728 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3729 ccline.cmdlen -= ccline.cmdpos - from;
3730 ccline.cmdpos = from;
3731}
3732#endif
3733
3734/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003735 * This function is called when the screen size changes and with incremental
3736 * search and in other situations where the command line may have been
3737 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 */
3739 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003740redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003742 redrawcmdline_ex(TRUE);
3743}
3744
3745 void
3746redrawcmdline_ex(int do_compute_cmdrow)
3747{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (cmd_silent)
3749 return;
3750 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003751 if (do_compute_cmdrow)
3752 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 redrawcmd();
3754 cursorcmd();
3755}
3756
3757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003758redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
3760 int i;
3761
3762 if (cmd_silent)
3763 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003764 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 msg_putchar(ccline.cmdfirstc);
3766 if (ccline.cmdprompt != NULL)
3767 {
3768 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3769 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3770 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003771 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 --ccline.cmdindent;
3773 }
3774 else
3775 for (i = ccline.cmdindent; i > 0; --i)
3776 msg_putchar(' ');
3777}
3778
3779/*
3780 * Redraw what is currently on the command line.
3781 */
3782 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003783redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 if (cmd_silent)
3786 return;
3787
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003788 /* when 'incsearch' is set there may be no command line while redrawing */
3789 if (ccline.cmdbuff == NULL)
3790 {
3791 windgoto(cmdline_row, 0);
3792 msg_clr_eos();
3793 return;
3794 }
3795
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 msg_start();
3797 redrawcmdprompt();
3798
3799 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3800 msg_no_more = TRUE;
3801 draw_cmdline(0, ccline.cmdlen);
3802 msg_clr_eos();
3803 msg_no_more = FALSE;
3804
3805 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003806 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003807 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
3809 /*
3810 * An emsg() before may have set msg_scroll. This is used in normal mode,
3811 * in cmdline mode we can reset them now.
3812 */
3813 msg_scroll = FALSE; /* next message overwrites cmdline */
3814
3815 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3816 * in cmdline mode */
3817 skip_redraw = FALSE;
3818}
3819
3820 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003821compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003823 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 cmdline_row = Rows - 1;
3825 else
3826 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003827 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828}
3829
3830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003831cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
3833 if (cmd_silent)
3834 return;
3835
3836#ifdef FEAT_RIGHTLEFT
3837 if (cmdmsg_rl)
3838 {
3839 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3840 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3841 if (msg_row <= 0)
3842 msg_row = Rows - 1;
3843 }
3844 else
3845#endif
3846 {
3847 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3848 msg_col = ccline.cmdspos % (int)Columns;
3849 if (msg_row >= Rows)
3850 msg_row = Rows - 1;
3851 }
3852
3853 windgoto(msg_row, msg_col);
3854#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003855 if (p_imst == IM_ON_THE_SPOT)
3856 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857#endif
3858#ifdef MCH_CURSOR_SHAPE
3859 mch_update_cursor();
3860#endif
3861}
3862
3863 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003864gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 msg_start();
3867#ifdef FEAT_RIGHTLEFT
3868 if (cmdmsg_rl)
3869 msg_col = Columns - 1;
3870 else
3871#endif
3872 msg_col = 0; /* always start in column 0 */
3873 if (clr) /* clear the bottom line(s) */
3874 msg_clr_eos(); /* will reset clear_cmdline */
3875 windgoto(cmdline_row, 0);
3876}
3877
3878/*
3879 * Check the word in front of the cursor for an abbreviation.
3880 * Called when the non-id character "c" has been entered.
3881 * When an abbreviation is recognized it is removed from the text with
3882 * backspaces and the replacement string is inserted, followed by "c".
3883 */
3884 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003885ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003887 int spos = 0;
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3890 return FALSE;
3891
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003892 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3893 * Actually accepts any mark. */
3894 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3895 spos++;
3896 if (ccline.cmdlen - spos > 5
3897 && ccline.cmdbuff[spos] == '\''
3898 && ccline.cmdbuff[spos + 2] == ','
3899 && ccline.cmdbuff[spos + 3] == '\'')
3900 spos += 5;
3901 else
3902 /* check abbreviation from the beginning of the commandline */
3903 spos = 0;
3904
3905 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906}
3907
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003908#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3909 static int
3910#ifdef __BORLANDC__
3911_RTLENTRYF
3912#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003913sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003914{
3915 char_u *p1 = *(char_u **)s1;
3916 char_u *p2 = *(char_u **)s2;
3917
3918 if (*p1 != '<' && *p2 == '<') return -1;
3919 if (*p1 == '<' && *p2 != '<') return 1;
3920 return STRCMP(p1, p2);
3921}
3922#endif
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924/*
3925 * Return FAIL if this is not an appropriate context in which to do
3926 * completion of anything, return OK if it is (even if there are no matches).
3927 * For the caller, this means that the character is just passed through like a
3928 * normal character (instead of being expanded). This allows :s/^I^D etc.
3929 */
3930 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003931nextwild(
3932 expand_T *xp,
3933 int type,
3934 int options, /* extra options for ExpandOne() */
3935 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 int i, j;
3938 char_u *p1;
3939 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 int difflen;
3941 int v;
3942
3943 if (xp->xp_numfiles == -1)
3944 {
3945 set_expand_context(xp);
3946 cmd_showtail = expand_showtail(xp);
3947 }
3948
3949 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3950 {
3951 beep_flush();
3952 return OK; /* Something illegal on command line */
3953 }
3954 if (xp->xp_context == EXPAND_NOTHING)
3955 {
3956 /* Caller can use the character as a normal char instead */
3957 return FAIL;
3958 }
3959
3960 MSG_PUTS("..."); /* show that we are busy */
3961 out_flush();
3962
3963 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003964 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
3966 if (type == WILD_NEXT || type == WILD_PREV)
3967 {
3968 /*
3969 * Get next/previous match for a previous expanded pattern.
3970 */
3971 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3972 }
3973 else
3974 {
3975 /*
3976 * Translate string into pattern and expand it.
3977 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003978 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3979 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 p2 = NULL;
3981 else
3982 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003983 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003984 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3985 if (escape)
3986 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003987
3988 if (p_wic)
3989 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003990 p2 = ExpandOne(xp, p1,
3991 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003992 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003994 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (p2 != NULL && type == WILD_LONGEST)
3996 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003997 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (ccline.cmdbuff[i + j] == '*'
3999 || ccline.cmdbuff[i + j] == '?')
4000 break;
4001 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004002 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004 }
4005 }
4006
4007 if (p2 != NULL && !got_int)
4008 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004009 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004010 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004012 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 xp->xp_pattern = ccline.cmdbuff + i;
4014 }
4015 else
4016 v = OK;
4017 if (v == OK)
4018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004019 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4020 &ccline.cmdbuff[ccline.cmdpos],
4021 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4022 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 ccline.cmdlen += difflen;
4024 ccline.cmdpos += difflen;
4025 }
4026 }
4027 vim_free(p2);
4028
4029 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004030 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
4032 /* When expanding a ":map" command and no matches are found, assume that
4033 * the key is supposed to be inserted literally */
4034 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4035 return FAIL;
4036
4037 if (xp->xp_numfiles <= 0 && p2 == NULL)
4038 beep_flush();
4039 else if (xp->xp_numfiles == 1)
4040 /* free expanded pattern */
4041 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4042
4043 return OK;
4044}
4045
4046/*
4047 * Do wildcard expansion on the string 'str'.
4048 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004049 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 * Return NULL for failure.
4051 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004052 * "orig" is the originally expanded string, copied to allocated memory. It
4053 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4054 * WILD_PREV "orig" should be NULL.
4055 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004056 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4057 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 *
4059 * mode = WILD_FREE: just free previously expanded matches
4060 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4061 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4062 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4063 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4064 * mode = WILD_ALL: return all matches concatenated
4065 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004066 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 *
4068 * options = WILD_LIST_NOTFOUND: list entries without a match
4069 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4070 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4071 * options = WILD_NO_BEEP: Don't beep for multiple matches
4072 * options = WILD_ADD_SLASH: add a slash after directory names
4073 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4074 * options = WILD_SILENT: don't print warning messages
4075 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004076 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 *
4078 * The variables xp->xp_context and xp->xp_backslash must have been set!
4079 */
4080 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004081ExpandOne(
4082 expand_T *xp,
4083 char_u *str,
4084 char_u *orig, /* allocated copy of original of expanded string */
4085 int options,
4086 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 char_u *ss = NULL;
4089 static int findex;
4090 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004091 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 int i;
4093 long_u len;
4094 int non_suf_match; /* number without matching suffix */
4095
4096 /*
4097 * first handle the case of using an old match
4098 */
4099 if (mode == WILD_NEXT || mode == WILD_PREV)
4100 {
4101 if (xp->xp_numfiles > 0)
4102 {
4103 if (mode == WILD_PREV)
4104 {
4105 if (findex == -1)
4106 findex = xp->xp_numfiles;
4107 --findex;
4108 }
4109 else /* mode == WILD_NEXT */
4110 ++findex;
4111
4112 /*
4113 * When wrapping around, return the original string, set findex to
4114 * -1.
4115 */
4116 if (findex < 0)
4117 {
4118 if (orig_save == NULL)
4119 findex = xp->xp_numfiles - 1;
4120 else
4121 findex = -1;
4122 }
4123 if (findex >= xp->xp_numfiles)
4124 {
4125 if (orig_save == NULL)
4126 findex = 0;
4127 else
4128 findex = -1;
4129 }
4130#ifdef FEAT_WILDMENU
4131 if (p_wmnu)
4132 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4133 findex, cmd_showtail);
4134#endif
4135 if (findex == -1)
4136 return vim_strsave(orig_save);
4137 return vim_strsave(xp->xp_files[findex]);
4138 }
4139 else
4140 return NULL;
4141 }
4142
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004143 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4145 {
4146 FreeWild(xp->xp_numfiles, xp->xp_files);
4147 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004148 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150 findex = 0;
4151
4152 if (mode == WILD_FREE) /* only release file name */
4153 return NULL;
4154
4155 if (xp->xp_numfiles == -1)
4156 {
4157 vim_free(orig_save);
4158 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004159 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Do the expansion.
4163 */
4164 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4165 options) == FAIL)
4166 {
4167#ifdef FNAME_ILLEGAL
4168 /* Illegal file name has been silently skipped. But when there
4169 * are wildcards, the real problem is that there was no match,
4170 * causing the pattern to be added, which has illegal characters.
4171 */
4172 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4173 EMSG2(_(e_nomatch2), str);
4174#endif
4175 }
4176 else if (xp->xp_numfiles == 0)
4177 {
4178 if (!(options & WILD_SILENT))
4179 EMSG2(_(e_nomatch2), str);
4180 }
4181 else
4182 {
4183 /* Escape the matches for use on the command line. */
4184 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4185
4186 /*
4187 * Check for matching suffixes in file names.
4188 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004189 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4190 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
4192 if (xp->xp_numfiles)
4193 non_suf_match = xp->xp_numfiles;
4194 else
4195 non_suf_match = 1;
4196 if ((xp->xp_context == EXPAND_FILES
4197 || xp->xp_context == EXPAND_DIRECTORIES)
4198 && xp->xp_numfiles > 1)
4199 {
4200 /*
4201 * More than one match; check suffix.
4202 * The files will have been sorted on matching suffix in
4203 * expand_wildcards, only need to check the first two.
4204 */
4205 non_suf_match = 0;
4206 for (i = 0; i < 2; ++i)
4207 if (match_suffix(xp->xp_files[i]))
4208 ++non_suf_match;
4209 }
4210 if (non_suf_match != 1)
4211 {
4212 /* Can we ever get here unless it's while expanding
4213 * interactively? If not, we can get rid of this all
4214 * together. Don't really want to wait for this message
4215 * (and possibly have to hit return to continue!).
4216 */
4217 if (!(options & WILD_SILENT))
4218 EMSG(_(e_toomany));
4219 else if (!(options & WILD_NO_BEEP))
4220 beep_flush();
4221 }
4222 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4223 ss = vim_strsave(xp->xp_files[0]);
4224 }
4225 }
4226 }
4227
4228 /* Find longest common part */
4229 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4230 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004231 int mb_len = 1;
4232 int c0, ci;
4233
4234 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004236#ifdef FEAT_MBYTE
4237 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004239 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4240 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4241 }
4242 else
4243#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004244 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004245 for (i = 1; i < xp->xp_numfiles; ++i)
4246 {
4247#ifdef FEAT_MBYTE
4248 if (has_mbyte)
4249 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4250 else
4251#endif
4252 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004253 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004255 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004256 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004258 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 break;
4260 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004261 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 break;
4263 }
4264 if (i < xp->xp_numfiles)
4265 {
4266 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004267 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 break;
4269 }
4270 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 ss = alloc((unsigned)len + 1);
4273 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004274 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 findex = -1; /* next p_wc gets first one */
4276 }
4277
4278 /* Concatenate all matching names */
4279 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4280 {
4281 len = 0;
4282 for (i = 0; i < xp->xp_numfiles; ++i)
4283 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4284 ss = lalloc(len, TRUE);
4285 if (ss != NULL)
4286 {
4287 *ss = NUL;
4288 for (i = 0; i < xp->xp_numfiles; ++i)
4289 {
4290 STRCAT(ss, xp->xp_files[i]);
4291 if (i != xp->xp_numfiles - 1)
4292 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4293 }
4294 }
4295 }
4296
4297 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4298 ExpandCleanup(xp);
4299
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004300 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004301 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004302 vim_free(orig);
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return ss;
4305}
4306
4307/*
4308 * Prepare an expand structure for use.
4309 */
4310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004311ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004313 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004314 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004316#ifndef BACKSLASH_IN_FILENAME
4317 xp->xp_shell = FALSE;
4318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 xp->xp_numfiles = -1;
4320 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004321#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4322 xp->xp_arg = NULL;
4323#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004324 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325}
4326
4327/*
4328 * Cleanup an expand structure after use.
4329 */
4330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004331ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
4333 if (xp->xp_numfiles >= 0)
4334 {
4335 FreeWild(xp->xp_numfiles, xp->xp_files);
4336 xp->xp_numfiles = -1;
4337 }
4338}
4339
4340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004341ExpandEscape(
4342 expand_T *xp,
4343 char_u *str,
4344 int numfiles,
4345 char_u **files,
4346 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
4348 int i;
4349 char_u *p;
4350
4351 /*
4352 * May change home directory back to "~"
4353 */
4354 if (options & WILD_HOME_REPLACE)
4355 tilde_replace(str, numfiles, files);
4356
4357 if (options & WILD_ESCAPE)
4358 {
4359 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004360 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004361 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 || xp->xp_context == EXPAND_BUFFERS
4363 || xp->xp_context == EXPAND_DIRECTORIES)
4364 {
4365 /*
4366 * Insert a backslash into a file name before a space, \, %, #
4367 * and wildmatch characters, except '~'.
4368 */
4369 for (i = 0; i < numfiles; ++i)
4370 {
4371 /* for ":set path=" we need to escape spaces twice */
4372 if (xp->xp_backslash == XP_BS_THREE)
4373 {
4374 p = vim_strsave_escaped(files[i], (char_u *)" ");
4375 if (p != NULL)
4376 {
4377 vim_free(files[i]);
4378 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004379#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 p = vim_strsave_escaped(files[i], (char_u *)" ");
4381 if (p != NULL)
4382 {
4383 vim_free(files[i]);
4384 files[i] = p;
4385 }
4386#endif
4387 }
4388 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004389#ifdef BACKSLASH_IN_FILENAME
4390 p = vim_strsave_fnameescape(files[i], FALSE);
4391#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004392 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (p != NULL)
4395 {
4396 vim_free(files[i]);
4397 files[i] = p;
4398 }
4399
4400 /* If 'str' starts with "\~", replace "~" at start of
4401 * files[i] with "\~". */
4402 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004403 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
4405 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004406
4407 /* If the first file starts with a '+' escape it. Otherwise it
4408 * could be seen as "+cmd". */
4409 if (*files[0] == '+')
4410 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 else if (xp->xp_context == EXPAND_TAGS)
4413 {
4414 /*
4415 * Insert a backslash before characters in a tag name that
4416 * would terminate the ":tag" command.
4417 */
4418 for (i = 0; i < numfiles; ++i)
4419 {
4420 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4421 if (p != NULL)
4422 {
4423 vim_free(files[i]);
4424 files[i] = p;
4425 }
4426 }
4427 }
4428 }
4429}
4430
4431/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004432 * Escape special characters in "fname" for when used as a file name argument
4433 * after a Vim command, or, when "shell" is non-zero, a shell command.
4434 * Returns the result in allocated memory.
4435 */
4436 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004437vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004438{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004439 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004440#ifdef BACKSLASH_IN_FILENAME
4441 char_u buf[20];
4442 int j = 0;
4443
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004444 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004445 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004446 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004447 buf[j++] = *p;
4448 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004449 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004450#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004451 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4452 if (shell && csh_like_shell() && p != NULL)
4453 {
4454 char_u *s;
4455
4456 /* For csh and similar shells need to put two backslashes before '!'.
4457 * One is taken by Vim, one by the shell. */
4458 s = vim_strsave_escaped(p, (char_u *)"!");
4459 vim_free(p);
4460 p = s;
4461 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004462#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004463
4464 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4465 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004466 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004467 escape_fname(&p);
4468
4469 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004470}
4471
4472/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004473 * Put a backslash before the file name in "pp", which is in allocated memory.
4474 */
4475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004476escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004477{
4478 char_u *p;
4479
4480 p = alloc((unsigned)(STRLEN(*pp) + 2));
4481 if (p != NULL)
4482 {
4483 p[0] = '\\';
4484 STRCPY(p + 1, *pp);
4485 vim_free(*pp);
4486 *pp = p;
4487 }
4488}
4489
4490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 * For each file name in files[num_files]:
4492 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4493 */
4494 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004495tilde_replace(
4496 char_u *orig_pat,
4497 int num_files,
4498 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
4500 int i;
4501 char_u *p;
4502
4503 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4504 {
4505 for (i = 0; i < num_files; ++i)
4506 {
4507 p = home_replace_save(NULL, files[i]);
4508 if (p != NULL)
4509 {
4510 vim_free(files[i]);
4511 files[i] = p;
4512 }
4513 }
4514 }
4515}
4516
4517/*
4518 * Show all matches for completion on the command line.
4519 * Returns EXPAND_NOTHING when the character that triggered expansion should
4520 * be inserted like a normal character.
4521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004523showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4526 int num_files;
4527 char_u **files_found;
4528 int i, j, k;
4529 int maxlen;
4530 int lines;
4531 int columns;
4532 char_u *p;
4533 int lastlen;
4534 int attr;
4535 int showtail;
4536
4537 if (xp->xp_numfiles == -1)
4538 {
4539 set_expand_context(xp);
4540 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4541 &num_files, &files_found);
4542 showtail = expand_showtail(xp);
4543 if (i != EXPAND_OK)
4544 return i;
4545
4546 }
4547 else
4548 {
4549 num_files = xp->xp_numfiles;
4550 files_found = xp->xp_files;
4551 showtail = cmd_showtail;
4552 }
4553
4554#ifdef FEAT_WILDMENU
4555 if (!wildmenu)
4556 {
4557#endif
4558 msg_didany = FALSE; /* lines_left will be set */
4559 msg_start(); /* prepare for paging */
4560 msg_putchar('\n');
4561 out_flush();
4562 cmdline_row = msg_row;
4563 msg_didany = FALSE; /* lines_left will be set again */
4564 msg_start(); /* prepare for paging */
4565#ifdef FEAT_WILDMENU
4566 }
4567#endif
4568
4569 if (got_int)
4570 got_int = FALSE; /* only int. the completion, not the cmd line */
4571#ifdef FEAT_WILDMENU
4572 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004573 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574#endif
4575 else
4576 {
4577 /* find the length of the longest file name */
4578 maxlen = 0;
4579 for (i = 0; i < num_files; ++i)
4580 {
4581 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004582 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 || xp->xp_context == EXPAND_BUFFERS))
4584 {
4585 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4586 j = vim_strsize(NameBuff);
4587 }
4588 else
4589 j = vim_strsize(L_SHOWFILE(i));
4590 if (j > maxlen)
4591 maxlen = j;
4592 }
4593
4594 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4595 lines = num_files;
4596 else
4597 {
4598 /* compute the number of columns and lines for the listing */
4599 maxlen += 2; /* two spaces between file names */
4600 columns = ((int)Columns + 2) / maxlen;
4601 if (columns < 1)
4602 columns = 1;
4603 lines = (num_files + columns - 1) / columns;
4604 }
4605
Bram Moolenaar8820b482017-03-16 17:23:31 +01004606 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4609 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004610 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 msg_clr_eos();
4612 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004613 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615
4616 /* list the files line by line */
4617 for (i = 0; i < lines; ++i)
4618 {
4619 lastlen = 999;
4620 for (k = i; k < num_files; k += lines)
4621 {
4622 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4623 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004624 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 p = files_found[k] + STRLEN(files_found[k]) + 1;
4626 msg_advance(maxlen + 1);
4627 msg_puts(p);
4628 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004629 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 break;
4631 }
4632 for (j = maxlen - lastlen; --j >= 0; )
4633 msg_putchar(' ');
4634 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004635 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 || xp->xp_context == EXPAND_BUFFERS)
4637 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004638 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004639 if (xp->xp_numfiles != -1)
4640 {
4641 char_u *halved_slash;
4642 char_u *exp_path;
4643
4644 /* Expansion was done before and special characters
4645 * were escaped, need to halve backslashes. Also
4646 * $HOME has been replaced with ~/. */
4647 exp_path = expand_env_save_opt(files_found[k], TRUE);
4648 halved_slash = backslash_halve_save(
4649 exp_path != NULL ? exp_path : files_found[k]);
4650 j = mch_isdir(halved_slash != NULL ? halved_slash
4651 : files_found[k]);
4652 vim_free(exp_path);
4653 vim_free(halved_slash);
4654 }
4655 else
4656 /* Expansion was done here, file names are literal. */
4657 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (showtail)
4659 p = L_SHOWFILE(k);
4660 else
4661 {
4662 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4663 TRUE);
4664 p = NameBuff;
4665 }
4666 }
4667 else
4668 {
4669 j = FALSE;
4670 p = L_SHOWFILE(k);
4671 }
4672 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4673 }
4674 if (msg_col > 0) /* when not wrapped around */
4675 {
4676 msg_clr_eos();
4677 msg_putchar('\n');
4678 }
4679 out_flush(); /* show one line at a time */
4680 if (got_int)
4681 {
4682 got_int = FALSE;
4683 break;
4684 }
4685 }
4686
4687 /*
4688 * we redraw the command below the lines that we have just listed
4689 * This is a bit tricky, but it saves a lot of screen updating.
4690 */
4691 cmdline_row = msg_row; /* will put it back later */
4692 }
4693
4694 if (xp->xp_numfiles == -1)
4695 FreeWild(num_files, files_found);
4696
4697 return EXPAND_OK;
4698}
4699
4700/*
4701 * Private gettail for showmatches() (and win_redr_status_matches()):
4702 * Find tail of file name path, but ignore trailing "/".
4703 */
4704 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 char_u *p;
4708 char_u *t = s;
4709 int had_sep = FALSE;
4710
4711 for (p = s; *p != NUL; )
4712 {
4713 if (vim_ispathsep(*p)
4714#ifdef BACKSLASH_IN_FILENAME
4715 && !rem_backslash(p)
4716#endif
4717 )
4718 had_sep = TRUE;
4719 else if (had_sep)
4720 {
4721 t = p;
4722 had_sep = FALSE;
4723 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004724 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726 return t;
4727}
4728
4729/*
4730 * Return TRUE if we only need to show the tail of completion matches.
4731 * When not completing file names or there is a wildcard in the path FALSE is
4732 * returned.
4733 */
4734 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 char_u *s;
4738 char_u *end;
4739
4740 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004741 if (xp->xp_context != EXPAND_FILES
4742 && xp->xp_context != EXPAND_SHELLCMD
4743 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FALSE;
4745
4746 end = gettail(xp->xp_pattern);
4747 if (end == xp->xp_pattern) /* there is no path separator */
4748 return FALSE;
4749
4750 for (s = xp->xp_pattern; s < end; s++)
4751 {
4752 /* Skip escaped wildcards. Only when the backslash is not a path
4753 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4754 if (rem_backslash(s))
4755 ++s;
4756 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4757 return FALSE;
4758 }
4759 return TRUE;
4760}
4761
4762/*
4763 * Prepare a string for expansion.
4764 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004765 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 * When expanding other names: The string will be used with regcomp(). Copy
4767 * the name into allocated memory and prepend "^".
4768 */
4769 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770addstar(
4771 char_u *fname,
4772 int len,
4773 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 char_u *retval;
4776 int i, j;
4777 int new_len;
4778 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004779 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004781 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004782 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004783 && context != EXPAND_SHELLCMD
4784 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 /*
4787 * Matching will be done internally (on something other than files).
4788 * So we convert the file-matching-type wildcards into our kind for
4789 * use with vim_regcomp(). First work out how long it will be:
4790 */
4791
4792 /* For help tags the translation is done in find_help_tags().
4793 * For a tag pattern starting with "/" no translation is needed. */
4794 if (context == EXPAND_HELP
4795 || context == EXPAND_COLORS
4796 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004797 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004798 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004799 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004800 || ((context == EXPAND_TAGS_LISTFILES
4801 || context == EXPAND_TAGS)
4802 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 retval = vim_strnsave(fname, len);
4804 else
4805 {
4806 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4807 for (i = 0; i < len; i++)
4808 {
4809 if (fname[i] == '*' || fname[i] == '~')
4810 new_len++; /* '*' needs to be replaced by ".*"
4811 '~' needs to be replaced by "\~" */
4812
4813 /* Buffer names are like file names. "." should be literal */
4814 if (context == EXPAND_BUFFERS && fname[i] == '.')
4815 new_len++; /* "." becomes "\." */
4816
4817 /* Custom expansion takes care of special things, match
4818 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004819 if ((context == EXPAND_USER_DEFINED
4820 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 new_len++; /* '\' becomes "\\" */
4822 }
4823 retval = alloc(new_len);
4824 if (retval != NULL)
4825 {
4826 retval[0] = '^';
4827 j = 1;
4828 for (i = 0; i < len; i++, j++)
4829 {
4830 /* Skip backslash. But why? At least keep it for custom
4831 * expansion. */
4832 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004833 && context != EXPAND_USER_LIST
4834 && fname[i] == '\\'
4835 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 break;
4837
4838 switch (fname[i])
4839 {
4840 case '*': retval[j++] = '.';
4841 break;
4842 case '~': retval[j++] = '\\';
4843 break;
4844 case '?': retval[j] = '.';
4845 continue;
4846 case '.': if (context == EXPAND_BUFFERS)
4847 retval[j++] = '\\';
4848 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004849 case '\\': if (context == EXPAND_USER_DEFINED
4850 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 retval[j++] = '\\';
4852 break;
4853 }
4854 retval[j] = fname[i];
4855 }
4856 retval[j] = NUL;
4857 }
4858 }
4859 }
4860 else
4861 {
4862 retval = alloc(len + 4);
4863 if (retval != NULL)
4864 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004865 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004868 * Don't add a star to *, ~, ~user, $var or `cmd`.
4869 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 * ~ would be at the start of the file name, but not the tail.
4871 * $ could be anywhere in the tail.
4872 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004873 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 */
4875 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004876 ends_in_star = (len > 0 && retval[len - 1] == '*');
4877#ifndef BACKSLASH_IN_FILENAME
4878 for (i = len - 2; i >= 0; --i)
4879 {
4880 if (retval[i] != '\\')
4881 break;
4882 ends_in_star = !ends_in_star;
4883 }
4884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004886 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 && vim_strchr(tail, '$') == NULL
4888 && vim_strchr(retval, '`') == NULL)
4889 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004890 else if (len > 0 && retval[len - 1] == '$')
4891 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 retval[len] = NUL;
4893 }
4894 }
4895 return retval;
4896}
4897
4898/*
4899 * Must parse the command line so far to work out what context we are in.
4900 * Completion can then be done based on that context.
4901 * This routine sets the variables:
4902 * xp->xp_pattern The start of the pattern to be expanded within
4903 * the command line (ends at the cursor).
4904 * xp->xp_context The type of thing to expand. Will be one of:
4905 *
4906 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4907 * the command line, like an unknown command. Caller
4908 * should beep.
4909 * EXPAND_NOTHING Unrecognised context for completion, use char like
4910 * a normal char, rather than for completion. eg
4911 * :s/^I/
4912 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4913 * it.
4914 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4915 * EXPAND_FILES After command with XFILE set, or after setting
4916 * with P_EXPAND set. eg :e ^I, :w>>^I
4917 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4918 * when we know only directories are of interest. eg
4919 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004920 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4922 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4923 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4924 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4925 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4926 * EXPAND_EVENTS Complete event names
4927 * EXPAND_SYNTAX Complete :syntax command arguments
4928 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4929 * EXPAND_AUGROUP Complete autocommand group names
4930 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4931 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4932 * eg :unmap a^I , :cunab x^I
4933 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4934 * eg :call sub^I
4935 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4936 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4937 * names in expressions, eg :while s^I
4938 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004939 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 */
4941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004942set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004944 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (ccline.cmdfirstc != ':'
4946#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004947 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004948 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#endif
4950 )
4951 {
4952 xp->xp_context = EXPAND_NOTHING;
4953 return;
4954 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004955 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956}
4957
4958 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004959set_cmd_context(
4960 expand_T *xp,
4961 char_u *str, /* start of command line */
4962 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004963 int col, /* position of cursor */
4964 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 int old_char = NUL;
4967 char_u *nextcomm;
4968
4969 /*
4970 * Avoid a UMR warning from Purify, only save the character if it has been
4971 * written before.
4972 */
4973 if (col < len)
4974 old_char = str[col];
4975 str[col] = NUL;
4976 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004977
4978#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004979 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004980 {
4981# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004982 /* pass CMD_SIZE because there is no real command */
4983 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004984# endif
4985 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004986 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004987 {
4988 xp->xp_context = ccline.xp_context;
4989 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004990# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004991 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004992# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004993 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004994 else
4995#endif
4996 while (nextcomm != NULL)
4997 nextcomm = set_one_cmd_context(xp, nextcomm);
4998
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004999 /* Store the string here so that call_user_expand_func() can get to them
5000 * easily. */
5001 xp->xp_line = str;
5002 xp->xp_col = col;
5003
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 str[col] = old_char;
5005}
5006
5007/*
5008 * Expand the command line "str" from context "xp".
5009 * "xp" must have been set by set_cmd_context().
5010 * xp->xp_pattern points into "str", to where the text that is to be expanded
5011 * starts.
5012 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
5013 * cursor.
5014 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
5015 * key that triggered expansion literally.
5016 * Returns EXPAND_OK otherwise.
5017 */
5018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005019expand_cmdline(
5020 expand_T *xp,
5021 char_u *str, /* start of command line */
5022 int col, /* position of cursor */
5023 int *matchcount, /* return: nr of matches */
5024 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
5026 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005027 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
5029 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5030 {
5031 beep_flush();
5032 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5033 }
5034 if (xp->xp_context == EXPAND_NOTHING)
5035 {
5036 /* Caller can use the character as a normal char instead */
5037 return EXPAND_NOTHING;
5038 }
5039
5040 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005041 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5042 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (file_str == NULL)
5044 return EXPAND_UNSUCCESSFUL;
5045
Bram Moolenaar94950a92010-12-02 16:01:29 +01005046 if (p_wic)
5047 options += WILD_ICASE;
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005050 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 *matchcount = 0;
5053 *matches = NULL;
5054 }
5055 vim_free(file_str);
5056
5057 return EXPAND_OK;
5058}
5059
5060#ifdef FEAT_MULTI_LANG
5061/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005062 * Cleanup matches for help tags:
5063 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5064 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005066static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005069cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070{
5071 int i, j;
5072 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005073 char_u buf[4];
5074 char_u *p = buf;
5075
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005076 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005077 {
5078 *p++ = '@';
5079 *p++ = p_hlg[0];
5080 *p++ = p_hlg[1];
5081 }
5082 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 for (i = 0; i < num_file; ++i)
5085 {
5086 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005087 if (len <= 0)
5088 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005089 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 /* Sorting on priority means the same item in another language may
5092 * be anywhere. Search all items for a match up to the "@en". */
5093 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005094 if (j != i && (int)STRLEN(file[j]) == len + 3
5095 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005098 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 file[i][len] = NUL;
5100 }
5101 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005102
5103 if (*buf != NUL)
5104 for (i = 0; i < num_file; ++i)
5105 {
5106 len = (int)STRLEN(file[i]) - 3;
5107 if (len <= 0)
5108 continue;
5109 if (STRCMP(file[i] + len, buf) == 0)
5110 {
5111 /* remove the default language */
5112 file[i][len] = NUL;
5113 }
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115}
5116#endif
5117
5118/*
5119 * Do the expansion based on xp->xp_context and "pat".
5120 */
5121 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005122ExpandFromContext(
5123 expand_T *xp,
5124 char_u *pat,
5125 int *num_file,
5126 char_u ***file,
5127 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128{
5129#ifdef FEAT_CMDL_COMPL
5130 regmatch_T regmatch;
5131#endif
5132 int ret;
5133 int flags;
5134
5135 flags = EW_DIR; /* include directories */
5136 if (options & WILD_LIST_NOTFOUND)
5137 flags |= EW_NOTFOUND;
5138 if (options & WILD_ADD_SLASH)
5139 flags |= EW_ADDSLASH;
5140 if (options & WILD_KEEP_ALL)
5141 flags |= EW_KEEPALL;
5142 if (options & WILD_SILENT)
5143 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005144 if (options & WILD_ALLLINKS)
5145 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005147 if (xp->xp_context == EXPAND_FILES
5148 || xp->xp_context == EXPAND_DIRECTORIES
5149 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 /*
5152 * Expand file or directory names.
5153 */
5154 int free_pat = FALSE;
5155 int i;
5156
5157 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5158 * space */
5159 if (xp->xp_backslash != XP_BS_NONE)
5160 {
5161 free_pat = TRUE;
5162 pat = vim_strsave(pat);
5163 for (i = 0; pat[i]; ++i)
5164 if (pat[i] == '\\')
5165 {
5166 if (xp->xp_backslash == XP_BS_THREE
5167 && pat[i + 1] == '\\'
5168 && pat[i + 2] == '\\'
5169 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005170 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (xp->xp_backslash == XP_BS_ONE
5172 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005173 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175 }
5176
5177 if (xp->xp_context == EXPAND_FILES)
5178 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005179 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5180 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 else
5182 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005183 if (options & WILD_ICASE)
5184 flags |= EW_ICASE;
5185
Bram Moolenaard7834d32009-12-02 16:14:36 +00005186 /* Expand wildcards, supporting %:h and the like. */
5187 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if (free_pat)
5189 vim_free(pat);
5190 return ret;
5191 }
5192
5193 *file = (char_u **)"";
5194 *num_file = 0;
5195 if (xp->xp_context == EXPAND_HELP)
5196 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005197 /* With an empty argument we would get all the help tags, which is
5198 * very slow. Get matches for "help" instead. */
5199 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5200 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
5202#ifdef FEAT_MULTI_LANG
5203 cleanup_help_tags(*num_file, *file);
5204#endif
5205 return OK;
5206 }
5207 return FAIL;
5208 }
5209
5210#ifndef FEAT_CMDL_COMPL
5211 return FAIL;
5212#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005213 if (xp->xp_context == EXPAND_SHELLCMD)
5214 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (xp->xp_context == EXPAND_OLD_SETTING)
5216 return ExpandOldSetting(num_file, file);
5217 if (xp->xp_context == EXPAND_BUFFERS)
5218 return ExpandBufnames(pat, num_file, file, options);
5219 if (xp->xp_context == EXPAND_TAGS
5220 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5221 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5222 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005223 {
5224 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005225 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5226 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005229 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005230 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005231 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005232 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005233 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005234 {
5235 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005236 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005237 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005238 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005239 {
5240 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005241 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005242 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005243# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5244 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005245 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005246# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005247 if (xp->xp_context == EXPAND_PACKADD)
5248 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5251 if (regmatch.regprog == NULL)
5252 return FAIL;
5253
5254 /* set ignore-case according to p_ic, p_scs and pat */
5255 regmatch.rm_ic = ignorecase(pat);
5256
5257 if (xp->xp_context == EXPAND_SETTINGS
5258 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5259 ret = ExpandSettings(xp, &regmatch, num_file, file);
5260 else if (xp->xp_context == EXPAND_MAPPINGS)
5261 ret = ExpandMappings(&regmatch, num_file, file);
5262# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5263 else if (xp->xp_context == EXPAND_USER_DEFINED)
5264 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5265# endif
5266 else
5267 {
5268 static struct expgen
5269 {
5270 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005271 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005273 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 } tab[] =
5275 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005276 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5277 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005278 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005279 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005280#ifdef FEAT_CMDHIST
5281 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005284 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005285 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005286 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5287 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5288 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#endif
5290#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005291 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5292 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5293 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5294 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
5296#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005297 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5298 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299#endif
5300#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005301 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005303#ifdef FEAT_PROFILE
5304 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5305#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005306 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005307 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5308 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005309#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005310 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005311#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005312#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005313 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005314#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005315#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005316 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5319 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005320 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5321 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005323 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005324 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005325 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 };
5327 int i;
5328
5329 /*
5330 * Find a context in the table and call the ExpandGeneric() with the
5331 * right function to do the expansion.
5332 */
5333 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005334 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 if (xp->xp_context == tab[i].context)
5336 {
5337 if (tab[i].ic)
5338 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005339 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005340 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 break;
5342 }
5343 }
5344
Bram Moolenaar473de612013-06-08 18:19:48 +02005345 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 return ret;
5348#endif /* FEAT_CMDL_COMPL */
5349}
5350
5351#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5352/*
5353 * Expand a list of names.
5354 *
5355 * Generic function for command line completion. It calls a function to
5356 * obtain strings, one by one. The strings are matched against a regexp
5357 * program. Matching strings are copied into an array, which is returned.
5358 *
5359 * Returns OK when no problems encountered, FAIL for error (out of memory).
5360 */
5361 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362ExpandGeneric(
5363 expand_T *xp,
5364 regmatch_T *regmatch,
5365 int *num_file,
5366 char_u ***file,
5367 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005369 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371 int i;
5372 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005373 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 char_u *str;
5375
5376 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005377 * round == 0: count the number of matching names
5378 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005380 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 for (i = 0; ; ++i)
5383 {
5384 str = (*func)(xp, i);
5385 if (str == NULL) /* end of list */
5386 break;
5387 if (*str == NUL) /* skip empty strings */
5388 continue;
5389
5390 if (vim_regexec(regmatch, str, (colnr_T)0))
5391 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005392 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005394 if (escaped)
5395 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5396 else
5397 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 (*file)[count] = str;
5399#ifdef FEAT_MENU
5400 if (func == get_menu_names && str != NULL)
5401 {
5402 /* test for separator added by get_menu_names() */
5403 str += STRLEN(str) - 1;
5404 if (*str == '\001')
5405 *str = '.';
5406 }
5407#endif
5408 }
5409 ++count;
5410 }
5411 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005412 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 if (count == 0)
5415 return OK;
5416 *num_file = count;
5417 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5418 if (*file == NULL)
5419 {
5420 *file = (char_u **)"";
5421 return FAIL;
5422 }
5423 count = 0;
5424 }
5425 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005426
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005427 /* Sort the results. Keep menu's in the specified order. */
5428 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005429 {
5430 if (xp->xp_context == EXPAND_EXPRESSION
5431 || xp->xp_context == EXPAND_FUNCTIONS
5432 || xp->xp_context == EXPAND_USER_FUNC)
5433 /* <SNR> functions should be sorted to the end. */
5434 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5435 sort_func_compare);
5436 else
5437 sort_strings(*file, *num_file);
5438 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005439
Bram Moolenaar4f688582007-07-24 12:34:30 +00005440#ifdef FEAT_CMDL_COMPL
5441 /* Reset the variables used for special highlight names expansion, so that
5442 * they don't show up when getting normal highlight names by ID. */
5443 reset_expand_highlight();
5444#endif
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 return OK;
5447}
5448
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005449/*
5450 * Complete a shell command.
5451 * Returns FAIL or OK;
5452 */
5453 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454expand_shellcmd(
5455 char_u *filepat, /* pattern to match with command names */
5456 int *num_file, /* return: number of matches */
5457 char_u ***file, /* return: array with matches */
5458 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005459{
5460 char_u *pat;
5461 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005462 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005463 int mustfree = FALSE;
5464 garray_T ga;
5465 char_u *buf = alloc(MAXPATHL);
5466 size_t l;
5467 char_u *s, *e;
5468 int flags = flagsarg;
5469 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005470 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005471 hashtab_T found_ht;
5472 hashitem_T *hi;
5473 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005474
5475 if (buf == NULL)
5476 return FAIL;
5477
5478 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5479 * space */
5480 pat = vim_strsave(filepat);
5481 for (i = 0; pat[i]; ++i)
5482 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005483 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005484
Bram Moolenaarb5971142015-03-21 17:32:19 +01005485 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005486
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005487 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5488 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005489 path = (char_u *)".";
5490 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005491 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005492 /* For an absolute name we don't use $PATH. */
5493 if (!mch_isFullName(pat))
5494 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005495 if (path == NULL)
5496 path = (char_u *)"";
5497 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005498
5499 /*
5500 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005501 * collect them in "ga". When "." is not in $PATH also expand for the
5502 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005503 */
5504 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005505 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005506 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005507 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005508#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005509 e = vim_strchr(s, ';');
5510#else
5511 e = vim_strchr(s, ':');
5512#endif
5513 if (e == NULL)
5514 e = s + STRLEN(s);
5515
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005516 if (*s == NUL)
5517 {
5518 if (did_curdir)
5519 break;
5520 // Find directories in the current directory, path is empty.
5521 did_curdir = TRUE;
5522 flags |= EW_DIR;
5523 }
5524 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5525 {
5526 did_curdir = TRUE;
5527 flags |= EW_DIR;
5528 }
5529 else
5530 // Do not match directories inside a $PATH item.
5531 flags &= ~EW_DIR;
5532
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005533 l = e - s;
5534 if (l > MAXPATHL - 5)
5535 break;
5536 vim_strncpy(buf, s, l);
5537 add_pathsep(buf);
5538 l = STRLEN(buf);
5539 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5540
5541 /* Expand matches in one directory of $PATH. */
5542 ret = expand_wildcards(1, &buf, num_file, file, flags);
5543 if (ret == OK)
5544 {
5545 if (ga_grow(&ga, *num_file) == FAIL)
5546 FreeWild(*num_file, *file);
5547 else
5548 {
5549 for (i = 0; i < *num_file; ++i)
5550 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005551 char_u *name = (*file)[i];
5552
5553 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005554 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005555 // Check if this name was already found.
5556 hash = hash_hash(name + l);
5557 hi = hash_lookup(&found_ht, name + l, hash);
5558 if (HASHITEM_EMPTY(hi))
5559 {
5560 // Remove the path that was prepended.
5561 STRMOVE(name, name + l);
5562 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5563 hash_add_item(&found_ht, hi, name, hash);
5564 name = NULL;
5565 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005566 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005567 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005568 }
5569 vim_free(*file);
5570 }
5571 }
5572 if (*e != NUL)
5573 ++e;
5574 }
5575 *file = ga.ga_data;
5576 *num_file = ga.ga_len;
5577
5578 vim_free(buf);
5579 vim_free(pat);
5580 if (mustfree)
5581 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005582 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005583 return OK;
5584}
5585
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5588/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005589 * Call "user_expand_func()" to invoke a user defined Vim script function and
5590 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005592 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005593call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005594 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595 expand_T *xp,
5596 int *num_file,
5597 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005599 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005600 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005601 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005602 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005603 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005604 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
Bram Moolenaarb7515462013-06-29 12:58:33 +02005606 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005607 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 *num_file = 0;
5609 *file = NULL;
5610
Bram Moolenaarb7515462013-06-29 12:58:33 +02005611 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005612 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005613 keep = ccline.cmdbuff[ccline.cmdlen];
5614 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005615 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005616
Bram Moolenaarffa96842018-06-12 22:05:14 +02005617 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5618
5619 args[0].v_type = VAR_STRING;
5620 args[0].vval.v_string = pat;
5621 args[1].v_type = VAR_STRING;
5622 args[1].vval.v_string = xp->xp_line;
5623 args[2].v_type = VAR_NUMBER;
5624 args[2].vval.v_number = xp->xp_col;
5625 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005627 /* Save the cmdline, we don't know what the function may do. */
5628 save_ccline = ccline;
5629 ccline.cmdbuff = NULL;
5630 ccline.cmdprompt = NULL;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005631 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005632
Bram Moolenaarded27a12018-08-01 19:06:03 +02005633 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005634
5635 ccline = save_ccline;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005636 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005637 if (ccline.cmdbuff != NULL)
5638 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005639
Bram Moolenaarffa96842018-06-12 22:05:14 +02005640 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005641 return ret;
5642}
5643
5644/*
5645 * Expand names with a function defined by the user.
5646 */
5647 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ExpandUserDefined(
5649 expand_T *xp,
5650 regmatch_T *regmatch,
5651 int *num_file,
5652 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005653{
5654 char_u *retstr;
5655 char_u *s;
5656 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005657 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005658 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005659 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005660
5661 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5662 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 return FAIL;
5664
5665 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005666 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 e = vim_strchr(s, '\n');
5669 if (e == NULL)
5670 e = s + STRLEN(s);
5671 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005672 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005674 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5675 *e = keep;
5676
5677 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005679 if (ga_grow(&ga, 1) == FAIL)
5680 break;
5681 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5682 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 if (*e != NUL)
5686 ++e;
5687 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005688 vim_free(retstr);
5689 *file = ga.ga_data;
5690 *num_file = ga.ga_len;
5691 return OK;
5692}
5693
5694/*
5695 * Expand names with a list returned by a function defined by the user.
5696 */
5697 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698ExpandUserList(
5699 expand_T *xp,
5700 int *num_file,
5701 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005702{
5703 list_T *retlist;
5704 listitem_T *li;
5705 garray_T ga;
5706
5707 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5708 if (retlist == NULL)
5709 return FAIL;
5710
5711 ga_init2(&ga, (int)sizeof(char *), 3);
5712 /* Loop over the items in the list. */
5713 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5714 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005715 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5716 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005717
5718 if (ga_grow(&ga, 1) == FAIL)
5719 break;
5720
5721 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005722 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005723 ++ga.ga_len;
5724 }
5725 list_unref(retlist);
5726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 *file = ga.ga_data;
5728 *num_file = ga.ga_len;
5729 return OK;
5730}
5731#endif
5732
5733/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005734 * Expand color scheme, compiler or filetype names.
5735 * Search from 'runtimepath':
5736 * 'runtimepath'/{dirnames}/{pat}.vim
5737 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5738 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5739 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5740 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005741 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 */
5743 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005744ExpandRTDir(
5745 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005746 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005747 int *num_file,
5748 char_u ***file,
5749 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 char_u *s;
5752 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005753 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005755 int i;
5756 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758 *num_file = 0;
5759 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005760 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005761 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005763 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005765 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5766 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005768 ga_clear_strings(&ga);
5769 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005771 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005772 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005773 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005775
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005776 if (flags & DIP_START) {
5777 for (i = 0; dirnames[i] != NULL; ++i)
5778 {
5779 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5780 if (s == NULL)
5781 {
5782 ga_clear_strings(&ga);
5783 return FAIL;
5784 }
5785 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5786 globpath(p_pp, s, &ga, 0);
5787 vim_free(s);
5788 }
5789 }
5790
5791 if (flags & DIP_OPT) {
5792 for (i = 0; dirnames[i] != NULL; ++i)
5793 {
5794 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5795 if (s == NULL)
5796 {
5797 ga_clear_strings(&ga);
5798 return FAIL;
5799 }
5800 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5801 globpath(p_pp, s, &ga, 0);
5802 vim_free(s);
5803 }
5804 }
5805
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005806 for (i = 0; i < ga.ga_len; ++i)
5807 {
5808 match = ((char_u **)ga.ga_data)[i];
5809 s = match;
5810 e = s + STRLEN(s);
5811 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5812 {
5813 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005814 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005815 if (s < match || vim_ispathsep(*s))
5816 break;
5817 ++s;
5818 *e = NUL;
5819 mch_memmove(match, s, e - s + 1);
5820 }
5821 }
5822
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005823 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005824 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005825
5826 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005827 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005828 remove_duplicates(&ga);
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 *file = ga.ga_data;
5831 *num_file = ga.ga_len;
5832 return OK;
5833}
5834
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005835/*
5836 * Expand loadplugin names:
5837 * 'packpath'/pack/ * /opt/{pat}
5838 */
5839 static int
5840ExpandPackAddDir(
5841 char_u *pat,
5842 int *num_file,
5843 char_u ***file)
5844{
5845 char_u *s;
5846 char_u *e;
5847 char_u *match;
5848 garray_T ga;
5849 int i;
5850 int pat_len;
5851
5852 *num_file = 0;
5853 *file = NULL;
5854 pat_len = (int)STRLEN(pat);
5855 ga_init2(&ga, (int)sizeof(char *), 10);
5856
5857 s = alloc((unsigned)(pat_len + 26));
5858 if (s == NULL)
5859 {
5860 ga_clear_strings(&ga);
5861 return FAIL;
5862 }
5863 sprintf((char *)s, "pack/*/opt/%s*", pat);
5864 globpath(p_pp, s, &ga, 0);
5865 vim_free(s);
5866
5867 for (i = 0; i < ga.ga_len; ++i)
5868 {
5869 match = ((char_u **)ga.ga_data)[i];
5870 s = gettail(match);
5871 e = s + STRLEN(s);
5872 mch_memmove(match, s, e - s + 1);
5873 }
5874
5875 if (ga.ga_len == 0)
5876 return FAIL;
5877
5878 /* Sort and remove duplicates which can happen when specifying multiple
5879 * directories in dirnames. */
5880 remove_duplicates(&ga);
5881
5882 *file = ga.ga_data;
5883 *num_file = ga.ga_len;
5884 return OK;
5885}
5886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#endif
5888
5889#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5890/*
5891 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005892 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005894 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005895globpath(
5896 char_u *path,
5897 char_u *file,
5898 garray_T *ga,
5899 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 expand_T xpc;
5902 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 int num_p;
5905 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
5907 buf = alloc(MAXPATHL);
5908 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005909 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005911 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 /* Loop over all entries in {path}. */
5915 while (*path != NUL)
5916 {
5917 /* Copy one item of the path to buf[] and concatenate the file name. */
5918 copy_option_part(&path, buf, MAXPATHL, ",");
5919 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5920 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005921# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005922 /* Using the platform's path separator (\) makes vim incorrectly
5923 * treat it as an escape character, use '/' instead. */
5924 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5925 STRCAT(buf, "/");
5926# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005930 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5931 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005933 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005935 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 for (i = 0; i < num_p; ++i)
5938 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005939 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005940 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005941 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 FreeWild(num_p, p);
5946 }
5947 }
5948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951}
5952
5953#endif
5954
5955#if defined(FEAT_CMDHIST) || defined(PROTO)
5956
5957/*********************************
5958 * Command line history stuff *
5959 *********************************/
5960
5961/*
5962 * Translate a history character to the associated type number.
5963 */
5964 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 if (c == ':')
5968 return HIST_CMD;
5969 if (c == '=')
5970 return HIST_EXPR;
5971 if (c == '@')
5972 return HIST_INPUT;
5973 if (c == '>')
5974 return HIST_DEBUG;
5975 return HIST_SEARCH; /* must be '?' or '/' */
5976}
5977
5978/*
5979 * Table of history names.
5980 * These names are used in :history and various hist...() functions.
5981 * It is sufficient to give the significant prefix of a history name.
5982 */
5983
5984static char *(history_names[]) =
5985{
5986 "cmd",
5987 "search",
5988 "expr",
5989 "input",
5990 "debug",
5991 NULL
5992};
5993
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005994#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5995/*
5996 * Function given to ExpandGeneric() to obtain the possible first
5997 * arguments of the ":history command.
5998 */
5999 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006000get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006001{
6002 static char_u compl[2] = { NUL, NUL };
6003 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02006004 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006005 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
6006
6007 if (idx < short_names_count)
6008 {
6009 compl[0] = (char_u)short_names[idx];
6010 return compl;
6011 }
6012 if (idx < short_names_count + history_name_count)
6013 return (char_u *)history_names[idx - short_names_count];
6014 if (idx == short_names_count + history_name_count)
6015 return (char_u *)"all";
6016 return NULL;
6017}
6018#endif
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020/*
6021 * init_history() - Initialize the command line history.
6022 * Also used to re-allocate the history when the size changes.
6023 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006024 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 int newlen; /* new length of history table */
6028 histentry_T *temp;
6029 int i;
6030 int j;
6031 int type;
6032
6033 /*
6034 * If size of history table changed, reallocate it
6035 */
6036 newlen = (int)p_hi;
6037 if (newlen != hislen) /* history length changed */
6038 {
6039 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6040 {
6041 if (newlen)
6042 {
6043 temp = (histentry_T *)lalloc(
6044 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6045 if (temp == NULL) /* out of memory! */
6046 {
6047 if (type == 0) /* first one: just keep the old length */
6048 {
6049 newlen = hislen;
6050 break;
6051 }
6052 /* Already changed one table, now we can only have zero
6053 * length for all tables. */
6054 newlen = 0;
6055 type = -1;
6056 continue;
6057 }
6058 }
6059 else
6060 temp = NULL;
6061 if (newlen == 0 || temp != NULL)
6062 {
6063 if (hisidx[type] < 0) /* there are no entries yet */
6064 {
6065 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006066 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 }
6068 else if (newlen > hislen) /* array becomes bigger */
6069 {
6070 for (i = 0; i <= hisidx[type]; ++i)
6071 temp[i] = history[type][i];
6072 j = i;
6073 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006074 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 for ( ; j < hislen; ++i, ++j)
6076 temp[i] = history[type][j];
6077 }
6078 else /* array becomes smaller or 0 */
6079 {
6080 j = hisidx[type];
6081 for (i = newlen - 1; ; --i)
6082 {
6083 if (i >= 0) /* copy newest entries */
6084 temp[i] = history[type][j];
6085 else /* remove older entries */
6086 vim_free(history[type][j].hisstr);
6087 if (--j < 0)
6088 j = hislen - 1;
6089 if (j == hisidx[type])
6090 break;
6091 }
6092 hisidx[type] = newlen - 1;
6093 }
6094 vim_free(history[type]);
6095 history[type] = temp;
6096 }
6097 }
6098 hislen = newlen;
6099 }
6100}
6101
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006103clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006104{
6105 hisptr->hisnum = 0;
6106 hisptr->viminfo = FALSE;
6107 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006108 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006109}
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111/*
6112 * Check if command line 'str' is already in history.
6113 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6114 */
6115 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006116in_history(
6117 int type,
6118 char_u *str,
6119 int move_to_front, /* Move the entry to the front if it exists */
6120 int sep,
6121 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 int i;
6124 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006125 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 if (hisidx[type] < 0)
6128 return FALSE;
6129 i = hisidx[type];
6130 do
6131 {
6132 if (history[type][i].hisstr == NULL)
6133 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006134
6135 /* For search history, check that the separator character matches as
6136 * well. */
6137 p = history[type][i].hisstr;
6138 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006139 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006140 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
6142 if (!move_to_front)
6143 return TRUE;
6144 last_i = i;
6145 break;
6146 }
6147 if (--i < 0)
6148 i = hislen - 1;
6149 } while (i != hisidx[type]);
6150
6151 if (last_i >= 0)
6152 {
6153 str = history[type][i].hisstr;
6154 while (i != hisidx[type])
6155 {
6156 if (++i >= hislen)
6157 i = 0;
6158 history[type][last_i] = history[type][i];
6159 last_i = i;
6160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006162 history[type][i].viminfo = FALSE;
6163 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006164 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 return TRUE;
6166 }
6167 return FALSE;
6168}
6169
6170/*
6171 * Convert history name (from table above) to its HIST_ equivalent.
6172 * When "name" is empty, return "cmd" history.
6173 * Returns -1 for unknown history name.
6174 */
6175 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006176get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
6178 int i;
6179 int len = (int)STRLEN(name);
6180
6181 /* No argument: use current history. */
6182 if (len == 0)
6183 return hist_char2type(ccline.cmdfirstc);
6184
6185 for (i = 0; history_names[i] != NULL; ++i)
6186 if (STRNICMP(name, history_names[i], len) == 0)
6187 return i;
6188
6189 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6190 return hist_char2type(name[0]);
6191
6192 return -1;
6193}
6194
6195static int last_maptick = -1; /* last seen maptick */
6196
6197/*
6198 * Add the given string to the given history. If the string is already in the
6199 * history then it is moved to the front. "histype" may be one of he HIST_
6200 * values.
6201 */
6202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006203add_to_history(
6204 int histype,
6205 char_u *new_entry,
6206 int in_map, /* consider maptick when inside a mapping */
6207 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 histentry_T *hisptr;
6210 int len;
6211
6212 if (hislen == 0) /* no history */
6213 return;
6214
Bram Moolenaara939e432013-11-09 05:30:26 +01006215 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6216 return;
6217
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 /*
6219 * Searches inside the same mapping overwrite each other, so that only
6220 * the last line is kept. Be careful not to remove a line that was moved
6221 * down, only lines that were added.
6222 */
6223 if (histype == HIST_SEARCH && in_map)
6224 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006225 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
6227 /* Current line is from the same mapping, remove it */
6228 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6229 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006230 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 --hisnum[histype];
6232 if (--hisidx[HIST_SEARCH] < 0)
6233 hisidx[HIST_SEARCH] = hislen - 1;
6234 }
6235 last_maptick = -1;
6236 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006237 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
6239 if (++hisidx[histype] == hislen)
6240 hisidx[histype] = 0;
6241 hisptr = &history[histype][hisidx[histype]];
6242 vim_free(hisptr->hisstr);
6243
6244 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006245 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6247 if (hisptr->hisstr != NULL)
6248 hisptr->hisstr[len + 1] = sep;
6249
6250 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006251 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006252 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 if (histype == HIST_SEARCH && in_map)
6254 last_maptick = maptick;
6255 }
6256}
6257
6258#if defined(FEAT_EVAL) || defined(PROTO)
6259
6260/*
6261 * Get identifier of newest history entry.
6262 * "histype" may be one of the HIST_ values.
6263 */
6264 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006265get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6268 || hisidx[histype] < 0)
6269 return -1;
6270
6271 return history[histype][hisidx[histype]].hisnum;
6272}
6273
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 * Calculate history index from a number:
6276 * num > 0: seen as identifying number of a history entry
6277 * num < 0: relative position in history wrt newest entry
6278 * "histype" may be one of the HIST_ values.
6279 */
6280 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006281calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 int i;
6284 histentry_T *hist;
6285 int wrapped = FALSE;
6286
6287 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6288 || (i = hisidx[histype]) < 0 || num == 0)
6289 return -1;
6290
6291 hist = history[histype];
6292 if (num > 0)
6293 {
6294 while (hist[i].hisnum > num)
6295 if (--i < 0)
6296 {
6297 if (wrapped)
6298 break;
6299 i += hislen;
6300 wrapped = TRUE;
6301 }
6302 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6303 return i;
6304 }
6305 else if (-num <= hislen)
6306 {
6307 i += num + 1;
6308 if (i < 0)
6309 i += hislen;
6310 if (hist[i].hisstr != NULL)
6311 return i;
6312 }
6313 return -1;
6314}
6315
6316/*
6317 * Get a history entry by its index.
6318 * "histype" may be one of the HIST_ values.
6319 */
6320 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006321get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 idx = calc_hist_idx(histype, idx);
6324 if (idx >= 0)
6325 return history[histype][idx].hisstr;
6326 else
6327 return (char_u *)"";
6328}
6329
6330/*
6331 * Clear all entries of a history.
6332 * "histype" may be one of the HIST_ values.
6333 */
6334 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006335clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 int i;
6338 histentry_T *hisptr;
6339
6340 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6341 {
6342 hisptr = history[histype];
6343 for (i = hislen; i--;)
6344 {
6345 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006346 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006347 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 }
6349 hisidx[histype] = -1; /* mark history as cleared */
6350 hisnum[histype] = 0; /* reset identifier counter */
6351 return OK;
6352 }
6353 return FAIL;
6354}
6355
6356/*
6357 * Remove all entries matching {str} from a history.
6358 * "histype" may be one of the HIST_ values.
6359 */
6360 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006361del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 regmatch_T regmatch;
6364 histentry_T *hisptr;
6365 int idx;
6366 int i;
6367 int last;
6368 int found = FALSE;
6369
6370 regmatch.regprog = NULL;
6371 regmatch.rm_ic = FALSE; /* always match case */
6372 if (hislen != 0
6373 && histype >= 0
6374 && histype < HIST_COUNT
6375 && *str != NUL
6376 && (idx = hisidx[histype]) >= 0
6377 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6378 != NULL)
6379 {
6380 i = last = idx;
6381 do
6382 {
6383 hisptr = &history[histype][i];
6384 if (hisptr->hisstr == NULL)
6385 break;
6386 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6387 {
6388 found = TRUE;
6389 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006390 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392 else
6393 {
6394 if (i != last)
6395 {
6396 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006397 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 if (--last < 0)
6400 last += hislen;
6401 }
6402 if (--i < 0)
6403 i += hislen;
6404 } while (i != idx);
6405 if (history[histype][idx].hisstr == NULL)
6406 hisidx[histype] = -1;
6407 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006408 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 return found;
6410}
6411
6412/*
6413 * Remove an indexed entry from a history.
6414 * "histype" may be one of the HIST_ values.
6415 */
6416 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006417del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
6419 int i, j;
6420
6421 i = calc_hist_idx(histype, idx);
6422 if (i < 0)
6423 return FALSE;
6424 idx = hisidx[histype];
6425 vim_free(history[histype][i].hisstr);
6426
6427 /* When deleting the last added search string in a mapping, reset
6428 * last_maptick, so that the last added search string isn't deleted again.
6429 */
6430 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6431 last_maptick = -1;
6432
6433 while (i != idx)
6434 {
6435 j = (i + 1) % hislen;
6436 history[histype][i] = history[histype][j];
6437 i = j;
6438 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006439 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (--i < 0)
6441 i += hislen;
6442 hisidx[histype] = i;
6443 return TRUE;
6444}
6445
6446#endif /* FEAT_EVAL */
6447
6448#if defined(FEAT_CRYPT) || defined(PROTO)
6449/*
6450 * Very specific function to remove the value in ":set key=val" from the
6451 * history.
6452 */
6453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006454remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 char_u *p;
6457 int i;
6458
6459 i = hisidx[HIST_CMD];
6460 if (i < 0)
6461 return;
6462 p = history[HIST_CMD][i].hisstr;
6463 if (p != NULL)
6464 for ( ; *p; ++p)
6465 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6466 {
6467 p = vim_strchr(p + 3, '=');
6468 if (p == NULL)
6469 break;
6470 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006471 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (p[i] == '\\' && p[i + 1])
6473 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006474 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 --p;
6476 }
6477}
6478#endif
6479
6480#endif /* FEAT_CMDHIST */
6481
Bram Moolenaar064154c2016-03-19 22:50:43 +01006482#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006483/*
6484 * Get pointer to the command line info to use. cmdline_paste() may clear
6485 * ccline and put the previous value in prev_ccline.
6486 */
6487 static struct cmdline_info *
6488get_ccline_ptr(void)
6489{
6490 if ((State & CMDLINE) == 0)
6491 return NULL;
6492 if (ccline.cmdbuff != NULL)
6493 return &ccline;
6494 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6495 return &prev_ccline;
6496 return NULL;
6497}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006498#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006499
Bram Moolenaar064154c2016-03-19 22:50:43 +01006500#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006501/*
6502 * Get the current command line in allocated memory.
6503 * Only works when the command line is being edited.
6504 * Returns NULL when something is wrong.
6505 */
6506 char_u *
6507get_cmdline_str(void)
6508{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006509 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006510
Bram Moolenaaree91c332018-09-25 22:27:35 +02006511 if (cmdline_star > 0)
6512 return NULL;
6513 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006514 if (p == NULL)
6515 return NULL;
6516 return vim_strnsave(p->cmdbuff, p->cmdlen);
6517}
6518
6519/*
6520 * Get the current command line position, counted in bytes.
6521 * Zero is the first position.
6522 * Only works when the command line is being edited.
6523 * Returns -1 when something is wrong.
6524 */
6525 int
6526get_cmdline_pos(void)
6527{
6528 struct cmdline_info *p = get_ccline_ptr();
6529
6530 if (p == NULL)
6531 return -1;
6532 return p->cmdpos;
6533}
6534
6535/*
6536 * Set the command line byte position to "pos". Zero is the first position.
6537 * Only works when the command line is being edited.
6538 * Returns 1 when failed, 0 when OK.
6539 */
6540 int
6541set_cmdline_pos(
6542 int pos)
6543{
6544 struct cmdline_info *p = get_ccline_ptr();
6545
6546 if (p == NULL)
6547 return 1;
6548
6549 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6550 * changed the command line. */
6551 if (pos < 0)
6552 new_cmdpos = 0;
6553 else
6554 new_cmdpos = pos;
6555 return 0;
6556}
6557#endif
6558
6559#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6560/*
6561 * Get the current command-line type.
6562 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6563 * Only works when the command line is being edited.
6564 * Returns NUL when something is wrong.
6565 */
6566 int
6567get_cmdline_type(void)
6568{
6569 struct cmdline_info *p = get_ccline_ptr();
6570
6571 if (p == NULL)
6572 return NUL;
6573 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006574 return
6575# ifdef FEAT_EVAL
6576 (p->input_fn) ? '@' :
6577# endif
6578 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006579 return p->cmdfirstc;
6580}
6581#endif
6582
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6584/*
6585 * Get indices "num1,num2" that specify a range within a list (not a range of
6586 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6587 * Returns OK if parsed successfully, otherwise FAIL.
6588 */
6589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006590get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 int len;
6593 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006594 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 *str = skipwhite(*str);
6597 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6598 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006599 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 *str += len;
6601 *num1 = (int)num;
6602 first = TRUE;
6603 }
6604 *str = skipwhite(*str);
6605 if (**str == ',') /* parse "to" part of range */
6606 {
6607 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006608 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 if (len > 0)
6610 {
6611 *num2 = (int)num;
6612 *str = skipwhite(*str + len);
6613 }
6614 else if (!first) /* no number given at all */
6615 return FAIL;
6616 }
6617 else if (first) /* only one number given */
6618 *num2 = *num1;
6619 return OK;
6620}
6621#endif
6622
6623#if defined(FEAT_CMDHIST) || defined(PROTO)
6624/*
6625 * :history command - print a history
6626 */
6627 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006628ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 histentry_T *hist;
6631 int histype1 = HIST_CMD;
6632 int histype2 = HIST_CMD;
6633 int hisidx1 = 1;
6634 int hisidx2 = -1;
6635 int idx;
6636 int i, j, k;
6637 char_u *end;
6638 char_u *arg = eap->arg;
6639
6640 if (hislen == 0)
6641 {
6642 MSG(_("'history' option is zero"));
6643 return;
6644 }
6645
6646 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6647 {
6648 end = arg;
6649 while (ASCII_ISALPHA(*end)
6650 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6651 end++;
6652 i = *end;
6653 *end = NUL;
6654 histype1 = get_histtype(arg);
6655 if (histype1 == -1)
6656 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006657 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
6659 histype1 = 0;
6660 histype2 = HIST_COUNT-1;
6661 }
6662 else
6663 {
6664 *end = i;
6665 EMSG(_(e_trailing));
6666 return;
6667 }
6668 }
6669 else
6670 histype2 = histype1;
6671 *end = i;
6672 }
6673 else
6674 end = arg;
6675 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6676 {
6677 EMSG(_(e_trailing));
6678 return;
6679 }
6680
6681 for (; !got_int && histype1 <= histype2; ++histype1)
6682 {
6683 STRCPY(IObuff, "\n # ");
6684 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6685 MSG_PUTS_TITLE(IObuff);
6686 idx = hisidx[histype1];
6687 hist = history[histype1];
6688 j = hisidx1;
6689 k = hisidx2;
6690 if (j < 0)
6691 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6692 if (k < 0)
6693 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6694 if (idx >= 0 && j <= k)
6695 for (i = idx + 1; !got_int; ++i)
6696 {
6697 if (i == hislen)
6698 i = 0;
6699 if (hist[i].hisstr != NULL
6700 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6701 {
6702 msg_putchar('\n');
6703 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6704 hist[i].hisnum);
6705 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6706 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006707 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 else
6709 STRCAT(IObuff, hist[i].hisstr);
6710 msg_outtrans(IObuff);
6711 out_flush();
6712 }
6713 if (i == idx)
6714 break;
6715 }
6716 }
6717}
6718#endif
6719
6720#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006721/*
6722 * Buffers for history read from a viminfo file. Only valid while reading.
6723 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006724static histentry_T *viminfo_history[HIST_COUNT] =
6725 {NULL, NULL, NULL, NULL, NULL};
6726static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6727static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728static int viminfo_add_at_front = FALSE;
6729
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006730static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
6732/*
6733 * Translate a history type number to the associated character.
6734 */
6735 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006736hist_type2char(
6737 int type,
6738 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 if (type == HIST_CMD)
6741 return ':';
6742 if (type == HIST_SEARCH)
6743 {
6744 if (use_question)
6745 return '?';
6746 else
6747 return '/';
6748 }
6749 if (type == HIST_EXPR)
6750 return '=';
6751 return '@';
6752}
6753
6754/*
6755 * Prepare for reading the history from the viminfo file.
6756 * This allocates history arrays to store the read history lines.
6757 */
6758 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006759prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 int i;
6762 int num;
6763 int type;
6764 int len;
6765
6766 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006767 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (asklen > hislen)
6769 asklen = hislen;
6770
6771 for (type = 0; type < HIST_COUNT; ++type)
6772 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006773 /* Count the number of empty spaces in the history list. Entries read
6774 * from viminfo previously are also considered empty. If there are
6775 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006777 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 num++;
6779 len = asklen;
6780 if (num > len)
6781 len = num;
6782 if (len <= 0)
6783 viminfo_history[type] = NULL;
6784 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006785 viminfo_history[type] = (histentry_T *)lalloc(
6786 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (viminfo_history[type] == NULL)
6788 len = 0;
6789 viminfo_hislen[type] = len;
6790 viminfo_hisidx[type] = 0;
6791 }
6792}
6793
6794/*
6795 * Accept a line from the viminfo, store it in the history array when it's
6796 * new.
6797 */
6798 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006799read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 int type;
6802 long_u len;
6803 char_u *val;
6804 char_u *p;
6805
6806 type = hist_char2type(virp->vir_line[0]);
6807 if (viminfo_hisidx[type] < viminfo_hislen[type])
6808 {
6809 val = viminfo_readstring(virp, 1, TRUE);
6810 if (val != NULL && *val != NUL)
6811 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006812 int sep = (*val == ' ' ? NUL : *val);
6813
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006815 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
6817 /* Need to re-allocate to append the separator byte. */
6818 len = STRLEN(val);
6819 p = lalloc(len + 2, TRUE);
6820 if (p != NULL)
6821 {
6822 if (type == HIST_SEARCH)
6823 {
6824 /* Search entry: Move the separator from the first
6825 * column to after the NUL. */
6826 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006827 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829 else
6830 {
6831 /* Not a search entry: No separator in the viminfo
6832 * file, add a NUL separator. */
6833 mch_memmove(p, val, (size_t)len + 1);
6834 p[len + 1] = NUL;
6835 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006836 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6837 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006838 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6839 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006840 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842 }
6843 }
6844 vim_free(val);
6845 }
6846 return viminfo_readline(virp);
6847}
6848
Bram Moolenaar07219f92013-04-14 23:19:36 +02006849/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006850 * Accept a new style history line from the viminfo, store it in the history
6851 * array when it's new.
6852 */
6853 void
6854handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006855 garray_T *values,
6856 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006857{
6858 int type;
6859 long_u len;
6860 char_u *val;
6861 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006862 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006863
6864 /* Check the format:
6865 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006866 if (values->ga_len < 4
6867 || vp[0].bv_type != BVAL_NR
6868 || vp[1].bv_type != BVAL_NR
6869 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6870 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006871 return;
6872
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006873 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006874 if (type >= HIST_COUNT)
6875 return;
6876 if (viminfo_hisidx[type] < viminfo_hislen[type])
6877 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006878 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006879 if (val != NULL && *val != NUL)
6880 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006881 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6882 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006883 int idx;
6884 int overwrite = FALSE;
6885
6886 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6887 {
6888 /* If lines were written by an older Vim we need to avoid
6889 * getting duplicates. See if the entry already exists. */
6890 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6891 {
6892 p = viminfo_history[type][idx].hisstr;
6893 if (STRCMP(val, p) == 0
6894 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6895 {
6896 overwrite = TRUE;
6897 break;
6898 }
6899 }
6900
6901 if (!overwrite)
6902 {
6903 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006904 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006905 p = lalloc(len + 2, TRUE);
6906 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006907 else
6908 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006909 if (p != NULL)
6910 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006911 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006912 if (!overwrite)
6913 {
6914 mch_memmove(p, val, (size_t)len + 1);
6915 /* Put the separator after the NUL. */
6916 p[len + 1] = sep;
6917 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006918 viminfo_history[type][idx].hisnum = 0;
6919 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006920 viminfo_hisidx[type]++;
6921 }
6922 }
6923 }
6924 }
6925 }
6926}
6927
6928/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006929 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006930 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006931 static void
6932concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
6934 int idx;
6935 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006936
6937 idx = hisidx[type] + viminfo_hisidx[type];
6938 if (idx >= hislen)
6939 idx -= hislen;
6940 else if (idx < 0)
6941 idx = hislen - 1;
6942 if (viminfo_add_at_front)
6943 hisidx[type] = idx;
6944 else
6945 {
6946 if (hisidx[type] == -1)
6947 hisidx[type] = hislen - 1;
6948 do
6949 {
6950 if (history[type][idx].hisstr != NULL
6951 || history[type][idx].viminfo)
6952 break;
6953 if (++idx == hislen)
6954 idx = 0;
6955 } while (idx != hisidx[type]);
6956 if (idx != hisidx[type] && --idx < 0)
6957 idx = hislen - 1;
6958 }
6959 for (i = 0; i < viminfo_hisidx[type]; i++)
6960 {
6961 vim_free(history[type][idx].hisstr);
6962 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6963 history[type][idx].viminfo = TRUE;
6964 history[type][idx].time_set = viminfo_history[type][i].time_set;
6965 if (--idx < 0)
6966 idx = hislen - 1;
6967 }
6968 idx += 1;
6969 idx %= hislen;
6970 for (i = 0; i < viminfo_hisidx[type]; i++)
6971 {
6972 history[type][idx++].hisnum = ++hisnum[type];
6973 idx %= hislen;
6974 }
6975}
6976
6977#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6978 static int
6979#ifdef __BORLANDC__
6980_RTLENTRYF
6981#endif
6982sort_hist(const void *s1, const void *s2)
6983{
6984 histentry_T *p1 = *(histentry_T **)s1;
6985 histentry_T *p2 = *(histentry_T **)s2;
6986
6987 if (p1->time_set < p2->time_set) return -1;
6988 if (p1->time_set > p2->time_set) return 1;
6989 return 0;
6990}
6991#endif
6992
6993/*
6994 * Merge history lines from viminfo and lines typed in this Vim based on the
6995 * timestamp;
6996 */
6997 static void
6998merge_history(int type)
6999{
7000 int max_len;
7001 histentry_T **tot_hist;
7002 histentry_T *new_hist;
7003 int i;
7004 int len;
7005
7006 /* Make one long list with all entries. */
7007 max_len = hislen + viminfo_hisidx[type];
7008 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
7009 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
7010 if (tot_hist == NULL || new_hist == NULL)
7011 {
7012 vim_free(tot_hist);
7013 vim_free(new_hist);
7014 return;
7015 }
7016 for (i = 0; i < viminfo_hisidx[type]; i++)
7017 tot_hist[i] = &viminfo_history[type][i];
7018 len = i;
7019 for (i = 0; i < hislen; i++)
7020 if (history[type][i].hisstr != NULL)
7021 tot_hist[len++] = &history[type][i];
7022
7023 /* Sort the list on timestamp. */
7024 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
7025
7026 /* Keep the newest ones. */
7027 for (i = 0; i < hislen; i++)
7028 {
7029 if (i < len)
7030 {
7031 new_hist[i] = *tot_hist[i];
7032 tot_hist[i]->hisstr = NULL;
7033 if (new_hist[i].hisnum == 0)
7034 new_hist[i].hisnum = ++hisnum[type];
7035 }
7036 else
7037 clear_hist_entry(&new_hist[i]);
7038 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007039 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007040
7041 /* Free what is not kept. */
7042 for (i = 0; i < viminfo_hisidx[type]; i++)
7043 vim_free(viminfo_history[type][i].hisstr);
7044 for (i = 0; i < hislen; i++)
7045 vim_free(history[type][i].hisstr);
7046 vim_free(history[type]);
7047 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007048 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007049}
7050
7051/*
7052 * Finish reading history lines from viminfo. Not used when writing viminfo.
7053 */
7054 void
7055finish_viminfo_history(vir_T *virp)
7056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007058 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
7060 for (type = 0; type < HIST_COUNT; ++type)
7061 {
7062 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007063 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007064
7065 if (merge)
7066 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007068 concat_history(type);
7069
Bram Moolenaard23a8232018-02-10 18:45:26 +01007070 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007071 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 }
7073}
7074
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007075/*
7076 * Write history to viminfo file in "fp".
7077 * When "merge" is TRUE merge history lines with a previously read viminfo
7078 * file, data is in viminfo_history[].
7079 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007082write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
7084 int i;
7085 int type;
7086 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007087 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
7089 init_history();
7090 if (hislen == 0)
7091 return;
7092 for (type = 0; type < HIST_COUNT; ++type)
7093 {
7094 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7095 if (num_saved == 0)
7096 continue;
7097 if (num_saved < 0) /* Use default */
7098 num_saved = hislen;
7099 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7100 type == HIST_CMD ? _("Command Line") :
7101 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007102 type == HIST_EXPR ? _("Expression") :
7103 type == HIST_INPUT ? _("Input Line") :
7104 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (num_saved > hislen)
7106 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007107
7108 /*
7109 * Merge typed and viminfo history:
7110 * round 1: history of typed commands.
7111 * round 2: history from recently read viminfo.
7112 */
7113 for (round = 1; round <= 2; ++round)
7114 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007115 if (round == 1)
7116 /* start at newest entry, somewhere in the list */
7117 i = hisidx[type];
7118 else if (viminfo_hisidx[type] > 0)
7119 /* start at newest entry, first in the list */
7120 i = 0;
7121 else
7122 /* empty list */
7123 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007124 if (i >= 0)
7125 while (num_saved > 0
7126 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007128 char_u *p;
7129 time_t timestamp;
7130 int c = NUL;
7131
7132 if (round == 1)
7133 {
7134 p = history[type][i].hisstr;
7135 timestamp = history[type][i].time_set;
7136 }
7137 else
7138 {
7139 p = viminfo_history[type] == NULL ? NULL
7140 : viminfo_history[type][i].hisstr;
7141 timestamp = viminfo_history[type] == NULL ? 0
7142 : viminfo_history[type][i].time_set;
7143 }
7144
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007145 if (p != NULL && (round == 2
7146 || !merge
7147 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007149 --num_saved;
7150 fputc(hist_type2char(type, TRUE), fp);
7151 /* For the search history: put the separator in the
7152 * second column; use a space if there isn't one. */
7153 if (type == HIST_SEARCH)
7154 {
7155 c = p[STRLEN(p) + 1];
7156 putc(c == NUL ? ' ' : c, fp);
7157 }
7158 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007159
7160 {
7161 char cbuf[NUMBUFLEN];
7162
7163 /* New style history with a bar line. Format:
7164 * |{bartype},{histtype},{timestamp},{separator},"text" */
7165 if (c == NUL)
7166 cbuf[0] = NUL;
7167 else
7168 sprintf(cbuf, "%d", c);
7169 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7170 type, (long)timestamp, cbuf);
7171 barline_writestring(fp, p, LSIZE - 20);
7172 putc('\n', fp);
7173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007175 if (round == 1)
7176 {
7177 /* Decrement index, loop around and stop when back at
7178 * the start. */
7179 if (--i < 0)
7180 i = hislen - 1;
7181 if (i == hisidx[type])
7182 break;
7183 }
7184 else
7185 {
7186 /* Increment index. Stop at the end in the while. */
7187 ++i;
7188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007190 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007191 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007192 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007193 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007194 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007195 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197}
7198#endif /* FEAT_VIMINFO */
7199
7200#if defined(FEAT_FKMAP) || defined(PROTO)
7201/*
7202 * Write a character at the current cursor+offset position.
7203 * It is directly written into the command buffer block.
7204 */
7205 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007206cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7209 {
7210 EMSG(_("E198: cmd_pchar beyond the command length"));
7211 return;
7212 }
7213 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7214 ccline.cmdbuff[ccline.cmdlen] = NUL;
7215}
7216
7217 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007218cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
7220 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7221 {
7222 /* EMSG(_("cmd_gchar beyond the command length")); */
7223 return NUL;
7224 }
7225 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7226}
7227#endif
7228
7229#if defined(FEAT_CMDWIN) || defined(PROTO)
7230/*
7231 * Open a window on the current command line and history. Allow editing in
7232 * the window. Returns when the window is closed.
7233 * Returns:
7234 * CR if the command is to be executed
7235 * Ctrl_C if it is to be abandoned
7236 * K_IGNORE if editing continues
7237 */
7238 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007239open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
7241 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007242 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007244 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 win_T *wp;
7246 int i;
7247 linenr_T lnum;
7248 int histtype;
7249 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 int save_restart_edit = restart_edit;
7251 int save_State = State;
7252 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007253#ifdef FEAT_RIGHTLEFT
7254 int save_cmdmsg_rl = cmdmsg_rl;
7255#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007256#ifdef FEAT_FOLDING
7257 int save_KeyTyped;
7258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
7260 /* Can't do this recursively. Can't do it when typing a password. */
7261 if (cmdwin_type != 0
7262# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7263 || cmdline_star > 0
7264# endif
7265 )
7266 {
7267 beep_flush();
7268 return K_IGNORE;
7269 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007270 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
7272 /* Save current window sizes. */
7273 win_size_save(&winsizes);
7274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007276 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007277
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007278 /* don't use a new tab page */
7279 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007280 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 /* Create a window for the command-line buffer. */
7283 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7284 {
7285 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007286 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 return K_IGNORE;
7288 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007289 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
7291 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007292 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007293 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007296#ifdef FEAT_FOLDING
7297 curwin->w_p_fen = FALSE;
7298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007300 curwin->w_p_rl = cmdmsg_rl;
7301 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007303 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007306 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007307 /* But don't allow switching to another buffer. */
7308 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309
Bram Moolenaar46152342005-09-07 21:18:43 +00007310 /* Showing the prompt may have set need_wait_return, reset it. */
7311 need_wait_return = FALSE;
7312
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007313 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7315 {
7316 if (p_wc == TAB)
7317 {
7318 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7319 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7320 }
7321 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7322 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007323 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007325 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7326 * sets 'textwidth' to 78). */
7327 curbuf->b_p_tw = 0;
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 /* Fill the buffer with the history. */
7330 init_history();
7331 if (hislen > 0)
7332 {
7333 i = hisidx[histtype];
7334 if (i >= 0)
7335 {
7336 lnum = 0;
7337 do
7338 {
7339 if (++i == hislen)
7340 i = 0;
7341 if (history[histtype][i].hisstr != NULL)
7342 ml_append(lnum++, history[histtype][i].hisstr,
7343 (colnr_T)0, FALSE);
7344 }
7345 while (i != hisidx[histtype]);
7346 }
7347 }
7348
7349 /* Replace the empty last line with the current command-line and put the
7350 * cursor there. */
7351 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7352 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7353 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007354 changed_line_abv_curs();
7355 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007356 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007359 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
7361 /* No Ex mode here! */
7362 exmode_active = 0;
7363
7364 State = NORMAL;
7365# ifdef FEAT_MOUSE
7366 setmouse();
7367# endif
7368
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007370 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007371 if (restart_edit != 0) /* autocmd with ":startinsert" */
7372 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
7374 i = RedrawingDisabled;
7375 RedrawingDisabled = 0;
7376
7377 /*
7378 * Call the main loop until <CR> or CTRL-C is typed.
7379 */
7380 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007381 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 RedrawingDisabled = i;
7384
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007385# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007386 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007387# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007388
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007390 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007391
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007392# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007393 /* Restore KeyTyped in case it is modified by autocommands */
7394 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395# endif
7396
Bram Moolenaarccc18222007-05-10 18:25:20 +00007397 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007398 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 cmdwin_type = 0;
7400
7401 exmode_active = save_exmode;
7402
Bram Moolenaarf9821062008-06-20 16:31:07 +00007403 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007405 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {
7407 cmdwin_result = Ctrl_C;
7408 EMSG(_("E199: Active window or buffer deleted"));
7409 }
7410 else
7411 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007412# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 /* autocmds may abort script processing */
7414 if (aborting() && cmdwin_result != K_IGNORE)
7415 cmdwin_result = Ctrl_C;
7416# endif
7417 /* Set the new command line from the cmdline buffer. */
7418 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007419 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007421 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7422
7423 if (histtype == HIST_CMD)
7424 {
7425 /* Execute the command directly. */
7426 ccline.cmdbuff = vim_strsave((char_u *)p);
7427 cmdwin_result = CAR;
7428 }
7429 else
7430 {
7431 /* First need to cancel what we were doing. */
7432 ccline.cmdbuff = NULL;
7433 stuffcharReadbuff(':');
7434 stuffReadbuff((char_u *)p);
7435 stuffcharReadbuff(CAR);
7436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 }
7438 else if (cmdwin_result == K_XF2) /* :qa typed */
7439 {
7440 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7441 cmdwin_result = CAR;
7442 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007443 else if (cmdwin_result == Ctrl_C)
7444 {
7445 /* :q or :close, don't execute any command
7446 * and don't modify the cmd window. */
7447 ccline.cmdbuff = NULL;
7448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 else
7450 ccline.cmdbuff = vim_strsave(ml_get_curline());
7451 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007452 {
7453 ccline.cmdbuff = vim_strsave((char_u *)"");
7454 ccline.cmdlen = 0;
7455 ccline.cmdbufflen = 1;
7456 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 else
7460 {
7461 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7462 ccline.cmdbufflen = ccline.cmdlen + 1;
7463 ccline.cmdpos = curwin->w_cursor.col;
7464 if (ccline.cmdpos > ccline.cmdlen)
7465 ccline.cmdpos = ccline.cmdlen;
7466 if (cmdwin_result == K_IGNORE)
7467 {
7468 set_cmdspos_cursor();
7469 redrawcmd();
7470 }
7471 }
7472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007474 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007475# ifdef FEAT_CONCEAL
7476 /* Avoid command-line window first character being concealed. */
7477 curwin->w_p_cole = 0;
7478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007480 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 win_goto(old_curwin);
7482 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007483
7484 /* win_close() may have already wiped the buffer when 'bh' is
7485 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007486 if (bufref_valid(&bufref))
7487 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
7489 /* Restore window sizes. */
7490 win_size_restore(&winsizes);
7491
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007492 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494
7495 ga_clear(&winsizes);
7496 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007497# ifdef FEAT_RIGHTLEFT
7498 cmdmsg_rl = save_cmdmsg_rl;
7499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 State = save_State;
7502# ifdef FEAT_MOUSE
7503 setmouse();
7504# endif
7505
7506 return cmdwin_result;
7507}
7508#endif /* FEAT_CMDWIN */
7509
7510/*
7511 * Used for commands that either take a simple command string argument, or:
7512 * cmd << endmarker
7513 * {script}
7514 * endmarker
7515 * Returns a pointer to allocated memory with {script} or NULL.
7516 */
7517 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007518script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
7520 char_u *theline;
7521 char *end_pattern = NULL;
7522 char dot[] = ".";
7523 garray_T ga;
7524
7525 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7526 return NULL;
7527
7528 ga_init2(&ga, 1, 0x400);
7529
7530 if (cmd[2] != NUL)
7531 end_pattern = (char *)skipwhite(cmd + 2);
7532 else
7533 end_pattern = dot;
7534
7535 for (;;)
7536 {
7537 theline = eap->getline(
7538#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007539 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540#endif
7541 NUL, eap->cookie, 0);
7542
7543 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007544 {
7545 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548
7549 ga_concat(&ga, theline);
7550 ga_append(&ga, '\n');
7551 vim_free(theline);
7552 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007553 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
7555 return (char_u *)ga.ga_data;
7556}