blob: 4514540d80bf2866e44528ae05abc8434ffd0873 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020/*
21 * Variables shared between getcmdline(), redrawcmdline() and others.
22 * These need to be saved when using CTRL-R |, that's why they are in a
23 * structure.
24 */
25struct cmdline_info
26{
27 char_u *cmdbuff; /* pointer to command line buffer */
28 int cmdbufflen; /* length of cmdbuff */
29 int cmdlen; /* number of chars in command line */
30 int cmdpos; /* current cursor position */
31 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020032 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 int cmdindent; /* number of spaces before cmdline */
34 char_u *cmdprompt; /* message in front of cmdline */
35 int cmdattr; /* attributes for prompt */
36 int overstrike; /* Typing mode on the command line. Shared by
37 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000038 expand_T *xpc; /* struct being used for expansion, xp_pattern
39 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040 int xp_context; /* type of expansion */
41# ifdef FEAT_EVAL
42 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000043 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
Bram Moolenaar438d1762018-09-30 17:11:48 +020047// The current cmdline_info. It is initialized in getcmdline() and after that
48// used by other functions. When invoking getcmdline() recursively it needs
49// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000050static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar438d1762018-09-30 17:11:48 +020052static int cmd_showtail; /* Only show path tail in lists ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef FEAT_EVAL
55static int new_cmdpos; /* position set by set_cmdline_pos() */
56#endif
57
Bram Moolenaara92522f2017-07-15 15:21:38 +020058static int extra_char = NUL; /* extra character to display when redrawing
59 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020060static int extra_char_shift;
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_CMDHIST
63typedef struct hist_entry
64{
65 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020066 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020068 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069} histentry_T;
70
71static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
72static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
73static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
74 /* identifying (unique) number of newest history entry */
75static int hislen = 0; /* actual length of history tables */
76
Bram Moolenaard25c16e2016-01-29 22:13:30 +010077static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
80#ifdef FEAT_RIGHTLEFT
81static int cmd_hkmap = 0; /* Hebrew mapping during command line */
82#endif
83
Bram Moolenaar438d1762018-09-30 17:11:48 +020084static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010085static int cmdline_charsize(int idx);
86static void set_cmdspos(void);
87static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010088static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static void alloc_cmdbuff(int len);
90static int realloc_cmdbuff(int len);
91static void draw_cmdline(int start, int len);
92static void save_cmdline(struct cmdline_info *ccp);
93static void restore_cmdline(struct cmdline_info *ccp);
94static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void redrawcmdprompt(void);
99static void cursorcmd(void);
100static int ccheck_abbr(int);
101static int nextwild(expand_T *xp, int type, int options, int escape);
102static void escape_fname(char_u **pp);
103static int showmatches(expand_T *xp, int wildmenu);
104static void set_expand_context(expand_T *xp);
105static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
106static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100109static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100110static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200111# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200113# endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200114# if defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100115static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
116static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200119#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100120static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200124static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200127#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaareae1b912019-05-09 15:12:55 +0200128static int sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200129#endif
130
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200131
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200132 static void
133trigger_cmd_autocmd(int typechar, int evt)
134{
135 char_u typestr[2];
136
137 typestr[0] = typechar;
138 typestr[1] = NUL;
139 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
140}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200143 * Abandon the command line.
144 */
145 static void
146abandon_cmdline(void)
147{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100148 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200149 if (msg_scrolled == 0)
150 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100151 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200152 redraw_cmdline = TRUE;
153}
154
Bram Moolenaaree219b02017-12-17 14:55:01 +0100155#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200156/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100157 * Guess that the pattern matches everything. Only finds specific cases, such
158 * as a trailing \|, which can happen while typing a pattern.
159 */
160 static int
161empty_pattern(char_u *p)
162{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100163 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100164
165 /* remove trailing \v and the like */
166 while (n >= 2 && p[n - 2] == '\\'
167 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
168 n -= 2;
169 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
170}
171
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200172// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200173typedef struct {
174 colnr_T vs_curswant;
175 colnr_T vs_leftcol;
176 linenr_T vs_topline;
177# ifdef FEAT_DIFF
178 int vs_topfill;
179# endif
180 linenr_T vs_botline;
181 linenr_T vs_empty_rows;
182} viewstate_T;
183
184 static void
185save_viewstate(viewstate_T *vs)
186{
187 vs->vs_curswant = curwin->w_curswant;
188 vs->vs_leftcol = curwin->w_leftcol;
189 vs->vs_topline = curwin->w_topline;
190# ifdef FEAT_DIFF
191 vs->vs_topfill = curwin->w_topfill;
192# endif
193 vs->vs_botline = curwin->w_botline;
194 vs->vs_empty_rows = curwin->w_empty_rows;
195}
196
197 static void
198restore_viewstate(viewstate_T *vs)
199{
200 curwin->w_curswant = vs->vs_curswant;
201 curwin->w_leftcol = vs->vs_leftcol;
202 curwin->w_topline = vs->vs_topline;
203# ifdef FEAT_DIFF
204 curwin->w_topfill = vs->vs_topfill;
205# endif
206 curwin->w_botline = vs->vs_botline;
207 curwin->w_empty_rows = vs->vs_empty_rows;
208}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200209
210// Struct to store the state of 'incsearch' highlighting.
211typedef struct {
212 pos_T search_start; // where 'incsearch' starts searching
213 pos_T save_cursor;
214 viewstate_T init_viewstate;
215 viewstate_T old_viewstate;
216 pos_T match_start;
217 pos_T match_end;
218 int did_incsearch;
219 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200220 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200221} incsearch_state_T;
222
223 static void
224init_incsearch_state(incsearch_state_T *is_state)
225{
226 is_state->match_start = curwin->w_cursor;
227 is_state->did_incsearch = FALSE;
228 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200229 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200230 CLEAR_POS(&is_state->match_end);
231 is_state->save_cursor = curwin->w_cursor; // may be restored later
232 is_state->search_start = curwin->w_cursor;
233 save_viewstate(&is_state->init_viewstate);
234 save_viewstate(&is_state->old_viewstate);
235}
236
237/*
238 * First move cursor to end of match, then to the start. This
239 * moves the whole match onto the screen when 'nowrap' is set.
240 */
241 static void
242set_search_match(pos_T *t)
243{
244 t->lnum += search_match_lines;
245 t->col = search_match_endcol;
246 if (t->lnum > curbuf->b_ml.ml_line_count)
247 {
248 t->lnum = curbuf->b_ml.ml_line_count;
249 coladvance((colnr_T)MAXCOL);
250 }
251}
252
253/*
254 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200255 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200256 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200257 */
258 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200259do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
260 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200261{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200262 char_u *cmd;
263 cmdmod_T save_cmdmod = cmdmod;
264 char_u *p;
265 int delim_optional = FALSE;
266 int delim;
267 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100268 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200269 exarg_T ea;
270 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200271 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200272
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200276 if (!p_is || cmd_silent)
277 return FALSE;
278
279 // by default search all lines
280 search_first_line = 0;
281 search_last_line = MAXLNUM;
282
283 if (firstc == '/' || firstc == '?')
284 return TRUE;
285 if (firstc != ':')
286 return FALSE;
287
288 vim_memset(&ea, 0, sizeof(ea));
289 ea.line1 = 1;
290 ea.line2 = 1;
291 ea.cmd = ccline.cmdbuff;
292 ea.addr_type = ADDR_LINES;
293
294 parse_command_modifiers(&ea, &dummy, TRUE);
295 cmdmod = save_cmdmod;
296
297 cmd = skip_range(ea.cmd, NULL);
298 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
299 return FALSE;
300
301 // Skip over "substitute" to find the pattern separator.
302 for (p = cmd; ASCII_ISALPHA(*p); ++p)
303 ;
304 if (*skipwhite(p) == NUL)
305 return FALSE;
306
307 if (STRNCMP(cmd, "substitute", p - cmd) == 0
308 || STRNCMP(cmd, "smagic", p - cmd) == 0
309 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
310 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200311 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200312 if (*cmd == 's' && cmd[1] == 'm')
313 p_magic = TRUE;
314 else if (*cmd == 's' && cmd[1] == 'n')
315 p_magic = FALSE;
316 }
317 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
318 {
319 // skip over flags
320 while (ASCII_ISALPHA(*(p = skipwhite(p))))
321 ++p;
322 if (*p == NUL)
323 return FALSE;
324 }
325 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
326 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
327 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
328 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
329 || STRNCMP(cmd, "global", p - cmd) == 0)
330 {
331 // skip over "!"
332 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200333 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200334 p++;
335 if (*skipwhite(p) == NUL)
336 return FALSE;
337 }
338 if (*cmd != 'g')
339 delim_optional = TRUE;
340 }
341 else
342 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200343
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200344 p = skipwhite(p);
345 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
346 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200347
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200348 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200349
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200350 if (end == p && !use_last_pat)
351 return FALSE;
352
353 // Don't do 'hlsearch' highlighting if the pattern matches everything.
354 if (!use_last_pat)
355 {
356 char c = *end;
357 int empty;
358
359 *end = NUL;
360 empty = empty_pattern(p);
361 *end = c;
362 if (empty)
363 return FALSE;
364 }
365
366 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200367 *skiplen = (int)(p - ccline.cmdbuff);
368 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200369
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200370 // parse the address range
371 save_cursor = curwin->w_cursor;
372 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200373 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200374 if (ea.addr_count > 0)
375 {
376 // Allow for reverse match.
377 if (ea.line2 < ea.line1)
378 {
379 search_first_line = ea.line2;
380 search_last_line = ea.line1;
381 }
382 else
383 {
384 search_first_line = ea.line1;
385 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200386 }
387 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200388 else if (cmd[0] == 's' && cmd[1] != 'o')
389 {
390 // :s defaults to the current line
391 search_first_line = curwin->w_cursor.lnum;
392 search_last_line = curwin->w_cursor.lnum;
393 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200394
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200395 curwin->w_cursor = save_cursor;
396 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200397}
398
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200399 static void
400finish_incsearch_highlighting(
401 int gotesc,
402 incsearch_state_T *is_state,
403 int call_update_screen)
404{
405 if (is_state->did_incsearch)
406 {
407 is_state->did_incsearch = FALSE;
408 if (gotesc)
409 curwin->w_cursor = is_state->save_cursor;
410 else
411 {
412 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
413 {
414 // put the '" mark at the original position
415 curwin->w_cursor = is_state->save_cursor;
416 setpcmark();
417 }
418 curwin->w_cursor = is_state->search_start;
419 }
420 restore_viewstate(&is_state->old_viewstate);
421 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200422
423 // by default search all lines
424 search_first_line = 0;
425 search_last_line = MAXLNUM;
426
427 p_magic = is_state->magic_save;
428
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200429 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200430 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200431 if (call_update_screen)
432 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200433 }
434}
435
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200436/*
437 * Do 'incsearch' highlighting if desired.
438 */
439 static void
440may_do_incsearch_highlighting(
441 int firstc,
442 long count,
443 incsearch_state_T *is_state)
444{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200445 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200446 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200447 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200448#ifdef FEAT_RELTIME
449 proftime_T tm;
450#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200451 int next_char;
452 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200453 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200454
Bram Moolenaar198cb662018-09-06 21:44:17 +0200455 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100456 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200457 save_last_search_pattern();
458
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200459 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200460 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200461 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200462 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200463 if (did_do_incsearch && vpeekc() == NUL)
464 // may have skipped a redraw, do it now
465 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200467 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200468
469 // If there is a character waiting, search and redraw later.
470 if (char_avail())
471 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200472 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473 is_state->incsearch_postponed = TRUE;
474 return;
475 }
476 is_state->incsearch_postponed = FALSE;
477
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200478 if (search_first_line == 0)
479 // start at the original cursor position
480 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100481 else if (search_first_line > curbuf->b_ml.ml_line_count)
482 {
483 // start after the last line
484 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
485 curwin->w_cursor.col = MAXCOL;
486 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200487 else
488 {
489 // start at the first line in the range
490 curwin->w_cursor.lnum = search_first_line;
491 curwin->w_cursor.col = 0;
492 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200493
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200494 // Use the previous pattern for ":s//".
495 next_char = ccline.cmdbuff[skiplen + patlen];
496 use_last_pat = patlen == 0 && skiplen > 0
497 && ccline.cmdbuff[skiplen - 1] == next_char;
498
499 // If there is no pattern, don't do anything.
500 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200501 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200502 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200503 set_no_hlsearch(TRUE); // turn off previous highlight
504 redraw_all_later(SOME_VALID);
505 }
506 else
507 {
508 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
509
510 cursor_off(); // so the user knows we're busy
511 out_flush();
512 ++emsg_off; // so it doesn't beep if bad expr
513#ifdef FEAT_RELTIME
514 // Set the time limit to half a second.
515 profile_setlimit(500L, &tm);
516#endif
517 if (!p_hls)
518 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200519 if (search_first_line != 0)
520 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200521 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200522 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200523 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200524#ifdef FEAT_RELTIME
525 &tm, NULL
526#else
527 NULL, NULL
528#endif
529 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200530 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200531 --emsg_off;
532
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200533 if (curwin->w_cursor.lnum < search_first_line
534 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200535 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200536 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200537 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200538 curwin->w_cursor = is_state->search_start;
539 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200540
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200541 // if interrupted while searching, behave like it failed
542 if (got_int)
543 {
544 (void)vpeekc(); // remove <C-C> from input stream
545 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200546 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200547 }
548 else if (char_avail())
549 // cancelled searching because a char was typed
550 is_state->incsearch_postponed = TRUE;
551 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200552 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200553 highlight_match = TRUE; // highlight position
554 else
555 highlight_match = FALSE; // remove highlight
556
557 // First restore the old curwin values, so the screen is positioned in the
558 // same way as the actual search command.
559 restore_viewstate(&is_state->old_viewstate);
560 changed_cline_bef_curs();
561 update_topline();
562
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200563 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564 {
565 pos_T save_pos = curwin->w_cursor;
566
567 is_state->match_start = curwin->w_cursor;
568 set_search_match(&curwin->w_cursor);
569 validate_cursor();
570 end_pos = curwin->w_cursor;
571 is_state->match_end = end_pos;
572 curwin->w_cursor = save_pos;
573 }
574 else
575 end_pos = curwin->w_cursor; // shutup gcc 4
576
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200577 // Disable 'hlsearch' highlighting if the pattern matches everything.
578 // Avoids a flash when typing "foo\|".
579 if (!use_last_pat)
580 {
581 next_char = ccline.cmdbuff[skiplen + patlen];
582 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200583 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
584 {
585 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200586 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200587 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200588 ccline.cmdbuff[skiplen + patlen] = next_char;
589 }
590
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591 validate_cursor();
592 // May redraw the status line to show the cursor position.
593 if (p_ru && curwin->w_status_height > 0)
594 curwin->w_redr_status = TRUE;
595
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 restore_last_search_pattern();
598
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200599 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
600 // end of the pattern, e.g. for ":s/pat/".
601 if (ccline.cmdbuff[skiplen + patlen] != NUL)
602 curwin->w_cursor = is_state->search_start;
603 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 curwin->w_cursor = end_pos;
605
606 msg_starthere();
607 redrawcmdline();
608 is_state->did_incsearch = TRUE;
609}
610
611/*
612 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
613 * or previous match.
614 * Returns FAIL when jumping to cmdline_not_changed;
615 */
616 static int
617may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200618 int firstc,
619 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200623 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 pos_T t;
625 char_u *pat;
626 int search_flags = SEARCH_NOOF;
627 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629
Bram Moolenaar198cb662018-09-06 21:44:17 +0200630 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100631 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200632 save_last_search_pattern();
633
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200634 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200635 {
636 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200637 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200638 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200639 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200640 {
641 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200642 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200643 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200644
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200645 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200646 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200647 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200648 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200649 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200650 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200652 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200654 cursor_off();
655 out_flush();
656 if (c == Ctrl_G)
657 {
658 t = is_state->match_end;
659 if (LT_POS(is_state->match_start, is_state->match_end))
660 // Start searching at the end of the match not at the beginning of
661 // the next column.
662 (void)decl(&t);
663 search_flags += SEARCH_COL;
664 }
665 else
666 t = is_state->match_start;
667 if (!p_hls)
668 search_flags += SEARCH_KEEP;
669 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200670 save = pat[patlen];
671 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100672 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200673 c == Ctrl_G ? FORWARD : BACKWARD,
674 pat, count, search_flags,
675 RE_SEARCH, 0, NULL, NULL);
676 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200677 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200678 if (i)
679 {
680 is_state->search_start = is_state->match_start;
681 is_state->match_end = t;
682 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200683 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200684 {
685 // Move just before the current match, so that when nv_search
686 // finishes the cursor will be put back on the match.
687 is_state->search_start = t;
688 (void)decl(&is_state->search_start);
689 }
690 else if (c == Ctrl_G && firstc == '?')
691 {
692 // Move just after the current match, so that when nv_search
693 // finishes the cursor will be put back on the match.
694 is_state->search_start = t;
695 (void)incl(&is_state->search_start);
696 }
697 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
698 {
699 // wrap around
700 is_state->search_start = t;
701 if (firstc == '?')
702 (void)incl(&is_state->search_start);
703 else
704 (void)decl(&is_state->search_start);
705 }
706
707 set_search_match(&is_state->match_end);
708 curwin->w_cursor = is_state->match_start;
709 changed_cline_bef_curs();
710 update_topline();
711 validate_cursor();
712 highlight_match = TRUE;
713 save_viewstate(&is_state->old_viewstate);
714 update_screen(NOT_VALID);
715 redrawcmdline();
716 }
717 else
718 vim_beep(BO_ERROR);
719 restore_last_search_pattern();
720 return FAIL;
721}
722
723/*
724 * When CTRL-L typed: add character from the match to the pattern.
725 * May set "*c" to the added character.
726 * Return OK when jumping to cmdline_not_changed.
727 */
728 static int
729may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
730{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200731 int skiplen, patlen;
732
Bram Moolenaar198cb662018-09-06 21:44:17 +0200733 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100734 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200735 save_last_search_pattern();
736
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200737 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200738 {
739 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200740 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200741 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100742 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200743
744 // Add a character from under the cursor for 'incsearch'.
745 if (is_state->did_incsearch)
746 {
747 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200748 *c = gchar_cursor();
749 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200750 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200751 // If 'ignorecase' and 'smartcase' are set and the
752 // command line has no uppercase characters, convert
753 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200754 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200755 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200756 if (*c == firstc || vim_strchr((char_u *)(
757 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200758 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200759 // put a backslash before special characters
760 stuffcharReadbuff(*c);
761 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200762 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200763 // add any composing characters
764 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
765 {
766 int save_c = *c;
767
768 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
769 {
770 curwin->w_cursor.col += mb_char2len(*c);
771 *c = gchar_cursor();
772 stuffcharReadbuff(*c);
773 }
774 *c = save_c;
775 }
776 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200777 }
778 }
779 return OK;
780}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200781#endif
782
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200783#ifdef FEAT_ARABIC
784/*
785 * Return TRUE if the command line has an Arabic character at or after "start"
786 * for "len" bytes.
787 */
788 static int
789cmdline_has_arabic(int start, int len)
790{
791 int j;
792 int mb_l;
793 int u8c;
794 char_u *p;
795 int u8cc[MAX_MCO];
796
797 if (!enc_utf8)
798 return FALSE;
799
800 for (j = start; j < start + len; j += mb_l)
801 {
802 p = ccline.cmdbuff + j;
803 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
804 mb_l = utfc_ptr2len_len(p, start + len - j);
805 if (ARABIC_CHAR(u8c))
806 return TRUE;
807 }
808 return FALSE;
809}
810#endif
811
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200812 void
813cmdline_init(void)
814{
815 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
816}
817
Bram Moolenaar66216052017-12-16 16:33:44 +0100818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 * getcmdline() - accept a command line starting with firstc.
820 *
821 * firstc == ':' get ":" command line.
822 * firstc == '/' or '?' get search pattern
823 * firstc == '=' get expression
824 * firstc == '@' get text for input() function
825 * firstc == '>' get text for debug mode
826 * firstc == NUL get text for :insert command
827 * firstc == -1 like NUL, and break on CTRL-C
828 *
829 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
830 * command line.
831 *
832 * Careful: getcmdline() can be called recursively!
833 *
834 * Return pointer to allocated string if there is a commandline, NULL
835 * otherwise.
836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100838getcmdline(
839 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200840 long count, // only used for incremental search
841 int indent) // indent for inside conditionals
842{
843 return getcmdline_int(firstc, count, indent, TRUE);
844}
845
846 static char_u *
847getcmdline_int(
848 int firstc,
849 long count UNUSED, // only used for incremental search
850 int indent, // indent for inside conditionals
851 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 int c;
854 int i;
855 int j;
856 int gotesc = FALSE; /* TRUE when <ESC> just typed */
857 int do_abbr; /* when TRUE check for abbr. */
858#ifdef FEAT_CMDHIST
859 char_u *lookfor = NULL; /* string to match */
860 int hiscnt; /* current history line in use */
861 int histype; /* history type to be used */
862#endif
863#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200864 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#endif
866 int did_wild_list = FALSE; /* did wild_list() recently */
867 int wim_index = 0; /* index in wim_flags[] */
868 int res;
869 int save_msg_scroll = msg_scroll;
870 int save_State = State; /* remember State when called */
871 int some_key_typed = FALSE; /* one of the keys was typed */
872#ifdef FEAT_MOUSE
873 /* mouse drag and release events are ignored, unless they are
874 * preceded with a mouse down event */
875 int ignore_drag_release = TRUE;
876#endif
877#ifdef FEAT_EVAL
878 int break_ctrl_c = FALSE;
879#endif
880 expand_T xpc;
881 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000882 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200883 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200884 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaar438d1762018-09-30 17:11:48 +0200886 if (ccline.cmdbuff != NULL)
887 {
888 // Being called recursively. Since ccline is global, we need to save
889 // the current buffer and restore it when returning.
890 save_cmdline(&save_ccline);
891 did_save_ccline = TRUE;
892 }
893 if (init_ccline)
894 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
895
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#ifdef FEAT_EVAL
897 if (firstc == -1)
898 {
899 firstc = NUL;
900 break_ctrl_c = TRUE;
901 }
902#endif
903#ifdef FEAT_RIGHTLEFT
904 /* start without Hebrew mapping for a command line */
905 if (firstc == ':' || firstc == '=' || firstc == '>')
906 cmd_hkmap = 0;
907#endif
908
909 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200912 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#endif
914
915 /*
916 * set some variables for redrawcmd()
917 */
918 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000919 ccline.cmdindent = (firstc > 0 ? indent : 0);
920
921 /* alloc initial ccline.cmdbuff */
922 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200924 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 ccline.cmdlen = ccline.cmdpos = 0;
926 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100927 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000929 /* autoindent for :insert and :append */
930 if (firstc <= 0)
931 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200932 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000933 ccline.cmdbuff[indent] = NUL;
934 ccline.cmdpos = indent;
935 ccline.cmdspos = indent;
936 ccline.cmdlen = indent;
937 }
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000940 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942#ifdef FEAT_RIGHTLEFT
943 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
944 && (firstc == '/' || firstc == '?'))
945 cmdmsg_rl = TRUE;
946 else
947 cmdmsg_rl = FALSE;
948#endif
949
950 redir_off = TRUE; /* don't redirect the typed command */
951 if (!cmd_silent)
952 {
953 i = msg_scrolled;
954 msg_scrolled = 0; /* avoid wait_return message */
955 gotocmdline(TRUE);
956 msg_scrolled += i;
957 redrawcmdprompt(); /* draw prompt or indent */
958 set_cmdspos();
959 }
960 xpc.xp_context = EXPAND_NOTHING;
961 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000962#ifndef BACKSLASH_IN_FILENAME
963 xpc.xp_shell = FALSE;
964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000966#if defined(FEAT_EVAL)
967 if (ccline.input_fn)
968 {
969 xpc.xp_context = ccline.xp_context;
970 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200971# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000972 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000973# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000974 }
975#endif
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 /*
978 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
979 * doing ":@0" when register 0 doesn't contain a CR.
980 */
981 msg_scroll = FALSE;
982
983 State = CMDLINE;
984
985 if (firstc == '/' || firstc == '?' || firstc == '@')
986 {
987 /* Use ":lmap" mappings for search pattern and input(). */
988 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
989 b_im_ptr = &curbuf->b_p_iminsert;
990 else
991 b_im_ptr = &curbuf->b_p_imsearch;
992 if (*b_im_ptr == B_IMODE_LMAP)
993 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100994#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 im_set_active(*b_im_ptr == B_IMODE_IM);
996#endif
997 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100998#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 else if (p_imcmdline)
1000 im_set_active(TRUE);
1001#endif
1002
1003#ifdef FEAT_MOUSE
1004 setmouse();
1005#endif
1006#ifdef CURSOR_SHAPE
1007 ui_cursor_shape(); /* may show different cursor shape */
1008#endif
1009
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001010 /* When inside an autocommand for writing "exiting" may be set and
1011 * terminal mode set to cooked. Need to set raw mode here then. */
1012 settmode(TMODE_RAW);
1013
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001014 /* Trigger CmdlineEnter autocommands. */
1015 cmdline_type = firstc == NUL ? '-' : firstc;
1016 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#ifdef FEAT_CMDHIST
1019 init_history();
1020 hiscnt = hislen; /* set hiscnt to impossible history value */
1021 histype = hist_char2type(firstc);
1022#endif
1023
1024#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +00001025 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#endif
1027
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001028 /* If something above caused an error, reset the flags, we do want to type
1029 * and execute commands. Display may be messed up a bit. */
1030 if (did_emsg)
1031 redrawcmd();
1032 did_emsg = FALSE;
1033 got_int = FALSE;
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 /*
1036 * Collect the command string, handling editing keys.
1037 */
1038 for (;;)
1039 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001040 redir_off = TRUE; /* Don't redirect the typed command.
1041 Repeated, because a ":redir" inside
1042 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043#ifdef USE_ON_FLY_SCROLL
1044 dont_scroll = FALSE; /* allow scrolling here */
1045#endif
1046 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1047
Bram Moolenaar72532d32018-04-07 19:09:09 +02001048 did_emsg = FALSE; /* There can't really be a reason why an error
1049 that occurs while typing a command should
1050 cause the command not to be executed. */
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001053
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001054 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1055 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001056 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001057 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001058 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (KeyTyped)
1061 {
1062 some_key_typed = TRUE;
1063#ifdef FEAT_RIGHTLEFT
1064 if (cmd_hkmap)
1065 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (cmdmsg_rl && !KeyStuffed)
1067 {
1068 /* Invert horizontal movements and operations. Only when
1069 * typed by the user directly, not when the result of a
1070 * mapping. */
1071 switch (c)
1072 {
1073 case K_RIGHT: c = K_LEFT; break;
1074 case K_S_RIGHT: c = K_S_LEFT; break;
1075 case K_C_RIGHT: c = K_C_LEFT; break;
1076 case K_LEFT: c = K_RIGHT; break;
1077 case K_S_LEFT: c = K_S_RIGHT; break;
1078 case K_C_LEFT: c = K_C_RIGHT; break;
1079 }
1080 }
1081#endif
1082 }
1083
1084 /*
1085 * Ignore got_int when CTRL-C was typed here.
1086 * Don't ignore it in :global, we really need to break then, e.g., for
1087 * ":g/pat/normal /pat" (without the <CR>).
1088 * Don't ignore it for the input() function.
1089 */
1090 if ((c == Ctrl_C
1091#ifdef UNIX
1092 || c == intr_char
1093#endif
1094 )
1095#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1096 && firstc != '@'
1097#endif
1098#ifdef FEAT_EVAL
1099 && !break_ctrl_c
1100#endif
1101 && !global_busy)
1102 got_int = FALSE;
1103
1104#ifdef FEAT_CMDHIST
1105 /* free old command line when finished moving around in the history
1106 * list */
1107 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001108 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001109 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 && c != K_PAGEDOWN && c != K_PAGEUP
1111 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001112 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001114 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#endif
1116
1117 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001118 * When there are matching completions to select <S-Tab> works like
1119 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001121 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 c = Ctrl_P;
1123
1124#ifdef FEAT_WILDMENU
1125 /* Special translations for 'wildmenu' */
1126 if (did_wild_list && p_wmnu)
1127 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001128 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001130 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 c = Ctrl_N;
1132 }
1133 /* Hitting CR after "emenu Name.": complete submenu */
1134 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1135 && ccline.cmdpos > 1
1136 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1137 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1138 && (c == '\n' || c == '\r' || c == K_KENTER))
1139 c = K_DOWN;
1140#endif
1141
1142 /* free expanded names when finished walking through matches */
1143 if (xpc.xp_numfiles != -1
1144 && !(c == p_wc && KeyTyped) && c != p_wcm
1145 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1146 && c != Ctrl_L)
1147 {
1148 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1149 did_wild_list = FALSE;
1150#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001151 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#endif
1153 xpc.xp_context = EXPAND_NOTHING;
1154 wim_index = 0;
1155#ifdef FEAT_WILDMENU
1156 if (p_wmnu && wild_menu_showing != 0)
1157 {
1158 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001159 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001160
1161 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001162 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 if (wild_menu_showing == WM_SCROLLED)
1165 {
1166 /* Entered command line, move it up */
1167 cmdline_row--;
1168 redrawcmd();
1169 }
1170 else if (save_p_ls != -1)
1171 {
1172 /* restore 'laststatus' and 'winminheight' */
1173 p_ls = save_p_ls;
1174 p_wmh = save_p_wmh;
1175 last_status(FALSE);
1176 update_screen(VALID); /* redraw the screen NOW */
1177 redrawcmd();
1178 save_p_ls = -1;
1179 }
1180 else
1181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 redraw_statuslines();
1184 }
1185 KeyTyped = skt;
1186 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001187 if (ccline.input_fn)
1188 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190#endif
1191 }
1192
1193#ifdef FEAT_WILDMENU
1194 /* Special translations for 'wildmenu' */
1195 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1196 {
1197 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001198 if (c == K_DOWN && ccline.cmdpos > 0
1199 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001201 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
1203 /* Hitting <Up>: Remove one submenu name in front of the
1204 * cursor */
1205 int found = FALSE;
1206
1207 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1208 i = 0;
1209 while (--j > 0)
1210 {
1211 /* check for start of menu name */
1212 if (ccline.cmdbuff[j] == ' '
1213 && ccline.cmdbuff[j - 1] != '\\')
1214 {
1215 i = j + 1;
1216 break;
1217 }
1218 /* check for start of submenu name */
1219 if (ccline.cmdbuff[j] == '.'
1220 && ccline.cmdbuff[j - 1] != '\\')
1221 {
1222 if (found)
1223 {
1224 i = j + 1;
1225 break;
1226 }
1227 else
1228 found = TRUE;
1229 }
1230 }
1231 if (i > 0)
1232 cmdline_del(i);
1233 c = p_wc;
1234 xpc.xp_context = EXPAND_NOTHING;
1235 }
1236 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001237 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001238 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001239 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
1241 char_u upseg[5];
1242
1243 upseg[0] = PATHSEP;
1244 upseg[1] = '.';
1245 upseg[2] = '.';
1246 upseg[3] = PATHSEP;
1247 upseg[4] = NUL;
1248
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001249 if (c == K_DOWN
1250 && ccline.cmdpos > 0
1251 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1252 && (ccline.cmdpos < 3
1253 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1255 {
1256 /* go down a directory */
1257 c = p_wc;
1258 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001259 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 /* If in a direct ancestor, strip off one ../ to go down */
1262 int found = FALSE;
1263
1264 j = ccline.cmdpos;
1265 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1266 while (--j > i)
1267 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001268 if (has_mbyte)
1269 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (vim_ispathsep(ccline.cmdbuff[j]))
1271 {
1272 found = TRUE;
1273 break;
1274 }
1275 }
1276 if (found
1277 && ccline.cmdbuff[j - 1] == '.'
1278 && ccline.cmdbuff[j - 2] == '.'
1279 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1280 {
1281 cmdline_del(j - 2);
1282 c = p_wc;
1283 }
1284 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001285 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
1287 /* go up a directory */
1288 int found = FALSE;
1289
1290 j = ccline.cmdpos - 1;
1291 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1292 while (--j > i)
1293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (has_mbyte)
1295 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (vim_ispathsep(ccline.cmdbuff[j])
1297#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001298 && vim_strchr((char_u *)" *?[{`$%#",
1299 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
1301 )
1302 {
1303 if (found)
1304 {
1305 i = j + 1;
1306 break;
1307 }
1308 else
1309 found = TRUE;
1310 }
1311 }
1312
1313 if (!found)
1314 j = i;
1315 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1316 j += 4;
1317 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1318 && j == i)
1319 j += 3;
1320 else
1321 j = 0;
1322 if (j > 0)
1323 {
1324 /* TODO this is only for DOS/UNIX systems - need to put in
1325 * machine-specific stuff here and in upseg init */
1326 cmdline_del(j);
1327 put_on_cmdline(upseg + 1, 3, FALSE);
1328 }
1329 else if (ccline.cmdpos > i)
1330 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001331
1332 /* Now complete in the new directory. Set KeyTyped in case the
1333 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001335 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339#endif /* FEAT_WILDMENU */
1340
1341 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1342 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1343 if (c == Ctrl_BSL)
1344 {
1345 ++no_mapping;
1346 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001347 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 --no_mapping;
1349 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001350 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1351 * is in a mapping. */
1352 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001353 || (ccline.cmdfirstc == '=' && KeyTyped)
1354#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001355 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001356#endif
1357 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 vungetc(c);
1360 c = Ctrl_BSL;
1361 }
1362#ifdef FEAT_EVAL
1363 else if (c == 'e')
1364 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001365 char_u *p = NULL;
1366 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
1368 /*
1369 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001370 * Need to save and restore the current command line, to be
1371 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 */
1373 if (ccline.cmdpos == ccline.cmdlen)
1374 new_cmdpos = 99999; /* keep it at the end */
1375 else
1376 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (c == '=')
1380 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001382 * to avoid nasty things like going to another buffer when
1383 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001387
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001388 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001390 len = (int)STRLEN(p);
1391 if (realloc_cmdbuff(len + 1) == OK)
1392 {
1393 ccline.cmdlen = len;
1394 STRCPY(ccline.cmdbuff, p);
1395 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001397 /* Restore the cursor or use the position set with
1398 * set_cmdline_pos(). */
1399 if (new_cmdpos > ccline.cmdlen)
1400 ccline.cmdpos = ccline.cmdlen;
1401 else
1402 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001404 KeyTyped = FALSE; /* Don't do p_wc completion. */
1405 redrawcmd();
1406 goto cmdline_changed;
1407 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001408 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410 }
1411 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001412 got_int = FALSE; /* don't abandon the command line */
1413 did_emsg = FALSE;
1414 emsg_on_display = FALSE;
1415 redrawcmd();
1416 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418#endif
1419 else
1420 {
1421 if (c == Ctrl_G && p_im && restart_edit == 0)
1422 restart_edit = 'a';
1423 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1424 in history */
1425 goto returncmd; /* back to Normal mode */
1426 }
1427 }
1428
1429#ifdef FEAT_CMDWIN
1430 if (c == cedit_key || c == K_CMDWIN)
1431 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001432 if (ex_normal_busy == 0 && got_int == FALSE)
1433 {
1434 /*
1435 * Open a window to edit the command line (and history).
1436 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001437 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001438 some_key_typed = TRUE;
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
1441# ifdef FEAT_DIGRAPHS
1442 else
1443# endif
1444#endif
1445#ifdef FEAT_DIGRAPHS
1446 c = do_digraph(c);
1447#endif
1448
1449 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1450 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1451 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001452 /* In Ex mode a backslash escapes a newline. */
1453 if (exmode_active
1454 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001455 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001456 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001457 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001459 if (c == K_KENTER)
1460 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001462 else
1463 {
1464 gotesc = FALSE; /* Might have typed ESC previously, don't
1465 truncate the cmdline now. */
1466 if (ccheck_abbr(c + ABBR_OFF))
1467 goto cmdline_changed;
1468 if (!cmd_silent)
1469 {
1470 windgoto(msg_row, 0);
1471 out_flush();
1472 }
1473 break;
1474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476
1477 /*
1478 * Completion for 'wildchar' or 'wildcharm' key.
1479 * - hitting <ESC> twice means: abandon command line.
1480 * - wildcard expansion is only done when the 'wildchar' key is really
1481 * typed, not when it comes from a macro
1482 */
1483 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1484 {
1485 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1486 {
1487 /* if 'wildmode' contains "list" may still need to list */
1488 if (xpc.xp_numfiles > 1
1489 && !did_wild_list
1490 && (wim_flags[wim_index] & WIM_LIST))
1491 {
1492 (void)showmatches(&xpc, FALSE);
1493 redrawcmd();
1494 did_wild_list = TRUE;
1495 }
1496 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001497 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1498 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001500 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1501 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 else
1503 res = OK; /* don't insert 'wildchar' now */
1504 }
1505 else /* typed p_wc first time */
1506 {
1507 wim_index = 0;
1508 j = ccline.cmdpos;
1509 /* if 'wildmode' first contains "longest", get longest
1510 * common part */
1511 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001512 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1513 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001515 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1516 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
1518 /* if interrupted while completing, behave like it failed */
1519 if (got_int)
1520 {
1521 (void)vpeekc(); /* remove <C-C> from input stream */
1522 got_int = FALSE; /* don't abandon the command line */
1523 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1524#ifdef FEAT_WILDMENU
1525 xpc.xp_context = EXPAND_NOTHING;
1526#endif
1527 goto cmdline_changed;
1528 }
1529
1530 /* when more than one match, and 'wildmode' first contains
1531 * "list", or no change and 'wildmode' contains "longest,list",
1532 * list all matches */
1533 if (res == OK && xpc.xp_numfiles > 1)
1534 {
1535 /* a "longest" that didn't do anything is skipped (but not
1536 * "list:longest") */
1537 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1538 wim_index = 1;
1539 if ((wim_flags[wim_index] & WIM_LIST)
1540#ifdef FEAT_WILDMENU
1541 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1542#endif
1543 )
1544 {
1545 if (!(wim_flags[0] & WIM_LONGEST))
1546 {
1547#ifdef FEAT_WILDMENU
1548 int p_wmnu_save = p_wmnu;
1549 p_wmnu = 0;
1550#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001551 /* remove match */
1552 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#ifdef FEAT_WILDMENU
1554 p_wmnu = p_wmnu_save;
1555#endif
1556 }
1557#ifdef FEAT_WILDMENU
1558 (void)showmatches(&xpc, p_wmnu
1559 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1560#else
1561 (void)showmatches(&xpc, FALSE);
1562#endif
1563 redrawcmd();
1564 did_wild_list = TRUE;
1565 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001566 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1567 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001569 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1570 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001573 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575#ifdef FEAT_WILDMENU
1576 else if (xpc.xp_numfiles == -1)
1577 xpc.xp_context = EXPAND_NOTHING;
1578#endif
1579 }
1580 if (wim_index < 3)
1581 ++wim_index;
1582 if (c == ESC)
1583 gotesc = TRUE;
1584 if (res == OK)
1585 goto cmdline_changed;
1586 }
1587
1588 gotesc = FALSE;
1589
1590 /* <S-Tab> goes to last match, in a clumsy way */
1591 if (c == K_S_TAB && KeyTyped)
1592 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001593 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1594 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1595 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 goto cmdline_changed;
1597 }
1598
1599 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1600 c = NL;
1601
1602 do_abbr = TRUE; /* default: check for abbreviation */
1603
1604 /*
1605 * Big switch for a typed command line character.
1606 */
1607 switch (c)
1608 {
1609 case K_BS:
1610 case Ctrl_H:
1611 case K_DEL:
1612 case K_KDEL:
1613 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (c == K_KDEL)
1615 c = K_DEL;
1616
1617 /*
1618 * delete current character is the same as backspace on next
1619 * character, except at end of line
1620 */
1621 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1622 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (has_mbyte && c == K_DEL)
1624 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1625 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (ccline.cmdpos > 0)
1627 {
1628 char_u *p;
1629
1630 j = ccline.cmdpos;
1631 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (has_mbyte)
1633 {
1634 p = mb_prevptr(ccline.cmdbuff, p);
1635 if (c == Ctrl_W)
1636 {
1637 while (p > ccline.cmdbuff && vim_isspace(*p))
1638 p = mb_prevptr(ccline.cmdbuff, p);
1639 i = mb_get_class(p);
1640 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1641 p = mb_prevptr(ccline.cmdbuff, p);
1642 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001643 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001646 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
1648 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1649 --p;
1650 i = vim_iswordc(p[-1]);
1651 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1652 && vim_iswordc(p[-1]) == i)
1653 --p;
1654 }
1655 else
1656 --p;
1657 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1658 ccline.cmdlen -= j - ccline.cmdpos;
1659 i = ccline.cmdpos;
1660 while (i < ccline.cmdlen)
1661 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1662
1663 /* Truncate at the end, required for multi-byte chars. */
1664 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001665#ifdef FEAT_SEARCH_EXTRA
1666 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001667 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001668 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001669 /* save view settings, so that the screen
1670 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001671 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001672 }
1673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 redrawcmd();
1675 }
1676 else if (ccline.cmdlen == 0 && c != Ctrl_W
1677 && ccline.cmdprompt == NULL && indent == 0)
1678 {
1679 /* In ex and debug mode it doesn't make sense to return. */
1680 if (exmode_active
1681#ifdef FEAT_EVAL
1682 || ccline.cmdfirstc == '>'
1683#endif
1684 )
1685 goto cmdline_not_changed;
1686
Bram Moolenaard23a8232018-02-10 18:45:26 +01001687 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (!cmd_silent)
1689 {
1690#ifdef FEAT_RIGHTLEFT
1691 if (cmdmsg_rl)
1692 msg_col = Columns;
1693 else
1694#endif
1695 msg_col = 0;
1696 msg_putchar(' '); /* delete ':' */
1697 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001698#ifdef FEAT_SEARCH_EXTRA
1699 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001700 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 redraw_cmdline = TRUE;
1703 goto returncmd; /* back to cmd mode */
1704 }
1705 goto cmdline_changed;
1706
1707 case K_INS:
1708 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 ccline.overstrike = !ccline.overstrike;
1710#ifdef CURSOR_SHAPE
1711 ui_cursor_shape(); /* may show different cursor shape */
1712#endif
1713 goto cmdline_not_changed;
1714
1715 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001716 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
1718 /* ":lmap" mappings exists, toggle use of mappings. */
1719 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 im_set_active(FALSE); /* Disable input method */
1722#endif
1723 if (b_im_ptr != NULL)
1724 {
1725 if (State & LANGMAP)
1726 *b_im_ptr = B_IMODE_LMAP;
1727 else
1728 *b_im_ptr = B_IMODE_NONE;
1729 }
1730 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001731#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 else
1733 {
1734 /* There are no ":lmap" mappings, toggle IM. When
1735 * 'imdisable' is set don't try getting the status, it's
1736 * always off. */
1737 if ((p_imdisable && b_im_ptr != NULL)
1738 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1739 {
1740 im_set_active(FALSE); /* Disable input method */
1741 if (b_im_ptr != NULL)
1742 *b_im_ptr = B_IMODE_NONE;
1743 }
1744 else
1745 {
1746 im_set_active(TRUE); /* Enable input method */
1747 if (b_im_ptr != NULL)
1748 *b_im_ptr = B_IMODE_IM;
1749 }
1750 }
1751#endif
1752 if (b_im_ptr != NULL)
1753 {
1754 if (b_im_ptr == &curbuf->b_p_iminsert)
1755 set_iminsert_global();
1756 else
1757 set_imsearch_global();
1758 }
1759#ifdef CURSOR_SHAPE
1760 ui_cursor_shape(); /* may show different cursor shape */
1761#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001762#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /* Show/unshow value of 'keymap' in status lines later. */
1764 status_redraw_curbuf();
1765#endif
1766 goto cmdline_not_changed;
1767
1768/* case '@': only in very old vi */
1769 case Ctrl_U:
1770 /* delete all characters left of the cursor */
1771 j = ccline.cmdpos;
1772 ccline.cmdlen -= j;
1773 i = ccline.cmdpos = 0;
1774 while (i < ccline.cmdlen)
1775 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1776 /* Truncate at the end, required for multi-byte chars. */
1777 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001778#ifdef FEAT_SEARCH_EXTRA
1779 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001780 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 redrawcmd();
1783 goto cmdline_changed;
1784
1785#ifdef FEAT_CLIPBOARD
1786 case Ctrl_Y:
1787 /* Copy the modeless selection, if there is one. */
1788 if (clip_star.state != SELECT_CLEARED)
1789 {
1790 if (clip_star.state == SELECT_DONE)
1791 clip_copy_modeless_selection(TRUE);
1792 goto cmdline_not_changed;
1793 }
1794 break;
1795#endif
1796
1797 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1798 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001799 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001800 * ":normal" runs out of characters. */
1801 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001802 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 goto cmdline_not_changed;
1804
1805 gotesc = TRUE; /* will free ccline.cmdbuff after
1806 putting it in history */
1807 goto returncmd; /* back to cmd mode */
1808
1809 case Ctrl_R: /* insert register */
1810#ifdef USE_ON_FLY_SCROLL
1811 dont_scroll = TRUE; /* disallow scrolling here */
1812#endif
1813 putcmdline('"', TRUE);
1814 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001815 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (i == Ctrl_O)
1817 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1818 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001819 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001820 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 --no_mapping;
1822#ifdef FEAT_EVAL
1823 /*
1824 * Insert the result of an expression.
1825 * Need to save the current command line, to be able to enter
1826 * a new one...
1827 */
1828 new_cmdpos = -1;
1829 if (c == '=')
1830 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001831 if (ccline.cmdfirstc == '=' // can't do this recursively
1832 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
1834 beep_flush();
1835 c = ESC;
1836 }
1837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840#endif
1841 if (c != ESC) /* use ESC to cancel inserting register */
1842 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001843 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001844
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001845#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001846 /* When there was a serious error abort getting the
1847 * command line. */
1848 if (aborting())
1849 {
1850 gotesc = TRUE; /* will free ccline.cmdbuff after
1851 putting it in history */
1852 goto returncmd; /* back to cmd mode */
1853 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 KeyTyped = FALSE; /* Don't do p_wc completion. */
1856#ifdef FEAT_EVAL
1857 if (new_cmdpos >= 0)
1858 {
1859 /* set_cmdline_pos() was used */
1860 if (new_cmdpos > ccline.cmdlen)
1861 ccline.cmdpos = ccline.cmdlen;
1862 else
1863 ccline.cmdpos = new_cmdpos;
1864 }
1865#endif
1866 }
1867 redrawcmd();
1868 goto cmdline_changed;
1869
1870 case Ctrl_D:
1871 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1872 break; /* Use ^D as normal char instead */
1873
1874 redrawcmd();
1875 continue; /* don't do incremental search now */
1876
1877 case K_RIGHT:
1878 case K_S_RIGHT:
1879 case K_C_RIGHT:
1880 do
1881 {
1882 if (ccline.cmdpos >= ccline.cmdlen)
1883 break;
1884 i = cmdline_charsize(ccline.cmdpos);
1885 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1886 break;
1887 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001889 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 + ccline.cmdpos);
1891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 ++ccline.cmdpos;
1893 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001894 while ((c == K_S_RIGHT || c == K_C_RIGHT
1895 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (has_mbyte)
1898 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 goto cmdline_not_changed;
1900
1901 case K_LEFT:
1902 case K_S_LEFT:
1903 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001904 if (ccline.cmdpos == 0)
1905 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 do
1907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (has_mbyte) /* move to first byte of char */
1910 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1911 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1913 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001914 while (ccline.cmdpos > 0
1915 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001916 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (has_mbyte)
1919 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 goto cmdline_not_changed;
1921
1922 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001923 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001924 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
Bram Moolenaar4f974752019-02-17 17:44:42 +01001926#ifdef FEAT_GUI_MSWIN
1927 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1928 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001929 case K_F4:
1930 if (mod_mask == MOD_MASK_ALT)
1931 {
1932 redrawcmd(); /* somehow the cmdline is cleared */
1933 goto cmdline_not_changed;
1934 }
1935 break;
1936#endif
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#ifdef FEAT_MOUSE
1939 case K_MIDDLEDRAG:
1940 case K_MIDDLERELEASE:
1941 goto cmdline_not_changed; /* Ignore mouse */
1942
1943 case K_MIDDLEMOUSE:
1944# ifdef FEAT_GUI
1945 /* When GUI is active, also paste when 'mouse' is empty */
1946 if (!gui.in_use)
1947# endif
1948 if (!mouse_has(MOUSE_COMMAND))
1949 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001950# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001952 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001954# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001955 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 redrawcmd();
1957 goto cmdline_changed;
1958
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001959# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001961 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 redrawcmd();
1963 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 case K_LEFTDRAG:
1967 case K_LEFTRELEASE:
1968 case K_RIGHTDRAG:
1969 case K_RIGHTRELEASE:
1970 /* Ignore drag and release events when the button-down wasn't
1971 * seen before. */
1972 if (ignore_drag_release)
1973 goto cmdline_not_changed;
1974 /* FALLTHROUGH */
1975 case K_LEFTMOUSE:
1976 case K_RIGHTMOUSE:
1977 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1978 ignore_drag_release = TRUE;
1979 else
1980 ignore_drag_release = FALSE;
1981# ifdef FEAT_GUI
1982 /* When GUI is active, also move when 'mouse' is empty */
1983 if (!gui.in_use)
1984# endif
1985 if (!mouse_has(MOUSE_COMMAND))
1986 goto cmdline_not_changed; /* Ignore mouse */
1987# ifdef FEAT_CLIPBOARD
1988 if (mouse_row < cmdline_row && clip_star.available)
1989 {
1990 int button, is_click, is_drag;
1991
1992 /*
1993 * Handle modeless selection.
1994 */
1995 button = get_mouse_button(KEY2TERMCAP1(c),
1996 &is_click, &is_drag);
1997 if (mouse_model_popup() && button == MOUSE_LEFT
1998 && (mod_mask & MOD_MASK_SHIFT))
1999 {
2000 /* Translate shift-left to right button. */
2001 button = MOUSE_RIGHT;
2002 mod_mask &= ~MOD_MASK_SHIFT;
2003 }
2004 clip_modeless(button, is_click, is_drag);
2005 goto cmdline_not_changed;
2006 }
2007# endif
2008
2009 set_cmdspos();
2010 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2011 ++ccline.cmdpos)
2012 {
2013 i = cmdline_charsize(ccline.cmdpos);
2014 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2015 && mouse_col < ccline.cmdspos % Columns + i)
2016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (has_mbyte)
2018 {
2019 /* Count ">" for double-wide char that doesn't fit. */
2020 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002021 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 + ccline.cmdpos) - 1;
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 ccline.cmdspos += i;
2025 }
2026 goto cmdline_not_changed;
2027
2028 /* Mouse scroll wheel: ignored here */
2029 case K_MOUSEDOWN:
2030 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002031 case K_MOUSELEFT:
2032 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 /* Alternate buttons ignored here */
2034 case K_X1MOUSE:
2035 case K_X1DRAG:
2036 case K_X1RELEASE:
2037 case K_X2MOUSE:
2038 case K_X2DRAG:
2039 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002040 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 goto cmdline_not_changed;
2042
2043#endif /* FEAT_MOUSE */
2044
2045#ifdef FEAT_GUI
2046 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2047 case K_LEFTRELEASE_NM:
2048 goto cmdline_not_changed;
2049
2050 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002051 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 gui_do_scroll();
2054 redrawcmd();
2055 }
2056 goto cmdline_not_changed;
2057
2058 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002059 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002061 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 redrawcmd();
2063 }
2064 goto cmdline_not_changed;
2065#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002066#ifdef FEAT_GUI_TABLINE
2067 case K_TABLINE:
2068 case K_TABMENU:
2069 /* Don't want to change any tabs here. Make sure the same tab
2070 * is still selected. */
2071 if (gui_use_tabline())
2072 gui_mch_set_curtab(tabpage_index(curtab));
2073 goto cmdline_not_changed;
2074#endif
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 case K_SELECT: /* end of Select mode mapping - ignore */
2077 goto cmdline_not_changed;
2078
2079 case Ctrl_B: /* begin of command line */
2080 case K_HOME:
2081 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 case K_S_HOME:
2083 case K_C_HOME:
2084 ccline.cmdpos = 0;
2085 set_cmdspos();
2086 goto cmdline_not_changed;
2087
2088 case Ctrl_E: /* end of command line */
2089 case K_END:
2090 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 case K_S_END:
2092 case K_C_END:
2093 ccline.cmdpos = ccline.cmdlen;
2094 set_cmdspos_cursor();
2095 goto cmdline_not_changed;
2096
2097 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002098 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 break;
2100 goto cmdline_changed;
2101
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002102 case Ctrl_L:
2103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002104 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002105 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002106#endif
2107
2108 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002109 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 break;
2111 goto cmdline_changed;
2112
2113 case Ctrl_N: /* next match */
2114 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002115 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002117 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2118 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002120 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002123 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 case K_UP:
2125 case K_DOWN:
2126 case K_S_UP:
2127 case K_S_DOWN:
2128 case K_PAGEUP:
2129 case K_KPAGEUP:
2130 case K_PAGEDOWN:
2131 case K_KPAGEDOWN:
2132 if (hislen == 0 || firstc == NUL) /* no history */
2133 goto cmdline_not_changed;
2134
2135 i = hiscnt;
2136
2137 /* save current command string so it can be restored later */
2138 if (lookfor == NULL)
2139 {
2140 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2141 goto cmdline_not_changed;
2142 lookfor[ccline.cmdpos] = NUL;
2143 }
2144
2145 j = (int)STRLEN(lookfor);
2146 for (;;)
2147 {
2148 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002149 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002150 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 if (hiscnt == hislen) /* first time */
2153 hiscnt = hisidx[histype];
2154 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2155 hiscnt = hislen - 1;
2156 else if (hiscnt != hisidx[histype] + 1)
2157 --hiscnt;
2158 else /* at top of list */
2159 {
2160 hiscnt = i;
2161 break;
2162 }
2163 }
2164 else /* one step forwards */
2165 {
2166 /* on last entry, clear the line */
2167 if (hiscnt == hisidx[histype])
2168 {
2169 hiscnt = hislen;
2170 break;
2171 }
2172
2173 /* not on a history line, nothing to do */
2174 if (hiscnt == hislen)
2175 break;
2176 if (hiscnt == hislen - 1) /* wrap around */
2177 hiscnt = 0;
2178 else
2179 ++hiscnt;
2180 }
2181 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2182 {
2183 hiscnt = i;
2184 break;
2185 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002186 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002187 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 || STRNCMP(history[histype][hiscnt].hisstr,
2189 lookfor, (size_t)j) == 0)
2190 break;
2191 }
2192
2193 if (hiscnt != i) /* jumped to other entry */
2194 {
2195 char_u *p;
2196 int len;
2197 int old_firstc;
2198
Bram Moolenaar438d1762018-09-30 17:11:48 +02002199 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002200 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (hiscnt == hislen)
2202 p = lookfor; /* back to the old one */
2203 else
2204 p = history[histype][hiscnt].hisstr;
2205
2206 if (histype == HIST_SEARCH
2207 && p != lookfor
2208 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2209 {
2210 /* Correct for the separator character used when
2211 * adding the history entry vs the one used now.
2212 * First loop: count length.
2213 * Second loop: copy the characters. */
2214 for (i = 0; i <= 1; ++i)
2215 {
2216 len = 0;
2217 for (j = 0; p[j] != NUL; ++j)
2218 {
2219 /* Replace old sep with new sep, unless it is
2220 * escaped. */
2221 if (p[j] == old_firstc
2222 && (j == 0 || p[j - 1] != '\\'))
2223 {
2224 if (i > 0)
2225 ccline.cmdbuff[len] = firstc;
2226 }
2227 else
2228 {
2229 /* Escape new sep, unless it is already
2230 * escaped. */
2231 if (p[j] == firstc
2232 && (j == 0 || p[j - 1] != '\\'))
2233 {
2234 if (i > 0)
2235 ccline.cmdbuff[len] = '\\';
2236 ++len;
2237 }
2238 if (i > 0)
2239 ccline.cmdbuff[len] = p[j];
2240 }
2241 ++len;
2242 }
2243 if (i == 0)
2244 {
2245 alloc_cmdbuff(len);
2246 if (ccline.cmdbuff == NULL)
2247 goto returncmd;
2248 }
2249 }
2250 ccline.cmdbuff[len] = NUL;
2251 }
2252 else
2253 {
2254 alloc_cmdbuff((int)STRLEN(p));
2255 if (ccline.cmdbuff == NULL)
2256 goto returncmd;
2257 STRCPY(ccline.cmdbuff, p);
2258 }
2259
2260 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2261 redrawcmd();
2262 goto cmdline_changed;
2263 }
2264 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002265#endif
2266 goto cmdline_not_changed;
2267
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002268#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002269 case Ctrl_G: /* next match */
2270 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002271 if (may_adjust_incsearch_highlighting(
2272 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002273 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#endif
2276
2277 case Ctrl_V:
2278 case Ctrl_Q:
2279#ifdef FEAT_MOUSE
2280 ignore_drag_release = TRUE;
2281#endif
2282 putcmdline('^', TRUE);
2283 c = get_literal(); /* get next (two) character(s) */
2284 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002285 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 /* may need to remove ^ when composing char was typed */
2287 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2288 {
2289 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2290 msg_putchar(' ');
2291 cursorcmd();
2292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 break;
2294
2295#ifdef FEAT_DIGRAPHS
2296 case Ctrl_K:
2297#ifdef FEAT_MOUSE
2298 ignore_drag_release = TRUE;
2299#endif
2300 putcmdline('?', TRUE);
2301#ifdef USE_ON_FLY_SCROLL
2302 dont_scroll = TRUE; /* disallow scrolling here */
2303#endif
2304 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002305 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (c != NUL)
2307 break;
2308
2309 redrawcmd();
2310 goto cmdline_not_changed;
2311#endif /* FEAT_DIGRAPHS */
2312
2313#ifdef FEAT_RIGHTLEFT
2314 case Ctrl__: /* CTRL-_: switch language mode */
2315 if (!p_ari)
2316 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002317 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 goto cmdline_not_changed;
2319#endif
2320
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002321 case K_PS:
2322 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2323 goto cmdline_changed;
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 default:
2326#ifdef UNIX
2327 if (c == intr_char)
2328 {
2329 gotesc = TRUE; /* will free ccline.cmdbuff after
2330 putting it in history */
2331 goto returncmd; /* back to Normal mode */
2332 }
2333#endif
2334 /*
2335 * Normal character with no special meaning. Just set mod_mask
2336 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2337 * the string <S-Space>. This should only happen after ^V.
2338 */
2339 if (!IS_SPECIAL(c))
2340 mod_mask = 0x0;
2341 break;
2342 }
2343 /*
2344 * End of switch on command line character.
2345 * We come here if we have a normal character.
2346 */
2347
Bram Moolenaar13505972019-01-24 15:04:48 +01002348 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2349 && (ccheck_abbr(
2350 // Add ABBR_OFF for characters above 0x100, this is
2351 // what check_abbr() expects.
2352 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2353 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 goto cmdline_changed;
2355
2356 /*
2357 * put the character in the command line
2358 */
2359 if (IS_SPECIAL(c) || mod_mask != 0)
2360 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2361 else
2362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (has_mbyte)
2364 {
2365 j = (*mb_char2bytes)(c, IObuff);
2366 IObuff[j] = NUL; /* exclude composing chars */
2367 put_on_cmdline(IObuff, j, TRUE);
2368 }
2369 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 IObuff[0] = c;
2372 put_on_cmdline(IObuff, 1, TRUE);
2373 }
2374 }
2375 goto cmdline_changed;
2376
2377/*
2378 * This part implements incremental searches for "/" and "?"
2379 * Jump to cmdline_not_changed when a character has been read but the command
2380 * line did not change. Then we only search and redraw if something changed in
2381 * the past.
2382 * Jump to cmdline_changed when the command line did change.
2383 * (Sorry for the goto's, I know it is ugly).
2384 */
2385cmdline_not_changed:
2386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002387 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 continue;
2389#endif
2390
2391cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002392 /* Trigger CmdlineChanged autocommands. */
2393 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002396 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#endif
2398
2399#ifdef FEAT_RIGHTLEFT
2400 if (cmdmsg_rl
2401# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002402 || (p_arshape && !p_tbidi
2403 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404# endif
2405 )
2406 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002407 * right-left typing. Not efficient, but it works.
2408 * Do it only when there are no characters left to read
2409 * to avoid useless intermediate redraws. */
2410 if (vpeekc() == NUL)
2411 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#endif
2413 }
2414
2415returncmd:
2416
2417#ifdef FEAT_RIGHTLEFT
2418 cmdmsg_rl = FALSE;
2419#endif
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002422 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002425 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426#endif
2427
2428 if (ccline.cmdbuff != NULL)
2429 {
2430 /*
2431 * Put line in history buffer (":" and "=" only when it was typed).
2432 */
2433#ifdef FEAT_CMDHIST
2434 if (ccline.cmdlen && firstc != NUL
2435 && (some_key_typed || histype == HIST_SEARCH))
2436 {
2437 add_to_history(histype, ccline.cmdbuff, TRUE,
2438 histype == HIST_SEARCH ? firstc : NUL);
2439 if (firstc == ':')
2440 {
2441 vim_free(new_last_cmdline);
2442 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2443 }
2444 }
2445#endif
2446
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002447 if (gotesc)
2448 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450
2451 /*
2452 * If the screen was shifted up, redraw the whole screen (later).
2453 * If the line is too long, clear it, so ruler and shown command do
2454 * not get printed in the middle of it.
2455 */
2456 msg_check();
2457 msg_scroll = save_msg_scroll;
2458 redir_off = FALSE;
2459
2460 /* When the command line was typed, no need for a wait-return prompt. */
2461 if (some_key_typed)
2462 need_wait_return = FALSE;
2463
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002464 /* Trigger CmdlineLeave autocommands. */
2465 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002468#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2470 im_save_status(b_im_ptr);
2471 im_set_active(FALSE);
2472#endif
2473#ifdef FEAT_MOUSE
2474 setmouse();
2475#endif
2476#ifdef CURSOR_SHAPE
2477 ui_cursor_shape(); /* may show different cursor shape */
2478#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002479 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
Bram Moolenaar438d1762018-09-30 17:11:48 +02002481theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002482 {
2483 char_u *p = ccline.cmdbuff;
2484
Bram Moolenaar438d1762018-09-30 17:11:48 +02002485 if (did_save_ccline)
2486 restore_cmdline(&save_ccline);
2487 else
2488 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002489 return p;
2490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491}
2492
2493#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2494/*
2495 * Get a command line with a prompt.
2496 * This is prepared to be called recursively from getcmdline() (e.g. by
2497 * f_input() when evaluating an expression from CTRL-R =).
2498 * Returns the command line in allocated memory, or NULL.
2499 */
2500 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002501getcmdline_prompt(
2502 int firstc,
2503 char_u *prompt, /* command line prompt */
2504 int attr, /* attributes for prompt */
2505 int xp_context, /* type of expansion */
2506 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 char_u *s;
2509 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002510 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002512 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
Bram Moolenaar438d1762018-09-30 17:11:48 +02002514 if (ccline.cmdbuff != NULL)
2515 {
2516 // Save the values of the current cmdline and restore them below.
2517 save_cmdline(&save_ccline);
2518 did_save_ccline = TRUE;
2519 }
2520
2521 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 ccline.cmdprompt = prompt;
2523 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002524# ifdef FEAT_EVAL
2525 ccline.xp_context = xp_context;
2526 ccline.xp_arg = xp_arg;
2527 ccline.input_fn = (firstc == '@');
2528# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002529 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002530 s = getcmdline_int(firstc, 1L, 0, FALSE);
2531
2532 if (did_save_ccline)
2533 restore_cmdline(&save_ccline);
2534
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002535 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002536 /* Restore msg_col, the prompt from input() may have changed it.
2537 * But only if called recursively and the commandline is therefore being
2538 * restored to an old one; if not, the input() prompt stays on the screen,
2539 * so we need its modified msg_col left intact. */
2540 if (ccline.cmdbuff != NULL)
2541 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 return s;
2544}
2545#endif
2546
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002547/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002548 * Return TRUE when the text must not be changed and we can't switch to
2549 * another window or buffer. Used when editing the command line, evaluating
2550 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002551 */
2552 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002553text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002554{
2555#ifdef FEAT_CMDWIN
2556 if (cmdwin_type != 0)
2557 return TRUE;
2558#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002559 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002560}
2561
2562/*
2563 * Give an error message for a command that isn't allowed while the cmdline
2564 * window is open or editing the cmdline in another way.
2565 */
2566 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002567text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002568{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002569 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002570}
2571
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002572 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002573get_text_locked_msg(void)
2574{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002575#ifdef FEAT_CMDWIN
2576 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002577 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002578#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002579 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002580}
2581
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002582/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002583 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2584 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002585 */
2586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002587curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002588{
2589 if (curbuf_lock > 0)
2590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002591 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002592 return TRUE;
2593 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002594 return allbuf_locked();
2595}
2596
2597/*
2598 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2599 * message.
2600 */
2601 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002603{
2604 if (allbuf_lock > 0)
2605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002606 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002607 return TRUE;
2608 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002609 return FALSE;
2610}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002611
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002613cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2616 if (cmdline_star > 0) /* showing '*', always 1 position */
2617 return 1;
2618#endif
2619 return ptr2cells(ccline.cmdbuff + idx);
2620}
2621
2622/*
2623 * Compute the offset of the cursor on the command line for the prompt and
2624 * indent.
2625 */
2626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002627set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002629 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 ccline.cmdspos = 1 + ccline.cmdindent;
2631 else
2632 ccline.cmdspos = 0 + ccline.cmdindent;
2633}
2634
2635/*
2636 * Compute the screen position for the cursor on the command line.
2637 */
2638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002639set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 int i, m, c;
2642
2643 set_cmdspos();
2644 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002647 if (m < 0) /* overflow, Columns or Rows at weird value */
2648 m = MAXCOL;
2649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 else
2651 m = MAXCOL;
2652 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2653 {
2654 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2656 if (has_mbyte)
2657 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002658 /* If the cmdline doesn't fit, show cursor on last visible char.
2659 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if ((ccline.cmdspos += c) >= m)
2661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ccline.cmdspos -= c;
2663 break;
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002666 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668}
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
2671 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2672 * character that doesn't fit, so that a ">" must be displayed.
2673 */
2674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002675correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002677 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2679 && ccline.cmdspos % Columns + cells > Columns)
2680 ccline.cmdspos++;
2681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
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);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002748 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002749 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 if (has_mbyte)
2812 {
2813 p = (char_u *)line_ga.ga_data;
2814 p[line_ga.ga_len] = NUL;
2815 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2816 line_ga.ga_len -= len;
2817 }
2818 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002819 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 goto redraw;
2821 }
2822 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002825 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 msg_col = startcol;
2828 msg_clr_eos();
2829 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002830 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 }
2832
2833 if (c1 == Ctrl_T)
2834 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002835 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002836 p = (char_u *)line_ga.ga_data;
2837 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002838 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002839 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002840add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002841 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002843 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002844 p = (char_u *)line_ga.ga_data;
2845 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2847 *s = ' ';
2848 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850redraw:
2851 /* redraw the line */
2852 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002854 p = (char_u *)line_ga.ga_data;
2855 p[line_ga.ga_len] = NUL;
2856 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002862 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002863 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002865 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002867 len = MB_PTR2LEN(p);
2868 msg_outtrans_len(p, len);
2869 vcol += ptr2cells(p);
2870 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002874 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875 continue;
2876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002878 if (c1 == Ctrl_D)
2879 {
2880 /* Delete one shiftwidth. */
2881 p = (char_u *)line_ga.ga_data;
2882 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002884 if (prev_char == '^')
2885 ex_keep_indent = TRUE;
2886 indent = 0;
2887 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889 else
2890 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002891 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002892 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002893 if (indent > 0)
2894 {
2895 --indent;
2896 indent -= indent % get_sw_value(curbuf);
2897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002899 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002901 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2903 --line_ga.ga_len;
2904 }
2905 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002907
2908 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2909 {
2910 escaped = TRUE;
2911 continue;
2912 }
2913
2914 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2915 if (IS_SPECIAL(c1))
2916 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002918
2919 if (IS_SPECIAL(c1))
2920 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002921 if (has_mbyte)
2922 len = (*mb_char2bytes)(c1,
2923 (char_u *)line_ga.ga_data + line_ga.ga_len);
2924 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002925 {
2926 len = 1;
2927 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2928 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002929 if (c1 == '\n')
2930 msg_putchar('\n');
2931 else if (c1 == TAB)
2932 {
2933 /* Don't use chartabsize(), 'ts' can be different */
2934 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002935 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002936 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002940 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002941 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002942 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002944 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002945 escaped = FALSE;
2946
2947 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002948 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002949
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002950 /* We are done when a NL is entered, but not when it comes after an
2951 * odd number of backslashes, that results in a NUL. */
2952 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002954 int bcount = 0;
2955
2956 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2957 ++bcount;
2958
2959 if (bcount > 0)
2960 {
2961 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2962 * "\NL", etc. */
2963 line_ga.ga_len -= (bcount + 1) / 2;
2964 pend -= (bcount + 1) / 2;
2965 pend[-1] = '\n';
2966 }
2967
2968 if ((bcount & 1) == 0)
2969 {
2970 --line_ga.ga_len;
2971 --pend;
2972 *pend = NUL;
2973 break;
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976 }
2977
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002978 --no_mapping;
2979 --allow_keys;
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 /* make following messages go to the next line */
2982 msg_didout = FALSE;
2983 msg_col = 0;
2984 if (msg_row < Rows - 1)
2985 ++msg_row;
2986 emsg_on_display = FALSE; /* don't want ui_delay() */
2987
2988 if (got_int)
2989 ga_clear(&line_ga);
2990
2991 return (char_u *)line_ga.ga_data;
2992}
2993
Bram Moolenaare344bea2005-09-01 20:46:49 +00002994# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2995 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996/*
2997 * Return TRUE if ccline.overstrike is on.
2998 */
2999 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003000cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 return ccline.overstrike;
3003}
3004
3005/*
3006 * Return TRUE if the cursor is at the end of the cmdline.
3007 */
3008 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003009cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 return (ccline.cmdpos >= ccline.cmdlen);
3012}
3013#endif
3014
Bram Moolenaar9372a112005-12-06 19:59:18 +00003015#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016/*
3017 * Return the virtual column number at the current cursor position.
3018 * This is used by the IM code to obtain the start of the preedit string.
3019 */
3020 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003021cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3024 return MAXCOL;
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (has_mbyte)
3027 {
3028 colnr_T col;
3029 int i = 0;
3030
3031 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003032 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034 return col;
3035 }
3036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 return ccline.cmdpos;
3038}
3039#endif
3040
3041#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3042/*
3043 * If part of the command line is an IM preedit string, redraw it with
3044 * IM feedback attributes. The cursor position is restored after drawing.
3045 */
3046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003047redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 if ((State & CMDLINE)
3050 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003051 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 && !p_imdisable
3053 && im_is_preediting())
3054 {
3055 int cmdpos = 0;
3056 int cmdspos;
3057 int old_row;
3058 int old_col;
3059 colnr_T col;
3060
3061 old_row = msg_row;
3062 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003063 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 if (has_mbyte)
3066 {
3067 for (col = 0; col < preedit_start_col
3068 && cmdpos < ccline.cmdlen; ++col)
3069 {
3070 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003071 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073 }
3074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 cmdspos += preedit_start_col;
3077 cmdpos += preedit_start_col;
3078 }
3079
3080 msg_row = cmdline_row + (cmdspos / (int)Columns);
3081 msg_col = cmdspos % (int)Columns;
3082 if (msg_row >= Rows)
3083 msg_row = Rows - 1;
3084
3085 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3086 {
3087 int char_len;
3088 int char_attr;
3089
3090 char_attr = im_get_feedback_attr(col);
3091 if (char_attr < 0)
3092 break; /* end of preedit string */
3093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003095 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 char_len = 1;
3098
3099 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3100 cmdpos += char_len;
3101 }
3102
3103 msg_row = old_row;
3104 msg_col = old_col;
3105 }
3106}
3107#endif /* FEAT_XIM && FEAT_GUI_GTK */
3108
3109/*
3110 * Allocate a new command line buffer.
3111 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 */
3113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003114alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 /*
3117 * give some extra space to avoid having to allocate all the time
3118 */
3119 if (len < 80)
3120 len = 100;
3121 else
3122 len += 20;
3123
3124 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3125 ccline.cmdbufflen = len;
3126}
3127
3128/*
3129 * Re-allocate the command line to length len + something extra.
3130 * return FAIL for failure, OK otherwise
3131 */
3132 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003133realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 char_u *p;
3136
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003137 if (len < ccline.cmdbufflen)
3138 return OK; /* no need to resize */
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 p = ccline.cmdbuff;
3141 alloc_cmdbuff(len); /* will get some more */
3142 if (ccline.cmdbuff == NULL) /* out of memory */
3143 {
3144 ccline.cmdbuff = p; /* keep the old one */
3145 return FAIL;
3146 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003147 /* There isn't always a NUL after the command, but it may need to be
3148 * there, thus copy up to the NUL and add a NUL. */
3149 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3150 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003152
3153 if (ccline.xpc != NULL
3154 && ccline.xpc->xp_pattern != NULL
3155 && ccline.xpc->xp_context != EXPAND_NOTHING
3156 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3157 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003158 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003159
3160 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3161 * to point into the newly allocated memory. */
3162 if (i >= 0 && i <= ccline.cmdlen)
3163 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3164 }
3165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 return OK;
3167}
3168
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003169#if defined(FEAT_ARABIC) || defined(PROTO)
3170static char_u *arshape_buf = NULL;
3171
3172# if defined(EXITFREE) || defined(PROTO)
3173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003174free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003175{
3176 vim_free(arshape_buf);
3177}
3178# endif
3179#endif
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181/*
3182 * Draw part of the cmdline at the current cursor position. But draw stars
3183 * when cmdline_star is TRUE.
3184 */
3185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003186draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3189 int i;
3190
3191 if (cmdline_star > 0)
3192 for (i = 0; i < len; ++i)
3193 {
3194 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003196 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
3198 else
3199#endif
3200#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003201 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 static int buflen = 0;
3204 char_u *p;
3205 int j;
3206 int newlen = 0;
3207 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003208 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 int prev_c = 0;
3210 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003211 int u8c;
3212 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 /*
3216 * Do arabic shaping into a temporary buffer. This is very
3217 * inefficient!
3218 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003219 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
3221 /* Re-allocate the buffer. We keep it around to avoid a lot of
3222 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003223 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003224 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003225 arshape_buf = alloc(buflen);
3226 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 return; /* out of memory */
3228 }
3229
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003230 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3231 {
3232 /* Prepend a space to draw the leading composing char on. */
3233 arshape_buf[0] = ' ';
3234 newlen = 1;
3235 }
3236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 for (j = start; j < start + len; j += mb_l)
3238 {
3239 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003240 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003241 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 if (ARABIC_CHAR(u8c))
3243 {
3244 /* Do Arabic shaping. */
3245 if (cmdmsg_rl)
3246 {
3247 /* displaying from right to left */
3248 pc = prev_c;
3249 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003250 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (j + mb_l >= start + len)
3252 nc = NUL;
3253 else
3254 nc = utf_ptr2char(p + mb_l);
3255 }
3256 else
3257 {
3258 /* displaying from left to right */
3259 if (j + mb_l >= start + len)
3260 pc = NUL;
3261 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003262 {
3263 int pcc[MAX_MCO];
3264
3265 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003267 pc1 = pcc[0];
3268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 nc = prev_c;
3270 }
3271 prev_c = u8c;
3272
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003273 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003276 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003278 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3279 if (u8cc[1] != 0)
3280 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003281 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283 }
3284 else
3285 {
3286 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 newlen += mb_l;
3289 }
3290 }
3291
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003292 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294 else
3295#endif
3296 msg_outtrans_len(ccline.cmdbuff + start, len);
3297}
3298
3299/*
3300 * Put a character on the command line. Shifts the following text to the
3301 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3302 * "c" must be printable (fit in one display cell)!
3303 */
3304 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003305putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 if (cmd_silent)
3308 return;
3309 msg_no_more = TRUE;
3310 msg_putchar(c);
3311 if (shift)
3312 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3313 msg_no_more = FALSE;
3314 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003315 extra_char = c;
3316 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317}
3318
3319/*
3320 * Undo a putcmdline(c, FALSE).
3321 */
3322 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003323unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 if (cmd_silent)
3326 return;
3327 msg_no_more = TRUE;
3328 if (ccline.cmdlen == ccline.cmdpos)
3329 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003330 else if (has_mbyte)
3331 draw_cmdline(ccline.cmdpos,
3332 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 else
3334 draw_cmdline(ccline.cmdpos, 1);
3335 msg_no_more = FALSE;
3336 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003337 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
3339
3340/*
3341 * Put the given string, of the given length, onto the command line.
3342 * If len is -1, then STRLEN() is used to calculate the length.
3343 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3344 * part will be redrawn, otherwise it will not. If this function is called
3345 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3346 * called afterwards.
3347 */
3348 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003349put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 int retval;
3352 int i;
3353 int m;
3354 int c;
3355
3356 if (len < 0)
3357 len = (int)STRLEN(str);
3358
3359 /* Check if ccline.cmdbuff needs to be longer */
3360 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003361 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 else
3363 retval = OK;
3364 if (retval == OK)
3365 {
3366 if (!ccline.overstrike)
3367 {
3368 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3369 ccline.cmdbuff + ccline.cmdpos,
3370 (size_t)(ccline.cmdlen - ccline.cmdpos));
3371 ccline.cmdlen += len;
3372 }
3373 else
3374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (has_mbyte)
3376 {
3377 /* Count nr of characters in the new string. */
3378 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003379 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 ++m;
3381 /* Count nr of bytes in cmdline that are overwritten by these
3382 * characters. */
3383 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003384 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 --m;
3386 if (i < ccline.cmdlen)
3387 {
3388 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3389 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3390 ccline.cmdlen += ccline.cmdpos + len - i;
3391 }
3392 else
3393 ccline.cmdlen = ccline.cmdpos + len;
3394 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003395 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 ccline.cmdlen = ccline.cmdpos + len;
3397 }
3398 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3399 ccline.cmdbuff[ccline.cmdlen] = NUL;
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (enc_utf8)
3402 {
3403 /* When the inserted text starts with a composing character,
3404 * backup to the character before it. There could be two of them.
3405 */
3406 i = 0;
3407 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3408 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3409 {
3410 i = (*mb_head_off)(ccline.cmdbuff,
3411 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3412 ccline.cmdpos -= i;
3413 len += i;
3414 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3415 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003416#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3418 {
3419 /* Check the previous character for Arabic combining pair. */
3420 i = (*mb_head_off)(ccline.cmdbuff,
3421 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3422 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3423 + ccline.cmdpos - i), c))
3424 {
3425 ccline.cmdpos -= i;
3426 len += i;
3427 }
3428 else
3429 i = 0;
3430 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (i != 0)
3433 {
3434 /* Also backup the cursor position. */
3435 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3436 ccline.cmdspos -= i;
3437 msg_col -= i;
3438 if (msg_col < 0)
3439 {
3440 msg_col += Columns;
3441 --msg_row;
3442 }
3443 }
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446 if (redraw && !cmd_silent)
3447 {
3448 msg_no_more = TRUE;
3449 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003450 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3452 /* Avoid clearing the rest of the line too often. */
3453 if (cmdline_row != i || ccline.overstrike)
3454 msg_clr_eos();
3455 msg_no_more = FALSE;
3456 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003457 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003459 m = Columns * Rows;
3460 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003462 }
3463 else
3464 m = MAXCOL;
3465 for (i = 0; i < len; ++i)
3466 {
3467 c = cmdline_charsize(ccline.cmdpos);
3468 /* count ">" for a double-wide char that doesn't fit. */
3469 if (has_mbyte)
3470 correct_cmdspos(ccline.cmdpos, c);
3471 /* Stop cursor at the end of the screen, but do increment the
3472 * insert position, so that entering a very long command
3473 * works, even though you can't see it. */
3474 if (ccline.cmdspos + c < m)
3475 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003476
Bram Moolenaar14184a32019-02-16 15:10:30 +01003477 if (has_mbyte)
3478 {
3479 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3480 if (c > len - i - 1)
3481 c = len - i - 1;
3482 ccline.cmdpos += c;
3483 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003485 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487 }
3488 if (redraw)
3489 msg_check();
3490 return retval;
3491}
3492
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003493static struct cmdline_info prev_ccline;
3494static int prev_ccline_used = FALSE;
3495
3496/*
3497 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3498 * and overwrite it. But get_cmdline_str() may need it, thus make it
3499 * available globally in prev_ccline.
3500 */
3501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003502save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003503{
3504 if (!prev_ccline_used)
3505 {
3506 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3507 prev_ccline_used = TRUE;
3508 }
3509 *ccp = prev_ccline;
3510 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003511 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003512}
3513
3514/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003515 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003516 */
3517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003518restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003519{
3520 ccline = prev_ccline;
3521 prev_ccline = *ccp;
3522}
3523
Bram Moolenaar8299df92004-07-10 09:47:34 +00003524/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003525 * Paste a yank register into the command line.
3526 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003527 * insert_reg() can't be used here, because special characters from the
3528 * register contents will be interpreted as commands.
3529 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003530 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531 */
3532 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003533cmdline_paste(
3534 int regname,
3535 int literally, /* Insert text literally instead of "as typed" */
3536 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003537{
3538 long i;
3539 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003540 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003542
3543 /* check for valid regname; also accept special characters for CTRL-R in
3544 * the command line */
3545 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003546 && regname != Ctrl_A && regname != Ctrl_L
3547 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003548 return FAIL;
3549
3550 /* A register containing CTRL-R can cause an endless loop. Allow using
3551 * CTRL-C to break the loop. */
3552 line_breakcheck();
3553 if (got_int)
3554 return FAIL;
3555
3556#ifdef FEAT_CLIPBOARD
3557 regname = may_get_selection(regname);
3558#endif
3559
Bram Moolenaar438d1762018-09-30 17:11:48 +02003560 // Need to set "textlock" to avoid nasty things like going to another
3561 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003562 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003563 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003564 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003565
3566 if (i)
3567 {
3568 /* Got the value of a special register in "arg". */
3569 if (arg == NULL)
3570 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003571
3572 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3573 * part of the word. */
3574 p = arg;
3575 if (p_is && regname == Ctrl_W)
3576 {
3577 char_u *w;
3578 int len;
3579
3580 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003581 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003582 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003583 if (has_mbyte)
3584 {
3585 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3586 if (!vim_iswordc(mb_ptr2char(w - len)))
3587 break;
3588 w -= len;
3589 }
3590 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003591 {
3592 if (!vim_iswordc(w[-1]))
3593 break;
3594 --w;
3595 }
3596 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003597 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003598 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3599 p += len;
3600 }
3601
3602 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603 if (allocated)
3604 vim_free(arg);
3605 return OK;
3606 }
3607
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003608 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003609}
3610
3611/*
3612 * Put a string on the command line.
3613 * When "literally" is TRUE, insert literally.
3614 * When "literally" is FALSE, insert as typed, but don't leave the command
3615 * line.
3616 */
3617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003618cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003619{
3620 int c, cv;
3621
3622 if (literally)
3623 put_on_cmdline(s, -1, TRUE);
3624 else
3625 while (*s != NUL)
3626 {
3627 cv = *s;
3628 if (cv == Ctrl_V && s[1])
3629 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003630 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003631 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003632 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003633 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003634 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3635 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003636#ifdef UNIX
3637 || c == intr_char
3638#endif
3639 || (c == Ctrl_BSL && *s == Ctrl_N))
3640 stuffcharReadbuff(Ctrl_V);
3641 stuffcharReadbuff(c);
3642 }
3643}
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645#ifdef FEAT_WILDMENU
3646/*
3647 * Delete characters on the command line, from "from" to the current
3648 * position.
3649 */
3650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003651cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3654 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3655 ccline.cmdlen -= ccline.cmdpos - from;
3656 ccline.cmdpos = from;
3657}
3658#endif
3659
3660/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003661 * This function is called when the screen size changes and with incremental
3662 * search and in other situations where the command line may have been
3663 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003666redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003668 redrawcmdline_ex(TRUE);
3669}
3670
3671 void
3672redrawcmdline_ex(int do_compute_cmdrow)
3673{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (cmd_silent)
3675 return;
3676 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003677 if (do_compute_cmdrow)
3678 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 redrawcmd();
3680 cursorcmd();
3681}
3682
3683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003684redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
3686 int i;
3687
3688 if (cmd_silent)
3689 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003690 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 msg_putchar(ccline.cmdfirstc);
3692 if (ccline.cmdprompt != NULL)
3693 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003694 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3696 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003697 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 --ccline.cmdindent;
3699 }
3700 else
3701 for (i = ccline.cmdindent; i > 0; --i)
3702 msg_putchar(' ');
3703}
3704
3705/*
3706 * Redraw what is currently on the command line.
3707 */
3708 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003709redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 if (cmd_silent)
3712 return;
3713
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003714 /* when 'incsearch' is set there may be no command line while redrawing */
3715 if (ccline.cmdbuff == NULL)
3716 {
3717 windgoto(cmdline_row, 0);
3718 msg_clr_eos();
3719 return;
3720 }
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 msg_start();
3723 redrawcmdprompt();
3724
3725 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3726 msg_no_more = TRUE;
3727 draw_cmdline(0, ccline.cmdlen);
3728 msg_clr_eos();
3729 msg_no_more = FALSE;
3730
3731 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003732 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003733 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 /*
3736 * An emsg() before may have set msg_scroll. This is used in normal mode,
3737 * in cmdline mode we can reset them now.
3738 */
3739 msg_scroll = FALSE; /* next message overwrites cmdline */
3740
3741 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3742 * in cmdline mode */
3743 skip_redraw = FALSE;
3744}
3745
3746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003747compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003749 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 cmdline_row = Rows - 1;
3751 else
3752 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003753 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754}
3755
3756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003757cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 if (cmd_silent)
3760 return;
3761
3762#ifdef FEAT_RIGHTLEFT
3763 if (cmdmsg_rl)
3764 {
3765 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3766 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3767 if (msg_row <= 0)
3768 msg_row = Rows - 1;
3769 }
3770 else
3771#endif
3772 {
3773 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3774 msg_col = ccline.cmdspos % (int)Columns;
3775 if (msg_row >= Rows)
3776 msg_row = Rows - 1;
3777 }
3778
3779 windgoto(msg_row, msg_col);
3780#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003781 if (p_imst == IM_ON_THE_SPOT)
3782 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783#endif
3784#ifdef MCH_CURSOR_SHAPE
3785 mch_update_cursor();
3786#endif
3787}
3788
3789 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003790gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
3792 msg_start();
3793#ifdef FEAT_RIGHTLEFT
3794 if (cmdmsg_rl)
3795 msg_col = Columns - 1;
3796 else
3797#endif
3798 msg_col = 0; /* always start in column 0 */
3799 if (clr) /* clear the bottom line(s) */
3800 msg_clr_eos(); /* will reset clear_cmdline */
3801 windgoto(cmdline_row, 0);
3802}
3803
3804/*
3805 * Check the word in front of the cursor for an abbreviation.
3806 * Called when the non-id character "c" has been entered.
3807 * When an abbreviation is recognized it is removed from the text with
3808 * backspaces and the replacement string is inserted, followed by "c".
3809 */
3810 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003811ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003813 int spos = 0;
3814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3816 return FALSE;
3817
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003818 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3819 * Actually accepts any mark. */
3820 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3821 spos++;
3822 if (ccline.cmdlen - spos > 5
3823 && ccline.cmdbuff[spos] == '\''
3824 && ccline.cmdbuff[spos + 2] == ','
3825 && ccline.cmdbuff[spos + 3] == '\'')
3826 spos += 5;
3827 else
3828 /* check abbreviation from the beginning of the commandline */
3829 spos = 0;
3830
3831 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832}
3833
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003834#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3835 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003836sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003837{
3838 char_u *p1 = *(char_u **)s1;
3839 char_u *p2 = *(char_u **)s2;
3840
3841 if (*p1 != '<' && *p2 == '<') return -1;
3842 if (*p1 == '<' && *p2 != '<') return 1;
3843 return STRCMP(p1, p2);
3844}
3845#endif
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847/*
3848 * Return FAIL if this is not an appropriate context in which to do
3849 * completion of anything, return OK if it is (even if there are no matches).
3850 * For the caller, this means that the character is just passed through like a
3851 * normal character (instead of being expanded). This allows :s/^I^D etc.
3852 */
3853 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003854nextwild(
3855 expand_T *xp,
3856 int type,
3857 int options, /* extra options for ExpandOne() */
3858 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 int i, j;
3861 char_u *p1;
3862 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 int difflen;
3864 int v;
3865
3866 if (xp->xp_numfiles == -1)
3867 {
3868 set_expand_context(xp);
3869 cmd_showtail = expand_showtail(xp);
3870 }
3871
3872 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3873 {
3874 beep_flush();
3875 return OK; /* Something illegal on command line */
3876 }
3877 if (xp->xp_context == EXPAND_NOTHING)
3878 {
3879 /* Caller can use the character as a normal char instead */
3880 return FAIL;
3881 }
3882
Bram Moolenaar32526b32019-01-19 17:43:09 +01003883 msg_puts("..."); /* show that we are busy */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 out_flush();
3885
3886 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003887 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
3889 if (type == WILD_NEXT || type == WILD_PREV)
3890 {
3891 /*
3892 * Get next/previous match for a previous expanded pattern.
3893 */
3894 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3895 }
3896 else
3897 {
3898 /*
3899 * Translate string into pattern and expand it.
3900 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003901 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3902 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 p2 = NULL;
3904 else
3905 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003906 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003907 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3908 if (escape)
3909 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003910
3911 if (p_wic)
3912 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003913 p2 = ExpandOne(xp, p1,
3914 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003915 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003917 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (p2 != NULL && type == WILD_LONGEST)
3919 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003920 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if (ccline.cmdbuff[i + j] == '*'
3922 || ccline.cmdbuff[i + j] == '?')
3923 break;
3924 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003925 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 }
3928 }
3929
3930 if (p2 != NULL && !got_int)
3931 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003932 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003933 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003935 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 xp->xp_pattern = ccline.cmdbuff + i;
3937 }
3938 else
3939 v = OK;
3940 if (v == OK)
3941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3943 &ccline.cmdbuff[ccline.cmdpos],
3944 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3945 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 ccline.cmdlen += difflen;
3947 ccline.cmdpos += difflen;
3948 }
3949 }
3950 vim_free(p2);
3951
3952 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003953 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
3955 /* When expanding a ":map" command and no matches are found, assume that
3956 * the key is supposed to be inserted literally */
3957 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3958 return FAIL;
3959
3960 if (xp->xp_numfiles <= 0 && p2 == NULL)
3961 beep_flush();
3962 else if (xp->xp_numfiles == 1)
3963 /* free expanded pattern */
3964 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3965
3966 return OK;
3967}
3968
3969/*
3970 * Do wildcard expansion on the string 'str'.
3971 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003972 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 * Return NULL for failure.
3974 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003975 * "orig" is the originally expanded string, copied to allocated memory. It
3976 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3977 * WILD_PREV "orig" should be NULL.
3978 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003979 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3980 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 *
3982 * mode = WILD_FREE: just free previously expanded matches
3983 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3984 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3985 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3986 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3987 * mode = WILD_ALL: return all matches concatenated
3988 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003989 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 *
3991 * options = WILD_LIST_NOTFOUND: list entries without a match
3992 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3993 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3994 * options = WILD_NO_BEEP: Don't beep for multiple matches
3995 * options = WILD_ADD_SLASH: add a slash after directory names
3996 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3997 * options = WILD_SILENT: don't print warning messages
3998 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003999 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 *
4001 * The variables xp->xp_context and xp->xp_backslash must have been set!
4002 */
4003 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004004ExpandOne(
4005 expand_T *xp,
4006 char_u *str,
4007 char_u *orig, /* allocated copy of original of expanded string */
4008 int options,
4009 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
4011 char_u *ss = NULL;
4012 static int findex;
4013 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004014 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 int i;
4016 long_u len;
4017 int non_suf_match; /* number without matching suffix */
4018
4019 /*
4020 * first handle the case of using an old match
4021 */
4022 if (mode == WILD_NEXT || mode == WILD_PREV)
4023 {
4024 if (xp->xp_numfiles > 0)
4025 {
4026 if (mode == WILD_PREV)
4027 {
4028 if (findex == -1)
4029 findex = xp->xp_numfiles;
4030 --findex;
4031 }
4032 else /* mode == WILD_NEXT */
4033 ++findex;
4034
4035 /*
4036 * When wrapping around, return the original string, set findex to
4037 * -1.
4038 */
4039 if (findex < 0)
4040 {
4041 if (orig_save == NULL)
4042 findex = xp->xp_numfiles - 1;
4043 else
4044 findex = -1;
4045 }
4046 if (findex >= xp->xp_numfiles)
4047 {
4048 if (orig_save == NULL)
4049 findex = 0;
4050 else
4051 findex = -1;
4052 }
4053#ifdef FEAT_WILDMENU
4054 if (p_wmnu)
4055 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4056 findex, cmd_showtail);
4057#endif
4058 if (findex == -1)
4059 return vim_strsave(orig_save);
4060 return vim_strsave(xp->xp_files[findex]);
4061 }
4062 else
4063 return NULL;
4064 }
4065
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004066 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4068 {
4069 FreeWild(xp->xp_numfiles, xp->xp_files);
4070 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004071 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
4073 findex = 0;
4074
4075 if (mode == WILD_FREE) /* only release file name */
4076 return NULL;
4077
4078 if (xp->xp_numfiles == -1)
4079 {
4080 vim_free(orig_save);
4081 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004082 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Do the expansion.
4086 */
4087 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4088 options) == FAIL)
4089 {
4090#ifdef FNAME_ILLEGAL
4091 /* Illegal file name has been silently skipped. But when there
4092 * are wildcards, the real problem is that there was no match,
4093 * causing the pattern to be added, which has illegal characters.
4094 */
4095 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004096 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097#endif
4098 }
4099 else if (xp->xp_numfiles == 0)
4100 {
4101 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004102 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 else
4105 {
4106 /* Escape the matches for use on the command line. */
4107 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4108
4109 /*
4110 * Check for matching suffixes in file names.
4111 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004112 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4113 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
4115 if (xp->xp_numfiles)
4116 non_suf_match = xp->xp_numfiles;
4117 else
4118 non_suf_match = 1;
4119 if ((xp->xp_context == EXPAND_FILES
4120 || xp->xp_context == EXPAND_DIRECTORIES)
4121 && xp->xp_numfiles > 1)
4122 {
4123 /*
4124 * More than one match; check suffix.
4125 * The files will have been sorted on matching suffix in
4126 * expand_wildcards, only need to check the first two.
4127 */
4128 non_suf_match = 0;
4129 for (i = 0; i < 2; ++i)
4130 if (match_suffix(xp->xp_files[i]))
4131 ++non_suf_match;
4132 }
4133 if (non_suf_match != 1)
4134 {
4135 /* Can we ever get here unless it's while expanding
4136 * interactively? If not, we can get rid of this all
4137 * together. Don't really want to wait for this message
4138 * (and possibly have to hit return to continue!).
4139 */
4140 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004141 emsg(_(e_toomany));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 else if (!(options & WILD_NO_BEEP))
4143 beep_flush();
4144 }
4145 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4146 ss = vim_strsave(xp->xp_files[0]);
4147 }
4148 }
4149 }
4150
4151 /* Find longest common part */
4152 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4153 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004154 int mb_len = 1;
4155 int c0, ci;
4156
4157 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004159 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004161 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4162 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4163 }
4164 else
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004165 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004166 for (i = 1; i < xp->xp_numfiles; ++i)
4167 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004168 if (has_mbyte)
4169 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4170 else
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004171 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004172 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004174 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004175 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004177 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 break;
4179 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004180 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 break;
4182 }
4183 if (i < xp->xp_numfiles)
4184 {
4185 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004186 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 break;
4188 }
4189 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004190
Bram Moolenaar964b3742019-05-24 18:54:09 +02004191 ss = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004193 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 findex = -1; /* next p_wc gets first one */
4195 }
4196
4197 /* Concatenate all matching names */
4198 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4199 {
4200 len = 0;
4201 for (i = 0; i < xp->xp_numfiles; ++i)
4202 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004203 ss = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (ss != NULL)
4205 {
4206 *ss = NUL;
4207 for (i = 0; i < xp->xp_numfiles; ++i)
4208 {
4209 STRCAT(ss, xp->xp_files[i]);
4210 if (i != xp->xp_numfiles - 1)
4211 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4212 }
4213 }
4214 }
4215
4216 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4217 ExpandCleanup(xp);
4218
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004219 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004220 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004221 vim_free(orig);
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return ss;
4224}
4225
4226/*
4227 * Prepare an expand structure for use.
4228 */
4229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004230ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004232 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004233 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004235#ifndef BACKSLASH_IN_FILENAME
4236 xp->xp_shell = FALSE;
4237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 xp->xp_numfiles = -1;
4239 xp->xp_files = NULL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004240#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004241 xp->xp_arg = NULL;
4242#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004243 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244}
4245
4246/*
4247 * Cleanup an expand structure after use.
4248 */
4249 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004250ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 if (xp->xp_numfiles >= 0)
4253 {
4254 FreeWild(xp->xp_numfiles, xp->xp_files);
4255 xp->xp_numfiles = -1;
4256 }
4257}
4258
4259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004260ExpandEscape(
4261 expand_T *xp,
4262 char_u *str,
4263 int numfiles,
4264 char_u **files,
4265 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
4267 int i;
4268 char_u *p;
4269
4270 /*
4271 * May change home directory back to "~"
4272 */
4273 if (options & WILD_HOME_REPLACE)
4274 tilde_replace(str, numfiles, files);
4275
4276 if (options & WILD_ESCAPE)
4277 {
4278 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004279 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004280 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 || xp->xp_context == EXPAND_BUFFERS
4282 || xp->xp_context == EXPAND_DIRECTORIES)
4283 {
4284 /*
4285 * Insert a backslash into a file name before a space, \, %, #
4286 * and wildmatch characters, except '~'.
4287 */
4288 for (i = 0; i < numfiles; ++i)
4289 {
4290 /* for ":set path=" we need to escape spaces twice */
4291 if (xp->xp_backslash == XP_BS_THREE)
4292 {
4293 p = vim_strsave_escaped(files[i], (char_u *)" ");
4294 if (p != NULL)
4295 {
4296 vim_free(files[i]);
4297 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004298#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 p = vim_strsave_escaped(files[i], (char_u *)" ");
4300 if (p != NULL)
4301 {
4302 vim_free(files[i]);
4303 files[i] = p;
4304 }
4305#endif
4306 }
4307 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004308#ifdef BACKSLASH_IN_FILENAME
4309 p = vim_strsave_fnameescape(files[i], FALSE);
4310#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004311 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (p != NULL)
4314 {
4315 vim_free(files[i]);
4316 files[i] = p;
4317 }
4318
4319 /* If 'str' starts with "\~", replace "~" at start of
4320 * files[i] with "\~". */
4321 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004322 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004325
4326 /* If the first file starts with a '+' escape it. Otherwise it
4327 * could be seen as "+cmd". */
4328 if (*files[0] == '+')
4329 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 else if (xp->xp_context == EXPAND_TAGS)
4332 {
4333 /*
4334 * Insert a backslash before characters in a tag name that
4335 * would terminate the ":tag" command.
4336 */
4337 for (i = 0; i < numfiles; ++i)
4338 {
4339 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4340 if (p != NULL)
4341 {
4342 vim_free(files[i]);
4343 files[i] = p;
4344 }
4345 }
4346 }
4347 }
4348}
4349
4350/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004351 * Escape special characters in "fname" for when used as a file name argument
4352 * after a Vim command, or, when "shell" is non-zero, a shell command.
4353 * Returns the result in allocated memory.
4354 */
4355 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004356vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004357{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004358 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004359#ifdef BACKSLASH_IN_FILENAME
4360 char_u buf[20];
4361 int j = 0;
4362
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004363 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004364 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004365 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004366 buf[j++] = *p;
4367 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004368 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004369#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004370 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4371 if (shell && csh_like_shell() && p != NULL)
4372 {
4373 char_u *s;
4374
4375 /* For csh and similar shells need to put two backslashes before '!'.
4376 * One is taken by Vim, one by the shell. */
4377 s = vim_strsave_escaped(p, (char_u *)"!");
4378 vim_free(p);
4379 p = s;
4380 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004381#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004382
4383 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4384 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004385 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004386 escape_fname(&p);
4387
4388 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004389}
4390
4391/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004392 * Put a backslash before the file name in "pp", which is in allocated memory.
4393 */
4394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004395escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004396{
4397 char_u *p;
4398
Bram Moolenaar964b3742019-05-24 18:54:09 +02004399 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00004400 if (p != NULL)
4401 {
4402 p[0] = '\\';
4403 STRCPY(p + 1, *pp);
4404 vim_free(*pp);
4405 *pp = p;
4406 }
4407}
4408
4409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 * For each file name in files[num_files]:
4411 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4412 */
4413 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004414tilde_replace(
4415 char_u *orig_pat,
4416 int num_files,
4417 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418{
4419 int i;
4420 char_u *p;
4421
4422 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4423 {
4424 for (i = 0; i < num_files; ++i)
4425 {
4426 p = home_replace_save(NULL, files[i]);
4427 if (p != NULL)
4428 {
4429 vim_free(files[i]);
4430 files[i] = p;
4431 }
4432 }
4433 }
4434}
4435
4436/*
4437 * Show all matches for completion on the command line.
4438 * Returns EXPAND_NOTHING when the character that triggered expansion should
4439 * be inserted like a normal character.
4440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004442showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4445 int num_files;
4446 char_u **files_found;
4447 int i, j, k;
4448 int maxlen;
4449 int lines;
4450 int columns;
4451 char_u *p;
4452 int lastlen;
4453 int attr;
4454 int showtail;
4455
4456 if (xp->xp_numfiles == -1)
4457 {
4458 set_expand_context(xp);
4459 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4460 &num_files, &files_found);
4461 showtail = expand_showtail(xp);
4462 if (i != EXPAND_OK)
4463 return i;
4464
4465 }
4466 else
4467 {
4468 num_files = xp->xp_numfiles;
4469 files_found = xp->xp_files;
4470 showtail = cmd_showtail;
4471 }
4472
4473#ifdef FEAT_WILDMENU
4474 if (!wildmenu)
4475 {
4476#endif
4477 msg_didany = FALSE; /* lines_left will be set */
4478 msg_start(); /* prepare for paging */
4479 msg_putchar('\n');
4480 out_flush();
4481 cmdline_row = msg_row;
4482 msg_didany = FALSE; /* lines_left will be set again */
4483 msg_start(); /* prepare for paging */
4484#ifdef FEAT_WILDMENU
4485 }
4486#endif
4487
4488 if (got_int)
4489 got_int = FALSE; /* only int. the completion, not the cmd line */
4490#ifdef FEAT_WILDMENU
4491 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004492 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493#endif
4494 else
4495 {
4496 /* find the length of the longest file name */
4497 maxlen = 0;
4498 for (i = 0; i < num_files; ++i)
4499 {
4500 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004501 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 || xp->xp_context == EXPAND_BUFFERS))
4503 {
4504 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4505 j = vim_strsize(NameBuff);
4506 }
4507 else
4508 j = vim_strsize(L_SHOWFILE(i));
4509 if (j > maxlen)
4510 maxlen = j;
4511 }
4512
4513 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4514 lines = num_files;
4515 else
4516 {
4517 /* compute the number of columns and lines for the listing */
4518 maxlen += 2; /* two spaces between file names */
4519 columns = ((int)Columns + 2) / maxlen;
4520 if (columns < 1)
4521 columns = 1;
4522 lines = (num_files + columns - 1) / columns;
4523 }
4524
Bram Moolenaar8820b482017-03-16 17:23:31 +01004525 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4528 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004529 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 msg_clr_eos();
4531 msg_advance(maxlen - 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004532 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534
4535 /* list the files line by line */
4536 for (i = 0; i < lines; ++i)
4537 {
4538 lastlen = 999;
4539 for (k = i; k < num_files; k += lines)
4540 {
4541 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4542 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004543 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 p = files_found[k] + STRLEN(files_found[k]) + 1;
4545 msg_advance(maxlen + 1);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004546 msg_puts((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 msg_advance(maxlen + 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004548 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 break;
4550 }
4551 for (j = maxlen - lastlen; --j >= 0; )
4552 msg_putchar(' ');
4553 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004554 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 || xp->xp_context == EXPAND_BUFFERS)
4556 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004557 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004558 if (xp->xp_numfiles != -1)
4559 {
4560 char_u *halved_slash;
4561 char_u *exp_path;
4562
4563 /* Expansion was done before and special characters
4564 * were escaped, need to halve backslashes. Also
4565 * $HOME has been replaced with ~/. */
4566 exp_path = expand_env_save_opt(files_found[k], TRUE);
4567 halved_slash = backslash_halve_save(
4568 exp_path != NULL ? exp_path : files_found[k]);
4569 j = mch_isdir(halved_slash != NULL ? halved_slash
4570 : files_found[k]);
4571 vim_free(exp_path);
4572 vim_free(halved_slash);
4573 }
4574 else
4575 /* Expansion was done here, file names are literal. */
4576 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (showtail)
4578 p = L_SHOWFILE(k);
4579 else
4580 {
4581 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4582 TRUE);
4583 p = NameBuff;
4584 }
4585 }
4586 else
4587 {
4588 j = FALSE;
4589 p = L_SHOWFILE(k);
4590 }
4591 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4592 }
4593 if (msg_col > 0) /* when not wrapped around */
4594 {
4595 msg_clr_eos();
4596 msg_putchar('\n');
4597 }
4598 out_flush(); /* show one line at a time */
4599 if (got_int)
4600 {
4601 got_int = FALSE;
4602 break;
4603 }
4604 }
4605
4606 /*
4607 * we redraw the command below the lines that we have just listed
4608 * This is a bit tricky, but it saves a lot of screen updating.
4609 */
4610 cmdline_row = msg_row; /* will put it back later */
4611 }
4612
4613 if (xp->xp_numfiles == -1)
4614 FreeWild(num_files, files_found);
4615
4616 return EXPAND_OK;
4617}
4618
4619/*
4620 * Private gettail for showmatches() (and win_redr_status_matches()):
4621 * Find tail of file name path, but ignore trailing "/".
4622 */
4623 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004624sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
4626 char_u *p;
4627 char_u *t = s;
4628 int had_sep = FALSE;
4629
4630 for (p = s; *p != NUL; )
4631 {
4632 if (vim_ispathsep(*p)
4633#ifdef BACKSLASH_IN_FILENAME
4634 && !rem_backslash(p)
4635#endif
4636 )
4637 had_sep = TRUE;
4638 else if (had_sep)
4639 {
4640 t = p;
4641 had_sep = FALSE;
4642 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004643 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 return t;
4646}
4647
4648/*
4649 * Return TRUE if we only need to show the tail of completion matches.
4650 * When not completing file names or there is a wildcard in the path FALSE is
4651 * returned.
4652 */
4653 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004654expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
4656 char_u *s;
4657 char_u *end;
4658
4659 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004660 if (xp->xp_context != EXPAND_FILES
4661 && xp->xp_context != EXPAND_SHELLCMD
4662 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 return FALSE;
4664
4665 end = gettail(xp->xp_pattern);
4666 if (end == xp->xp_pattern) /* there is no path separator */
4667 return FALSE;
4668
4669 for (s = xp->xp_pattern; s < end; s++)
4670 {
4671 /* Skip escaped wildcards. Only when the backslash is not a path
4672 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4673 if (rem_backslash(s))
4674 ++s;
4675 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4676 return FALSE;
4677 }
4678 return TRUE;
4679}
4680
4681/*
4682 * Prepare a string for expansion.
4683 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004684 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 * When expanding other names: The string will be used with regcomp(). Copy
4686 * the name into allocated memory and prepend "^".
4687 */
4688 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004689addstar(
4690 char_u *fname,
4691 int len,
4692 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 char_u *retval;
4695 int i, j;
4696 int new_len;
4697 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004698 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004700 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004701 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004702 && context != EXPAND_SHELLCMD
4703 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
4705 /*
4706 * Matching will be done internally (on something other than files).
4707 * So we convert the file-matching-type wildcards into our kind for
4708 * use with vim_regcomp(). First work out how long it will be:
4709 */
4710
4711 /* For help tags the translation is done in find_help_tags().
4712 * For a tag pattern starting with "/" no translation is needed. */
4713 if (context == EXPAND_HELP
4714 || context == EXPAND_COLORS
4715 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004716 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004717 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004718 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004719 || ((context == EXPAND_TAGS_LISTFILES
4720 || context == EXPAND_TAGS)
4721 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 retval = vim_strnsave(fname, len);
4723 else
4724 {
4725 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4726 for (i = 0; i < len; i++)
4727 {
4728 if (fname[i] == '*' || fname[i] == '~')
4729 new_len++; /* '*' needs to be replaced by ".*"
4730 '~' needs to be replaced by "\~" */
4731
4732 /* Buffer names are like file names. "." should be literal */
4733 if (context == EXPAND_BUFFERS && fname[i] == '.')
4734 new_len++; /* "." becomes "\." */
4735
4736 /* Custom expansion takes care of special things, match
4737 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004738 if ((context == EXPAND_USER_DEFINED
4739 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 new_len++; /* '\' becomes "\\" */
4741 }
4742 retval = alloc(new_len);
4743 if (retval != NULL)
4744 {
4745 retval[0] = '^';
4746 j = 1;
4747 for (i = 0; i < len; i++, j++)
4748 {
4749 /* Skip backslash. But why? At least keep it for custom
4750 * expansion. */
4751 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004752 && context != EXPAND_USER_LIST
4753 && fname[i] == '\\'
4754 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 break;
4756
4757 switch (fname[i])
4758 {
4759 case '*': retval[j++] = '.';
4760 break;
4761 case '~': retval[j++] = '\\';
4762 break;
4763 case '?': retval[j] = '.';
4764 continue;
4765 case '.': if (context == EXPAND_BUFFERS)
4766 retval[j++] = '\\';
4767 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004768 case '\\': if (context == EXPAND_USER_DEFINED
4769 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 retval[j++] = '\\';
4771 break;
4772 }
4773 retval[j] = fname[i];
4774 }
4775 retval[j] = NUL;
4776 }
4777 }
4778 }
4779 else
4780 {
4781 retval = alloc(len + 4);
4782 if (retval != NULL)
4783 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004784 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004787 * Don't add a star to *, ~, ~user, $var or `cmd`.
4788 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 * ~ would be at the start of the file name, but not the tail.
4790 * $ could be anywhere in the tail.
4791 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004792 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 */
4794 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004795 ends_in_star = (len > 0 && retval[len - 1] == '*');
4796#ifndef BACKSLASH_IN_FILENAME
4797 for (i = len - 2; i >= 0; --i)
4798 {
4799 if (retval[i] != '\\')
4800 break;
4801 ends_in_star = !ends_in_star;
4802 }
4803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004805 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 && vim_strchr(tail, '$') == NULL
4807 && vim_strchr(retval, '`') == NULL)
4808 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004809 else if (len > 0 && retval[len - 1] == '$')
4810 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 retval[len] = NUL;
4812 }
4813 }
4814 return retval;
4815}
4816
4817/*
4818 * Must parse the command line so far to work out what context we are in.
4819 * Completion can then be done based on that context.
4820 * This routine sets the variables:
4821 * xp->xp_pattern The start of the pattern to be expanded within
4822 * the command line (ends at the cursor).
4823 * xp->xp_context The type of thing to expand. Will be one of:
4824 *
4825 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4826 * the command line, like an unknown command. Caller
4827 * should beep.
4828 * EXPAND_NOTHING Unrecognised context for completion, use char like
4829 * a normal char, rather than for completion. eg
4830 * :s/^I/
4831 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4832 * it.
4833 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4834 * EXPAND_FILES After command with XFILE set, or after setting
4835 * with P_EXPAND set. eg :e ^I, :w>>^I
4836 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4837 * when we know only directories are of interest. eg
4838 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004839 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4841 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4842 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4843 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4844 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4845 * EXPAND_EVENTS Complete event names
4846 * EXPAND_SYNTAX Complete :syntax command arguments
4847 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4848 * EXPAND_AUGROUP Complete autocommand group names
4849 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4850 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4851 * eg :unmap a^I , :cunab x^I
4852 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4853 * eg :call sub^I
4854 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4855 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4856 * names in expressions, eg :while s^I
4857 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004858 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 */
4860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004861set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004863 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 if (ccline.cmdfirstc != ':'
4865#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004866 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004867 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#endif
4869 )
4870 {
4871 xp->xp_context = EXPAND_NOTHING;
4872 return;
4873 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004874 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875}
4876
4877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004878set_cmd_context(
4879 expand_T *xp,
4880 char_u *str, /* start of command line */
4881 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004882 int col, /* position of cursor */
4883 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884{
4885 int old_char = NUL;
4886 char_u *nextcomm;
4887
4888 /*
4889 * Avoid a UMR warning from Purify, only save the character if it has been
4890 * written before.
4891 */
4892 if (col < len)
4893 old_char = str[col];
4894 str[col] = NUL;
4895 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004896
4897#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004898 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004899 {
4900# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004901 /* pass CMD_SIZE because there is no real command */
4902 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004903# endif
4904 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004905 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004906 {
4907 xp->xp_context = ccline.xp_context;
4908 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004909# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004910 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004911# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004912 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004913 else
4914#endif
4915 while (nextcomm != NULL)
4916 nextcomm = set_one_cmd_context(xp, nextcomm);
4917
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004918 /* Store the string here so that call_user_expand_func() can get to them
4919 * easily. */
4920 xp->xp_line = str;
4921 xp->xp_col = col;
4922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 str[col] = old_char;
4924}
4925
4926/*
4927 * Expand the command line "str" from context "xp".
4928 * "xp" must have been set by set_cmd_context().
4929 * xp->xp_pattern points into "str", to where the text that is to be expanded
4930 * starts.
4931 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4932 * cursor.
4933 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4934 * key that triggered expansion literally.
4935 * Returns EXPAND_OK otherwise.
4936 */
4937 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004938expand_cmdline(
4939 expand_T *xp,
4940 char_u *str, /* start of command line */
4941 int col, /* position of cursor */
4942 int *matchcount, /* return: nr of matches */
4943 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004946 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4949 {
4950 beep_flush();
4951 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4952 }
4953 if (xp->xp_context == EXPAND_NOTHING)
4954 {
4955 /* Caller can use the character as a normal char instead */
4956 return EXPAND_NOTHING;
4957 }
4958
4959 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004960 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4961 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (file_str == NULL)
4963 return EXPAND_UNSUCCESSFUL;
4964
Bram Moolenaar94950a92010-12-02 16:01:29 +01004965 if (p_wic)
4966 options += WILD_ICASE;
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004969 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 *matchcount = 0;
4972 *matches = NULL;
4973 }
4974 vim_free(file_str);
4975
4976 return EXPAND_OK;
4977}
4978
4979#ifdef FEAT_MULTI_LANG
4980/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004981 * Cleanup matches for help tags:
4982 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4983 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004986cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987{
4988 int i, j;
4989 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004990 char_u buf[4];
4991 char_u *p = buf;
4992
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004993 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004994 {
4995 *p++ = '@';
4996 *p++ = p_hlg[0];
4997 *p++ = p_hlg[1];
4998 }
4999 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 for (i = 0; i < num_file; ++i)
5002 {
5003 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005004 if (len <= 0)
5005 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005006 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
5008 /* Sorting on priority means the same item in another language may
5009 * be anywhere. Search all items for a match up to the "@en". */
5010 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005011 if (j != i && (int)STRLEN(file[j]) == len + 3
5012 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 break;
5014 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005015 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 file[i][len] = NUL;
5017 }
5018 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005019
5020 if (*buf != NUL)
5021 for (i = 0; i < num_file; ++i)
5022 {
5023 len = (int)STRLEN(file[i]) - 3;
5024 if (len <= 0)
5025 continue;
5026 if (STRCMP(file[i] + len, buf) == 0)
5027 {
5028 /* remove the default language */
5029 file[i][len] = NUL;
5030 }
5031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032}
5033#endif
5034
5035/*
5036 * Do the expansion based on xp->xp_context and "pat".
5037 */
5038 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005039ExpandFromContext(
5040 expand_T *xp,
5041 char_u *pat,
5042 int *num_file,
5043 char_u ***file,
5044 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
5046#ifdef FEAT_CMDL_COMPL
5047 regmatch_T regmatch;
5048#endif
5049 int ret;
5050 int flags;
5051
5052 flags = EW_DIR; /* include directories */
5053 if (options & WILD_LIST_NOTFOUND)
5054 flags |= EW_NOTFOUND;
5055 if (options & WILD_ADD_SLASH)
5056 flags |= EW_ADDSLASH;
5057 if (options & WILD_KEEP_ALL)
5058 flags |= EW_KEEPALL;
5059 if (options & WILD_SILENT)
5060 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005061 if (options & WILD_ALLLINKS)
5062 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005064 if (xp->xp_context == EXPAND_FILES
5065 || xp->xp_context == EXPAND_DIRECTORIES
5066 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
5068 /*
5069 * Expand file or directory names.
5070 */
5071 int free_pat = FALSE;
5072 int i;
5073
5074 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5075 * space */
5076 if (xp->xp_backslash != XP_BS_NONE)
5077 {
5078 free_pat = TRUE;
5079 pat = vim_strsave(pat);
5080 for (i = 0; pat[i]; ++i)
5081 if (pat[i] == '\\')
5082 {
5083 if (xp->xp_backslash == XP_BS_THREE
5084 && pat[i + 1] == '\\'
5085 && pat[i + 2] == '\\'
5086 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005087 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (xp->xp_backslash == XP_BS_ONE
5089 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005090 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 }
5093
5094 if (xp->xp_context == EXPAND_FILES)
5095 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005096 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5097 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 else
5099 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005100 if (options & WILD_ICASE)
5101 flags |= EW_ICASE;
5102
Bram Moolenaard7834d32009-12-02 16:14:36 +00005103 /* Expand wildcards, supporting %:h and the like. */
5104 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (free_pat)
5106 vim_free(pat);
5107 return ret;
5108 }
5109
5110 *file = (char_u **)"";
5111 *num_file = 0;
5112 if (xp->xp_context == EXPAND_HELP)
5113 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005114 /* With an empty argument we would get all the help tags, which is
5115 * very slow. Get matches for "help" instead. */
5116 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5117 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119#ifdef FEAT_MULTI_LANG
5120 cleanup_help_tags(*num_file, *file);
5121#endif
5122 return OK;
5123 }
5124 return FAIL;
5125 }
5126
5127#ifndef FEAT_CMDL_COMPL
5128 return FAIL;
5129#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005130 if (xp->xp_context == EXPAND_SHELLCMD)
5131 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (xp->xp_context == EXPAND_OLD_SETTING)
5133 return ExpandOldSetting(num_file, file);
5134 if (xp->xp_context == EXPAND_BUFFERS)
5135 return ExpandBufnames(pat, num_file, file, options);
5136 if (xp->xp_context == EXPAND_TAGS
5137 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5138 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5139 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005140 {
5141 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005142 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5143 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005146 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005147 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005148 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005149 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005150 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005151 {
5152 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005153 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005154 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005155 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005156 {
5157 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005158 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005159 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005160# if defined(FEAT_EVAL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005161 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005162 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005163# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005164 if (xp->xp_context == EXPAND_PACKADD)
5165 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
5167 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5168 if (regmatch.regprog == NULL)
5169 return FAIL;
5170
5171 /* set ignore-case according to p_ic, p_scs and pat */
5172 regmatch.rm_ic = ignorecase(pat);
5173
5174 if (xp->xp_context == EXPAND_SETTINGS
5175 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5176 ret = ExpandSettings(xp, &regmatch, num_file, file);
5177 else if (xp->xp_context == EXPAND_MAPPINGS)
5178 ret = ExpandMappings(&regmatch, num_file, file);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005179# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 else if (xp->xp_context == EXPAND_USER_DEFINED)
5181 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5182# endif
5183 else
5184 {
5185 static struct expgen
5186 {
5187 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005188 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005190 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 } tab[] =
5192 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005193 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5194 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005195 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005196 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005197#ifdef FEAT_CMDHIST
5198 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5199#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005200 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005201 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005202 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5203 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5204 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005206 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5207 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5208 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5209 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#endif
5211#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005212 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5213 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
5215#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005218#ifdef FEAT_PROFILE
5219 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5220#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005221 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5223 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005224#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005225 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005226#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005227#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005228 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005229#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005230#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005231 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005232#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005233#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005234 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5235 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005237 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005238 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005239 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 };
5241 int i;
5242
5243 /*
5244 * Find a context in the table and call the ExpandGeneric() with the
5245 * right function to do the expansion.
5246 */
5247 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005248 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (xp->xp_context == tab[i].context)
5250 {
5251 if (tab[i].ic)
5252 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005253 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005254 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 break;
5256 }
5257 }
5258
Bram Moolenaar473de612013-06-08 18:19:48 +02005259 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 return ret;
5262#endif /* FEAT_CMDL_COMPL */
5263}
5264
5265#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5266/*
5267 * Expand a list of names.
5268 *
5269 * Generic function for command line completion. It calls a function to
5270 * obtain strings, one by one. The strings are matched against a regexp
5271 * program. Matching strings are copied into an array, which is returned.
5272 *
5273 * Returns OK when no problems encountered, FAIL for error (out of memory).
5274 */
5275 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276ExpandGeneric(
5277 expand_T *xp,
5278 regmatch_T *regmatch,
5279 int *num_file,
5280 char_u ***file,
5281 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005283 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
5285 int i;
5286 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005287 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 char_u *str;
5289
5290 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005291 * round == 0: count the number of matching names
5292 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005294 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
5296 for (i = 0; ; ++i)
5297 {
5298 str = (*func)(xp, i);
5299 if (str == NULL) /* end of list */
5300 break;
5301 if (*str == NUL) /* skip empty strings */
5302 continue;
5303
5304 if (vim_regexec(regmatch, str, (colnr_T)0))
5305 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005306 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005308 if (escaped)
5309 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5310 else
5311 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 (*file)[count] = str;
5313#ifdef FEAT_MENU
5314 if (func == get_menu_names && str != NULL)
5315 {
5316 /* test for separator added by get_menu_names() */
5317 str += STRLEN(str) - 1;
5318 if (*str == '\001')
5319 *str = '.';
5320 }
5321#endif
5322 }
5323 ++count;
5324 }
5325 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005326 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
5328 if (count == 0)
5329 return OK;
5330 *num_file = count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005331 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (*file == NULL)
5333 {
5334 *file = (char_u **)"";
5335 return FAIL;
5336 }
5337 count = 0;
5338 }
5339 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005340
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005341 /* Sort the results. Keep menu's in the specified order. */
5342 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005343 {
5344 if (xp->xp_context == EXPAND_EXPRESSION
5345 || xp->xp_context == EXPAND_FUNCTIONS
5346 || xp->xp_context == EXPAND_USER_FUNC)
5347 /* <SNR> functions should be sorted to the end. */
5348 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5349 sort_func_compare);
5350 else
5351 sort_strings(*file, *num_file);
5352 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005353
Bram Moolenaar4f688582007-07-24 12:34:30 +00005354#ifdef FEAT_CMDL_COMPL
5355 /* Reset the variables used for special highlight names expansion, so that
5356 * they don't show up when getting normal highlight names by ID. */
5357 reset_expand_highlight();
5358#endif
5359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 return OK;
5361}
5362
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005363/*
5364 * Complete a shell command.
5365 * Returns FAIL or OK;
5366 */
5367 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005368expand_shellcmd(
5369 char_u *filepat, /* pattern to match with command names */
5370 int *num_file, /* return: number of matches */
5371 char_u ***file, /* return: array with matches */
5372 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005373{
5374 char_u *pat;
5375 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005376 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005377 int mustfree = FALSE;
5378 garray_T ga;
5379 char_u *buf = alloc(MAXPATHL);
5380 size_t l;
5381 char_u *s, *e;
5382 int flags = flagsarg;
5383 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005384 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005385 hashtab_T found_ht;
5386 hashitem_T *hi;
5387 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005388
5389 if (buf == NULL)
5390 return FAIL;
5391
5392 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5393 * space */
5394 pat = vim_strsave(filepat);
5395 for (i = 0; pat[i]; ++i)
5396 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005397 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005398
Bram Moolenaarb5971142015-03-21 17:32:19 +01005399 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005400
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005401 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5402 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005403 path = (char_u *)".";
5404 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005405 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005406 /* For an absolute name we don't use $PATH. */
5407 if (!mch_isFullName(pat))
5408 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005409 if (path == NULL)
5410 path = (char_u *)"";
5411 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005412
5413 /*
5414 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005415 * collect them in "ga". When "." is not in $PATH also expand for the
5416 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005417 */
5418 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005419 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005420 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005421 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005422#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005423 e = vim_strchr(s, ';');
5424#else
5425 e = vim_strchr(s, ':');
5426#endif
5427 if (e == NULL)
5428 e = s + STRLEN(s);
5429
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005430 if (*s == NUL)
5431 {
5432 if (did_curdir)
5433 break;
5434 // Find directories in the current directory, path is empty.
5435 did_curdir = TRUE;
5436 flags |= EW_DIR;
5437 }
5438 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5439 {
5440 did_curdir = TRUE;
5441 flags |= EW_DIR;
5442 }
5443 else
5444 // Do not match directories inside a $PATH item.
5445 flags &= ~EW_DIR;
5446
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005447 l = e - s;
5448 if (l > MAXPATHL - 5)
5449 break;
5450 vim_strncpy(buf, s, l);
5451 add_pathsep(buf);
5452 l = STRLEN(buf);
5453 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5454
5455 /* Expand matches in one directory of $PATH. */
5456 ret = expand_wildcards(1, &buf, num_file, file, flags);
5457 if (ret == OK)
5458 {
5459 if (ga_grow(&ga, *num_file) == FAIL)
5460 FreeWild(*num_file, *file);
5461 else
5462 {
5463 for (i = 0; i < *num_file; ++i)
5464 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005465 char_u *name = (*file)[i];
5466
5467 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005468 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005469 // Check if this name was already found.
5470 hash = hash_hash(name + l);
5471 hi = hash_lookup(&found_ht, name + l, hash);
5472 if (HASHITEM_EMPTY(hi))
5473 {
5474 // Remove the path that was prepended.
5475 STRMOVE(name, name + l);
5476 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5477 hash_add_item(&found_ht, hi, name, hash);
5478 name = NULL;
5479 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005480 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005481 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005482 }
5483 vim_free(*file);
5484 }
5485 }
5486 if (*e != NUL)
5487 ++e;
5488 }
5489 *file = ga.ga_data;
5490 *num_file = ga.ga_len;
5491
5492 vim_free(buf);
5493 vim_free(pat);
5494 if (mustfree)
5495 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005496 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005497 return OK;
5498}
5499
5500
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005501# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005503 * Call "user_expand_func()" to invoke a user defined Vim script function and
5504 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005506 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005508 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005509 expand_T *xp,
5510 int *num_file,
5511 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005513 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005514 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005515 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005516 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005517 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
Bram Moolenaarb7515462013-06-29 12:58:33 +02005519 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005520 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 *num_file = 0;
5522 *file = NULL;
5523
Bram Moolenaarb7515462013-06-29 12:58:33 +02005524 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005525 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005526 keep = ccline.cmdbuff[ccline.cmdlen];
5527 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005528 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005529
Bram Moolenaarffa96842018-06-12 22:05:14 +02005530 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5531
5532 args[0].v_type = VAR_STRING;
5533 args[0].vval.v_string = pat;
5534 args[1].v_type = VAR_STRING;
5535 args[1].vval.v_string = xp->xp_line;
5536 args[2].v_type = VAR_NUMBER;
5537 args[2].vval.v_number = xp->xp_col;
5538 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005540 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005541
Bram Moolenaarded27a12018-08-01 19:06:03 +02005542 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005543
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005544 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005545 if (ccline.cmdbuff != NULL)
5546 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005547
Bram Moolenaarffa96842018-06-12 22:05:14 +02005548 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005549 return ret;
5550}
5551
5552/*
5553 * Expand names with a function defined by the user.
5554 */
5555 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ExpandUserDefined(
5557 expand_T *xp,
5558 regmatch_T *regmatch,
5559 int *num_file,
5560 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005561{
5562 char_u *retstr;
5563 char_u *s;
5564 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005565 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005566 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005567 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005568
5569 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5570 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 return FAIL;
5572
5573 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005574 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 e = vim_strchr(s, '\n');
5577 if (e == NULL)
5578 e = s + STRLEN(s);
5579 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005580 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005582 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5583 *e = keep;
5584
5585 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005587 if (ga_grow(&ga, 1) == FAIL)
5588 break;
5589 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5590 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 if (*e != NUL)
5594 ++e;
5595 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005596 vim_free(retstr);
5597 *file = ga.ga_data;
5598 *num_file = ga.ga_len;
5599 return OK;
5600}
5601
5602/*
5603 * Expand names with a list returned by a function defined by the user.
5604 */
5605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ExpandUserList(
5607 expand_T *xp,
5608 int *num_file,
5609 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005610{
5611 list_T *retlist;
5612 listitem_T *li;
5613 garray_T ga;
5614
5615 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5616 if (retlist == NULL)
5617 return FAIL;
5618
5619 ga_init2(&ga, (int)sizeof(char *), 3);
5620 /* Loop over the items in the list. */
5621 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5622 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005623 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5624 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005625
5626 if (ga_grow(&ga, 1) == FAIL)
5627 break;
5628
5629 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005630 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005631 ++ga.ga_len;
5632 }
5633 list_unref(retlist);
5634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 *file = ga.ga_data;
5636 *num_file = ga.ga_len;
5637 return OK;
5638}
5639#endif
5640
5641/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005642 * Expand color scheme, compiler or filetype names.
5643 * Search from 'runtimepath':
5644 * 'runtimepath'/{dirnames}/{pat}.vim
5645 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5646 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5647 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5648 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005649 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 */
5651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652ExpandRTDir(
5653 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005654 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655 int *num_file,
5656 char_u ***file,
5657 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 char_u *s;
5660 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005661 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005663 int i;
5664 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 *num_file = 0;
5667 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005668 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005669 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005671 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005673 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005674 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005676 ga_clear_strings(&ga);
5677 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005679 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005680 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005681 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005683
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005684 if (flags & DIP_START) {
5685 for (i = 0; dirnames[i] != NULL; ++i)
5686 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005687 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005688 if (s == NULL)
5689 {
5690 ga_clear_strings(&ga);
5691 return FAIL;
5692 }
5693 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5694 globpath(p_pp, s, &ga, 0);
5695 vim_free(s);
5696 }
5697 }
5698
5699 if (flags & DIP_OPT) {
5700 for (i = 0; dirnames[i] != NULL; ++i)
5701 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005702 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005703 if (s == NULL)
5704 {
5705 ga_clear_strings(&ga);
5706 return FAIL;
5707 }
5708 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5709 globpath(p_pp, s, &ga, 0);
5710 vim_free(s);
5711 }
5712 }
5713
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005714 for (i = 0; i < ga.ga_len; ++i)
5715 {
5716 match = ((char_u **)ga.ga_data)[i];
5717 s = match;
5718 e = s + STRLEN(s);
5719 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5720 {
5721 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005722 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005723 if (s < match || vim_ispathsep(*s))
5724 break;
5725 ++s;
5726 *e = NUL;
5727 mch_memmove(match, s, e - s + 1);
5728 }
5729 }
5730
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005731 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005732 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005733
5734 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005735 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005736 remove_duplicates(&ga);
5737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 *file = ga.ga_data;
5739 *num_file = ga.ga_len;
5740 return OK;
5741}
5742
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005743/*
5744 * Expand loadplugin names:
5745 * 'packpath'/pack/ * /opt/{pat}
5746 */
5747 static int
5748ExpandPackAddDir(
5749 char_u *pat,
5750 int *num_file,
5751 char_u ***file)
5752{
5753 char_u *s;
5754 char_u *e;
5755 char_u *match;
5756 garray_T ga;
5757 int i;
5758 int pat_len;
5759
5760 *num_file = 0;
5761 *file = NULL;
5762 pat_len = (int)STRLEN(pat);
5763 ga_init2(&ga, (int)sizeof(char *), 10);
5764
Bram Moolenaar964b3742019-05-24 18:54:09 +02005765 s = alloc(pat_len + 26);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005766 if (s == NULL)
5767 {
5768 ga_clear_strings(&ga);
5769 return FAIL;
5770 }
5771 sprintf((char *)s, "pack/*/opt/%s*", pat);
5772 globpath(p_pp, s, &ga, 0);
5773 vim_free(s);
5774
5775 for (i = 0; i < ga.ga_len; ++i)
5776 {
5777 match = ((char_u **)ga.ga_data)[i];
5778 s = gettail(match);
5779 e = s + STRLEN(s);
5780 mch_memmove(match, s, e - s + 1);
5781 }
5782
5783 if (ga.ga_len == 0)
5784 return FAIL;
5785
5786 /* Sort and remove duplicates which can happen when specifying multiple
5787 * directories in dirnames. */
5788 remove_duplicates(&ga);
5789
5790 *file = ga.ga_data;
5791 *num_file = ga.ga_len;
5792 return OK;
5793}
5794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795#endif
5796
5797#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5798/*
5799 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005800 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005802 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005803globpath(
5804 char_u *path,
5805 char_u *file,
5806 garray_T *ga,
5807 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 expand_T xpc;
5810 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 int num_p;
5813 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 buf = alloc(MAXPATHL);
5816 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005817 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005819 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005821
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 /* Loop over all entries in {path}. */
5823 while (*path != NUL)
5824 {
5825 /* Copy one item of the path to buf[] and concatenate the file name. */
5826 copy_option_part(&path, buf, MAXPATHL, ",");
5827 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5828 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005829# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005830 /* Using the platform's path separator (\) makes vim incorrectly
5831 * treat it as an escape character, use '/' instead. */
5832 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5833 STRCAT(buf, "/");
5834# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005838 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5839 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005841 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005843 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 for (i = 0; i < num_p; ++i)
5846 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005847 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005848 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005849 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 FreeWild(num_p, p);
5854 }
5855 }
5856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859}
5860
5861#endif
5862
5863#if defined(FEAT_CMDHIST) || defined(PROTO)
5864
5865/*********************************
5866 * Command line history stuff *
5867 *********************************/
5868
5869/*
5870 * Translate a history character to the associated type number.
5871 */
5872 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005873hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 if (c == ':')
5876 return HIST_CMD;
5877 if (c == '=')
5878 return HIST_EXPR;
5879 if (c == '@')
5880 return HIST_INPUT;
5881 if (c == '>')
5882 return HIST_DEBUG;
5883 return HIST_SEARCH; /* must be '?' or '/' */
5884}
5885
5886/*
5887 * Table of history names.
5888 * These names are used in :history and various hist...() functions.
5889 * It is sufficient to give the significant prefix of a history name.
5890 */
5891
5892static char *(history_names[]) =
5893{
5894 "cmd",
5895 "search",
5896 "expr",
5897 "input",
5898 "debug",
5899 NULL
5900};
5901
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005902#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5903/*
5904 * Function given to ExpandGeneric() to obtain the possible first
5905 * arguments of the ":history command.
5906 */
5907 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005908get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005909{
5910 static char_u compl[2] = { NUL, NUL };
5911 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005912 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005913 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5914
5915 if (idx < short_names_count)
5916 {
5917 compl[0] = (char_u)short_names[idx];
5918 return compl;
5919 }
5920 if (idx < short_names_count + history_name_count)
5921 return (char_u *)history_names[idx - short_names_count];
5922 if (idx == short_names_count + history_name_count)
5923 return (char_u *)"all";
5924 return NULL;
5925}
5926#endif
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * init_history() - Initialize the command line history.
5930 * Also used to re-allocate the history when the size changes.
5931 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005933init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 int newlen; /* new length of history table */
5936 histentry_T *temp;
5937 int i;
5938 int j;
5939 int type;
5940
5941 /*
5942 * If size of history table changed, reallocate it
5943 */
5944 newlen = (int)p_hi;
5945 if (newlen != hislen) /* history length changed */
5946 {
5947 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5948 {
5949 if (newlen)
5950 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005951 temp = ALLOC_MULT(histentry_T, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 if (temp == NULL) /* out of memory! */
5953 {
5954 if (type == 0) /* first one: just keep the old length */
5955 {
5956 newlen = hislen;
5957 break;
5958 }
5959 /* Already changed one table, now we can only have zero
5960 * length for all tables. */
5961 newlen = 0;
5962 type = -1;
5963 continue;
5964 }
5965 }
5966 else
5967 temp = NULL;
5968 if (newlen == 0 || temp != NULL)
5969 {
5970 if (hisidx[type] < 0) /* there are no entries yet */
5971 {
5972 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005973 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
5975 else if (newlen > hislen) /* array becomes bigger */
5976 {
5977 for (i = 0; i <= hisidx[type]; ++i)
5978 temp[i] = history[type][i];
5979 j = i;
5980 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005981 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 for ( ; j < hislen; ++i, ++j)
5983 temp[i] = history[type][j];
5984 }
5985 else /* array becomes smaller or 0 */
5986 {
5987 j = hisidx[type];
5988 for (i = newlen - 1; ; --i)
5989 {
5990 if (i >= 0) /* copy newest entries */
5991 temp[i] = history[type][j];
5992 else /* remove older entries */
5993 vim_free(history[type][j].hisstr);
5994 if (--j < 0)
5995 j = hislen - 1;
5996 if (j == hisidx[type])
5997 break;
5998 }
5999 hisidx[type] = newlen - 1;
6000 }
6001 vim_free(history[type]);
6002 history[type] = temp;
6003 }
6004 }
6005 hislen = newlen;
6006 }
6007}
6008
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006010clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006011{
6012 hisptr->hisnum = 0;
6013 hisptr->viminfo = FALSE;
6014 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006015 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006016}
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018/*
6019 * Check if command line 'str' is already in history.
6020 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6021 */
6022 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023in_history(
6024 int type,
6025 char_u *str,
6026 int move_to_front, /* Move the entry to the front if it exists */
6027 int sep,
6028 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 int i;
6031 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006032 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033
6034 if (hisidx[type] < 0)
6035 return FALSE;
6036 i = hisidx[type];
6037 do
6038 {
6039 if (history[type][i].hisstr == NULL)
6040 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006041
6042 /* For search history, check that the separator character matches as
6043 * well. */
6044 p = history[type][i].hisstr;
6045 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006046 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006047 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
6049 if (!move_to_front)
6050 return TRUE;
6051 last_i = i;
6052 break;
6053 }
6054 if (--i < 0)
6055 i = hislen - 1;
6056 } while (i != hisidx[type]);
6057
6058 if (last_i >= 0)
6059 {
6060 str = history[type][i].hisstr;
6061 while (i != hisidx[type])
6062 {
6063 if (++i >= hislen)
6064 i = 0;
6065 history[type][last_i] = history[type][i];
6066 last_i = i;
6067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006069 history[type][i].viminfo = FALSE;
6070 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006071 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 return TRUE;
6073 }
6074 return FALSE;
6075}
6076
6077/*
6078 * Convert history name (from table above) to its HIST_ equivalent.
6079 * When "name" is empty, return "cmd" history.
6080 * Returns -1 for unknown history name.
6081 */
6082 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006083get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
6085 int i;
6086 int len = (int)STRLEN(name);
6087
6088 /* No argument: use current history. */
6089 if (len == 0)
6090 return hist_char2type(ccline.cmdfirstc);
6091
6092 for (i = 0; history_names[i] != NULL; ++i)
6093 if (STRNICMP(name, history_names[i], len) == 0)
6094 return i;
6095
6096 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6097 return hist_char2type(name[0]);
6098
6099 return -1;
6100}
6101
6102static int last_maptick = -1; /* last seen maptick */
6103
6104/*
6105 * Add the given string to the given history. If the string is already in the
6106 * history then it is moved to the front. "histype" may be one of he HIST_
6107 * values.
6108 */
6109 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006110add_to_history(
6111 int histype,
6112 char_u *new_entry,
6113 int in_map, /* consider maptick when inside a mapping */
6114 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
6116 histentry_T *hisptr;
6117 int len;
6118
6119 if (hislen == 0) /* no history */
6120 return;
6121
Bram Moolenaara939e432013-11-09 05:30:26 +01006122 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6123 return;
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 /*
6126 * Searches inside the same mapping overwrite each other, so that only
6127 * the last line is kept. Be careful not to remove a line that was moved
6128 * down, only lines that were added.
6129 */
6130 if (histype == HIST_SEARCH && in_map)
6131 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006132 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
6134 /* Current line is from the same mapping, remove it */
6135 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6136 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006137 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 --hisnum[histype];
6139 if (--hisidx[HIST_SEARCH] < 0)
6140 hisidx[HIST_SEARCH] = hislen - 1;
6141 }
6142 last_maptick = -1;
6143 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006144 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 if (++hisidx[histype] == hislen)
6147 hisidx[histype] = 0;
6148 hisptr = &history[histype][hisidx[histype]];
6149 vim_free(hisptr->hisstr);
6150
6151 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006152 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6154 if (hisptr->hisstr != NULL)
6155 hisptr->hisstr[len + 1] = sep;
6156
6157 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006158 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006159 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (histype == HIST_SEARCH && in_map)
6161 last_maptick = maptick;
6162 }
6163}
6164
6165#if defined(FEAT_EVAL) || defined(PROTO)
6166
6167/*
6168 * Get identifier of newest history entry.
6169 * "histype" may be one of the HIST_ values.
6170 */
6171 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006172get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6175 || hisidx[histype] < 0)
6176 return -1;
6177
6178 return history[histype][hisidx[histype]].hisnum;
6179}
6180
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 * Calculate history index from a number:
6183 * num > 0: seen as identifying number of a history entry
6184 * num < 0: relative position in history wrt newest entry
6185 * "histype" may be one of the HIST_ values.
6186 */
6187 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006188calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
6190 int i;
6191 histentry_T *hist;
6192 int wrapped = FALSE;
6193
6194 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6195 || (i = hisidx[histype]) < 0 || num == 0)
6196 return -1;
6197
6198 hist = history[histype];
6199 if (num > 0)
6200 {
6201 while (hist[i].hisnum > num)
6202 if (--i < 0)
6203 {
6204 if (wrapped)
6205 break;
6206 i += hislen;
6207 wrapped = TRUE;
6208 }
6209 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6210 return i;
6211 }
6212 else if (-num <= hislen)
6213 {
6214 i += num + 1;
6215 if (i < 0)
6216 i += hislen;
6217 if (hist[i].hisstr != NULL)
6218 return i;
6219 }
6220 return -1;
6221}
6222
6223/*
6224 * Get a history entry by its index.
6225 * "histype" may be one of the HIST_ values.
6226 */
6227 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006228get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 idx = calc_hist_idx(histype, idx);
6231 if (idx >= 0)
6232 return history[histype][idx].hisstr;
6233 else
6234 return (char_u *)"";
6235}
6236
6237/*
6238 * Clear all entries of a history.
6239 * "histype" may be one of the HIST_ values.
6240 */
6241 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006242clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 int i;
6245 histentry_T *hisptr;
6246
6247 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6248 {
6249 hisptr = history[histype];
6250 for (i = hislen; i--;)
6251 {
6252 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006253 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006254 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 }
6256 hisidx[histype] = -1; /* mark history as cleared */
6257 hisnum[histype] = 0; /* reset identifier counter */
6258 return OK;
6259 }
6260 return FAIL;
6261}
6262
6263/*
6264 * Remove all entries matching {str} from a history.
6265 * "histype" may be one of the HIST_ values.
6266 */
6267 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006268del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
6270 regmatch_T regmatch;
6271 histentry_T *hisptr;
6272 int idx;
6273 int i;
6274 int last;
6275 int found = FALSE;
6276
6277 regmatch.regprog = NULL;
6278 regmatch.rm_ic = FALSE; /* always match case */
6279 if (hislen != 0
6280 && histype >= 0
6281 && histype < HIST_COUNT
6282 && *str != NUL
6283 && (idx = hisidx[histype]) >= 0
6284 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6285 != NULL)
6286 {
6287 i = last = idx;
6288 do
6289 {
6290 hisptr = &history[histype][i];
6291 if (hisptr->hisstr == NULL)
6292 break;
6293 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6294 {
6295 found = TRUE;
6296 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006297 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299 else
6300 {
6301 if (i != last)
6302 {
6303 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006304 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306 if (--last < 0)
6307 last += hislen;
6308 }
6309 if (--i < 0)
6310 i += hislen;
6311 } while (i != idx);
6312 if (history[histype][idx].hisstr == NULL)
6313 hisidx[histype] = -1;
6314 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006315 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 return found;
6317}
6318
6319/*
6320 * Remove an indexed entry from a history.
6321 * "histype" may be one of the HIST_ values.
6322 */
6323 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 int i, j;
6327
6328 i = calc_hist_idx(histype, idx);
6329 if (i < 0)
6330 return FALSE;
6331 idx = hisidx[histype];
6332 vim_free(history[histype][i].hisstr);
6333
6334 /* When deleting the last added search string in a mapping, reset
6335 * last_maptick, so that the last added search string isn't deleted again.
6336 */
6337 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6338 last_maptick = -1;
6339
6340 while (i != idx)
6341 {
6342 j = (i + 1) % hislen;
6343 history[histype][i] = history[histype][j];
6344 i = j;
6345 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006346 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if (--i < 0)
6348 i += hislen;
6349 hisidx[histype] = i;
6350 return TRUE;
6351}
6352
6353#endif /* FEAT_EVAL */
6354
6355#if defined(FEAT_CRYPT) || defined(PROTO)
6356/*
6357 * Very specific function to remove the value in ":set key=val" from the
6358 * history.
6359 */
6360 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006361remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 char_u *p;
6364 int i;
6365
6366 i = hisidx[HIST_CMD];
6367 if (i < 0)
6368 return;
6369 p = history[HIST_CMD][i].hisstr;
6370 if (p != NULL)
6371 for ( ; *p; ++p)
6372 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6373 {
6374 p = vim_strchr(p + 3, '=');
6375 if (p == NULL)
6376 break;
6377 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006378 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 if (p[i] == '\\' && p[i + 1])
6380 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006381 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 --p;
6383 }
6384}
6385#endif
6386
6387#endif /* FEAT_CMDHIST */
6388
Bram Moolenaar064154c2016-03-19 22:50:43 +01006389#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006390/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006391 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006392 * ccline and put the previous value in prev_ccline.
6393 */
6394 static struct cmdline_info *
6395get_ccline_ptr(void)
6396{
6397 if ((State & CMDLINE) == 0)
6398 return NULL;
6399 if (ccline.cmdbuff != NULL)
6400 return &ccline;
6401 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6402 return &prev_ccline;
6403 return NULL;
6404}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006405#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006406
Bram Moolenaar064154c2016-03-19 22:50:43 +01006407#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006408/*
6409 * Get the current command line in allocated memory.
6410 * Only works when the command line is being edited.
6411 * Returns NULL when something is wrong.
6412 */
6413 char_u *
6414get_cmdline_str(void)
6415{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006416 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006417
Bram Moolenaaree91c332018-09-25 22:27:35 +02006418 if (cmdline_star > 0)
6419 return NULL;
6420 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006421 if (p == NULL)
6422 return NULL;
6423 return vim_strnsave(p->cmdbuff, p->cmdlen);
6424}
6425
6426/*
6427 * Get the current command line position, counted in bytes.
6428 * Zero is the first position.
6429 * Only works when the command line is being edited.
6430 * Returns -1 when something is wrong.
6431 */
6432 int
6433get_cmdline_pos(void)
6434{
6435 struct cmdline_info *p = get_ccline_ptr();
6436
6437 if (p == NULL)
6438 return -1;
6439 return p->cmdpos;
6440}
6441
6442/*
6443 * Set the command line byte position to "pos". Zero is the first position.
6444 * Only works when the command line is being edited.
6445 * Returns 1 when failed, 0 when OK.
6446 */
6447 int
6448set_cmdline_pos(
6449 int pos)
6450{
6451 struct cmdline_info *p = get_ccline_ptr();
6452
6453 if (p == NULL)
6454 return 1;
6455
6456 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6457 * changed the command line. */
6458 if (pos < 0)
6459 new_cmdpos = 0;
6460 else
6461 new_cmdpos = pos;
6462 return 0;
6463}
6464#endif
6465
6466#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6467/*
6468 * Get the current command-line type.
6469 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6470 * Only works when the command line is being edited.
6471 * Returns NUL when something is wrong.
6472 */
6473 int
6474get_cmdline_type(void)
6475{
6476 struct cmdline_info *p = get_ccline_ptr();
6477
6478 if (p == NULL)
6479 return NUL;
6480 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006481 return
6482# ifdef FEAT_EVAL
6483 (p->input_fn) ? '@' :
6484# endif
6485 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006486 return p->cmdfirstc;
6487}
6488#endif
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6491/*
6492 * Get indices "num1,num2" that specify a range within a list (not a range of
6493 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6494 * Returns OK if parsed successfully, otherwise FAIL.
6495 */
6496 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006497get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 int len;
6500 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006501 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503 *str = skipwhite(*str);
6504 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6505 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006506 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 *str += len;
6508 *num1 = (int)num;
6509 first = TRUE;
6510 }
6511 *str = skipwhite(*str);
6512 if (**str == ',') /* parse "to" part of range */
6513 {
6514 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006515 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (len > 0)
6517 {
6518 *num2 = (int)num;
6519 *str = skipwhite(*str + len);
6520 }
6521 else if (!first) /* no number given at all */
6522 return FAIL;
6523 }
6524 else if (first) /* only one number given */
6525 *num2 = *num1;
6526 return OK;
6527}
6528#endif
6529
6530#if defined(FEAT_CMDHIST) || defined(PROTO)
6531/*
6532 * :history command - print a history
6533 */
6534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006535ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 histentry_T *hist;
6538 int histype1 = HIST_CMD;
6539 int histype2 = HIST_CMD;
6540 int hisidx1 = 1;
6541 int hisidx2 = -1;
6542 int idx;
6543 int i, j, k;
6544 char_u *end;
6545 char_u *arg = eap->arg;
6546
6547 if (hislen == 0)
6548 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006549 msg(_("'history' option is zero"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 return;
6551 }
6552
6553 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6554 {
6555 end = arg;
6556 while (ASCII_ISALPHA(*end)
6557 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6558 end++;
6559 i = *end;
6560 *end = NUL;
6561 histype1 = get_histtype(arg);
6562 if (histype1 == -1)
6563 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006564 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 {
6566 histype1 = 0;
6567 histype2 = HIST_COUNT-1;
6568 }
6569 else
6570 {
6571 *end = i;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006572 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 return;
6574 }
6575 }
6576 else
6577 histype2 = histype1;
6578 *end = i;
6579 }
6580 else
6581 end = arg;
6582 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006584 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 return;
6586 }
6587
6588 for (; !got_int && histype1 <= histype2; ++histype1)
6589 {
6590 STRCPY(IObuff, "\n # ");
6591 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
Bram Moolenaar32526b32019-01-19 17:43:09 +01006592 msg_puts_title((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 idx = hisidx[histype1];
6594 hist = history[histype1];
6595 j = hisidx1;
6596 k = hisidx2;
6597 if (j < 0)
6598 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6599 if (k < 0)
6600 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6601 if (idx >= 0 && j <= k)
6602 for (i = idx + 1; !got_int; ++i)
6603 {
6604 if (i == hislen)
6605 i = 0;
6606 if (hist[i].hisstr != NULL
6607 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6608 {
6609 msg_putchar('\n');
6610 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6611 hist[i].hisnum);
6612 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6613 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006614 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 else
6616 STRCAT(IObuff, hist[i].hisstr);
6617 msg_outtrans(IObuff);
6618 out_flush();
6619 }
6620 if (i == idx)
6621 break;
6622 }
6623 }
6624}
6625#endif
6626
6627#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006628/*
6629 * Buffers for history read from a viminfo file. Only valid while reading.
6630 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006631static histentry_T *viminfo_history[HIST_COUNT] =
6632 {NULL, NULL, NULL, NULL, NULL};
6633static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6634static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635static int viminfo_add_at_front = FALSE;
6636
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637/*
6638 * Translate a history type number to the associated character.
6639 */
6640 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006641hist_type2char(
6642 int type,
6643 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 if (type == HIST_CMD)
6646 return ':';
6647 if (type == HIST_SEARCH)
6648 {
6649 if (use_question)
6650 return '?';
6651 else
6652 return '/';
6653 }
6654 if (type == HIST_EXPR)
6655 return '=';
6656 return '@';
6657}
6658
6659/*
6660 * Prepare for reading the history from the viminfo file.
6661 * This allocates history arrays to store the read history lines.
6662 */
6663 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006664prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 int i;
6667 int num;
6668 int type;
6669 int len;
6670
6671 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006672 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (asklen > hislen)
6674 asklen = hislen;
6675
6676 for (type = 0; type < HIST_COUNT; ++type)
6677 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006678 /* Count the number of empty spaces in the history list. Entries read
6679 * from viminfo previously are also considered empty. If there are
6680 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006682 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 num++;
6684 len = asklen;
6685 if (num > len)
6686 len = num;
6687 if (len <= 0)
6688 viminfo_history[type] = NULL;
6689 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006690 viminfo_history[type] = LALLOC_MULT(histentry_T, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (viminfo_history[type] == NULL)
6692 len = 0;
6693 viminfo_hislen[type] = len;
6694 viminfo_hisidx[type] = 0;
6695 }
6696}
6697
6698/*
6699 * Accept a line from the viminfo, store it in the history array when it's
6700 * new.
6701 */
6702 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006703read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705 int type;
6706 long_u len;
6707 char_u *val;
6708 char_u *p;
6709
6710 type = hist_char2type(virp->vir_line[0]);
6711 if (viminfo_hisidx[type] < viminfo_hislen[type])
6712 {
6713 val = viminfo_readstring(virp, 1, TRUE);
6714 if (val != NULL && *val != NUL)
6715 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006716 int sep = (*val == ' ' ? NUL : *val);
6717
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006719 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
6721 /* Need to re-allocate to append the separator byte. */
6722 len = STRLEN(val);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006723 p = alloc(len + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (p != NULL)
6725 {
6726 if (type == HIST_SEARCH)
6727 {
6728 /* Search entry: Move the separator from the first
6729 * column to after the NUL. */
6730 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006731 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
6733 else
6734 {
6735 /* Not a search entry: No separator in the viminfo
6736 * file, add a NUL separator. */
6737 mch_memmove(p, val, (size_t)len + 1);
6738 p[len + 1] = NUL;
6739 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006740 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6741 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006742 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6743 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006744 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 }
6746 }
6747 }
6748 vim_free(val);
6749 }
6750 return viminfo_readline(virp);
6751}
6752
Bram Moolenaar07219f92013-04-14 23:19:36 +02006753/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006754 * Accept a new style history line from the viminfo, store it in the history
6755 * array when it's new.
6756 */
6757 void
6758handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006759 garray_T *values,
6760 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006761{
6762 int type;
6763 long_u len;
6764 char_u *val;
6765 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006766 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006767
6768 /* Check the format:
6769 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006770 if (values->ga_len < 4
6771 || vp[0].bv_type != BVAL_NR
6772 || vp[1].bv_type != BVAL_NR
6773 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6774 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006775 return;
6776
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006777 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006778 if (type >= HIST_COUNT)
6779 return;
6780 if (viminfo_hisidx[type] < viminfo_hislen[type])
6781 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006782 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006783 if (val != NULL && *val != NUL)
6784 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006785 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6786 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006787 int idx;
6788 int overwrite = FALSE;
6789
6790 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6791 {
6792 /* If lines were written by an older Vim we need to avoid
6793 * getting duplicates. See if the entry already exists. */
6794 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6795 {
6796 p = viminfo_history[type][idx].hisstr;
6797 if (STRCMP(val, p) == 0
6798 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6799 {
6800 overwrite = TRUE;
6801 break;
6802 }
6803 }
6804
6805 if (!overwrite)
6806 {
6807 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006808 len = vp[3].bv_len;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006809 p = alloc(len + 2);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006810 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006811 else
6812 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006813 if (p != NULL)
6814 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006815 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006816 if (!overwrite)
6817 {
6818 mch_memmove(p, val, (size_t)len + 1);
6819 /* Put the separator after the NUL. */
6820 p[len + 1] = sep;
6821 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006822 viminfo_history[type][idx].hisnum = 0;
6823 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006824 viminfo_hisidx[type]++;
6825 }
6826 }
6827 }
6828 }
6829 }
6830}
6831
6832/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006833 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006834 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006835 static void
6836concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 int idx;
6839 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006840
6841 idx = hisidx[type] + viminfo_hisidx[type];
6842 if (idx >= hislen)
6843 idx -= hislen;
6844 else if (idx < 0)
6845 idx = hislen - 1;
6846 if (viminfo_add_at_front)
6847 hisidx[type] = idx;
6848 else
6849 {
6850 if (hisidx[type] == -1)
6851 hisidx[type] = hislen - 1;
6852 do
6853 {
6854 if (history[type][idx].hisstr != NULL
6855 || history[type][idx].viminfo)
6856 break;
6857 if (++idx == hislen)
6858 idx = 0;
6859 } while (idx != hisidx[type]);
6860 if (idx != hisidx[type] && --idx < 0)
6861 idx = hislen - 1;
6862 }
6863 for (i = 0; i < viminfo_hisidx[type]; i++)
6864 {
6865 vim_free(history[type][idx].hisstr);
6866 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6867 history[type][idx].viminfo = TRUE;
6868 history[type][idx].time_set = viminfo_history[type][i].time_set;
6869 if (--idx < 0)
6870 idx = hislen - 1;
6871 }
6872 idx += 1;
6873 idx %= hislen;
6874 for (i = 0; i < viminfo_hisidx[type]; i++)
6875 {
6876 history[type][idx++].hisnum = ++hisnum[type];
6877 idx %= hislen;
6878 }
6879}
6880
6881#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6882 static int
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006883sort_hist(const void *s1, const void *s2)
6884{
6885 histentry_T *p1 = *(histentry_T **)s1;
6886 histentry_T *p2 = *(histentry_T **)s2;
6887
6888 if (p1->time_set < p2->time_set) return -1;
6889 if (p1->time_set > p2->time_set) return 1;
6890 return 0;
6891}
6892#endif
6893
6894/*
6895 * Merge history lines from viminfo and lines typed in this Vim based on the
6896 * timestamp;
6897 */
6898 static void
6899merge_history(int type)
6900{
6901 int max_len;
6902 histentry_T **tot_hist;
6903 histentry_T *new_hist;
6904 int i;
6905 int len;
6906
6907 /* Make one long list with all entries. */
6908 max_len = hislen + viminfo_hisidx[type];
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006909 tot_hist = ALLOC_MULT(histentry_T *, max_len);
6910 new_hist = ALLOC_MULT(histentry_T, hislen );
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006911 if (tot_hist == NULL || new_hist == NULL)
6912 {
6913 vim_free(tot_hist);
6914 vim_free(new_hist);
6915 return;
6916 }
6917 for (i = 0; i < viminfo_hisidx[type]; i++)
6918 tot_hist[i] = &viminfo_history[type][i];
6919 len = i;
6920 for (i = 0; i < hislen; i++)
6921 if (history[type][i].hisstr != NULL)
6922 tot_hist[len++] = &history[type][i];
6923
6924 /* Sort the list on timestamp. */
6925 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6926
6927 /* Keep the newest ones. */
6928 for (i = 0; i < hislen; i++)
6929 {
6930 if (i < len)
6931 {
6932 new_hist[i] = *tot_hist[i];
6933 tot_hist[i]->hisstr = NULL;
6934 if (new_hist[i].hisnum == 0)
6935 new_hist[i].hisnum = ++hisnum[type];
6936 }
6937 else
6938 clear_hist_entry(&new_hist[i]);
6939 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006940 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006941
6942 /* Free what is not kept. */
6943 for (i = 0; i < viminfo_hisidx[type]; i++)
6944 vim_free(viminfo_history[type][i].hisstr);
6945 for (i = 0; i < hislen; i++)
6946 vim_free(history[type][i].hisstr);
6947 vim_free(history[type]);
6948 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006949 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006950}
6951
6952/*
6953 * Finish reading history lines from viminfo. Not used when writing viminfo.
6954 */
6955 void
6956finish_viminfo_history(vir_T *virp)
6957{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006959 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 for (type = 0; type < HIST_COUNT; ++type)
6962 {
6963 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006964 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006965
6966 if (merge)
6967 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006969 concat_history(type);
6970
Bram Moolenaard23a8232018-02-10 18:45:26 +01006971 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006972 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974}
6975
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006976/*
6977 * Write history to viminfo file in "fp".
6978 * When "merge" is TRUE merge history lines with a previously read viminfo
6979 * file, data is in viminfo_history[].
6980 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006983write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984{
6985 int i;
6986 int type;
6987 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006988 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
6990 init_history();
6991 if (hislen == 0)
6992 return;
6993 for (type = 0; type < HIST_COUNT; ++type)
6994 {
6995 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6996 if (num_saved == 0)
6997 continue;
6998 if (num_saved < 0) /* Use default */
6999 num_saved = hislen;
7000 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7001 type == HIST_CMD ? _("Command Line") :
7002 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007003 type == HIST_EXPR ? _("Expression") :
7004 type == HIST_INPUT ? _("Input Line") :
7005 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (num_saved > hislen)
7007 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007008
7009 /*
7010 * Merge typed and viminfo history:
7011 * round 1: history of typed commands.
7012 * round 2: history from recently read viminfo.
7013 */
7014 for (round = 1; round <= 2; ++round)
7015 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007016 if (round == 1)
7017 /* start at newest entry, somewhere in the list */
7018 i = hisidx[type];
7019 else if (viminfo_hisidx[type] > 0)
7020 /* start at newest entry, first in the list */
7021 i = 0;
7022 else
7023 /* empty list */
7024 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007025 if (i >= 0)
7026 while (num_saved > 0
7027 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007029 char_u *p;
7030 time_t timestamp;
7031 int c = NUL;
7032
7033 if (round == 1)
7034 {
7035 p = history[type][i].hisstr;
7036 timestamp = history[type][i].time_set;
7037 }
7038 else
7039 {
7040 p = viminfo_history[type] == NULL ? NULL
7041 : viminfo_history[type][i].hisstr;
7042 timestamp = viminfo_history[type] == NULL ? 0
7043 : viminfo_history[type][i].time_set;
7044 }
7045
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007046 if (p != NULL && (round == 2
7047 || !merge
7048 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007050 --num_saved;
7051 fputc(hist_type2char(type, TRUE), fp);
7052 /* For the search history: put the separator in the
7053 * second column; use a space if there isn't one. */
7054 if (type == HIST_SEARCH)
7055 {
7056 c = p[STRLEN(p) + 1];
7057 putc(c == NUL ? ' ' : c, fp);
7058 }
7059 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007060
7061 {
7062 char cbuf[NUMBUFLEN];
7063
7064 /* New style history with a bar line. Format:
7065 * |{bartype},{histtype},{timestamp},{separator},"text" */
7066 if (c == NUL)
7067 cbuf[0] = NUL;
7068 else
7069 sprintf(cbuf, "%d", c);
7070 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7071 type, (long)timestamp, cbuf);
7072 barline_writestring(fp, p, LSIZE - 20);
7073 putc('\n', fp);
7074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007076 if (round == 1)
7077 {
7078 /* Decrement index, loop around and stop when back at
7079 * the start. */
7080 if (--i < 0)
7081 i = hislen - 1;
7082 if (i == hisidx[type])
7083 break;
7084 }
7085 else
7086 {
7087 /* Increment index. Stop at the end in the while. */
7088 ++i;
7089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007091 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007092 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007093 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007094 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007095 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007096 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
7098}
7099#endif /* FEAT_VIMINFO */
7100
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101#if defined(FEAT_CMDWIN) || defined(PROTO)
7102/*
7103 * Open a window on the current command line and history. Allow editing in
7104 * the window. Returns when the window is closed.
7105 * Returns:
7106 * CR if the command is to be executed
7107 * Ctrl_C if it is to be abandoned
7108 * K_IGNORE if editing continues
7109 */
7110 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007111open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007113 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007115 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 win_T *wp;
7117 int i;
7118 linenr_T lnum;
7119 int histtype;
7120 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 int save_restart_edit = restart_edit;
7122 int save_State = State;
7123 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007124#ifdef FEAT_RIGHTLEFT
7125 int save_cmdmsg_rl = cmdmsg_rl;
7126#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007127#ifdef FEAT_FOLDING
7128 int save_KeyTyped;
7129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
7131 /* Can't do this recursively. Can't do it when typing a password. */
7132 if (cmdwin_type != 0
7133# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7134 || cmdline_star > 0
7135# endif
7136 )
7137 {
7138 beep_flush();
7139 return K_IGNORE;
7140 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007141 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
7143 /* Save current window sizes. */
7144 win_size_save(&winsizes);
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007147 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007148
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007149#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007150 // When using completion in Insert mode with <C-R>=<C-F> one can open the
7151 // command line window, but we don't want the popup menu then.
7152 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007153#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007154
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007155 /* don't use a new tab page */
7156 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007157 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 /* Create a window for the command-line buffer. */
7160 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7161 {
7162 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007163 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 return K_IGNORE;
7165 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007166 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007169 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007170 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007173#ifdef FEAT_FOLDING
7174 curwin->w_p_fen = FALSE;
7175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007177 curwin->w_p_rl = cmdmsg_rl;
7178 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007180 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007183 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007184 /* But don't allow switching to another buffer. */
7185 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar46152342005-09-07 21:18:43 +00007187 /* Showing the prompt may have set need_wait_return, reset it. */
7188 need_wait_return = FALSE;
7189
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007190 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7192 {
7193 if (p_wc == TAB)
7194 {
7195 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7196 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7197 }
7198 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7199 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007200 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007202 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7203 * sets 'textwidth' to 78). */
7204 curbuf->b_p_tw = 0;
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 /* Fill the buffer with the history. */
7207 init_history();
7208 if (hislen > 0)
7209 {
7210 i = hisidx[histtype];
7211 if (i >= 0)
7212 {
7213 lnum = 0;
7214 do
7215 {
7216 if (++i == hislen)
7217 i = 0;
7218 if (history[histtype][i].hisstr != NULL)
7219 ml_append(lnum++, history[histtype][i].hisstr,
7220 (colnr_T)0, FALSE);
7221 }
7222 while (i != hisidx[histtype]);
7223 }
7224 }
7225
7226 /* Replace the empty last line with the current command-line and put the
7227 * cursor there. */
7228 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7229 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7230 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007231 changed_line_abv_curs();
7232 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007233 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 /* No Ex mode here! */
7236 exmode_active = 0;
7237
7238 State = NORMAL;
7239# ifdef FEAT_MOUSE
7240 setmouse();
7241# endif
7242
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007244 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007245 if (restart_edit != 0) /* autocmd with ":startinsert" */
7246 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
7248 i = RedrawingDisabled;
7249 RedrawingDisabled = 0;
7250
7251 /*
7252 * Call the main loop until <CR> or CTRL-C is typed.
7253 */
7254 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007255 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
7257 RedrawingDisabled = i;
7258
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007259# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007260 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007261# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007262
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007264 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007265
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007266# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007267 /* Restore KeyTyped in case it is modified by autocommands */
7268 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269# endif
7270
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 exmode_active = save_exmode;
7273
Bram Moolenaarf9821062008-06-20 16:31:07 +00007274 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007276 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
7278 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007279 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 else
7282 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007283# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 /* autocmds may abort script processing */
7285 if (aborting() && cmdwin_result != K_IGNORE)
7286 cmdwin_result = Ctrl_C;
7287# endif
7288 /* Set the new command line from the cmdline buffer. */
7289 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007290 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007292 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7293
7294 if (histtype == HIST_CMD)
7295 {
7296 /* Execute the command directly. */
7297 ccline.cmdbuff = vim_strsave((char_u *)p);
7298 cmdwin_result = CAR;
7299 }
7300 else
7301 {
7302 /* First need to cancel what we were doing. */
7303 ccline.cmdbuff = NULL;
7304 stuffcharReadbuff(':');
7305 stuffReadbuff((char_u *)p);
7306 stuffcharReadbuff(CAR);
7307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 }
7309 else if (cmdwin_result == K_XF2) /* :qa typed */
7310 {
7311 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7312 cmdwin_result = CAR;
7313 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007314 else if (cmdwin_result == Ctrl_C)
7315 {
7316 /* :q or :close, don't execute any command
7317 * and don't modify the cmd window. */
7318 ccline.cmdbuff = NULL;
7319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 else
7321 ccline.cmdbuff = vim_strsave(ml_get_curline());
7322 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007323 {
7324 ccline.cmdbuff = vim_strsave((char_u *)"");
7325 ccline.cmdlen = 0;
7326 ccline.cmdbufflen = 1;
7327 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 else
7331 {
7332 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7333 ccline.cmdbufflen = ccline.cmdlen + 1;
7334 ccline.cmdpos = curwin->w_cursor.col;
7335 if (ccline.cmdpos > ccline.cmdlen)
7336 ccline.cmdpos = ccline.cmdlen;
7337 if (cmdwin_result == K_IGNORE)
7338 {
7339 set_cmdspos_cursor();
7340 redrawcmd();
7341 }
7342 }
7343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007345 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007346# ifdef FEAT_CONCEAL
7347 /* Avoid command-line window first character being concealed. */
7348 curwin->w_p_cole = 0;
7349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007351 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 win_goto(old_curwin);
7353 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007354
7355 /* win_close() may have already wiped the buffer when 'bh' is
7356 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007357 if (bufref_valid(&bufref))
7358 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359
7360 /* Restore window sizes. */
7361 win_size_restore(&winsizes);
7362
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007363 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
7365
7366 ga_clear(&winsizes);
7367 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007368# ifdef FEAT_RIGHTLEFT
7369 cmdmsg_rl = save_cmdmsg_rl;
7370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
7372 State = save_State;
7373# ifdef FEAT_MOUSE
7374 setmouse();
7375# endif
7376
7377 return cmdwin_result;
7378}
7379#endif /* FEAT_CMDWIN */
7380
7381/*
7382 * Used for commands that either take a simple command string argument, or:
7383 * cmd << endmarker
7384 * {script}
7385 * endmarker
7386 * Returns a pointer to allocated memory with {script} or NULL.
7387 */
7388 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007389script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 char_u *theline;
7392 char *end_pattern = NULL;
7393 char dot[] = ".";
7394 garray_T ga;
7395
7396 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7397 return NULL;
7398
7399 ga_init2(&ga, 1, 0x400);
7400
7401 if (cmd[2] != NUL)
7402 end_pattern = (char *)skipwhite(cmd + 2);
7403 else
7404 end_pattern = dot;
7405
7406 for (;;)
7407 {
7408 theline = eap->getline(
7409#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007410 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411#endif
7412 NUL, eap->cookie, 0);
7413
7414 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007415 {
7416 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
7420 ga_concat(&ga, theline);
7421 ga_append(&ga, '\n');
7422 vim_free(theline);
7423 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007424 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
7426 return (char_u *)ga.ga_data;
7427}