blob: a8c5cfb001cb4f5bf7dfd84d043fb0242f1927bd [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
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
144
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200146 static void
147trigger_cmd_autocmd(int typechar, int evt)
148{
149 char_u typestr[2];
150
151 typestr[0] = typechar;
152 typestr[1] = NUL;
153 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
154}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200157 * Abandon the command line.
158 */
159 static void
160abandon_cmdline(void)
161{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100162 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200163 if (msg_scrolled == 0)
164 compute_cmdrow();
165 MSG("");
166 redraw_cmdline = TRUE;
167}
168
Bram Moolenaaree219b02017-12-17 14:55:01 +0100169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200170/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100171 * Guess that the pattern matches everything. Only finds specific cases, such
172 * as a trailing \|, which can happen while typing a pattern.
173 */
174 static int
175empty_pattern(char_u *p)
176{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100177 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100178
179 /* remove trailing \v and the like */
180 while (n >= 2 && p[n - 2] == '\\'
181 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
182 n -= 2;
183 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
184}
185
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200186// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200187typedef struct {
188 colnr_T vs_curswant;
189 colnr_T vs_leftcol;
190 linenr_T vs_topline;
191# ifdef FEAT_DIFF
192 int vs_topfill;
193# endif
194 linenr_T vs_botline;
195 linenr_T vs_empty_rows;
196} viewstate_T;
197
198 static void
199save_viewstate(viewstate_T *vs)
200{
201 vs->vs_curswant = curwin->w_curswant;
202 vs->vs_leftcol = curwin->w_leftcol;
203 vs->vs_topline = curwin->w_topline;
204# ifdef FEAT_DIFF
205 vs->vs_topfill = curwin->w_topfill;
206# endif
207 vs->vs_botline = curwin->w_botline;
208 vs->vs_empty_rows = curwin->w_empty_rows;
209}
210
211 static void
212restore_viewstate(viewstate_T *vs)
213{
214 curwin->w_curswant = vs->vs_curswant;
215 curwin->w_leftcol = vs->vs_leftcol;
216 curwin->w_topline = vs->vs_topline;
217# ifdef FEAT_DIFF
218 curwin->w_topfill = vs->vs_topfill;
219# endif
220 curwin->w_botline = vs->vs_botline;
221 curwin->w_empty_rows = vs->vs_empty_rows;
222}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200223
224// Struct to store the state of 'incsearch' highlighting.
225typedef struct {
226 pos_T search_start; // where 'incsearch' starts searching
227 pos_T save_cursor;
228 viewstate_T init_viewstate;
229 viewstate_T old_viewstate;
230 pos_T match_start;
231 pos_T match_end;
232 int did_incsearch;
233 int incsearch_postponed;
234} incsearch_state_T;
235
236 static void
237init_incsearch_state(incsearch_state_T *is_state)
238{
239 is_state->match_start = curwin->w_cursor;
240 is_state->did_incsearch = FALSE;
241 is_state->incsearch_postponed = FALSE;
242 CLEAR_POS(&is_state->match_end);
243 is_state->save_cursor = curwin->w_cursor; // may be restored later
244 is_state->search_start = curwin->w_cursor;
245 save_viewstate(&is_state->init_viewstate);
246 save_viewstate(&is_state->old_viewstate);
247}
248
249/*
250 * First move cursor to end of match, then to the start. This
251 * moves the whole match onto the screen when 'nowrap' is set.
252 */
253 static void
254set_search_match(pos_T *t)
255{
256 t->lnum += search_match_lines;
257 t->col = search_match_endcol;
258 if (t->lnum > curbuf->b_ml.ml_line_count)
259 {
260 t->lnum = curbuf->b_ml.ml_line_count;
261 coladvance((colnr_T)MAXCOL);
262 }
263}
264
265/*
266 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200267 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200268 */
269 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200270do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
271 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200272{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
276 if (p_is && !cmd_silent)
277 {
278 // by default search all lines
279 search_first_line = 0;
280 search_last_line = MAXLNUM;
281
282 if (firstc == '/' || firstc == '?')
283 return TRUE;
284 if (firstc == ':')
285 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200286 char_u *cmd;
287 cmdmod_T save_cmdmod = cmdmod;
288 char_u *p;
289 int delim;
290 char_u *end;
291 char_u *dummy;
292 exarg_T ea;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200293
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200294 vim_memset(&ea, 0, sizeof(ea));
295 ea.line1 = 1;
296 ea.line2 = 1;
297 ea.cmd = ccline.cmdbuff;
298 ea.addr_type = ADDR_LINES;
299
300 parse_command_modifiers(&ea, &dummy, TRUE);
301 cmdmod = save_cmdmod;
302
303 cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200304 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
305 {
306 // Skip over "substitute" to find the pattern separator.
307 for (p = cmd; ASCII_ISALPHA(*p); ++p)
308 ;
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200309 if (*skipwhite(p) != NUL
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200310 && (STRNCMP(cmd, "substitute", p - cmd) == 0
311 || STRNCMP(cmd, "global", p - cmd) == 0
312 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200313 {
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200314 // Check for "global!/".
315 if (*cmd == 'g' && *p == '!')
316 {
317 p++;
318 if (*skipwhite(p) == NUL)
319 return FALSE;
320 }
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200321 p = skipwhite(p);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200322 delim = *p++;
323 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200324 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200325 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200326 pos_T save_cursor = curwin->w_cursor;
327
328 // found a non-empty pattern
329 *skiplen = (int)(p - ccline.cmdbuff);
330 *patlen = (int)(end - p);
331
332 // parse the address range
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200333 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200334 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200335 if (ea.addr_count > 0)
336 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200337 // Allow for reverse match.
338 if (ea.line2 < ea.line1)
339 {
340 search_first_line = ea.line2;
341 search_last_line = ea.line1;
342 }
343 else
344 {
345 search_first_line = ea.line1;
346 search_last_line = ea.line2;
347 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200348 }
349 else if (*cmd == 's')
350 {
351 // :s defaults to the current line
352 search_first_line = curwin->w_cursor.lnum;
353 search_last_line = curwin->w_cursor.lnum;
354 }
355
356 curwin->w_cursor = save_cursor;
357 return TRUE;
358 }
359 }
360 }
361 }
362 }
363
364 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200365}
366
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200367 static void
368finish_incsearch_highlighting(
369 int gotesc,
370 incsearch_state_T *is_state,
371 int call_update_screen)
372{
373 if (is_state->did_incsearch)
374 {
375 is_state->did_incsearch = FALSE;
376 if (gotesc)
377 curwin->w_cursor = is_state->save_cursor;
378 else
379 {
380 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
381 {
382 // put the '" mark at the original position
383 curwin->w_cursor = is_state->save_cursor;
384 setpcmark();
385 }
386 curwin->w_cursor = is_state->search_start;
387 }
388 restore_viewstate(&is_state->old_viewstate);
389 highlight_match = FALSE;
390 validate_cursor(); /* needed for TAB */
391 if (call_update_screen)
392 update_screen(SOME_VALID);
393 else
394 redraw_all_later(SOME_VALID);
395 }
396}
397
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200398/*
399 * Do 'incsearch' highlighting if desired.
400 */
401 static void
402may_do_incsearch_highlighting(
403 int firstc,
404 long count,
405 incsearch_state_T *is_state)
406{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200407 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200408 int i;
409 pos_T end_pos;
410 struct cmdline_info save_ccline;
411#ifdef FEAT_RELTIME
412 proftime_T tm;
413#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200414 int next_char;
415 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200416
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200417 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200418 {
419 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200420 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200421 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422
423 // If there is a character waiting, search and redraw later.
424 if (char_avail())
425 {
426 is_state->incsearch_postponed = TRUE;
427 return;
428 }
429 is_state->incsearch_postponed = FALSE;
430
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200431 if (search_first_line == 0)
432 // start at the original cursor position
433 curwin->w_cursor = is_state->search_start;
434 else
435 {
436 // start at the first line in the range
437 curwin->w_cursor.lnum = search_first_line;
438 curwin->w_cursor.col = 0;
439 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200440 save_last_search_pattern();
441
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200442 // Use the previous pattern for ":s//".
443 next_char = ccline.cmdbuff[skiplen + patlen];
444 use_last_pat = patlen == 0 && skiplen > 0
445 && ccline.cmdbuff[skiplen - 1] == next_char;
446
447 // If there is no pattern, don't do anything.
448 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200449 {
450 i = 0;
451 set_no_hlsearch(TRUE); // turn off previous highlight
452 redraw_all_later(SOME_VALID);
453 }
454 else
455 {
456 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
457
458 cursor_off(); // so the user knows we're busy
459 out_flush();
460 ++emsg_off; // so it doesn't beep if bad expr
461#ifdef FEAT_RELTIME
462 // Set the time limit to half a second.
463 profile_setlimit(500L, &tm);
464#endif
465 if (!p_hls)
466 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200467 if (search_first_line != 0)
468 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200469 ccline.cmdbuff[skiplen + patlen] = NUL;
470 i = do_search(NULL, firstc == ':' ? '/' : firstc,
471 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472#ifdef FEAT_RELTIME
473 &tm, NULL
474#else
475 NULL, NULL
476#endif
477 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200478 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 --emsg_off;
480
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200481 if (curwin->w_cursor.lnum < search_first_line
482 || curwin->w_cursor.lnum > search_last_line)
483 // match outside of address range
484 i = 0;
485
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200486 // if interrupted while searching, behave like it failed
487 if (got_int)
488 {
489 (void)vpeekc(); // remove <C-C> from input stream
490 got_int = FALSE; // don't abandon the command line
491 i = 0;
492 }
493 else if (char_avail())
494 // cancelled searching because a char was typed
495 is_state->incsearch_postponed = TRUE;
496 }
497 if (i != 0)
498 highlight_match = TRUE; // highlight position
499 else
500 highlight_match = FALSE; // remove highlight
501
502 // First restore the old curwin values, so the screen is positioned in the
503 // same way as the actual search command.
504 restore_viewstate(&is_state->old_viewstate);
505 changed_cline_bef_curs();
506 update_topline();
507
508 if (i != 0)
509 {
510 pos_T save_pos = curwin->w_cursor;
511
512 is_state->match_start = curwin->w_cursor;
513 set_search_match(&curwin->w_cursor);
514 validate_cursor();
515 end_pos = curwin->w_cursor;
516 is_state->match_end = end_pos;
517 curwin->w_cursor = save_pos;
518 }
519 else
520 end_pos = curwin->w_cursor; // shutup gcc 4
521
522 // Disable 'hlsearch' highlighting if the pattern matches everything.
523 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200524 if (!use_last_pat)
525 {
526 next_char = ccline.cmdbuff[skiplen + patlen];
527 ccline.cmdbuff[skiplen + patlen] = NUL;
528 if (empty_pattern(ccline.cmdbuff))
529 set_no_hlsearch(TRUE);
530 ccline.cmdbuff[skiplen + patlen] = next_char;
531 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200532
533 validate_cursor();
534 // May redraw the status line to show the cursor position.
535 if (p_ru && curwin->w_status_height > 0)
536 curwin->w_redr_status = TRUE;
537
538 save_cmdline(&save_ccline);
539 update_screen(SOME_VALID);
540 restore_cmdline(&save_ccline);
541 restore_last_search_pattern();
542
543 // Leave it at the end to make CTRL-R CTRL-W work.
544 if (i != 0)
545 curwin->w_cursor = end_pos;
546
547 msg_starthere();
548 redrawcmdline();
549 is_state->did_incsearch = TRUE;
550}
551
552/*
553 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
554 * or previous match.
555 * Returns FAIL when jumping to cmdline_not_changed;
556 */
557 static int
558may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200559 int firstc,
560 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200563{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200564 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200565 pos_T t;
566 char_u *pat;
567 int search_flags = SEARCH_NOOF;
568 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200569 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200570
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200571 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200572 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200573 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574 return FAIL;
575
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200576 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200577 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200579 skiplen = 0;
580 patlen = STRLEN(pat);
581 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200582 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200583 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200584
585 save_last_search_pattern();
586 cursor_off();
587 out_flush();
588 if (c == Ctrl_G)
589 {
590 t = is_state->match_end;
591 if (LT_POS(is_state->match_start, is_state->match_end))
592 // Start searching at the end of the match not at the beginning of
593 // the next column.
594 (void)decl(&t);
595 search_flags += SEARCH_COL;
596 }
597 else
598 t = is_state->match_start;
599 if (!p_hls)
600 search_flags += SEARCH_KEEP;
601 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200602 save = pat[patlen];
603 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 i = searchit(curwin, curbuf, &t,
605 c == Ctrl_G ? FORWARD : BACKWARD,
606 pat, count, search_flags,
607 RE_SEARCH, 0, NULL, NULL);
608 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200609 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200610 if (i)
611 {
612 is_state->search_start = is_state->match_start;
613 is_state->match_end = t;
614 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616 {
617 // Move just before the current match, so that when nv_search
618 // finishes the cursor will be put back on the match.
619 is_state->search_start = t;
620 (void)decl(&is_state->search_start);
621 }
622 else if (c == Ctrl_G && firstc == '?')
623 {
624 // Move just after the current match, so that when nv_search
625 // finishes the cursor will be put back on the match.
626 is_state->search_start = t;
627 (void)incl(&is_state->search_start);
628 }
629 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
630 {
631 // wrap around
632 is_state->search_start = t;
633 if (firstc == '?')
634 (void)incl(&is_state->search_start);
635 else
636 (void)decl(&is_state->search_start);
637 }
638
639 set_search_match(&is_state->match_end);
640 curwin->w_cursor = is_state->match_start;
641 changed_cline_bef_curs();
642 update_topline();
643 validate_cursor();
644 highlight_match = TRUE;
645 save_viewstate(&is_state->old_viewstate);
646 update_screen(NOT_VALID);
647 redrawcmdline();
648 }
649 else
650 vim_beep(BO_ERROR);
651 restore_last_search_pattern();
652 return FAIL;
653}
654
655/*
656 * When CTRL-L typed: add character from the match to the pattern.
657 * May set "*c" to the added character.
658 * Return OK when jumping to cmdline_not_changed.
659 */
660 static int
661may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
662{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200663 int skiplen, patlen;
664
665 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200666 return FAIL;
667
668 // Add a character from under the cursor for 'incsearch'.
669 if (is_state->did_incsearch)
670 {
671 curwin->w_cursor = is_state->match_end;
672 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
673 {
674 *c = gchar_cursor();
675
676 // If 'ignorecase' and 'smartcase' are set and the
677 // command line has no uppercase characters, convert
678 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200679 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200680 *c = MB_TOLOWER(*c);
681 if (*c != NUL)
682 {
683 if (*c == firstc || vim_strchr((char_u *)(
684 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
685 {
686 // put a backslash before special characters
687 stuffcharReadbuff(*c);
688 *c = '\\';
689 }
690 return FAIL;
691 }
692 }
693 }
694 return OK;
695}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200696#endif
697
Bram Moolenaar66216052017-12-16 16:33:44 +0100698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 * getcmdline() - accept a command line starting with firstc.
700 *
701 * firstc == ':' get ":" command line.
702 * firstc == '/' or '?' get search pattern
703 * firstc == '=' get expression
704 * firstc == '@' get text for input() function
705 * firstc == '>' get text for debug mode
706 * firstc == NUL get text for :insert command
707 * firstc == -1 like NUL, and break on CTRL-C
708 *
709 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
710 * command line.
711 *
712 * Careful: getcmdline() can be called recursively!
713 *
714 * Return pointer to allocated string if there is a commandline, NULL
715 * otherwise.
716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100718getcmdline(
719 int firstc,
720 long count UNUSED, /* only used for incremental search */
721 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 int c;
724 int i;
725 int j;
726 int gotesc = FALSE; /* TRUE when <ESC> just typed */
727 int do_abbr; /* when TRUE check for abbr. */
728#ifdef FEAT_CMDHIST
729 char_u *lookfor = NULL; /* string to match */
730 int hiscnt; /* current history line in use */
731 int histype; /* history type to be used */
732#endif
733#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200734 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#endif
736 int did_wild_list = FALSE; /* did wild_list() recently */
737 int wim_index = 0; /* index in wim_flags[] */
738 int res;
739 int save_msg_scroll = msg_scroll;
740 int save_State = State; /* remember State when called */
741 int some_key_typed = FALSE; /* one of the keys was typed */
742#ifdef FEAT_MOUSE
743 /* mouse drag and release events are ignored, unless they are
744 * preceded with a mouse down event */
745 int ignore_drag_release = TRUE;
746#endif
747#ifdef FEAT_EVAL
748 int break_ctrl_c = FALSE;
749#endif
750 expand_T xpc;
751 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200752#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000753 /* Everything that may work recursively should save and restore the
754 * current command line in save_ccline. That includes update_screen(), a
755 * custom status line may invoke ":normal". */
756 struct cmdline_info save_ccline;
757#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200758 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#ifdef FEAT_EVAL
761 if (firstc == -1)
762 {
763 firstc = NUL;
764 break_ctrl_c = TRUE;
765 }
766#endif
767#ifdef FEAT_RIGHTLEFT
768 /* start without Hebrew mapping for a command line */
769 if (firstc == ':' || firstc == '=' || firstc == '>')
770 cmd_hkmap = 0;
771#endif
772
773 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200776 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778
779 /*
780 * set some variables for redrawcmd()
781 */
782 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000783 ccline.cmdindent = (firstc > 0 ? indent : 0);
784
785 /* alloc initial ccline.cmdbuff */
786 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (ccline.cmdbuff == NULL)
788 return NULL; /* out of memory */
789 ccline.cmdlen = ccline.cmdpos = 0;
790 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100791 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000793 /* autoindent for :insert and :append */
794 if (firstc <= 0)
795 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200796 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000797 ccline.cmdbuff[indent] = NUL;
798 ccline.cmdpos = indent;
799 ccline.cmdspos = indent;
800 ccline.cmdlen = indent;
801 }
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000804 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
806#ifdef FEAT_RIGHTLEFT
807 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
808 && (firstc == '/' || firstc == '?'))
809 cmdmsg_rl = TRUE;
810 else
811 cmdmsg_rl = FALSE;
812#endif
813
814 redir_off = TRUE; /* don't redirect the typed command */
815 if (!cmd_silent)
816 {
817 i = msg_scrolled;
818 msg_scrolled = 0; /* avoid wait_return message */
819 gotocmdline(TRUE);
820 msg_scrolled += i;
821 redrawcmdprompt(); /* draw prompt or indent */
822 set_cmdspos();
823 }
824 xpc.xp_context = EXPAND_NOTHING;
825 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000826#ifndef BACKSLASH_IN_FILENAME
827 xpc.xp_shell = FALSE;
828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000830#if defined(FEAT_EVAL)
831 if (ccline.input_fn)
832 {
833 xpc.xp_context = ccline.xp_context;
834 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000835# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000836 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000837# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000838 }
839#endif
840
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 /*
842 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
843 * doing ":@0" when register 0 doesn't contain a CR.
844 */
845 msg_scroll = FALSE;
846
847 State = CMDLINE;
848
849 if (firstc == '/' || firstc == '?' || firstc == '@')
850 {
851 /* Use ":lmap" mappings for search pattern and input(). */
852 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
853 b_im_ptr = &curbuf->b_p_iminsert;
854 else
855 b_im_ptr = &curbuf->b_p_imsearch;
856 if (*b_im_ptr == B_IMODE_LMAP)
857 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100858#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 im_set_active(*b_im_ptr == B_IMODE_IM);
860#endif
861 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100862#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 else if (p_imcmdline)
864 im_set_active(TRUE);
865#endif
866
867#ifdef FEAT_MOUSE
868 setmouse();
869#endif
870#ifdef CURSOR_SHAPE
871 ui_cursor_shape(); /* may show different cursor shape */
872#endif
873
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000874 /* When inside an autocommand for writing "exiting" may be set and
875 * terminal mode set to cooked. Need to set raw mode here then. */
876 settmode(TMODE_RAW);
877
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200878 /* Trigger CmdlineEnter autocommands. */
879 cmdline_type = firstc == NUL ? '-' : firstc;
880 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#ifdef FEAT_CMDHIST
883 init_history();
884 hiscnt = hislen; /* set hiscnt to impossible history value */
885 histype = hist_char2type(firstc);
886#endif
887
888#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000889 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890#endif
891
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200892 /* If something above caused an error, reset the flags, we do want to type
893 * and execute commands. Display may be messed up a bit. */
894 if (did_emsg)
895 redrawcmd();
896 did_emsg = FALSE;
897 got_int = FALSE;
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 /*
900 * Collect the command string, handling editing keys.
901 */
902 for (;;)
903 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000904 redir_off = TRUE; /* Don't redirect the typed command.
905 Repeated, because a ":redir" inside
906 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#ifdef USE_ON_FLY_SCROLL
908 dont_scroll = FALSE; /* allow scrolling here */
909#endif
910 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
911
Bram Moolenaar72532d32018-04-07 19:09:09 +0200912 did_emsg = FALSE; /* There can't really be a reason why an error
913 that occurs while typing a command should
914 cause the command not to be executed. */
915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000917
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100918 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
919 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000920 do
921 {
922 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100923 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000924
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (KeyTyped)
926 {
927 some_key_typed = TRUE;
928#ifdef FEAT_RIGHTLEFT
929 if (cmd_hkmap)
930 c = hkmap(c);
931# ifdef FEAT_FKMAP
932 if (cmd_fkmap)
933 c = cmdl_fkmap(c);
934# endif
935 if (cmdmsg_rl && !KeyStuffed)
936 {
937 /* Invert horizontal movements and operations. Only when
938 * typed by the user directly, not when the result of a
939 * mapping. */
940 switch (c)
941 {
942 case K_RIGHT: c = K_LEFT; break;
943 case K_S_RIGHT: c = K_S_LEFT; break;
944 case K_C_RIGHT: c = K_C_LEFT; break;
945 case K_LEFT: c = K_RIGHT; break;
946 case K_S_LEFT: c = K_S_RIGHT; break;
947 case K_C_LEFT: c = K_C_RIGHT; break;
948 }
949 }
950#endif
951 }
952
953 /*
954 * Ignore got_int when CTRL-C was typed here.
955 * Don't ignore it in :global, we really need to break then, e.g., for
956 * ":g/pat/normal /pat" (without the <CR>).
957 * Don't ignore it for the input() function.
958 */
959 if ((c == Ctrl_C
960#ifdef UNIX
961 || c == intr_char
962#endif
963 )
964#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
965 && firstc != '@'
966#endif
967#ifdef FEAT_EVAL
968 && !break_ctrl_c
969#endif
970 && !global_busy)
971 got_int = FALSE;
972
973#ifdef FEAT_CMDHIST
974 /* free old command line when finished moving around in the history
975 * list */
976 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000977 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 && c != K_PAGEDOWN && c != K_PAGEUP
980 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000981 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100983 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#endif
985
986 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000987 * When there are matching completions to select <S-Tab> works like
988 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000990 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 c = Ctrl_P;
992
993#ifdef FEAT_WILDMENU
994 /* Special translations for 'wildmenu' */
995 if (did_wild_list && p_wmnu)
996 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000997 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000999 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 c = Ctrl_N;
1001 }
1002 /* Hitting CR after "emenu Name.": complete submenu */
1003 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1004 && ccline.cmdpos > 1
1005 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1006 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1007 && (c == '\n' || c == '\r' || c == K_KENTER))
1008 c = K_DOWN;
1009#endif
1010
1011 /* free expanded names when finished walking through matches */
1012 if (xpc.xp_numfiles != -1
1013 && !(c == p_wc && KeyTyped) && c != p_wcm
1014 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1015 && c != Ctrl_L)
1016 {
1017 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1018 did_wild_list = FALSE;
1019#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001020 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#endif
1022 xpc.xp_context = EXPAND_NOTHING;
1023 wim_index = 0;
1024#ifdef FEAT_WILDMENU
1025 if (p_wmnu && wild_menu_showing != 0)
1026 {
1027 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001028 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001029
1030 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001031 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 if (wild_menu_showing == WM_SCROLLED)
1034 {
1035 /* Entered command line, move it up */
1036 cmdline_row--;
1037 redrawcmd();
1038 }
1039 else if (save_p_ls != -1)
1040 {
1041 /* restore 'laststatus' and 'winminheight' */
1042 p_ls = save_p_ls;
1043 p_wmh = save_p_wmh;
1044 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001045 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001047 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 redrawcmd();
1049 save_p_ls = -1;
1050 }
1051 else
1052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 redraw_statuslines();
1055 }
1056 KeyTyped = skt;
1057 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001058 if (ccline.input_fn)
1059 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
1061#endif
1062 }
1063
1064#ifdef FEAT_WILDMENU
1065 /* Special translations for 'wildmenu' */
1066 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1067 {
1068 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001069 if (c == K_DOWN && ccline.cmdpos > 0
1070 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001072 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
1074 /* Hitting <Up>: Remove one submenu name in front of the
1075 * cursor */
1076 int found = FALSE;
1077
1078 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1079 i = 0;
1080 while (--j > 0)
1081 {
1082 /* check for start of menu name */
1083 if (ccline.cmdbuff[j] == ' '
1084 && ccline.cmdbuff[j - 1] != '\\')
1085 {
1086 i = j + 1;
1087 break;
1088 }
1089 /* check for start of submenu name */
1090 if (ccline.cmdbuff[j] == '.'
1091 && ccline.cmdbuff[j - 1] != '\\')
1092 {
1093 if (found)
1094 {
1095 i = j + 1;
1096 break;
1097 }
1098 else
1099 found = TRUE;
1100 }
1101 }
1102 if (i > 0)
1103 cmdline_del(i);
1104 c = p_wc;
1105 xpc.xp_context = EXPAND_NOTHING;
1106 }
1107 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001108 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001109 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001110 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
1112 char_u upseg[5];
1113
1114 upseg[0] = PATHSEP;
1115 upseg[1] = '.';
1116 upseg[2] = '.';
1117 upseg[3] = PATHSEP;
1118 upseg[4] = NUL;
1119
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001120 if (c == K_DOWN
1121 && ccline.cmdpos > 0
1122 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1123 && (ccline.cmdpos < 3
1124 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1126 {
1127 /* go down a directory */
1128 c = p_wc;
1129 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001130 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
1132 /* If in a direct ancestor, strip off one ../ to go down */
1133 int found = FALSE;
1134
1135 j = ccline.cmdpos;
1136 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1137 while (--j > i)
1138 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001139#ifdef FEAT_MBYTE
1140 if (has_mbyte)
1141 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (vim_ispathsep(ccline.cmdbuff[j]))
1144 {
1145 found = TRUE;
1146 break;
1147 }
1148 }
1149 if (found
1150 && ccline.cmdbuff[j - 1] == '.'
1151 && ccline.cmdbuff[j - 2] == '.'
1152 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1153 {
1154 cmdline_del(j - 2);
1155 c = p_wc;
1156 }
1157 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001158 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 /* go up a directory */
1161 int found = FALSE;
1162
1163 j = ccline.cmdpos - 1;
1164 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1165 while (--j > i)
1166 {
1167#ifdef FEAT_MBYTE
1168 if (has_mbyte)
1169 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1170#endif
1171 if (vim_ispathsep(ccline.cmdbuff[j])
1172#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001173 && vim_strchr((char_u *)" *?[{`$%#",
1174 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#endif
1176 )
1177 {
1178 if (found)
1179 {
1180 i = j + 1;
1181 break;
1182 }
1183 else
1184 found = TRUE;
1185 }
1186 }
1187
1188 if (!found)
1189 j = i;
1190 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1191 j += 4;
1192 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1193 && j == i)
1194 j += 3;
1195 else
1196 j = 0;
1197 if (j > 0)
1198 {
1199 /* TODO this is only for DOS/UNIX systems - need to put in
1200 * machine-specific stuff here and in upseg init */
1201 cmdline_del(j);
1202 put_on_cmdline(upseg + 1, 3, FALSE);
1203 }
1204 else if (ccline.cmdpos > i)
1205 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001206
1207 /* Now complete in the new directory. Set KeyTyped in case the
1208 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001210 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214#endif /* FEAT_WILDMENU */
1215
1216 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1217 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1218 if (c == Ctrl_BSL)
1219 {
1220 ++no_mapping;
1221 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001222 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 --no_mapping;
1224 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001225 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1226 * is in a mapping. */
1227 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1228 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
1230 vungetc(c);
1231 c = Ctrl_BSL;
1232 }
1233#ifdef FEAT_EVAL
1234 else if (c == 'e')
1235 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001236 char_u *p = NULL;
1237 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
1239 /*
1240 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001241 * Need to save and restore the current command line, to be
1242 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 */
1244 if (ccline.cmdpos == ccline.cmdlen)
1245 new_cmdpos = 99999; /* keep it at the end */
1246 else
1247 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001248
1249 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001251 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (c == '=')
1253 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001254 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001255 * to avoid nasty things like going to another buffer when
1256 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001257 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001258 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001260 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001261 restore_cmdline(&save_ccline);
1262
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001263 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001265 len = (int)STRLEN(p);
1266 if (realloc_cmdbuff(len + 1) == OK)
1267 {
1268 ccline.cmdlen = len;
1269 STRCPY(ccline.cmdbuff, p);
1270 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001272 /* Restore the cursor or use the position set with
1273 * set_cmdline_pos(). */
1274 if (new_cmdpos > ccline.cmdlen)
1275 ccline.cmdpos = ccline.cmdlen;
1276 else
1277 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001279 KeyTyped = FALSE; /* Don't do p_wc completion. */
1280 redrawcmd();
1281 goto cmdline_changed;
1282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 }
1285 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001286 got_int = FALSE; /* don't abandon the command line */
1287 did_emsg = FALSE;
1288 emsg_on_display = FALSE;
1289 redrawcmd();
1290 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292#endif
1293 else
1294 {
1295 if (c == Ctrl_G && p_im && restart_edit == 0)
1296 restart_edit = 'a';
1297 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1298 in history */
1299 goto returncmd; /* back to Normal mode */
1300 }
1301 }
1302
1303#ifdef FEAT_CMDWIN
1304 if (c == cedit_key || c == K_CMDWIN)
1305 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001306 if (ex_normal_busy == 0 && got_int == FALSE)
1307 {
1308 /*
1309 * Open a window to edit the command line (and history).
1310 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001311 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001312 some_key_typed = TRUE;
1313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315# ifdef FEAT_DIGRAPHS
1316 else
1317# endif
1318#endif
1319#ifdef FEAT_DIGRAPHS
1320 c = do_digraph(c);
1321#endif
1322
1323 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1324 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1325 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001326 /* In Ex mode a backslash escapes a newline. */
1327 if (exmode_active
1328 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001329 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001330 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001331 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001333 if (c == K_KENTER)
1334 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001336 else
1337 {
1338 gotesc = FALSE; /* Might have typed ESC previously, don't
1339 truncate the cmdline now. */
1340 if (ccheck_abbr(c + ABBR_OFF))
1341 goto cmdline_changed;
1342 if (!cmd_silent)
1343 {
1344 windgoto(msg_row, 0);
1345 out_flush();
1346 }
1347 break;
1348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350
1351 /*
1352 * Completion for 'wildchar' or 'wildcharm' key.
1353 * - hitting <ESC> twice means: abandon command line.
1354 * - wildcard expansion is only done when the 'wildchar' key is really
1355 * typed, not when it comes from a macro
1356 */
1357 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1358 {
1359 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1360 {
1361 /* if 'wildmode' contains "list" may still need to list */
1362 if (xpc.xp_numfiles > 1
1363 && !did_wild_list
1364 && (wim_flags[wim_index] & WIM_LIST))
1365 {
1366 (void)showmatches(&xpc, FALSE);
1367 redrawcmd();
1368 did_wild_list = TRUE;
1369 }
1370 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001371 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1372 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001374 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1375 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 else
1377 res = OK; /* don't insert 'wildchar' now */
1378 }
1379 else /* typed p_wc first time */
1380 {
1381 wim_index = 0;
1382 j = ccline.cmdpos;
1383 /* if 'wildmode' first contains "longest", get longest
1384 * common part */
1385 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001386 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1387 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001389 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1390 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 /* if interrupted while completing, behave like it failed */
1393 if (got_int)
1394 {
1395 (void)vpeekc(); /* remove <C-C> from input stream */
1396 got_int = FALSE; /* don't abandon the command line */
1397 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1398#ifdef FEAT_WILDMENU
1399 xpc.xp_context = EXPAND_NOTHING;
1400#endif
1401 goto cmdline_changed;
1402 }
1403
1404 /* when more than one match, and 'wildmode' first contains
1405 * "list", or no change and 'wildmode' contains "longest,list",
1406 * list all matches */
1407 if (res == OK && xpc.xp_numfiles > 1)
1408 {
1409 /* a "longest" that didn't do anything is skipped (but not
1410 * "list:longest") */
1411 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1412 wim_index = 1;
1413 if ((wim_flags[wim_index] & WIM_LIST)
1414#ifdef FEAT_WILDMENU
1415 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1416#endif
1417 )
1418 {
1419 if (!(wim_flags[0] & WIM_LONGEST))
1420 {
1421#ifdef FEAT_WILDMENU
1422 int p_wmnu_save = p_wmnu;
1423 p_wmnu = 0;
1424#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001425 /* remove match */
1426 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#ifdef FEAT_WILDMENU
1428 p_wmnu = p_wmnu_save;
1429#endif
1430 }
1431#ifdef FEAT_WILDMENU
1432 (void)showmatches(&xpc, p_wmnu
1433 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1434#else
1435 (void)showmatches(&xpc, FALSE);
1436#endif
1437 redrawcmd();
1438 did_wild_list = TRUE;
1439 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001440 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1441 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001443 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1444 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001447 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449#ifdef FEAT_WILDMENU
1450 else if (xpc.xp_numfiles == -1)
1451 xpc.xp_context = EXPAND_NOTHING;
1452#endif
1453 }
1454 if (wim_index < 3)
1455 ++wim_index;
1456 if (c == ESC)
1457 gotesc = TRUE;
1458 if (res == OK)
1459 goto cmdline_changed;
1460 }
1461
1462 gotesc = FALSE;
1463
1464 /* <S-Tab> goes to last match, in a clumsy way */
1465 if (c == K_S_TAB && KeyTyped)
1466 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001467 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1468 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1469 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 goto cmdline_changed;
1471 }
1472
1473 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1474 c = NL;
1475
1476 do_abbr = TRUE; /* default: check for abbreviation */
1477
1478 /*
1479 * Big switch for a typed command line character.
1480 */
1481 switch (c)
1482 {
1483 case K_BS:
1484 case Ctrl_H:
1485 case K_DEL:
1486 case K_KDEL:
1487 case Ctrl_W:
1488#ifdef FEAT_FKMAP
1489 if (cmd_fkmap && c == K_BS)
1490 c = K_DEL;
1491#endif
1492 if (c == K_KDEL)
1493 c = K_DEL;
1494
1495 /*
1496 * delete current character is the same as backspace on next
1497 * character, except at end of line
1498 */
1499 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1500 ++ccline.cmdpos;
1501#ifdef FEAT_MBYTE
1502 if (has_mbyte && c == K_DEL)
1503 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1504 ccline.cmdbuff + ccline.cmdpos);
1505#endif
1506 if (ccline.cmdpos > 0)
1507 {
1508 char_u *p;
1509
1510 j = ccline.cmdpos;
1511 p = ccline.cmdbuff + j;
1512#ifdef FEAT_MBYTE
1513 if (has_mbyte)
1514 {
1515 p = mb_prevptr(ccline.cmdbuff, p);
1516 if (c == Ctrl_W)
1517 {
1518 while (p > ccline.cmdbuff && vim_isspace(*p))
1519 p = mb_prevptr(ccline.cmdbuff, p);
1520 i = mb_get_class(p);
1521 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1522 p = mb_prevptr(ccline.cmdbuff, p);
1523 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001524 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526 }
1527 else
1528#endif
1529 if (c == Ctrl_W)
1530 {
1531 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1532 --p;
1533 i = vim_iswordc(p[-1]);
1534 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1535 && vim_iswordc(p[-1]) == i)
1536 --p;
1537 }
1538 else
1539 --p;
1540 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1541 ccline.cmdlen -= j - ccline.cmdpos;
1542 i = ccline.cmdpos;
1543 while (i < ccline.cmdlen)
1544 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1545
1546 /* Truncate at the end, required for multi-byte chars. */
1547 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001548#ifdef FEAT_SEARCH_EXTRA
1549 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001550 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001551 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001552 /* save view settings, so that the screen
1553 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001554 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001555 }
1556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 redrawcmd();
1558 }
1559 else if (ccline.cmdlen == 0 && c != Ctrl_W
1560 && ccline.cmdprompt == NULL && indent == 0)
1561 {
1562 /* In ex and debug mode it doesn't make sense to return. */
1563 if (exmode_active
1564#ifdef FEAT_EVAL
1565 || ccline.cmdfirstc == '>'
1566#endif
1567 )
1568 goto cmdline_not_changed;
1569
Bram Moolenaard23a8232018-02-10 18:45:26 +01001570 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (!cmd_silent)
1572 {
1573#ifdef FEAT_RIGHTLEFT
1574 if (cmdmsg_rl)
1575 msg_col = Columns;
1576 else
1577#endif
1578 msg_col = 0;
1579 msg_putchar(' '); /* delete ':' */
1580 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001581#ifdef FEAT_SEARCH_EXTRA
1582 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001583 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 redraw_cmdline = TRUE;
1586 goto returncmd; /* back to cmd mode */
1587 }
1588 goto cmdline_changed;
1589
1590 case K_INS:
1591 case K_KINS:
1592#ifdef FEAT_FKMAP
1593 /* if Farsi mode set, we are in reverse insert mode -
1594 Do not change the mode */
1595 if (cmd_fkmap)
1596 beep_flush();
1597 else
1598#endif
1599 ccline.overstrike = !ccline.overstrike;
1600#ifdef CURSOR_SHAPE
1601 ui_cursor_shape(); /* may show different cursor shape */
1602#endif
1603 goto cmdline_not_changed;
1604
1605 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001606 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 /* ":lmap" mappings exists, toggle use of mappings. */
1609 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001610#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 im_set_active(FALSE); /* Disable input method */
1612#endif
1613 if (b_im_ptr != NULL)
1614 {
1615 if (State & LANGMAP)
1616 *b_im_ptr = B_IMODE_LMAP;
1617 else
1618 *b_im_ptr = B_IMODE_NONE;
1619 }
1620 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001621#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 else
1623 {
1624 /* There are no ":lmap" mappings, toggle IM. When
1625 * 'imdisable' is set don't try getting the status, it's
1626 * always off. */
1627 if ((p_imdisable && b_im_ptr != NULL)
1628 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1629 {
1630 im_set_active(FALSE); /* Disable input method */
1631 if (b_im_ptr != NULL)
1632 *b_im_ptr = B_IMODE_NONE;
1633 }
1634 else
1635 {
1636 im_set_active(TRUE); /* Enable input method */
1637 if (b_im_ptr != NULL)
1638 *b_im_ptr = B_IMODE_IM;
1639 }
1640 }
1641#endif
1642 if (b_im_ptr != NULL)
1643 {
1644 if (b_im_ptr == &curbuf->b_p_iminsert)
1645 set_iminsert_global();
1646 else
1647 set_imsearch_global();
1648 }
1649#ifdef CURSOR_SHAPE
1650 ui_cursor_shape(); /* may show different cursor shape */
1651#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001652#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 /* Show/unshow value of 'keymap' in status lines later. */
1654 status_redraw_curbuf();
1655#endif
1656 goto cmdline_not_changed;
1657
1658/* case '@': only in very old vi */
1659 case Ctrl_U:
1660 /* delete all characters left of the cursor */
1661 j = ccline.cmdpos;
1662 ccline.cmdlen -= j;
1663 i = ccline.cmdpos = 0;
1664 while (i < ccline.cmdlen)
1665 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1666 /* Truncate at the end, required for multi-byte chars. */
1667 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001668#ifdef FEAT_SEARCH_EXTRA
1669 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001670 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 redrawcmd();
1673 goto cmdline_changed;
1674
1675#ifdef FEAT_CLIPBOARD
1676 case Ctrl_Y:
1677 /* Copy the modeless selection, if there is one. */
1678 if (clip_star.state != SELECT_CLEARED)
1679 {
1680 if (clip_star.state == SELECT_DONE)
1681 clip_copy_modeless_selection(TRUE);
1682 goto cmdline_not_changed;
1683 }
1684 break;
1685#endif
1686
1687 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1688 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001689 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001690 * ":normal" runs out of characters. */
1691 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001692 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 goto cmdline_not_changed;
1694
1695 gotesc = TRUE; /* will free ccline.cmdbuff after
1696 putting it in history */
1697 goto returncmd; /* back to cmd mode */
1698
1699 case Ctrl_R: /* insert register */
1700#ifdef USE_ON_FLY_SCROLL
1701 dont_scroll = TRUE; /* disallow scrolling here */
1702#endif
1703 putcmdline('"', TRUE);
1704 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001705 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (i == Ctrl_O)
1707 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1708 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001709 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001710 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 --no_mapping;
1712#ifdef FEAT_EVAL
1713 /*
1714 * Insert the result of an expression.
1715 * Need to save the current command line, to be able to enter
1716 * a new one...
1717 */
1718 new_cmdpos = -1;
1719 if (c == '=')
1720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1722 {
1723 beep_flush();
1724 c = ESC;
1725 }
1726 else
1727 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001728 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001730 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
1732 }
1733#endif
1734 if (c != ESC) /* use ESC to cancel inserting register */
1735 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001736 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001737
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001738#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001739 /* When there was a serious error abort getting the
1740 * command line. */
1741 if (aborting())
1742 {
1743 gotesc = TRUE; /* will free ccline.cmdbuff after
1744 putting it in history */
1745 goto returncmd; /* back to cmd mode */
1746 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 KeyTyped = FALSE; /* Don't do p_wc completion. */
1749#ifdef FEAT_EVAL
1750 if (new_cmdpos >= 0)
1751 {
1752 /* set_cmdline_pos() was used */
1753 if (new_cmdpos > ccline.cmdlen)
1754 ccline.cmdpos = ccline.cmdlen;
1755 else
1756 ccline.cmdpos = new_cmdpos;
1757 }
1758#endif
1759 }
1760 redrawcmd();
1761 goto cmdline_changed;
1762
1763 case Ctrl_D:
1764 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1765 break; /* Use ^D as normal char instead */
1766
1767 redrawcmd();
1768 continue; /* don't do incremental search now */
1769
1770 case K_RIGHT:
1771 case K_S_RIGHT:
1772 case K_C_RIGHT:
1773 do
1774 {
1775 if (ccline.cmdpos >= ccline.cmdlen)
1776 break;
1777 i = cmdline_charsize(ccline.cmdpos);
1778 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1779 break;
1780 ccline.cmdspos += i;
1781#ifdef FEAT_MBYTE
1782 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001783 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 + ccline.cmdpos);
1785 else
1786#endif
1787 ++ccline.cmdpos;
1788 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001789 while ((c == K_S_RIGHT || c == K_C_RIGHT
1790 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1792#ifdef FEAT_MBYTE
1793 if (has_mbyte)
1794 set_cmdspos_cursor();
1795#endif
1796 goto cmdline_not_changed;
1797
1798 case K_LEFT:
1799 case K_S_LEFT:
1800 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001801 if (ccline.cmdpos == 0)
1802 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 do
1804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 --ccline.cmdpos;
1806#ifdef FEAT_MBYTE
1807 if (has_mbyte) /* move to first byte of char */
1808 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1809 ccline.cmdbuff + ccline.cmdpos);
1810#endif
1811 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1812 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001813 while (ccline.cmdpos > 0
1814 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001815 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1817#ifdef FEAT_MBYTE
1818 if (has_mbyte)
1819 set_cmdspos_cursor();
1820#endif
1821 goto cmdline_not_changed;
1822
1823 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001824 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001825 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaar4770d092006-01-12 23:22:24 +00001827#ifdef FEAT_GUI_W32
1828 /* On Win32 ignore <M-F4>, we get it when closing the window was
1829 * cancelled. */
1830 case K_F4:
1831 if (mod_mask == MOD_MASK_ALT)
1832 {
1833 redrawcmd(); /* somehow the cmdline is cleared */
1834 goto cmdline_not_changed;
1835 }
1836 break;
1837#endif
1838
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#ifdef FEAT_MOUSE
1840 case K_MIDDLEDRAG:
1841 case K_MIDDLERELEASE:
1842 goto cmdline_not_changed; /* Ignore mouse */
1843
1844 case K_MIDDLEMOUSE:
1845# ifdef FEAT_GUI
1846 /* When GUI is active, also paste when 'mouse' is empty */
1847 if (!gui.in_use)
1848# endif
1849 if (!mouse_has(MOUSE_COMMAND))
1850 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001851# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001853 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001855# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001856 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 redrawcmd();
1858 goto cmdline_changed;
1859
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001860# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001862 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 redrawcmd();
1864 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001865# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
1867 case K_LEFTDRAG:
1868 case K_LEFTRELEASE:
1869 case K_RIGHTDRAG:
1870 case K_RIGHTRELEASE:
1871 /* Ignore drag and release events when the button-down wasn't
1872 * seen before. */
1873 if (ignore_drag_release)
1874 goto cmdline_not_changed;
1875 /* FALLTHROUGH */
1876 case K_LEFTMOUSE:
1877 case K_RIGHTMOUSE:
1878 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1879 ignore_drag_release = TRUE;
1880 else
1881 ignore_drag_release = FALSE;
1882# ifdef FEAT_GUI
1883 /* When GUI is active, also move when 'mouse' is empty */
1884 if (!gui.in_use)
1885# endif
1886 if (!mouse_has(MOUSE_COMMAND))
1887 goto cmdline_not_changed; /* Ignore mouse */
1888# ifdef FEAT_CLIPBOARD
1889 if (mouse_row < cmdline_row && clip_star.available)
1890 {
1891 int button, is_click, is_drag;
1892
1893 /*
1894 * Handle modeless selection.
1895 */
1896 button = get_mouse_button(KEY2TERMCAP1(c),
1897 &is_click, &is_drag);
1898 if (mouse_model_popup() && button == MOUSE_LEFT
1899 && (mod_mask & MOD_MASK_SHIFT))
1900 {
1901 /* Translate shift-left to right button. */
1902 button = MOUSE_RIGHT;
1903 mod_mask &= ~MOD_MASK_SHIFT;
1904 }
1905 clip_modeless(button, is_click, is_drag);
1906 goto cmdline_not_changed;
1907 }
1908# endif
1909
1910 set_cmdspos();
1911 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1912 ++ccline.cmdpos)
1913 {
1914 i = cmdline_charsize(ccline.cmdpos);
1915 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1916 && mouse_col < ccline.cmdspos % Columns + i)
1917 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001918# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (has_mbyte)
1920 {
1921 /* Count ">" for double-wide char that doesn't fit. */
1922 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001923 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 + ccline.cmdpos) - 1;
1925 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 ccline.cmdspos += i;
1928 }
1929 goto cmdline_not_changed;
1930
1931 /* Mouse scroll wheel: ignored here */
1932 case K_MOUSEDOWN:
1933 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001934 case K_MOUSELEFT:
1935 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 /* Alternate buttons ignored here */
1937 case K_X1MOUSE:
1938 case K_X1DRAG:
1939 case K_X1RELEASE:
1940 case K_X2MOUSE:
1941 case K_X2DRAG:
1942 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001943 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 goto cmdline_not_changed;
1945
1946#endif /* FEAT_MOUSE */
1947
1948#ifdef FEAT_GUI
1949 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1950 case K_LEFTRELEASE_NM:
1951 goto cmdline_not_changed;
1952
1953 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001954 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 gui_do_scroll();
1957 redrawcmd();
1958 }
1959 goto cmdline_not_changed;
1960
1961 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001962 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001964 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 redrawcmd();
1966 }
1967 goto cmdline_not_changed;
1968#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001969#ifdef FEAT_GUI_TABLINE
1970 case K_TABLINE:
1971 case K_TABMENU:
1972 /* Don't want to change any tabs here. Make sure the same tab
1973 * is still selected. */
1974 if (gui_use_tabline())
1975 gui_mch_set_curtab(tabpage_index(curtab));
1976 goto cmdline_not_changed;
1977#endif
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 case K_SELECT: /* end of Select mode mapping - ignore */
1980 goto cmdline_not_changed;
1981
1982 case Ctrl_B: /* begin of command line */
1983 case K_HOME:
1984 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 case K_S_HOME:
1986 case K_C_HOME:
1987 ccline.cmdpos = 0;
1988 set_cmdspos();
1989 goto cmdline_not_changed;
1990
1991 case Ctrl_E: /* end of command line */
1992 case K_END:
1993 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 case K_S_END:
1995 case K_C_END:
1996 ccline.cmdpos = ccline.cmdlen;
1997 set_cmdspos_cursor();
1998 goto cmdline_not_changed;
1999
2000 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002001 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 break;
2003 goto cmdline_changed;
2004
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002005 case Ctrl_L:
2006#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002007 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002008 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002009#endif
2010
2011 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002012 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 break;
2014 goto cmdline_changed;
2015
2016 case Ctrl_N: /* next match */
2017 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002018 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002020 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2021 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002023 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002026 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 case K_UP:
2028 case K_DOWN:
2029 case K_S_UP:
2030 case K_S_DOWN:
2031 case K_PAGEUP:
2032 case K_KPAGEUP:
2033 case K_PAGEDOWN:
2034 case K_KPAGEDOWN:
2035 if (hislen == 0 || firstc == NUL) /* no history */
2036 goto cmdline_not_changed;
2037
2038 i = hiscnt;
2039
2040 /* save current command string so it can be restored later */
2041 if (lookfor == NULL)
2042 {
2043 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2044 goto cmdline_not_changed;
2045 lookfor[ccline.cmdpos] = NUL;
2046 }
2047
2048 j = (int)STRLEN(lookfor);
2049 for (;;)
2050 {
2051 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002052 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002053 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 if (hiscnt == hislen) /* first time */
2056 hiscnt = hisidx[histype];
2057 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2058 hiscnt = hislen - 1;
2059 else if (hiscnt != hisidx[histype] + 1)
2060 --hiscnt;
2061 else /* at top of list */
2062 {
2063 hiscnt = i;
2064 break;
2065 }
2066 }
2067 else /* one step forwards */
2068 {
2069 /* on last entry, clear the line */
2070 if (hiscnt == hisidx[histype])
2071 {
2072 hiscnt = hislen;
2073 break;
2074 }
2075
2076 /* not on a history line, nothing to do */
2077 if (hiscnt == hislen)
2078 break;
2079 if (hiscnt == hislen - 1) /* wrap around */
2080 hiscnt = 0;
2081 else
2082 ++hiscnt;
2083 }
2084 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2085 {
2086 hiscnt = i;
2087 break;
2088 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002089 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002090 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 || STRNCMP(history[histype][hiscnt].hisstr,
2092 lookfor, (size_t)j) == 0)
2093 break;
2094 }
2095
2096 if (hiscnt != i) /* jumped to other entry */
2097 {
2098 char_u *p;
2099 int len;
2100 int old_firstc;
2101
2102 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002103 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if (hiscnt == hislen)
2105 p = lookfor; /* back to the old one */
2106 else
2107 p = history[histype][hiscnt].hisstr;
2108
2109 if (histype == HIST_SEARCH
2110 && p != lookfor
2111 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2112 {
2113 /* Correct for the separator character used when
2114 * adding the history entry vs the one used now.
2115 * First loop: count length.
2116 * Second loop: copy the characters. */
2117 for (i = 0; i <= 1; ++i)
2118 {
2119 len = 0;
2120 for (j = 0; p[j] != NUL; ++j)
2121 {
2122 /* Replace old sep with new sep, unless it is
2123 * escaped. */
2124 if (p[j] == old_firstc
2125 && (j == 0 || p[j - 1] != '\\'))
2126 {
2127 if (i > 0)
2128 ccline.cmdbuff[len] = firstc;
2129 }
2130 else
2131 {
2132 /* Escape new sep, unless it is already
2133 * escaped. */
2134 if (p[j] == firstc
2135 && (j == 0 || p[j - 1] != '\\'))
2136 {
2137 if (i > 0)
2138 ccline.cmdbuff[len] = '\\';
2139 ++len;
2140 }
2141 if (i > 0)
2142 ccline.cmdbuff[len] = p[j];
2143 }
2144 ++len;
2145 }
2146 if (i == 0)
2147 {
2148 alloc_cmdbuff(len);
2149 if (ccline.cmdbuff == NULL)
2150 goto returncmd;
2151 }
2152 }
2153 ccline.cmdbuff[len] = NUL;
2154 }
2155 else
2156 {
2157 alloc_cmdbuff((int)STRLEN(p));
2158 if (ccline.cmdbuff == NULL)
2159 goto returncmd;
2160 STRCPY(ccline.cmdbuff, p);
2161 }
2162
2163 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2164 redrawcmd();
2165 goto cmdline_changed;
2166 }
2167 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002168#endif
2169 goto cmdline_not_changed;
2170
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002171#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002172 case Ctrl_G: /* next match */
2173 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002174 if (may_adjust_incsearch_highlighting(
2175 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002176 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#endif
2179
2180 case Ctrl_V:
2181 case Ctrl_Q:
2182#ifdef FEAT_MOUSE
2183 ignore_drag_release = TRUE;
2184#endif
2185 putcmdline('^', TRUE);
2186 c = get_literal(); /* get next (two) character(s) */
2187 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002188 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#ifdef FEAT_MBYTE
2190 /* may need to remove ^ when composing char was typed */
2191 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2192 {
2193 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2194 msg_putchar(' ');
2195 cursorcmd();
2196 }
2197#endif
2198 break;
2199
2200#ifdef FEAT_DIGRAPHS
2201 case Ctrl_K:
2202#ifdef FEAT_MOUSE
2203 ignore_drag_release = TRUE;
2204#endif
2205 putcmdline('?', TRUE);
2206#ifdef USE_ON_FLY_SCROLL
2207 dont_scroll = TRUE; /* disallow scrolling here */
2208#endif
2209 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002210 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (c != NUL)
2212 break;
2213
2214 redrawcmd();
2215 goto cmdline_not_changed;
2216#endif /* FEAT_DIGRAPHS */
2217
2218#ifdef FEAT_RIGHTLEFT
2219 case Ctrl__: /* CTRL-_: switch language mode */
2220 if (!p_ari)
2221 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002222# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (p_altkeymap)
2224 {
2225 cmd_fkmap = !cmd_fkmap;
2226 if (cmd_fkmap) /* in Farsi always in Insert mode */
2227 ccline.overstrike = FALSE;
2228 }
2229 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 cmd_hkmap = !cmd_hkmap;
2232 goto cmdline_not_changed;
2233#endif
2234
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002235 case K_PS:
2236 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2237 goto cmdline_changed;
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 default:
2240#ifdef UNIX
2241 if (c == intr_char)
2242 {
2243 gotesc = TRUE; /* will free ccline.cmdbuff after
2244 putting it in history */
2245 goto returncmd; /* back to Normal mode */
2246 }
2247#endif
2248 /*
2249 * Normal character with no special meaning. Just set mod_mask
2250 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2251 * the string <S-Space>. This should only happen after ^V.
2252 */
2253 if (!IS_SPECIAL(c))
2254 mod_mask = 0x0;
2255 break;
2256 }
2257 /*
2258 * End of switch on command line character.
2259 * We come here if we have a normal character.
2260 */
2261
Bram Moolenaarede3e632013-06-23 16:16:19 +02002262 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#ifdef FEAT_MBYTE
2264 /* Add ABBR_OFF for characters above 0x100, this is
2265 * what check_abbr() expects. */
2266 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2267#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002268 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 goto cmdline_changed;
2270
2271 /*
2272 * put the character in the command line
2273 */
2274 if (IS_SPECIAL(c) || mod_mask != 0)
2275 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2276 else
2277 {
2278#ifdef FEAT_MBYTE
2279 if (has_mbyte)
2280 {
2281 j = (*mb_char2bytes)(c, IObuff);
2282 IObuff[j] = NUL; /* exclude composing chars */
2283 put_on_cmdline(IObuff, j, TRUE);
2284 }
2285 else
2286#endif
2287 {
2288 IObuff[0] = c;
2289 put_on_cmdline(IObuff, 1, TRUE);
2290 }
2291 }
2292 goto cmdline_changed;
2293
2294/*
2295 * This part implements incremental searches for "/" and "?"
2296 * Jump to cmdline_not_changed when a character has been read but the command
2297 * line did not change. Then we only search and redraw if something changed in
2298 * the past.
2299 * Jump to cmdline_changed when the command line did change.
2300 * (Sorry for the goto's, I know it is ugly).
2301 */
2302cmdline_not_changed:
2303#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002304 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 continue;
2306#endif
2307
2308cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002309 /* Trigger CmdlineChanged autocommands. */
2310 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002313 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315
2316#ifdef FEAT_RIGHTLEFT
2317 if (cmdmsg_rl
2318# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002319 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320# endif
2321 )
2322 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002323 * right-left typing. Not efficient, but it works.
2324 * Do it only when there are no characters left to read
2325 * to avoid useless intermediate redraws. */
2326 if (vpeekc() == NUL)
2327 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329 }
2330
2331returncmd:
2332
2333#ifdef FEAT_RIGHTLEFT
2334 cmdmsg_rl = FALSE;
2335#endif
2336
2337#ifdef FEAT_FKMAP
2338 cmd_fkmap = 0;
2339#endif
2340
2341 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002342 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002345 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#endif
2347
2348 if (ccline.cmdbuff != NULL)
2349 {
2350 /*
2351 * Put line in history buffer (":" and "=" only when it was typed).
2352 */
2353#ifdef FEAT_CMDHIST
2354 if (ccline.cmdlen && firstc != NUL
2355 && (some_key_typed || histype == HIST_SEARCH))
2356 {
2357 add_to_history(histype, ccline.cmdbuff, TRUE,
2358 histype == HIST_SEARCH ? firstc : NUL);
2359 if (firstc == ':')
2360 {
2361 vim_free(new_last_cmdline);
2362 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2363 }
2364 }
2365#endif
2366
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002367 if (gotesc)
2368 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370
2371 /*
2372 * If the screen was shifted up, redraw the whole screen (later).
2373 * If the line is too long, clear it, so ruler and shown command do
2374 * not get printed in the middle of it.
2375 */
2376 msg_check();
2377 msg_scroll = save_msg_scroll;
2378 redir_off = FALSE;
2379
2380 /* When the command line was typed, no need for a wait-return prompt. */
2381 if (some_key_typed)
2382 need_wait_return = FALSE;
2383
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002384 /* Trigger CmdlineLeave autocommands. */
2385 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002388#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2390 im_save_status(b_im_ptr);
2391 im_set_active(FALSE);
2392#endif
2393#ifdef FEAT_MOUSE
2394 setmouse();
2395#endif
2396#ifdef CURSOR_SHAPE
2397 ui_cursor_shape(); /* may show different cursor shape */
2398#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002399 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002401 {
2402 char_u *p = ccline.cmdbuff;
2403
2404 /* Make ccline empty, getcmdline() may try to use it. */
2405 ccline.cmdbuff = NULL;
2406 return p;
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408}
2409
2410#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2411/*
2412 * Get a command line with a prompt.
2413 * This is prepared to be called recursively from getcmdline() (e.g. by
2414 * f_input() when evaluating an expression from CTRL-R =).
2415 * Returns the command line in allocated memory, or NULL.
2416 */
2417 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002418getcmdline_prompt(
2419 int firstc,
2420 char_u *prompt, /* command line prompt */
2421 int attr, /* attributes for prompt */
2422 int xp_context, /* type of expansion */
2423 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
2425 char_u *s;
2426 struct cmdline_info save_ccline;
2427 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002428 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002430 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 ccline.cmdprompt = prompt;
2432 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002433# ifdef FEAT_EVAL
2434 ccline.xp_context = xp_context;
2435 ccline.xp_arg = xp_arg;
2436 ccline.input_fn = (firstc == '@');
2437# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002438 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002440 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002441 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002442 /* Restore msg_col, the prompt from input() may have changed it.
2443 * But only if called recursively and the commandline is therefore being
2444 * restored to an old one; if not, the input() prompt stays on the screen,
2445 * so we need its modified msg_col left intact. */
2446 if (ccline.cmdbuff != NULL)
2447 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 return s;
2450}
2451#endif
2452
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002453/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002454 * Return TRUE when the text must not be changed and we can't switch to
2455 * another window or buffer. Used when editing the command line, evaluating
2456 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002457 */
2458 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002459text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002460{
2461#ifdef FEAT_CMDWIN
2462 if (cmdwin_type != 0)
2463 return TRUE;
2464#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002465 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002466}
2467
2468/*
2469 * Give an error message for a command that isn't allowed while the cmdline
2470 * window is open or editing the cmdline in another way.
2471 */
2472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002473text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002474{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002475 EMSG(_(get_text_locked_msg()));
2476}
2477
2478 char_u *
2479get_text_locked_msg(void)
2480{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002481#ifdef FEAT_CMDWIN
2482 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002483 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002484#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002485 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002486}
2487
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002488/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002489 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2490 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002491 */
2492 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002493curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002494{
2495 if (curbuf_lock > 0)
2496 {
2497 EMSG(_("E788: Not allowed to edit another buffer now"));
2498 return TRUE;
2499 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002500 return allbuf_locked();
2501}
2502
2503/*
2504 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2505 * message.
2506 */
2507 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002508allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002509{
2510 if (allbuf_lock > 0)
2511 {
2512 EMSG(_("E811: Not allowed to change buffer information now"));
2513 return TRUE;
2514 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002515 return FALSE;
2516}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002519cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2522 if (cmdline_star > 0) /* showing '*', always 1 position */
2523 return 1;
2524#endif
2525 return ptr2cells(ccline.cmdbuff + idx);
2526}
2527
2528/*
2529 * Compute the offset of the cursor on the command line for the prompt and
2530 * indent.
2531 */
2532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002533set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002535 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 ccline.cmdspos = 1 + ccline.cmdindent;
2537 else
2538 ccline.cmdspos = 0 + ccline.cmdindent;
2539}
2540
2541/*
2542 * Compute the screen position for the cursor on the command line.
2543 */
2544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002545set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int i, m, c;
2548
2549 set_cmdspos();
2550 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002553 if (m < 0) /* overflow, Columns or Rows at weird value */
2554 m = MAXCOL;
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 else
2557 m = MAXCOL;
2558 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2559 {
2560 c = cmdline_charsize(i);
2561#ifdef FEAT_MBYTE
2562 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2563 if (has_mbyte)
2564 correct_cmdspos(i, c);
2565#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002566 /* If the cmdline doesn't fit, show cursor on last visible char.
2567 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if ((ccline.cmdspos += c) >= m)
2569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 ccline.cmdspos -= c;
2571 break;
2572 }
2573#ifdef FEAT_MBYTE
2574 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002575 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
2577 }
2578}
2579
2580#ifdef FEAT_MBYTE
2581/*
2582 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2583 * character that doesn't fit, so that a ">" must be displayed.
2584 */
2585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002586correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002588 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2590 && ccline.cmdspos % Columns + cells > Columns)
2591 ccline.cmdspos++;
2592}
2593#endif
2594
2595/*
2596 * Get an Ex command line for the ":" command.
2597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002599getexline(
2600 int c, /* normally ':', NUL for ":append" */
2601 void *cookie UNUSED,
2602 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 /* When executing a register, remove ':' that's in front of each line. */
2605 if (exec_from_reg && vpeekc() == ':')
2606 (void)vgetc();
2607 return getcmdline(c, 1L, indent);
2608}
2609
2610/*
2611 * Get an Ex command line for Ex mode.
2612 * In Ex mode we only use the OS supplied line editing features and no
2613 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002614 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002617getexmodeline(
2618 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002619 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002620 void *cookie UNUSED,
2621 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002623 garray_T line_ga;
2624 char_u *pend;
2625 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002626 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002627 int escaped = FALSE; /* CTRL-V typed */
2628 int vcol = 0;
2629 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002630 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002631 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 /* Switch cursor on now. This avoids that it happens after the "\n", which
2634 * confuses the system function that computes tabstops. */
2635 cursor_on();
2636
2637 /* always start in column 0; write a newline if necessary */
2638 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002639 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002643 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 if (p_prompt)
2645 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 while (indent-- > 0)
2647 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650
2651 ga_init2(&line_ga, 1, 30);
2652
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002653 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002654 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002655 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002656 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002657 while (indent >= 8)
2658 {
2659 ga_append(&line_ga, TAB);
2660 msg_puts((char_u *)" ");
2661 indent -= 8;
2662 }
2663 while (indent-- > 0)
2664 {
2665 ga_append(&line_ga, ' ');
2666 msg_putchar(' ');
2667 }
2668 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002669 ++no_mapping;
2670 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /*
2673 * Get the line, one character at a time.
2674 */
2675 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002676 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002678 long sw;
2679 char_u *s;
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (ga_grow(&line_ga, 40) == FAIL)
2682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaarba748c82017-02-26 14:00:07 +01002684 /*
2685 * Get one character at a time.
2686 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002687 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002688
2689 /* Check for a ":normal" command and no more characters left. */
2690 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2691 c1 = '\n';
2692 else
2693 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 * Handle line editing.
2697 * Previously this was left to the system, putting the terminal in
2698 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 msg_putchar('\n');
2703 break;
2704 }
2705
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002706 if (c1 == K_PS)
2707 {
2708 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2709 goto redraw;
2710 }
2711
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002712 if (!escaped)
2713 {
2714 /* CR typed means "enter", which is NL */
2715 if (c1 == '\r')
2716 c1 = '\n';
2717
2718 if (c1 == BS || c1 == K_BS
2719 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002721 if (line_ga.ga_len > 0)
2722 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002723#ifdef FEAT_MBYTE
2724 if (has_mbyte)
2725 {
2726 p = (char_u *)line_ga.ga_data;
2727 p[line_ga.ga_len] = NUL;
2728 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2729 line_ga.ga_len -= len;
2730 }
2731 else
2732#endif
2733 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002734 goto redraw;
2735 }
2736 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
2738
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002739 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002741 msg_col = startcol;
2742 msg_clr_eos();
2743 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002744 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002745 }
2746
2747 if (c1 == Ctrl_T)
2748 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002749 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750 p = (char_u *)line_ga.ga_data;
2751 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002752 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002753 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002755 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002757 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002758 p = (char_u *)line_ga.ga_data;
2759 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2761 *s = ' ';
2762 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764redraw:
2765 /* redraw the line */
2766 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002768 p = (char_u *)line_ga.ga_data;
2769 p[line_ga.ga_len] = NUL;
2770 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002772 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776 msg_putchar(' ');
2777 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002778 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002782 len = MB_PTR2LEN(p);
2783 msg_outtrans_len(p, len);
2784 vcol += ptr2cells(p);
2785 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002788 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002789 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790 continue;
2791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 if (c1 == Ctrl_D)
2794 {
2795 /* Delete one shiftwidth. */
2796 p = (char_u *)line_ga.ga_data;
2797 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799 if (prev_char == '^')
2800 ex_keep_indent = TRUE;
2801 indent = 0;
2802 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
2805 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002807 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002808 if (indent > 0)
2809 {
2810 --indent;
2811 indent -= indent % get_sw_value(curbuf);
2812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002814 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002816 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2818 --line_ga.ga_len;
2819 }
2820 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822
2823 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2824 {
2825 escaped = TRUE;
2826 continue;
2827 }
2828
2829 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2830 if (IS_SPECIAL(c1))
2831 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833
2834 if (IS_SPECIAL(c1))
2835 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002836#ifdef FEAT_MBYTE
2837 if (has_mbyte)
2838 len = (*mb_char2bytes)(c1,
2839 (char_u *)line_ga.ga_data + line_ga.ga_len);
2840 else
2841#endif
2842 {
2843 len = 1;
2844 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2845 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 if (c1 == '\n')
2847 msg_putchar('\n');
2848 else if (c1 == TAB)
2849 {
2850 /* Don't use chartabsize(), 'ts' can be different */
2851 do
2852 {
2853 msg_putchar(' ');
2854 } while (++vcol % 8);
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002859 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002862 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 escaped = FALSE;
2864
2865 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002866 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002867
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002868 /* We are done when a NL is entered, but not when it comes after an
2869 * odd number of backslashes, that results in a NUL. */
2870 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002872 int bcount = 0;
2873
2874 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2875 ++bcount;
2876
2877 if (bcount > 0)
2878 {
2879 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2880 * "\NL", etc. */
2881 line_ga.ga_len -= (bcount + 1) / 2;
2882 pend -= (bcount + 1) / 2;
2883 pend[-1] = '\n';
2884 }
2885
2886 if ((bcount & 1) == 0)
2887 {
2888 --line_ga.ga_len;
2889 --pend;
2890 *pend = NUL;
2891 break;
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894 }
2895
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002896 --no_mapping;
2897 --allow_keys;
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /* make following messages go to the next line */
2900 msg_didout = FALSE;
2901 msg_col = 0;
2902 if (msg_row < Rows - 1)
2903 ++msg_row;
2904 emsg_on_display = FALSE; /* don't want ui_delay() */
2905
2906 if (got_int)
2907 ga_clear(&line_ga);
2908
2909 return (char_u *)line_ga.ga_data;
2910}
2911
Bram Moolenaare344bea2005-09-01 20:46:49 +00002912# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2913 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914/*
2915 * Return TRUE if ccline.overstrike is on.
2916 */
2917 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002918cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
2920 return ccline.overstrike;
2921}
2922
2923/*
2924 * Return TRUE if the cursor is at the end of the cmdline.
2925 */
2926 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002927cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 return (ccline.cmdpos >= ccline.cmdlen);
2930}
2931#endif
2932
Bram Moolenaar9372a112005-12-06 19:59:18 +00002933#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934/*
2935 * Return the virtual column number at the current cursor position.
2936 * This is used by the IM code to obtain the start of the preedit string.
2937 */
2938 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002939cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2942 return MAXCOL;
2943
2944# ifdef FEAT_MBYTE
2945 if (has_mbyte)
2946 {
2947 colnr_T col;
2948 int i = 0;
2949
2950 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002951 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
2953 return col;
2954 }
2955 else
2956# endif
2957 return ccline.cmdpos;
2958}
2959#endif
2960
2961#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2962/*
2963 * If part of the command line is an IM preedit string, redraw it with
2964 * IM feedback attributes. The cursor position is restored after drawing.
2965 */
2966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002967redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 if ((State & CMDLINE)
2970 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002971 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 && !p_imdisable
2973 && im_is_preediting())
2974 {
2975 int cmdpos = 0;
2976 int cmdspos;
2977 int old_row;
2978 int old_col;
2979 colnr_T col;
2980
2981 old_row = msg_row;
2982 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002983 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985# ifdef FEAT_MBYTE
2986 if (has_mbyte)
2987 {
2988 for (col = 0; col < preedit_start_col
2989 && cmdpos < ccline.cmdlen; ++col)
2990 {
2991 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002992 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994 }
2995 else
2996# endif
2997 {
2998 cmdspos += preedit_start_col;
2999 cmdpos += preedit_start_col;
3000 }
3001
3002 msg_row = cmdline_row + (cmdspos / (int)Columns);
3003 msg_col = cmdspos % (int)Columns;
3004 if (msg_row >= Rows)
3005 msg_row = Rows - 1;
3006
3007 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3008 {
3009 int char_len;
3010 int char_attr;
3011
3012 char_attr = im_get_feedback_attr(col);
3013 if (char_attr < 0)
3014 break; /* end of preedit string */
3015
3016# ifdef FEAT_MBYTE
3017 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003018 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 else
3020# endif
3021 char_len = 1;
3022
3023 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3024 cmdpos += char_len;
3025 }
3026
3027 msg_row = old_row;
3028 msg_col = old_col;
3029 }
3030}
3031#endif /* FEAT_XIM && FEAT_GUI_GTK */
3032
3033/*
3034 * Allocate a new command line buffer.
3035 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3036 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3037 */
3038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003039alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 /*
3042 * give some extra space to avoid having to allocate all the time
3043 */
3044 if (len < 80)
3045 len = 100;
3046 else
3047 len += 20;
3048
3049 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3050 ccline.cmdbufflen = len;
3051}
3052
3053/*
3054 * Re-allocate the command line to length len + something extra.
3055 * return FAIL for failure, OK otherwise
3056 */
3057 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003058realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 char_u *p;
3061
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003062 if (len < ccline.cmdbufflen)
3063 return OK; /* no need to resize */
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 p = ccline.cmdbuff;
3066 alloc_cmdbuff(len); /* will get some more */
3067 if (ccline.cmdbuff == NULL) /* out of memory */
3068 {
3069 ccline.cmdbuff = p; /* keep the old one */
3070 return FAIL;
3071 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003072 /* There isn't always a NUL after the command, but it may need to be
3073 * there, thus copy up to the NUL and add a NUL. */
3074 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3075 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003077
3078 if (ccline.xpc != NULL
3079 && ccline.xpc->xp_pattern != NULL
3080 && ccline.xpc->xp_context != EXPAND_NOTHING
3081 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3082 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003083 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003084
3085 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3086 * to point into the newly allocated memory. */
3087 if (i >= 0 && i <= ccline.cmdlen)
3088 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3089 }
3090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 return OK;
3092}
3093
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003094#if defined(FEAT_ARABIC) || defined(PROTO)
3095static char_u *arshape_buf = NULL;
3096
3097# if defined(EXITFREE) || defined(PROTO)
3098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003099free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003100{
3101 vim_free(arshape_buf);
3102}
3103# endif
3104#endif
3105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106/*
3107 * Draw part of the cmdline at the current cursor position. But draw stars
3108 * when cmdline_star is TRUE.
3109 */
3110 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003111draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
3113#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3114 int i;
3115
3116 if (cmdline_star > 0)
3117 for (i = 0; i < len; ++i)
3118 {
3119 msg_putchar('*');
3120# ifdef FEAT_MBYTE
3121 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003122 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123# endif
3124 }
3125 else
3126#endif
3127#ifdef FEAT_ARABIC
3128 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 static int buflen = 0;
3131 char_u *p;
3132 int j;
3133 int newlen = 0;
3134 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003135 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 int prev_c = 0;
3137 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003138 int u8c;
3139 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 /*
3143 * Do arabic shaping into a temporary buffer. This is very
3144 * inefficient!
3145 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003146 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 /* Re-allocate the buffer. We keep it around to avoid a lot of
3149 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003150 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003151 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003152 arshape_buf = alloc(buflen);
3153 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return; /* out of memory */
3155 }
3156
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003157 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3158 {
3159 /* Prepend a space to draw the leading composing char on. */
3160 arshape_buf[0] = ' ';
3161 newlen = 1;
3162 }
3163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 for (j = start; j < start + len; j += mb_l)
3165 {
3166 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003167 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003168 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (ARABIC_CHAR(u8c))
3170 {
3171 /* Do Arabic shaping. */
3172 if (cmdmsg_rl)
3173 {
3174 /* displaying from right to left */
3175 pc = prev_c;
3176 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003177 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 if (j + mb_l >= start + len)
3179 nc = NUL;
3180 else
3181 nc = utf_ptr2char(p + mb_l);
3182 }
3183 else
3184 {
3185 /* displaying from left to right */
3186 if (j + mb_l >= start + len)
3187 pc = NUL;
3188 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003189 {
3190 int pcc[MAX_MCO];
3191
3192 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003194 pc1 = pcc[0];
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 nc = prev_c;
3197 }
3198 prev_c = u8c;
3199
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003200 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003203 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003205 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3206 if (u8cc[1] != 0)
3207 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003208 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210 }
3211 else
3212 {
3213 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003214 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 newlen += mb_l;
3216 }
3217 }
3218
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003219 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
3221 else
3222#endif
3223 msg_outtrans_len(ccline.cmdbuff + start, len);
3224}
3225
3226/*
3227 * Put a character on the command line. Shifts the following text to the
3228 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3229 * "c" must be printable (fit in one display cell)!
3230 */
3231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003232putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
3234 if (cmd_silent)
3235 return;
3236 msg_no_more = TRUE;
3237 msg_putchar(c);
3238 if (shift)
3239 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3240 msg_no_more = FALSE;
3241 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003242 extra_char = c;
3243 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246/*
3247 * Undo a putcmdline(c, FALSE).
3248 */
3249 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003250unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 if (cmd_silent)
3253 return;
3254 msg_no_more = TRUE;
3255 if (ccline.cmdlen == ccline.cmdpos)
3256 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003257#ifdef FEAT_MBYTE
3258 else if (has_mbyte)
3259 draw_cmdline(ccline.cmdpos,
3260 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 else
3263 draw_cmdline(ccline.cmdpos, 1);
3264 msg_no_more = FALSE;
3265 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003266 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267}
3268
3269/*
3270 * Put the given string, of the given length, onto the command line.
3271 * If len is -1, then STRLEN() is used to calculate the length.
3272 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3273 * part will be redrawn, otherwise it will not. If this function is called
3274 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3275 * called afterwards.
3276 */
3277 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003278put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 int retval;
3281 int i;
3282 int m;
3283 int c;
3284
3285 if (len < 0)
3286 len = (int)STRLEN(str);
3287
3288 /* Check if ccline.cmdbuff needs to be longer */
3289 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003290 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else
3292 retval = OK;
3293 if (retval == OK)
3294 {
3295 if (!ccline.overstrike)
3296 {
3297 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3298 ccline.cmdbuff + ccline.cmdpos,
3299 (size_t)(ccline.cmdlen - ccline.cmdpos));
3300 ccline.cmdlen += len;
3301 }
3302 else
3303 {
3304#ifdef FEAT_MBYTE
3305 if (has_mbyte)
3306 {
3307 /* Count nr of characters in the new string. */
3308 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003309 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 ++m;
3311 /* Count nr of bytes in cmdline that are overwritten by these
3312 * characters. */
3313 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003314 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 --m;
3316 if (i < ccline.cmdlen)
3317 {
3318 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3319 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3320 ccline.cmdlen += ccline.cmdpos + len - i;
3321 }
3322 else
3323 ccline.cmdlen = ccline.cmdpos + len;
3324 }
3325 else
3326#endif
3327 if (ccline.cmdpos + len > ccline.cmdlen)
3328 ccline.cmdlen = ccline.cmdpos + len;
3329 }
3330 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3331 ccline.cmdbuff[ccline.cmdlen] = NUL;
3332
3333#ifdef FEAT_MBYTE
3334 if (enc_utf8)
3335 {
3336 /* When the inserted text starts with a composing character,
3337 * backup to the character before it. There could be two of them.
3338 */
3339 i = 0;
3340 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3341 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3342 {
3343 i = (*mb_head_off)(ccline.cmdbuff,
3344 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3345 ccline.cmdpos -= i;
3346 len += i;
3347 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3348 }
3349# ifdef FEAT_ARABIC
3350 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3351 {
3352 /* Check the previous character for Arabic combining pair. */
3353 i = (*mb_head_off)(ccline.cmdbuff,
3354 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3355 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3356 + ccline.cmdpos - i), c))
3357 {
3358 ccline.cmdpos -= i;
3359 len += i;
3360 }
3361 else
3362 i = 0;
3363 }
3364# endif
3365 if (i != 0)
3366 {
3367 /* Also backup the cursor position. */
3368 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3369 ccline.cmdspos -= i;
3370 msg_col -= i;
3371 if (msg_col < 0)
3372 {
3373 msg_col += Columns;
3374 --msg_row;
3375 }
3376 }
3377 }
3378#endif
3379
3380 if (redraw && !cmd_silent)
3381 {
3382 msg_no_more = TRUE;
3383 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003384 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3386 /* Avoid clearing the rest of the line too often. */
3387 if (cmdline_row != i || ccline.overstrike)
3388 msg_clr_eos();
3389 msg_no_more = FALSE;
3390 }
3391#ifdef FEAT_FKMAP
3392 /*
3393 * If we are in Farsi command mode, the character input must be in
3394 * Insert mode. So do not advance the cmdpos.
3395 */
3396 if (!cmd_fkmap)
3397#endif
3398 {
3399 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003402 if (m < 0) /* overflow, Columns or Rows at weird value */
3403 m = MAXCOL;
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else
3406 m = MAXCOL;
3407 for (i = 0; i < len; ++i)
3408 {
3409 c = cmdline_charsize(ccline.cmdpos);
3410#ifdef FEAT_MBYTE
3411 /* count ">" for a double-wide char that doesn't fit. */
3412 if (has_mbyte)
3413 correct_cmdspos(ccline.cmdpos, c);
3414#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003415 /* Stop cursor at the end of the screen, but do increment the
3416 * insert position, so that entering a very long command
3417 * works, even though you can't see it. */
3418 if (ccline.cmdspos + c < m)
3419 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#ifdef FEAT_MBYTE
3421 if (has_mbyte)
3422 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003423 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (c > len - i - 1)
3425 c = len - i - 1;
3426 ccline.cmdpos += c;
3427 i += c;
3428 }
3429#endif
3430 ++ccline.cmdpos;
3431 }
3432 }
3433 }
3434 if (redraw)
3435 msg_check();
3436 return retval;
3437}
3438
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003439static struct cmdline_info prev_ccline;
3440static int prev_ccline_used = FALSE;
3441
3442/*
3443 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3444 * and overwrite it. But get_cmdline_str() may need it, thus make it
3445 * available globally in prev_ccline.
3446 */
3447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003448save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003449{
3450 if (!prev_ccline_used)
3451 {
3452 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3453 prev_ccline_used = TRUE;
3454 }
3455 *ccp = prev_ccline;
3456 prev_ccline = ccline;
3457 ccline.cmdbuff = NULL;
3458 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003459 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003460}
3461
3462/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003463 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003464 */
3465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003466restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003467{
3468 ccline = prev_ccline;
3469 prev_ccline = *ccp;
3470}
3471
Bram Moolenaar5a305422006-04-28 22:38:25 +00003472#if defined(FEAT_EVAL) || defined(PROTO)
3473/*
3474 * Save the command line into allocated memory. Returns a pointer to be
3475 * passed to restore_cmdline_alloc() later.
3476 * Returns NULL when failed.
3477 */
3478 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003479save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003480{
3481 struct cmdline_info *p;
3482
3483 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3484 if (p != NULL)
3485 save_cmdline(p);
3486 return (char_u *)p;
3487}
3488
3489/*
3490 * Restore the command line from the return value of save_cmdline_alloc().
3491 */
3492 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003493restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003494{
3495 if (p != NULL)
3496 {
3497 restore_cmdline((struct cmdline_info *)p);
3498 vim_free(p);
3499 }
3500}
3501#endif
3502
Bram Moolenaar8299df92004-07-10 09:47:34 +00003503/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003504 * Paste a yank register into the command line.
3505 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003506 * insert_reg() can't be used here, because special characters from the
3507 * register contents will be interpreted as commands.
3508 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003509 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003510 */
3511 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003512cmdline_paste(
3513 int regname,
3514 int literally, /* Insert text literally instead of "as typed" */
3515 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003516{
3517 long i;
3518 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003519 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003520 int allocated;
3521 struct cmdline_info save_ccline;
3522
3523 /* check for valid regname; also accept special characters for CTRL-R in
3524 * the command line */
3525 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003526 && regname != Ctrl_A && regname != Ctrl_L
3527 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003528 return FAIL;
3529
3530 /* A register containing CTRL-R can cause an endless loop. Allow using
3531 * CTRL-C to break the loop. */
3532 line_breakcheck();
3533 if (got_int)
3534 return FAIL;
3535
3536#ifdef FEAT_CLIPBOARD
3537 regname = may_get_selection(regname);
3538#endif
3539
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003540 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003541 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003542 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003543 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003544 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003545 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003546 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003547
3548 if (i)
3549 {
3550 /* Got the value of a special register in "arg". */
3551 if (arg == NULL)
3552 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003553
3554 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3555 * part of the word. */
3556 p = arg;
3557 if (p_is && regname == Ctrl_W)
3558 {
3559 char_u *w;
3560 int len;
3561
3562 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003563 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003564 {
3565#ifdef FEAT_MBYTE
3566 if (has_mbyte)
3567 {
3568 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3569 if (!vim_iswordc(mb_ptr2char(w - len)))
3570 break;
3571 w -= len;
3572 }
3573 else
3574#endif
3575 {
3576 if (!vim_iswordc(w[-1]))
3577 break;
3578 --w;
3579 }
3580 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003581 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003582 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3583 p += len;
3584 }
3585
3586 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003587 if (allocated)
3588 vim_free(arg);
3589 return OK;
3590 }
3591
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003592 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003593}
3594
3595/*
3596 * Put a string on the command line.
3597 * When "literally" is TRUE, insert literally.
3598 * When "literally" is FALSE, insert as typed, but don't leave the command
3599 * line.
3600 */
3601 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003602cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603{
3604 int c, cv;
3605
3606 if (literally)
3607 put_on_cmdline(s, -1, TRUE);
3608 else
3609 while (*s != NUL)
3610 {
3611 cv = *s;
3612 if (cv == Ctrl_V && s[1])
3613 ++s;
3614#ifdef FEAT_MBYTE
3615 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003616 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003617 else
3618#endif
3619 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003620 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3621 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003622#ifdef UNIX
3623 || c == intr_char
3624#endif
3625 || (c == Ctrl_BSL && *s == Ctrl_N))
3626 stuffcharReadbuff(Ctrl_V);
3627 stuffcharReadbuff(c);
3628 }
3629}
3630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631#ifdef FEAT_WILDMENU
3632/*
3633 * Delete characters on the command line, from "from" to the current
3634 * position.
3635 */
3636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003637cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3640 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3641 ccline.cmdlen -= ccline.cmdpos - from;
3642 ccline.cmdpos = from;
3643}
3644#endif
3645
3646/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003647 * This function is called when the screen size changes and with incremental
3648 * search and in other situations where the command line may have been
3649 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
3651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003652redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003654 redrawcmdline_ex(TRUE);
3655}
3656
3657 void
3658redrawcmdline_ex(int do_compute_cmdrow)
3659{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (cmd_silent)
3661 return;
3662 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003663 if (do_compute_cmdrow)
3664 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 redrawcmd();
3666 cursorcmd();
3667}
3668
3669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003670redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 int i;
3673
3674 if (cmd_silent)
3675 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003676 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 msg_putchar(ccline.cmdfirstc);
3678 if (ccline.cmdprompt != NULL)
3679 {
3680 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3681 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3682 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003683 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 --ccline.cmdindent;
3685 }
3686 else
3687 for (i = ccline.cmdindent; i > 0; --i)
3688 msg_putchar(' ');
3689}
3690
3691/*
3692 * Redraw what is currently on the command line.
3693 */
3694 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003695redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
3697 if (cmd_silent)
3698 return;
3699
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003700 /* when 'incsearch' is set there may be no command line while redrawing */
3701 if (ccline.cmdbuff == NULL)
3702 {
3703 windgoto(cmdline_row, 0);
3704 msg_clr_eos();
3705 return;
3706 }
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 msg_start();
3709 redrawcmdprompt();
3710
3711 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3712 msg_no_more = TRUE;
3713 draw_cmdline(0, ccline.cmdlen);
3714 msg_clr_eos();
3715 msg_no_more = FALSE;
3716
3717 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003718 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003719 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 /*
3722 * An emsg() before may have set msg_scroll. This is used in normal mode,
3723 * in cmdline mode we can reset them now.
3724 */
3725 msg_scroll = FALSE; /* next message overwrites cmdline */
3726
3727 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3728 * in cmdline mode */
3729 skip_redraw = FALSE;
3730}
3731
3732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003733compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003735 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 cmdline_row = Rows - 1;
3737 else
3738 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003739 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740}
3741
3742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003743cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 if (cmd_silent)
3746 return;
3747
3748#ifdef FEAT_RIGHTLEFT
3749 if (cmdmsg_rl)
3750 {
3751 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3752 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3753 if (msg_row <= 0)
3754 msg_row = Rows - 1;
3755 }
3756 else
3757#endif
3758 {
3759 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3760 msg_col = ccline.cmdspos % (int)Columns;
3761 if (msg_row >= Rows)
3762 msg_row = Rows - 1;
3763 }
3764
3765 windgoto(msg_row, msg_col);
3766#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003767 if (p_imst == IM_ON_THE_SPOT)
3768 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#endif
3770#ifdef MCH_CURSOR_SHAPE
3771 mch_update_cursor();
3772#endif
3773}
3774
3775 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003776gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
3778 msg_start();
3779#ifdef FEAT_RIGHTLEFT
3780 if (cmdmsg_rl)
3781 msg_col = Columns - 1;
3782 else
3783#endif
3784 msg_col = 0; /* always start in column 0 */
3785 if (clr) /* clear the bottom line(s) */
3786 msg_clr_eos(); /* will reset clear_cmdline */
3787 windgoto(cmdline_row, 0);
3788}
3789
3790/*
3791 * Check the word in front of the cursor for an abbreviation.
3792 * Called when the non-id character "c" has been entered.
3793 * When an abbreviation is recognized it is removed from the text with
3794 * backspaces and the replacement string is inserted, followed by "c".
3795 */
3796 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003797ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003799 int spos = 0;
3800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3802 return FALSE;
3803
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003804 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3805 * Actually accepts any mark. */
3806 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3807 spos++;
3808 if (ccline.cmdlen - spos > 5
3809 && ccline.cmdbuff[spos] == '\''
3810 && ccline.cmdbuff[spos + 2] == ','
3811 && ccline.cmdbuff[spos + 3] == '\'')
3812 spos += 5;
3813 else
3814 /* check abbreviation from the beginning of the commandline */
3815 spos = 0;
3816
3817 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818}
3819
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003820#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3821 static int
3822#ifdef __BORLANDC__
3823_RTLENTRYF
3824#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003825sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003826{
3827 char_u *p1 = *(char_u **)s1;
3828 char_u *p2 = *(char_u **)s2;
3829
3830 if (*p1 != '<' && *p2 == '<') return -1;
3831 if (*p1 == '<' && *p2 != '<') return 1;
3832 return STRCMP(p1, p2);
3833}
3834#endif
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836/*
3837 * Return FAIL if this is not an appropriate context in which to do
3838 * completion of anything, return OK if it is (even if there are no matches).
3839 * For the caller, this means that the character is just passed through like a
3840 * normal character (instead of being expanded). This allows :s/^I^D etc.
3841 */
3842 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003843nextwild(
3844 expand_T *xp,
3845 int type,
3846 int options, /* extra options for ExpandOne() */
3847 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
3849 int i, j;
3850 char_u *p1;
3851 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 int difflen;
3853 int v;
3854
3855 if (xp->xp_numfiles == -1)
3856 {
3857 set_expand_context(xp);
3858 cmd_showtail = expand_showtail(xp);
3859 }
3860
3861 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3862 {
3863 beep_flush();
3864 return OK; /* Something illegal on command line */
3865 }
3866 if (xp->xp_context == EXPAND_NOTHING)
3867 {
3868 /* Caller can use the character as a normal char instead */
3869 return FAIL;
3870 }
3871
3872 MSG_PUTS("..."); /* show that we are busy */
3873 out_flush();
3874
3875 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003876 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
3878 if (type == WILD_NEXT || type == WILD_PREV)
3879 {
3880 /*
3881 * Get next/previous match for a previous expanded pattern.
3882 */
3883 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3884 }
3885 else
3886 {
3887 /*
3888 * Translate string into pattern and expand it.
3889 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003890 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3891 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 p2 = NULL;
3893 else
3894 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003895 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003896 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3897 if (escape)
3898 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003899
3900 if (p_wic)
3901 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003902 p2 = ExpandOne(xp, p1,
3903 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003904 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003906 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (p2 != NULL && type == WILD_LONGEST)
3908 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003909 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if (ccline.cmdbuff[i + j] == '*'
3911 || ccline.cmdbuff[i + j] == '?')
3912 break;
3913 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003914 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 }
3917 }
3918
3919 if (p2 != NULL && !got_int)
3920 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003921 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003922 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003924 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 xp->xp_pattern = ccline.cmdbuff + i;
3926 }
3927 else
3928 v = OK;
3929 if (v == OK)
3930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003931 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3932 &ccline.cmdbuff[ccline.cmdpos],
3933 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3934 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 ccline.cmdlen += difflen;
3936 ccline.cmdpos += difflen;
3937 }
3938 }
3939 vim_free(p2);
3940
3941 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003942 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 /* When expanding a ":map" command and no matches are found, assume that
3945 * the key is supposed to be inserted literally */
3946 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3947 return FAIL;
3948
3949 if (xp->xp_numfiles <= 0 && p2 == NULL)
3950 beep_flush();
3951 else if (xp->xp_numfiles == 1)
3952 /* free expanded pattern */
3953 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3954
3955 return OK;
3956}
3957
3958/*
3959 * Do wildcard expansion on the string 'str'.
3960 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003961 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 * Return NULL for failure.
3963 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003964 * "orig" is the originally expanded string, copied to allocated memory. It
3965 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3966 * WILD_PREV "orig" should be NULL.
3967 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003968 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3969 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 *
3971 * mode = WILD_FREE: just free previously expanded matches
3972 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3973 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3974 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3975 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3976 * mode = WILD_ALL: return all matches concatenated
3977 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003978 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 *
3980 * options = WILD_LIST_NOTFOUND: list entries without a match
3981 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3982 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3983 * options = WILD_NO_BEEP: Don't beep for multiple matches
3984 * options = WILD_ADD_SLASH: add a slash after directory names
3985 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3986 * options = WILD_SILENT: don't print warning messages
3987 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003988 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 *
3990 * The variables xp->xp_context and xp->xp_backslash must have been set!
3991 */
3992 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003993ExpandOne(
3994 expand_T *xp,
3995 char_u *str,
3996 char_u *orig, /* allocated copy of original of expanded string */
3997 int options,
3998 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
4000 char_u *ss = NULL;
4001 static int findex;
4002 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004003 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int i;
4005 long_u len;
4006 int non_suf_match; /* number without matching suffix */
4007
4008 /*
4009 * first handle the case of using an old match
4010 */
4011 if (mode == WILD_NEXT || mode == WILD_PREV)
4012 {
4013 if (xp->xp_numfiles > 0)
4014 {
4015 if (mode == WILD_PREV)
4016 {
4017 if (findex == -1)
4018 findex = xp->xp_numfiles;
4019 --findex;
4020 }
4021 else /* mode == WILD_NEXT */
4022 ++findex;
4023
4024 /*
4025 * When wrapping around, return the original string, set findex to
4026 * -1.
4027 */
4028 if (findex < 0)
4029 {
4030 if (orig_save == NULL)
4031 findex = xp->xp_numfiles - 1;
4032 else
4033 findex = -1;
4034 }
4035 if (findex >= xp->xp_numfiles)
4036 {
4037 if (orig_save == NULL)
4038 findex = 0;
4039 else
4040 findex = -1;
4041 }
4042#ifdef FEAT_WILDMENU
4043 if (p_wmnu)
4044 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4045 findex, cmd_showtail);
4046#endif
4047 if (findex == -1)
4048 return vim_strsave(orig_save);
4049 return vim_strsave(xp->xp_files[findex]);
4050 }
4051 else
4052 return NULL;
4053 }
4054
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004055 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4057 {
4058 FreeWild(xp->xp_numfiles, xp->xp_files);
4059 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004060 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 findex = 0;
4063
4064 if (mode == WILD_FREE) /* only release file name */
4065 return NULL;
4066
4067 if (xp->xp_numfiles == -1)
4068 {
4069 vim_free(orig_save);
4070 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004071 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 /*
4074 * Do the expansion.
4075 */
4076 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4077 options) == FAIL)
4078 {
4079#ifdef FNAME_ILLEGAL
4080 /* Illegal file name has been silently skipped. But when there
4081 * are wildcards, the real problem is that there was no match,
4082 * causing the pattern to be added, which has illegal characters.
4083 */
4084 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4085 EMSG2(_(e_nomatch2), str);
4086#endif
4087 }
4088 else if (xp->xp_numfiles == 0)
4089 {
4090 if (!(options & WILD_SILENT))
4091 EMSG2(_(e_nomatch2), str);
4092 }
4093 else
4094 {
4095 /* Escape the matches for use on the command line. */
4096 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4097
4098 /*
4099 * Check for matching suffixes in file names.
4100 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004101 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4102 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
4104 if (xp->xp_numfiles)
4105 non_suf_match = xp->xp_numfiles;
4106 else
4107 non_suf_match = 1;
4108 if ((xp->xp_context == EXPAND_FILES
4109 || xp->xp_context == EXPAND_DIRECTORIES)
4110 && xp->xp_numfiles > 1)
4111 {
4112 /*
4113 * More than one match; check suffix.
4114 * The files will have been sorted on matching suffix in
4115 * expand_wildcards, only need to check the first two.
4116 */
4117 non_suf_match = 0;
4118 for (i = 0; i < 2; ++i)
4119 if (match_suffix(xp->xp_files[i]))
4120 ++non_suf_match;
4121 }
4122 if (non_suf_match != 1)
4123 {
4124 /* Can we ever get here unless it's while expanding
4125 * interactively? If not, we can get rid of this all
4126 * together. Don't really want to wait for this message
4127 * (and possibly have to hit return to continue!).
4128 */
4129 if (!(options & WILD_SILENT))
4130 EMSG(_(e_toomany));
4131 else if (!(options & WILD_NO_BEEP))
4132 beep_flush();
4133 }
4134 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4135 ss = vim_strsave(xp->xp_files[0]);
4136 }
4137 }
4138 }
4139
4140 /* Find longest common part */
4141 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4142 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004143 int mb_len = 1;
4144 int c0, ci;
4145
4146 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004148#ifdef FEAT_MBYTE
4149 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004151 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4152 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4153 }
4154 else
4155#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004156 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004157 for (i = 1; i < xp->xp_numfiles; ++i)
4158 {
4159#ifdef FEAT_MBYTE
4160 if (has_mbyte)
4161 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4162 else
4163#endif
4164 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004165 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004167 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004168 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004170 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 break;
4172 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004173 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 break;
4175 }
4176 if (i < xp->xp_numfiles)
4177 {
4178 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004179 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 break;
4181 }
4182 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 ss = alloc((unsigned)len + 1);
4185 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004186 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 findex = -1; /* next p_wc gets first one */
4188 }
4189
4190 /* Concatenate all matching names */
4191 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4192 {
4193 len = 0;
4194 for (i = 0; i < xp->xp_numfiles; ++i)
4195 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4196 ss = lalloc(len, TRUE);
4197 if (ss != NULL)
4198 {
4199 *ss = NUL;
4200 for (i = 0; i < xp->xp_numfiles; ++i)
4201 {
4202 STRCAT(ss, xp->xp_files[i]);
4203 if (i != xp->xp_numfiles - 1)
4204 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4205 }
4206 }
4207 }
4208
4209 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4210 ExpandCleanup(xp);
4211
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004212 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004213 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004214 vim_free(orig);
4215
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return ss;
4217}
4218
4219/*
4220 * Prepare an expand structure for use.
4221 */
4222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004223ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004225 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004226 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004228#ifndef BACKSLASH_IN_FILENAME
4229 xp->xp_shell = FALSE;
4230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 xp->xp_numfiles = -1;
4232 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004233#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4234 xp->xp_arg = NULL;
4235#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004236 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237}
4238
4239/*
4240 * Cleanup an expand structure after use.
4241 */
4242 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004243ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 if (xp->xp_numfiles >= 0)
4246 {
4247 FreeWild(xp->xp_numfiles, xp->xp_files);
4248 xp->xp_numfiles = -1;
4249 }
4250}
4251
4252 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004253ExpandEscape(
4254 expand_T *xp,
4255 char_u *str,
4256 int numfiles,
4257 char_u **files,
4258 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 int i;
4261 char_u *p;
4262
4263 /*
4264 * May change home directory back to "~"
4265 */
4266 if (options & WILD_HOME_REPLACE)
4267 tilde_replace(str, numfiles, files);
4268
4269 if (options & WILD_ESCAPE)
4270 {
4271 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004272 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004273 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 || xp->xp_context == EXPAND_BUFFERS
4275 || xp->xp_context == EXPAND_DIRECTORIES)
4276 {
4277 /*
4278 * Insert a backslash into a file name before a space, \, %, #
4279 * and wildmatch characters, except '~'.
4280 */
4281 for (i = 0; i < numfiles; ++i)
4282 {
4283 /* for ":set path=" we need to escape spaces twice */
4284 if (xp->xp_backslash == XP_BS_THREE)
4285 {
4286 p = vim_strsave_escaped(files[i], (char_u *)" ");
4287 if (p != NULL)
4288 {
4289 vim_free(files[i]);
4290 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004291#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 p = vim_strsave_escaped(files[i], (char_u *)" ");
4293 if (p != NULL)
4294 {
4295 vim_free(files[i]);
4296 files[i] = p;
4297 }
4298#endif
4299 }
4300 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004301#ifdef BACKSLASH_IN_FILENAME
4302 p = vim_strsave_fnameescape(files[i], FALSE);
4303#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004304 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (p != NULL)
4307 {
4308 vim_free(files[i]);
4309 files[i] = p;
4310 }
4311
4312 /* If 'str' starts with "\~", replace "~" at start of
4313 * files[i] with "\~". */
4314 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004315 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004318
4319 /* If the first file starts with a '+' escape it. Otherwise it
4320 * could be seen as "+cmd". */
4321 if (*files[0] == '+')
4322 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 else if (xp->xp_context == EXPAND_TAGS)
4325 {
4326 /*
4327 * Insert a backslash before characters in a tag name that
4328 * would terminate the ":tag" command.
4329 */
4330 for (i = 0; i < numfiles; ++i)
4331 {
4332 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4333 if (p != NULL)
4334 {
4335 vim_free(files[i]);
4336 files[i] = p;
4337 }
4338 }
4339 }
4340 }
4341}
4342
4343/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004344 * Escape special characters in "fname" for when used as a file name argument
4345 * after a Vim command, or, when "shell" is non-zero, a shell command.
4346 * Returns the result in allocated memory.
4347 */
4348 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004349vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004350{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004351 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004352#ifdef BACKSLASH_IN_FILENAME
4353 char_u buf[20];
4354 int j = 0;
4355
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004356 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004357 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004358 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004359 buf[j++] = *p;
4360 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004361 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004362#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004363 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4364 if (shell && csh_like_shell() && p != NULL)
4365 {
4366 char_u *s;
4367
4368 /* For csh and similar shells need to put two backslashes before '!'.
4369 * One is taken by Vim, one by the shell. */
4370 s = vim_strsave_escaped(p, (char_u *)"!");
4371 vim_free(p);
4372 p = s;
4373 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004374#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004375
4376 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4377 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004378 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004379 escape_fname(&p);
4380
4381 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004382}
4383
4384/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004385 * Put a backslash before the file name in "pp", which is in allocated memory.
4386 */
4387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004388escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004389{
4390 char_u *p;
4391
4392 p = alloc((unsigned)(STRLEN(*pp) + 2));
4393 if (p != NULL)
4394 {
4395 p[0] = '\\';
4396 STRCPY(p + 1, *pp);
4397 vim_free(*pp);
4398 *pp = p;
4399 }
4400}
4401
4402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 * For each file name in files[num_files]:
4404 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4405 */
4406 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004407tilde_replace(
4408 char_u *orig_pat,
4409 int num_files,
4410 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 int i;
4413 char_u *p;
4414
4415 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4416 {
4417 for (i = 0; i < num_files; ++i)
4418 {
4419 p = home_replace_save(NULL, files[i]);
4420 if (p != NULL)
4421 {
4422 vim_free(files[i]);
4423 files[i] = p;
4424 }
4425 }
4426 }
4427}
4428
4429/*
4430 * Show all matches for completion on the command line.
4431 * Returns EXPAND_NOTHING when the character that triggered expansion should
4432 * be inserted like a normal character.
4433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004435showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436{
4437#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4438 int num_files;
4439 char_u **files_found;
4440 int i, j, k;
4441 int maxlen;
4442 int lines;
4443 int columns;
4444 char_u *p;
4445 int lastlen;
4446 int attr;
4447 int showtail;
4448
4449 if (xp->xp_numfiles == -1)
4450 {
4451 set_expand_context(xp);
4452 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4453 &num_files, &files_found);
4454 showtail = expand_showtail(xp);
4455 if (i != EXPAND_OK)
4456 return i;
4457
4458 }
4459 else
4460 {
4461 num_files = xp->xp_numfiles;
4462 files_found = xp->xp_files;
4463 showtail = cmd_showtail;
4464 }
4465
4466#ifdef FEAT_WILDMENU
4467 if (!wildmenu)
4468 {
4469#endif
4470 msg_didany = FALSE; /* lines_left will be set */
4471 msg_start(); /* prepare for paging */
4472 msg_putchar('\n');
4473 out_flush();
4474 cmdline_row = msg_row;
4475 msg_didany = FALSE; /* lines_left will be set again */
4476 msg_start(); /* prepare for paging */
4477#ifdef FEAT_WILDMENU
4478 }
4479#endif
4480
4481 if (got_int)
4482 got_int = FALSE; /* only int. the completion, not the cmd line */
4483#ifdef FEAT_WILDMENU
4484 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004485 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#endif
4487 else
4488 {
4489 /* find the length of the longest file name */
4490 maxlen = 0;
4491 for (i = 0; i < num_files; ++i)
4492 {
4493 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004494 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 || xp->xp_context == EXPAND_BUFFERS))
4496 {
4497 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4498 j = vim_strsize(NameBuff);
4499 }
4500 else
4501 j = vim_strsize(L_SHOWFILE(i));
4502 if (j > maxlen)
4503 maxlen = j;
4504 }
4505
4506 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4507 lines = num_files;
4508 else
4509 {
4510 /* compute the number of columns and lines for the listing */
4511 maxlen += 2; /* two spaces between file names */
4512 columns = ((int)Columns + 2) / maxlen;
4513 if (columns < 1)
4514 columns = 1;
4515 lines = (num_files + columns - 1) / columns;
4516 }
4517
Bram Moolenaar8820b482017-03-16 17:23:31 +01004518 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
4520 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4521 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004522 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 msg_clr_eos();
4524 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004525 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
4528 /* list the files line by line */
4529 for (i = 0; i < lines; ++i)
4530 {
4531 lastlen = 999;
4532 for (k = i; k < num_files; k += lines)
4533 {
4534 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4535 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004536 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 p = files_found[k] + STRLEN(files_found[k]) + 1;
4538 msg_advance(maxlen + 1);
4539 msg_puts(p);
4540 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004541 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 break;
4543 }
4544 for (j = maxlen - lastlen; --j >= 0; )
4545 msg_putchar(' ');
4546 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004547 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 || xp->xp_context == EXPAND_BUFFERS)
4549 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004550 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004551 if (xp->xp_numfiles != -1)
4552 {
4553 char_u *halved_slash;
4554 char_u *exp_path;
4555
4556 /* Expansion was done before and special characters
4557 * were escaped, need to halve backslashes. Also
4558 * $HOME has been replaced with ~/. */
4559 exp_path = expand_env_save_opt(files_found[k], TRUE);
4560 halved_slash = backslash_halve_save(
4561 exp_path != NULL ? exp_path : files_found[k]);
4562 j = mch_isdir(halved_slash != NULL ? halved_slash
4563 : files_found[k]);
4564 vim_free(exp_path);
4565 vim_free(halved_slash);
4566 }
4567 else
4568 /* Expansion was done here, file names are literal. */
4569 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (showtail)
4571 p = L_SHOWFILE(k);
4572 else
4573 {
4574 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4575 TRUE);
4576 p = NameBuff;
4577 }
4578 }
4579 else
4580 {
4581 j = FALSE;
4582 p = L_SHOWFILE(k);
4583 }
4584 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4585 }
4586 if (msg_col > 0) /* when not wrapped around */
4587 {
4588 msg_clr_eos();
4589 msg_putchar('\n');
4590 }
4591 out_flush(); /* show one line at a time */
4592 if (got_int)
4593 {
4594 got_int = FALSE;
4595 break;
4596 }
4597 }
4598
4599 /*
4600 * we redraw the command below the lines that we have just listed
4601 * This is a bit tricky, but it saves a lot of screen updating.
4602 */
4603 cmdline_row = msg_row; /* will put it back later */
4604 }
4605
4606 if (xp->xp_numfiles == -1)
4607 FreeWild(num_files, files_found);
4608
4609 return EXPAND_OK;
4610}
4611
4612/*
4613 * Private gettail for showmatches() (and win_redr_status_matches()):
4614 * Find tail of file name path, but ignore trailing "/".
4615 */
4616 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004617sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618{
4619 char_u *p;
4620 char_u *t = s;
4621 int had_sep = FALSE;
4622
4623 for (p = s; *p != NUL; )
4624 {
4625 if (vim_ispathsep(*p)
4626#ifdef BACKSLASH_IN_FILENAME
4627 && !rem_backslash(p)
4628#endif
4629 )
4630 had_sep = TRUE;
4631 else if (had_sep)
4632 {
4633 t = p;
4634 had_sep = FALSE;
4635 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004636 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638 return t;
4639}
4640
4641/*
4642 * Return TRUE if we only need to show the tail of completion matches.
4643 * When not completing file names or there is a wildcard in the path FALSE is
4644 * returned.
4645 */
4646 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004647expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
4649 char_u *s;
4650 char_u *end;
4651
4652 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004653 if (xp->xp_context != EXPAND_FILES
4654 && xp->xp_context != EXPAND_SHELLCMD
4655 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 return FALSE;
4657
4658 end = gettail(xp->xp_pattern);
4659 if (end == xp->xp_pattern) /* there is no path separator */
4660 return FALSE;
4661
4662 for (s = xp->xp_pattern; s < end; s++)
4663 {
4664 /* Skip escaped wildcards. Only when the backslash is not a path
4665 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4666 if (rem_backslash(s))
4667 ++s;
4668 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4669 return FALSE;
4670 }
4671 return TRUE;
4672}
4673
4674/*
4675 * Prepare a string for expansion.
4676 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004677 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 * When expanding other names: The string will be used with regcomp(). Copy
4679 * the name into allocated memory and prepend "^".
4680 */
4681 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004682addstar(
4683 char_u *fname,
4684 int len,
4685 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 char_u *retval;
4688 int i, j;
4689 int new_len;
4690 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004691 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004693 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004694 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004695 && context != EXPAND_SHELLCMD
4696 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 /*
4699 * Matching will be done internally (on something other than files).
4700 * So we convert the file-matching-type wildcards into our kind for
4701 * use with vim_regcomp(). First work out how long it will be:
4702 */
4703
4704 /* For help tags the translation is done in find_help_tags().
4705 * For a tag pattern starting with "/" no translation is needed. */
4706 if (context == EXPAND_HELP
4707 || context == EXPAND_COLORS
4708 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004709 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004710 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004711 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004712 || ((context == EXPAND_TAGS_LISTFILES
4713 || context == EXPAND_TAGS)
4714 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 retval = vim_strnsave(fname, len);
4716 else
4717 {
4718 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4719 for (i = 0; i < len; i++)
4720 {
4721 if (fname[i] == '*' || fname[i] == '~')
4722 new_len++; /* '*' needs to be replaced by ".*"
4723 '~' needs to be replaced by "\~" */
4724
4725 /* Buffer names are like file names. "." should be literal */
4726 if (context == EXPAND_BUFFERS && fname[i] == '.')
4727 new_len++; /* "." becomes "\." */
4728
4729 /* Custom expansion takes care of special things, match
4730 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004731 if ((context == EXPAND_USER_DEFINED
4732 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 new_len++; /* '\' becomes "\\" */
4734 }
4735 retval = alloc(new_len);
4736 if (retval != NULL)
4737 {
4738 retval[0] = '^';
4739 j = 1;
4740 for (i = 0; i < len; i++, j++)
4741 {
4742 /* Skip backslash. But why? At least keep it for custom
4743 * expansion. */
4744 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004745 && context != EXPAND_USER_LIST
4746 && fname[i] == '\\'
4747 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 break;
4749
4750 switch (fname[i])
4751 {
4752 case '*': retval[j++] = '.';
4753 break;
4754 case '~': retval[j++] = '\\';
4755 break;
4756 case '?': retval[j] = '.';
4757 continue;
4758 case '.': if (context == EXPAND_BUFFERS)
4759 retval[j++] = '\\';
4760 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004761 case '\\': if (context == EXPAND_USER_DEFINED
4762 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 retval[j++] = '\\';
4764 break;
4765 }
4766 retval[j] = fname[i];
4767 }
4768 retval[j] = NUL;
4769 }
4770 }
4771 }
4772 else
4773 {
4774 retval = alloc(len + 4);
4775 if (retval != NULL)
4776 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004777 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004780 * Don't add a star to *, ~, ~user, $var or `cmd`.
4781 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 * ~ would be at the start of the file name, but not the tail.
4783 * $ could be anywhere in the tail.
4784 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004785 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 */
4787 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004788 ends_in_star = (len > 0 && retval[len - 1] == '*');
4789#ifndef BACKSLASH_IN_FILENAME
4790 for (i = len - 2; i >= 0; --i)
4791 {
4792 if (retval[i] != '\\')
4793 break;
4794 ends_in_star = !ends_in_star;
4795 }
4796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004798 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 && vim_strchr(tail, '$') == NULL
4800 && vim_strchr(retval, '`') == NULL)
4801 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004802 else if (len > 0 && retval[len - 1] == '$')
4803 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 retval[len] = NUL;
4805 }
4806 }
4807 return retval;
4808}
4809
4810/*
4811 * Must parse the command line so far to work out what context we are in.
4812 * Completion can then be done based on that context.
4813 * This routine sets the variables:
4814 * xp->xp_pattern The start of the pattern to be expanded within
4815 * the command line (ends at the cursor).
4816 * xp->xp_context The type of thing to expand. Will be one of:
4817 *
4818 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4819 * the command line, like an unknown command. Caller
4820 * should beep.
4821 * EXPAND_NOTHING Unrecognised context for completion, use char like
4822 * a normal char, rather than for completion. eg
4823 * :s/^I/
4824 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4825 * it.
4826 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4827 * EXPAND_FILES After command with XFILE set, or after setting
4828 * with P_EXPAND set. eg :e ^I, :w>>^I
4829 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4830 * when we know only directories are of interest. eg
4831 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004832 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4834 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4835 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4836 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4837 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4838 * EXPAND_EVENTS Complete event names
4839 * EXPAND_SYNTAX Complete :syntax command arguments
4840 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4841 * EXPAND_AUGROUP Complete autocommand group names
4842 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4843 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4844 * eg :unmap a^I , :cunab x^I
4845 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4846 * eg :call sub^I
4847 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4848 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4849 * names in expressions, eg :while s^I
4850 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004851 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 */
4853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004854set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004856 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (ccline.cmdfirstc != ':'
4858#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004859 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004860 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#endif
4862 )
4863 {
4864 xp->xp_context = EXPAND_NOTHING;
4865 return;
4866 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004867 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868}
4869
4870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004871set_cmd_context(
4872 expand_T *xp,
4873 char_u *str, /* start of command line */
4874 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004875 int col, /* position of cursor */
4876 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
4878 int old_char = NUL;
4879 char_u *nextcomm;
4880
4881 /*
4882 * Avoid a UMR warning from Purify, only save the character if it has been
4883 * written before.
4884 */
4885 if (col < len)
4886 old_char = str[col];
4887 str[col] = NUL;
4888 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004889
4890#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004891 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004892 {
4893# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004894 /* pass CMD_SIZE because there is no real command */
4895 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004896# endif
4897 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004898 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004899 {
4900 xp->xp_context = ccline.xp_context;
4901 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004902# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004903 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004904# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004905 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004906 else
4907#endif
4908 while (nextcomm != NULL)
4909 nextcomm = set_one_cmd_context(xp, nextcomm);
4910
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004911 /* Store the string here so that call_user_expand_func() can get to them
4912 * easily. */
4913 xp->xp_line = str;
4914 xp->xp_col = col;
4915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 str[col] = old_char;
4917}
4918
4919/*
4920 * Expand the command line "str" from context "xp".
4921 * "xp" must have been set by set_cmd_context().
4922 * xp->xp_pattern points into "str", to where the text that is to be expanded
4923 * starts.
4924 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4925 * cursor.
4926 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4927 * key that triggered expansion literally.
4928 * Returns EXPAND_OK otherwise.
4929 */
4930 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004931expand_cmdline(
4932 expand_T *xp,
4933 char_u *str, /* start of command line */
4934 int col, /* position of cursor */
4935 int *matchcount, /* return: nr of matches */
4936 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
4938 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004939 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4942 {
4943 beep_flush();
4944 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4945 }
4946 if (xp->xp_context == EXPAND_NOTHING)
4947 {
4948 /* Caller can use the character as a normal char instead */
4949 return EXPAND_NOTHING;
4950 }
4951
4952 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004953 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4954 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (file_str == NULL)
4956 return EXPAND_UNSUCCESSFUL;
4957
Bram Moolenaar94950a92010-12-02 16:01:29 +01004958 if (p_wic)
4959 options += WILD_ICASE;
4960
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004962 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
4964 *matchcount = 0;
4965 *matches = NULL;
4966 }
4967 vim_free(file_str);
4968
4969 return EXPAND_OK;
4970}
4971
4972#ifdef FEAT_MULTI_LANG
4973/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004974 * Cleanup matches for help tags:
4975 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4976 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004978static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004981cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
4983 int i, j;
4984 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004985 char_u buf[4];
4986 char_u *p = buf;
4987
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004988 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004989 {
4990 *p++ = '@';
4991 *p++ = p_hlg[0];
4992 *p++ = p_hlg[1];
4993 }
4994 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 for (i = 0; i < num_file; ++i)
4997 {
4998 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004999 if (len <= 0)
5000 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005001 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
5003 /* Sorting on priority means the same item in another language may
5004 * be anywhere. Search all items for a match up to the "@en". */
5005 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005006 if (j != i && (int)STRLEN(file[j]) == len + 3
5007 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 break;
5009 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005010 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 file[i][len] = NUL;
5012 }
5013 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005014
5015 if (*buf != NUL)
5016 for (i = 0; i < num_file; ++i)
5017 {
5018 len = (int)STRLEN(file[i]) - 3;
5019 if (len <= 0)
5020 continue;
5021 if (STRCMP(file[i] + len, buf) == 0)
5022 {
5023 /* remove the default language */
5024 file[i][len] = NUL;
5025 }
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027}
5028#endif
5029
5030/*
5031 * Do the expansion based on xp->xp_context and "pat".
5032 */
5033 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034ExpandFromContext(
5035 expand_T *xp,
5036 char_u *pat,
5037 int *num_file,
5038 char_u ***file,
5039 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040{
5041#ifdef FEAT_CMDL_COMPL
5042 regmatch_T regmatch;
5043#endif
5044 int ret;
5045 int flags;
5046
5047 flags = EW_DIR; /* include directories */
5048 if (options & WILD_LIST_NOTFOUND)
5049 flags |= EW_NOTFOUND;
5050 if (options & WILD_ADD_SLASH)
5051 flags |= EW_ADDSLASH;
5052 if (options & WILD_KEEP_ALL)
5053 flags |= EW_KEEPALL;
5054 if (options & WILD_SILENT)
5055 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005056 if (options & WILD_ALLLINKS)
5057 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005059 if (xp->xp_context == EXPAND_FILES
5060 || xp->xp_context == EXPAND_DIRECTORIES
5061 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 /*
5064 * Expand file or directory names.
5065 */
5066 int free_pat = FALSE;
5067 int i;
5068
5069 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5070 * space */
5071 if (xp->xp_backslash != XP_BS_NONE)
5072 {
5073 free_pat = TRUE;
5074 pat = vim_strsave(pat);
5075 for (i = 0; pat[i]; ++i)
5076 if (pat[i] == '\\')
5077 {
5078 if (xp->xp_backslash == XP_BS_THREE
5079 && pat[i + 1] == '\\'
5080 && pat[i + 2] == '\\'
5081 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005082 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 if (xp->xp_backslash == XP_BS_ONE
5084 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005085 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 }
5088
5089 if (xp->xp_context == EXPAND_FILES)
5090 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005091 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5092 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 else
5094 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005095 if (options & WILD_ICASE)
5096 flags |= EW_ICASE;
5097
Bram Moolenaard7834d32009-12-02 16:14:36 +00005098 /* Expand wildcards, supporting %:h and the like. */
5099 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (free_pat)
5101 vim_free(pat);
5102 return ret;
5103 }
5104
5105 *file = (char_u **)"";
5106 *num_file = 0;
5107 if (xp->xp_context == EXPAND_HELP)
5108 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005109 /* With an empty argument we would get all the help tags, which is
5110 * very slow. Get matches for "help" instead. */
5111 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5112 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114#ifdef FEAT_MULTI_LANG
5115 cleanup_help_tags(*num_file, *file);
5116#endif
5117 return OK;
5118 }
5119 return FAIL;
5120 }
5121
5122#ifndef FEAT_CMDL_COMPL
5123 return FAIL;
5124#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005125 if (xp->xp_context == EXPAND_SHELLCMD)
5126 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (xp->xp_context == EXPAND_OLD_SETTING)
5128 return ExpandOldSetting(num_file, file);
5129 if (xp->xp_context == EXPAND_BUFFERS)
5130 return ExpandBufnames(pat, num_file, file, options);
5131 if (xp->xp_context == EXPAND_TAGS
5132 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5133 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5134 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005135 {
5136 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005137 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5138 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005141 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005142 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005143 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005145 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005146 {
5147 char *directories[] = {"syntax", 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_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005151 {
5152 char *directories[] = {"syntax", "indent", "ftplugin", 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 Moolenaar35fdbb52005-07-09 21:08:57 +00005155# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5156 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005157 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005158# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005159 if (xp->xp_context == EXPAND_PACKADD)
5160 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5163 if (regmatch.regprog == NULL)
5164 return FAIL;
5165
5166 /* set ignore-case according to p_ic, p_scs and pat */
5167 regmatch.rm_ic = ignorecase(pat);
5168
5169 if (xp->xp_context == EXPAND_SETTINGS
5170 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5171 ret = ExpandSettings(xp, &regmatch, num_file, file);
5172 else if (xp->xp_context == EXPAND_MAPPINGS)
5173 ret = ExpandMappings(&regmatch, num_file, file);
5174# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5175 else if (xp->xp_context == EXPAND_USER_DEFINED)
5176 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5177# endif
5178 else
5179 {
5180 static struct expgen
5181 {
5182 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005183 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005185 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 } tab[] =
5187 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005188 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5189 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005190 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005191 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005192#ifdef FEAT_CMDHIST
5193 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005196 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005197 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005198 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5199 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5200 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
5202#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005203 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5204 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5205 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5206 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#endif
5208#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005209 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5210 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#endif
5212#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005213 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005215#ifdef FEAT_PROFILE
5216 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5217#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005218 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005219 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5220 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005221#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005223#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005224#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005225 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005226#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005227#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005228 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5231 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005232 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5233 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005235 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005236 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005237 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 };
5239 int i;
5240
5241 /*
5242 * Find a context in the table and call the ExpandGeneric() with the
5243 * right function to do the expansion.
5244 */
5245 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005246 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (xp->xp_context == tab[i].context)
5248 {
5249 if (tab[i].ic)
5250 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005251 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005252 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 break;
5254 }
5255 }
5256
Bram Moolenaar473de612013-06-08 18:19:48 +02005257 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 return ret;
5260#endif /* FEAT_CMDL_COMPL */
5261}
5262
5263#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5264/*
5265 * Expand a list of names.
5266 *
5267 * Generic function for command line completion. It calls a function to
5268 * obtain strings, one by one. The strings are matched against a regexp
5269 * program. Matching strings are copied into an array, which is returned.
5270 *
5271 * Returns OK when no problems encountered, FAIL for error (out of memory).
5272 */
5273 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005274ExpandGeneric(
5275 expand_T *xp,
5276 regmatch_T *regmatch,
5277 int *num_file,
5278 char_u ***file,
5279 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005281 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
5283 int i;
5284 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005285 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 char_u *str;
5287
5288 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005289 * round == 0: count the number of matching names
5290 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005292 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 for (i = 0; ; ++i)
5295 {
5296 str = (*func)(xp, i);
5297 if (str == NULL) /* end of list */
5298 break;
5299 if (*str == NUL) /* skip empty strings */
5300 continue;
5301
5302 if (vim_regexec(regmatch, str, (colnr_T)0))
5303 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005304 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005306 if (escaped)
5307 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5308 else
5309 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 (*file)[count] = str;
5311#ifdef FEAT_MENU
5312 if (func == get_menu_names && str != NULL)
5313 {
5314 /* test for separator added by get_menu_names() */
5315 str += STRLEN(str) - 1;
5316 if (*str == '\001')
5317 *str = '.';
5318 }
5319#endif
5320 }
5321 ++count;
5322 }
5323 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005324 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 if (count == 0)
5327 return OK;
5328 *num_file = count;
5329 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5330 if (*file == NULL)
5331 {
5332 *file = (char_u **)"";
5333 return FAIL;
5334 }
5335 count = 0;
5336 }
5337 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005338
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005339 /* Sort the results. Keep menu's in the specified order. */
5340 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005341 {
5342 if (xp->xp_context == EXPAND_EXPRESSION
5343 || xp->xp_context == EXPAND_FUNCTIONS
5344 || xp->xp_context == EXPAND_USER_FUNC)
5345 /* <SNR> functions should be sorted to the end. */
5346 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5347 sort_func_compare);
5348 else
5349 sort_strings(*file, *num_file);
5350 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005351
Bram Moolenaar4f688582007-07-24 12:34:30 +00005352#ifdef FEAT_CMDL_COMPL
5353 /* Reset the variables used for special highlight names expansion, so that
5354 * they don't show up when getting normal highlight names by ID. */
5355 reset_expand_highlight();
5356#endif
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 return OK;
5359}
5360
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005361/*
5362 * Complete a shell command.
5363 * Returns FAIL or OK;
5364 */
5365 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005366expand_shellcmd(
5367 char_u *filepat, /* pattern to match with command names */
5368 int *num_file, /* return: number of matches */
5369 char_u ***file, /* return: array with matches */
5370 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005371{
5372 char_u *pat;
5373 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005374 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005375 int mustfree = FALSE;
5376 garray_T ga;
5377 char_u *buf = alloc(MAXPATHL);
5378 size_t l;
5379 char_u *s, *e;
5380 int flags = flagsarg;
5381 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005382 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005383 hashtab_T found_ht;
5384 hashitem_T *hi;
5385 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005386
5387 if (buf == NULL)
5388 return FAIL;
5389
5390 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5391 * space */
5392 pat = vim_strsave(filepat);
5393 for (i = 0; pat[i]; ++i)
5394 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005395 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005396
Bram Moolenaarb5971142015-03-21 17:32:19 +01005397 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005398
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005399 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5400 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005401 path = (char_u *)".";
5402 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005403 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005404 /* For an absolute name we don't use $PATH. */
5405 if (!mch_isFullName(pat))
5406 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005407 if (path == NULL)
5408 path = (char_u *)"";
5409 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005410
5411 /*
5412 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005413 * collect them in "ga". When "." is not in $PATH also expand for the
5414 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005415 */
5416 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005417 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005418 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005419 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005420#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005421 e = vim_strchr(s, ';');
5422#else
5423 e = vim_strchr(s, ':');
5424#endif
5425 if (e == NULL)
5426 e = s + STRLEN(s);
5427
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005428 if (*s == NUL)
5429 {
5430 if (did_curdir)
5431 break;
5432 // Find directories in the current directory, path is empty.
5433 did_curdir = TRUE;
5434 flags |= EW_DIR;
5435 }
5436 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5437 {
5438 did_curdir = TRUE;
5439 flags |= EW_DIR;
5440 }
5441 else
5442 // Do not match directories inside a $PATH item.
5443 flags &= ~EW_DIR;
5444
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005445 l = e - s;
5446 if (l > MAXPATHL - 5)
5447 break;
5448 vim_strncpy(buf, s, l);
5449 add_pathsep(buf);
5450 l = STRLEN(buf);
5451 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5452
5453 /* Expand matches in one directory of $PATH. */
5454 ret = expand_wildcards(1, &buf, num_file, file, flags);
5455 if (ret == OK)
5456 {
5457 if (ga_grow(&ga, *num_file) == FAIL)
5458 FreeWild(*num_file, *file);
5459 else
5460 {
5461 for (i = 0; i < *num_file; ++i)
5462 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005463 char_u *name = (*file)[i];
5464
5465 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005466 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005467 // Check if this name was already found.
5468 hash = hash_hash(name + l);
5469 hi = hash_lookup(&found_ht, name + l, hash);
5470 if (HASHITEM_EMPTY(hi))
5471 {
5472 // Remove the path that was prepended.
5473 STRMOVE(name, name + l);
5474 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5475 hash_add_item(&found_ht, hi, name, hash);
5476 name = NULL;
5477 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005478 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005479 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005480 }
5481 vim_free(*file);
5482 }
5483 }
5484 if (*e != NUL)
5485 ++e;
5486 }
5487 *file = ga.ga_data;
5488 *num_file = ga.ga_len;
5489
5490 vim_free(buf);
5491 vim_free(pat);
5492 if (mustfree)
5493 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005494 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005495 return OK;
5496}
5497
5498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5500/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005501 * Call "user_expand_func()" to invoke a user defined Vim script function and
5502 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005504 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005506 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507 expand_T *xp,
5508 int *num_file,
5509 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005511 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005512 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005514 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005515 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005516 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
Bram Moolenaarb7515462013-06-29 12:58:33 +02005518 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005519 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 *num_file = 0;
5521 *file = NULL;
5522
Bram Moolenaarb7515462013-06-29 12:58:33 +02005523 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005524 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005525 keep = ccline.cmdbuff[ccline.cmdlen];
5526 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005527 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005528
Bram Moolenaarffa96842018-06-12 22:05:14 +02005529 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5530
5531 args[0].v_type = VAR_STRING;
5532 args[0].vval.v_string = pat;
5533 args[1].v_type = VAR_STRING;
5534 args[1].vval.v_string = xp->xp_line;
5535 args[2].v_type = VAR_NUMBER;
5536 args[2].vval.v_number = xp->xp_col;
5537 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005539 /* Save the cmdline, we don't know what the function may do. */
5540 save_ccline = ccline;
5541 ccline.cmdbuff = NULL;
5542 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005544
Bram Moolenaarded27a12018-08-01 19:06:03 +02005545 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005546
5547 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005549 if (ccline.cmdbuff != NULL)
5550 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005551
Bram Moolenaarffa96842018-06-12 22:05:14 +02005552 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005553 return ret;
5554}
5555
5556/*
5557 * Expand names with a function defined by the user.
5558 */
5559 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ExpandUserDefined(
5561 expand_T *xp,
5562 regmatch_T *regmatch,
5563 int *num_file,
5564 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005565{
5566 char_u *retstr;
5567 char_u *s;
5568 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005569 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005570 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005571 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005572
5573 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5574 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 return FAIL;
5576
5577 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005578 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 e = vim_strchr(s, '\n');
5581 if (e == NULL)
5582 e = s + STRLEN(s);
5583 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005584 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005586 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5587 *e = keep;
5588
5589 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005591 if (ga_grow(&ga, 1) == FAIL)
5592 break;
5593 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5594 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (*e != NUL)
5598 ++e;
5599 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005600 vim_free(retstr);
5601 *file = ga.ga_data;
5602 *num_file = ga.ga_len;
5603 return OK;
5604}
5605
5606/*
5607 * Expand names with a list returned by a function defined by the user.
5608 */
5609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ExpandUserList(
5611 expand_T *xp,
5612 int *num_file,
5613 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005614{
5615 list_T *retlist;
5616 listitem_T *li;
5617 garray_T ga;
5618
5619 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5620 if (retlist == NULL)
5621 return FAIL;
5622
5623 ga_init2(&ga, (int)sizeof(char *), 3);
5624 /* Loop over the items in the list. */
5625 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5626 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005627 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5628 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005629
5630 if (ga_grow(&ga, 1) == FAIL)
5631 break;
5632
5633 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005634 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005635 ++ga.ga_len;
5636 }
5637 list_unref(retlist);
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 *file = ga.ga_data;
5640 *num_file = ga.ga_len;
5641 return OK;
5642}
5643#endif
5644
5645/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005646 * Expand color scheme, compiler or filetype names.
5647 * Search from 'runtimepath':
5648 * 'runtimepath'/{dirnames}/{pat}.vim
5649 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5650 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5651 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5652 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005653 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 */
5655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656ExpandRTDir(
5657 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005658 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659 int *num_file,
5660 char_u ***file,
5661 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 char_u *s;
5664 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005665 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005667 int i;
5668 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670 *num_file = 0;
5671 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005672 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005673 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005675 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005677 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5678 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005680 ga_clear_strings(&ga);
5681 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005683 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005684 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005685 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005687
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005688 if (flags & DIP_START) {
5689 for (i = 0; dirnames[i] != NULL; ++i)
5690 {
5691 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5692 if (s == NULL)
5693 {
5694 ga_clear_strings(&ga);
5695 return FAIL;
5696 }
5697 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5698 globpath(p_pp, s, &ga, 0);
5699 vim_free(s);
5700 }
5701 }
5702
5703 if (flags & DIP_OPT) {
5704 for (i = 0; dirnames[i] != NULL; ++i)
5705 {
5706 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5707 if (s == NULL)
5708 {
5709 ga_clear_strings(&ga);
5710 return FAIL;
5711 }
5712 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5713 globpath(p_pp, s, &ga, 0);
5714 vim_free(s);
5715 }
5716 }
5717
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005718 for (i = 0; i < ga.ga_len; ++i)
5719 {
5720 match = ((char_u **)ga.ga_data)[i];
5721 s = match;
5722 e = s + STRLEN(s);
5723 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5724 {
5725 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005726 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005727 if (s < match || vim_ispathsep(*s))
5728 break;
5729 ++s;
5730 *e = NUL;
5731 mch_memmove(match, s, e - s + 1);
5732 }
5733 }
5734
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005735 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005736 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005737
5738 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005739 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005740 remove_duplicates(&ga);
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 *file = ga.ga_data;
5743 *num_file = ga.ga_len;
5744 return OK;
5745}
5746
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005747/*
5748 * Expand loadplugin names:
5749 * 'packpath'/pack/ * /opt/{pat}
5750 */
5751 static int
5752ExpandPackAddDir(
5753 char_u *pat,
5754 int *num_file,
5755 char_u ***file)
5756{
5757 char_u *s;
5758 char_u *e;
5759 char_u *match;
5760 garray_T ga;
5761 int i;
5762 int pat_len;
5763
5764 *num_file = 0;
5765 *file = NULL;
5766 pat_len = (int)STRLEN(pat);
5767 ga_init2(&ga, (int)sizeof(char *), 10);
5768
5769 s = alloc((unsigned)(pat_len + 26));
5770 if (s == NULL)
5771 {
5772 ga_clear_strings(&ga);
5773 return FAIL;
5774 }
5775 sprintf((char *)s, "pack/*/opt/%s*", pat);
5776 globpath(p_pp, s, &ga, 0);
5777 vim_free(s);
5778
5779 for (i = 0; i < ga.ga_len; ++i)
5780 {
5781 match = ((char_u **)ga.ga_data)[i];
5782 s = gettail(match);
5783 e = s + STRLEN(s);
5784 mch_memmove(match, s, e - s + 1);
5785 }
5786
5787 if (ga.ga_len == 0)
5788 return FAIL;
5789
5790 /* Sort and remove duplicates which can happen when specifying multiple
5791 * directories in dirnames. */
5792 remove_duplicates(&ga);
5793
5794 *file = ga.ga_data;
5795 *num_file = ga.ga_len;
5796 return OK;
5797}
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799#endif
5800
5801#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5802/*
5803 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005804 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005806 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005807globpath(
5808 char_u *path,
5809 char_u *file,
5810 garray_T *ga,
5811 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 expand_T xpc;
5814 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 int num_p;
5817 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
5819 buf = alloc(MAXPATHL);
5820 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005821 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005823 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 /* Loop over all entries in {path}. */
5827 while (*path != NUL)
5828 {
5829 /* Copy one item of the path to buf[] and concatenate the file name. */
5830 copy_option_part(&path, buf, MAXPATHL, ",");
5831 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5832 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005833# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005834 /* Using the platform's path separator (\) makes vim incorrectly
5835 * treat it as an escape character, use '/' instead. */
5836 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5837 STRCAT(buf, "/");
5838# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005842 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5843 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005845 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005847 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 for (i = 0; i < num_p; ++i)
5850 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005851 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005852 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005853 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 FreeWild(num_p, p);
5858 }
5859 }
5860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863}
5864
5865#endif
5866
5867#if defined(FEAT_CMDHIST) || defined(PROTO)
5868
5869/*********************************
5870 * Command line history stuff *
5871 *********************************/
5872
5873/*
5874 * Translate a history character to the associated type number.
5875 */
5876 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005877hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 if (c == ':')
5880 return HIST_CMD;
5881 if (c == '=')
5882 return HIST_EXPR;
5883 if (c == '@')
5884 return HIST_INPUT;
5885 if (c == '>')
5886 return HIST_DEBUG;
5887 return HIST_SEARCH; /* must be '?' or '/' */
5888}
5889
5890/*
5891 * Table of history names.
5892 * These names are used in :history and various hist...() functions.
5893 * It is sufficient to give the significant prefix of a history name.
5894 */
5895
5896static char *(history_names[]) =
5897{
5898 "cmd",
5899 "search",
5900 "expr",
5901 "input",
5902 "debug",
5903 NULL
5904};
5905
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005906#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5907/*
5908 * Function given to ExpandGeneric() to obtain the possible first
5909 * arguments of the ":history command.
5910 */
5911 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005912get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005913{
5914 static char_u compl[2] = { NUL, NUL };
5915 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005916 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005917 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5918
5919 if (idx < short_names_count)
5920 {
5921 compl[0] = (char_u)short_names[idx];
5922 return compl;
5923 }
5924 if (idx < short_names_count + history_name_count)
5925 return (char_u *)history_names[idx - short_names_count];
5926 if (idx == short_names_count + history_name_count)
5927 return (char_u *)"all";
5928 return NULL;
5929}
5930#endif
5931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932/*
5933 * init_history() - Initialize the command line history.
5934 * Also used to re-allocate the history when the size changes.
5935 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005936 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005937init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 int newlen; /* new length of history table */
5940 histentry_T *temp;
5941 int i;
5942 int j;
5943 int type;
5944
5945 /*
5946 * If size of history table changed, reallocate it
5947 */
5948 newlen = (int)p_hi;
5949 if (newlen != hislen) /* history length changed */
5950 {
5951 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5952 {
5953 if (newlen)
5954 {
5955 temp = (histentry_T *)lalloc(
5956 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5957 if (temp == NULL) /* out of memory! */
5958 {
5959 if (type == 0) /* first one: just keep the old length */
5960 {
5961 newlen = hislen;
5962 break;
5963 }
5964 /* Already changed one table, now we can only have zero
5965 * length for all tables. */
5966 newlen = 0;
5967 type = -1;
5968 continue;
5969 }
5970 }
5971 else
5972 temp = NULL;
5973 if (newlen == 0 || temp != NULL)
5974 {
5975 if (hisidx[type] < 0) /* there are no entries yet */
5976 {
5977 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005978 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980 else if (newlen > hislen) /* array becomes bigger */
5981 {
5982 for (i = 0; i <= hisidx[type]; ++i)
5983 temp[i] = history[type][i];
5984 j = i;
5985 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005986 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 for ( ; j < hislen; ++i, ++j)
5988 temp[i] = history[type][j];
5989 }
5990 else /* array becomes smaller or 0 */
5991 {
5992 j = hisidx[type];
5993 for (i = newlen - 1; ; --i)
5994 {
5995 if (i >= 0) /* copy newest entries */
5996 temp[i] = history[type][j];
5997 else /* remove older entries */
5998 vim_free(history[type][j].hisstr);
5999 if (--j < 0)
6000 j = hislen - 1;
6001 if (j == hisidx[type])
6002 break;
6003 }
6004 hisidx[type] = newlen - 1;
6005 }
6006 vim_free(history[type]);
6007 history[type] = temp;
6008 }
6009 }
6010 hislen = newlen;
6011 }
6012}
6013
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006016{
6017 hisptr->hisnum = 0;
6018 hisptr->viminfo = FALSE;
6019 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006020 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006021}
6022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023/*
6024 * Check if command line 'str' is already in history.
6025 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6026 */
6027 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006028in_history(
6029 int type,
6030 char_u *str,
6031 int move_to_front, /* Move the entry to the front if it exists */
6032 int sep,
6033 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
6035 int i;
6036 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006037 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039 if (hisidx[type] < 0)
6040 return FALSE;
6041 i = hisidx[type];
6042 do
6043 {
6044 if (history[type][i].hisstr == NULL)
6045 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006046
6047 /* For search history, check that the separator character matches as
6048 * well. */
6049 p = history[type][i].hisstr;
6050 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006051 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006052 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
6054 if (!move_to_front)
6055 return TRUE;
6056 last_i = i;
6057 break;
6058 }
6059 if (--i < 0)
6060 i = hislen - 1;
6061 } while (i != hisidx[type]);
6062
6063 if (last_i >= 0)
6064 {
6065 str = history[type][i].hisstr;
6066 while (i != hisidx[type])
6067 {
6068 if (++i >= hislen)
6069 i = 0;
6070 history[type][last_i] = history[type][i];
6071 last_i = i;
6072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006074 history[type][i].viminfo = FALSE;
6075 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006076 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 return TRUE;
6078 }
6079 return FALSE;
6080}
6081
6082/*
6083 * Convert history name (from table above) to its HIST_ equivalent.
6084 * When "name" is empty, return "cmd" history.
6085 * Returns -1 for unknown history name.
6086 */
6087 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006088get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 int i;
6091 int len = (int)STRLEN(name);
6092
6093 /* No argument: use current history. */
6094 if (len == 0)
6095 return hist_char2type(ccline.cmdfirstc);
6096
6097 for (i = 0; history_names[i] != NULL; ++i)
6098 if (STRNICMP(name, history_names[i], len) == 0)
6099 return i;
6100
6101 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6102 return hist_char2type(name[0]);
6103
6104 return -1;
6105}
6106
6107static int last_maptick = -1; /* last seen maptick */
6108
6109/*
6110 * Add the given string to the given history. If the string is already in the
6111 * history then it is moved to the front. "histype" may be one of he HIST_
6112 * values.
6113 */
6114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006115add_to_history(
6116 int histype,
6117 char_u *new_entry,
6118 int in_map, /* consider maptick when inside a mapping */
6119 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 histentry_T *hisptr;
6122 int len;
6123
6124 if (hislen == 0) /* no history */
6125 return;
6126
Bram Moolenaara939e432013-11-09 05:30:26 +01006127 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6128 return;
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 /*
6131 * Searches inside the same mapping overwrite each other, so that only
6132 * the last line is kept. Be careful not to remove a line that was moved
6133 * down, only lines that were added.
6134 */
6135 if (histype == HIST_SEARCH && in_map)
6136 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006137 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
6139 /* Current line is from the same mapping, remove it */
6140 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6141 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006142 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 --hisnum[histype];
6144 if (--hisidx[HIST_SEARCH] < 0)
6145 hisidx[HIST_SEARCH] = hislen - 1;
6146 }
6147 last_maptick = -1;
6148 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006149 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 {
6151 if (++hisidx[histype] == hislen)
6152 hisidx[histype] = 0;
6153 hisptr = &history[histype][hisidx[histype]];
6154 vim_free(hisptr->hisstr);
6155
6156 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006157 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6159 if (hisptr->hisstr != NULL)
6160 hisptr->hisstr[len + 1] = sep;
6161
6162 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006163 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006164 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (histype == HIST_SEARCH && in_map)
6166 last_maptick = maptick;
6167 }
6168}
6169
6170#if defined(FEAT_EVAL) || defined(PROTO)
6171
6172/*
6173 * Get identifier of newest history entry.
6174 * "histype" may be one of the HIST_ values.
6175 */
6176 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006177get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6180 || hisidx[histype] < 0)
6181 return -1;
6182
6183 return history[histype][hisidx[histype]].hisnum;
6184}
6185
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 * Calculate history index from a number:
6188 * num > 0: seen as identifying number of a history entry
6189 * num < 0: relative position in history wrt newest entry
6190 * "histype" may be one of the HIST_ values.
6191 */
6192 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 int i;
6196 histentry_T *hist;
6197 int wrapped = FALSE;
6198
6199 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6200 || (i = hisidx[histype]) < 0 || num == 0)
6201 return -1;
6202
6203 hist = history[histype];
6204 if (num > 0)
6205 {
6206 while (hist[i].hisnum > num)
6207 if (--i < 0)
6208 {
6209 if (wrapped)
6210 break;
6211 i += hislen;
6212 wrapped = TRUE;
6213 }
6214 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6215 return i;
6216 }
6217 else if (-num <= hislen)
6218 {
6219 i += num + 1;
6220 if (i < 0)
6221 i += hislen;
6222 if (hist[i].hisstr != NULL)
6223 return i;
6224 }
6225 return -1;
6226}
6227
6228/*
6229 * Get a history entry by its index.
6230 * "histype" may be one of the HIST_ values.
6231 */
6232 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006233get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
6235 idx = calc_hist_idx(histype, idx);
6236 if (idx >= 0)
6237 return history[histype][idx].hisstr;
6238 else
6239 return (char_u *)"";
6240}
6241
6242/*
6243 * Clear all entries of a history.
6244 * "histype" may be one of the HIST_ values.
6245 */
6246 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006247clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 int i;
6250 histentry_T *hisptr;
6251
6252 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6253 {
6254 hisptr = history[histype];
6255 for (i = hislen; i--;)
6256 {
6257 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006258 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006259 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
6261 hisidx[histype] = -1; /* mark history as cleared */
6262 hisnum[histype] = 0; /* reset identifier counter */
6263 return OK;
6264 }
6265 return FAIL;
6266}
6267
6268/*
6269 * Remove all entries matching {str} from a history.
6270 * "histype" may be one of the HIST_ values.
6271 */
6272 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006273del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 regmatch_T regmatch;
6276 histentry_T *hisptr;
6277 int idx;
6278 int i;
6279 int last;
6280 int found = FALSE;
6281
6282 regmatch.regprog = NULL;
6283 regmatch.rm_ic = FALSE; /* always match case */
6284 if (hislen != 0
6285 && histype >= 0
6286 && histype < HIST_COUNT
6287 && *str != NUL
6288 && (idx = hisidx[histype]) >= 0
6289 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6290 != NULL)
6291 {
6292 i = last = idx;
6293 do
6294 {
6295 hisptr = &history[histype][i];
6296 if (hisptr->hisstr == NULL)
6297 break;
6298 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6299 {
6300 found = TRUE;
6301 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006302 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 }
6304 else
6305 {
6306 if (i != last)
6307 {
6308 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006309 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 }
6311 if (--last < 0)
6312 last += hislen;
6313 }
6314 if (--i < 0)
6315 i += hislen;
6316 } while (i != idx);
6317 if (history[histype][idx].hisstr == NULL)
6318 hisidx[histype] = -1;
6319 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006320 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 return found;
6322}
6323
6324/*
6325 * Remove an indexed entry from a history.
6326 * "histype" may be one of the HIST_ values.
6327 */
6328 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006329del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 int i, j;
6332
6333 i = calc_hist_idx(histype, idx);
6334 if (i < 0)
6335 return FALSE;
6336 idx = hisidx[histype];
6337 vim_free(history[histype][i].hisstr);
6338
6339 /* When deleting the last added search string in a mapping, reset
6340 * last_maptick, so that the last added search string isn't deleted again.
6341 */
6342 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6343 last_maptick = -1;
6344
6345 while (i != idx)
6346 {
6347 j = (i + 1) % hislen;
6348 history[histype][i] = history[histype][j];
6349 i = j;
6350 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006351 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if (--i < 0)
6353 i += hislen;
6354 hisidx[histype] = i;
6355 return TRUE;
6356}
6357
6358#endif /* FEAT_EVAL */
6359
6360#if defined(FEAT_CRYPT) || defined(PROTO)
6361/*
6362 * Very specific function to remove the value in ":set key=val" from the
6363 * history.
6364 */
6365 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006366remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 char_u *p;
6369 int i;
6370
6371 i = hisidx[HIST_CMD];
6372 if (i < 0)
6373 return;
6374 p = history[HIST_CMD][i].hisstr;
6375 if (p != NULL)
6376 for ( ; *p; ++p)
6377 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6378 {
6379 p = vim_strchr(p + 3, '=');
6380 if (p == NULL)
6381 break;
6382 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006383 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 if (p[i] == '\\' && p[i + 1])
6385 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006386 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 --p;
6388 }
6389}
6390#endif
6391
6392#endif /* FEAT_CMDHIST */
6393
Bram Moolenaar064154c2016-03-19 22:50:43 +01006394#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006395/*
6396 * Get pointer to the command line info to use. cmdline_paste() may clear
6397 * ccline and put the previous value in prev_ccline.
6398 */
6399 static struct cmdline_info *
6400get_ccline_ptr(void)
6401{
6402 if ((State & CMDLINE) == 0)
6403 return NULL;
6404 if (ccline.cmdbuff != NULL)
6405 return &ccline;
6406 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6407 return &prev_ccline;
6408 return NULL;
6409}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006410#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006411
Bram Moolenaar064154c2016-03-19 22:50:43 +01006412#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006413/*
6414 * Get the current command line in allocated memory.
6415 * Only works when the command line is being edited.
6416 * Returns NULL when something is wrong.
6417 */
6418 char_u *
6419get_cmdline_str(void)
6420{
6421 struct cmdline_info *p = get_ccline_ptr();
6422
6423 if (p == NULL)
6424 return NULL;
6425 return vim_strnsave(p->cmdbuff, p->cmdlen);
6426}
6427
6428/*
6429 * Get the current command line position, counted in bytes.
6430 * Zero is the first position.
6431 * Only works when the command line is being edited.
6432 * Returns -1 when something is wrong.
6433 */
6434 int
6435get_cmdline_pos(void)
6436{
6437 struct cmdline_info *p = get_ccline_ptr();
6438
6439 if (p == NULL)
6440 return -1;
6441 return p->cmdpos;
6442}
6443
6444/*
6445 * Set the command line byte position to "pos". Zero is the first position.
6446 * Only works when the command line is being edited.
6447 * Returns 1 when failed, 0 when OK.
6448 */
6449 int
6450set_cmdline_pos(
6451 int pos)
6452{
6453 struct cmdline_info *p = get_ccline_ptr();
6454
6455 if (p == NULL)
6456 return 1;
6457
6458 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6459 * changed the command line. */
6460 if (pos < 0)
6461 new_cmdpos = 0;
6462 else
6463 new_cmdpos = pos;
6464 return 0;
6465}
6466#endif
6467
6468#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6469/*
6470 * Get the current command-line type.
6471 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6472 * Only works when the command line is being edited.
6473 * Returns NUL when something is wrong.
6474 */
6475 int
6476get_cmdline_type(void)
6477{
6478 struct cmdline_info *p = get_ccline_ptr();
6479
6480 if (p == NULL)
6481 return NUL;
6482 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006483 return
6484# ifdef FEAT_EVAL
6485 (p->input_fn) ? '@' :
6486# endif
6487 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006488 return p->cmdfirstc;
6489}
6490#endif
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6493/*
6494 * Get indices "num1,num2" that specify a range within a list (not a range of
6495 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6496 * Returns OK if parsed successfully, otherwise FAIL.
6497 */
6498 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006499get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
6501 int len;
6502 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006503 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
6505 *str = skipwhite(*str);
6506 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6507 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006508 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 *str += len;
6510 *num1 = (int)num;
6511 first = TRUE;
6512 }
6513 *str = skipwhite(*str);
6514 if (**str == ',') /* parse "to" part of range */
6515 {
6516 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006517 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (len > 0)
6519 {
6520 *num2 = (int)num;
6521 *str = skipwhite(*str + len);
6522 }
6523 else if (!first) /* no number given at all */
6524 return FAIL;
6525 }
6526 else if (first) /* only one number given */
6527 *num2 = *num1;
6528 return OK;
6529}
6530#endif
6531
6532#if defined(FEAT_CMDHIST) || defined(PROTO)
6533/*
6534 * :history command - print a history
6535 */
6536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006537ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538{
6539 histentry_T *hist;
6540 int histype1 = HIST_CMD;
6541 int histype2 = HIST_CMD;
6542 int hisidx1 = 1;
6543 int hisidx2 = -1;
6544 int idx;
6545 int i, j, k;
6546 char_u *end;
6547 char_u *arg = eap->arg;
6548
6549 if (hislen == 0)
6550 {
6551 MSG(_("'history' option is zero"));
6552 return;
6553 }
6554
6555 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6556 {
6557 end = arg;
6558 while (ASCII_ISALPHA(*end)
6559 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6560 end++;
6561 i = *end;
6562 *end = NUL;
6563 histype1 = get_histtype(arg);
6564 if (histype1 == -1)
6565 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006566 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
6568 histype1 = 0;
6569 histype2 = HIST_COUNT-1;
6570 }
6571 else
6572 {
6573 *end = i;
6574 EMSG(_(e_trailing));
6575 return;
6576 }
6577 }
6578 else
6579 histype2 = histype1;
6580 *end = i;
6581 }
6582 else
6583 end = arg;
6584 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6585 {
6586 EMSG(_(e_trailing));
6587 return;
6588 }
6589
6590 for (; !got_int && histype1 <= histype2; ++histype1)
6591 {
6592 STRCPY(IObuff, "\n # ");
6593 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6594 MSG_PUTS_TITLE(IObuff);
6595 idx = hisidx[histype1];
6596 hist = history[histype1];
6597 j = hisidx1;
6598 k = hisidx2;
6599 if (j < 0)
6600 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6601 if (k < 0)
6602 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6603 if (idx >= 0 && j <= k)
6604 for (i = idx + 1; !got_int; ++i)
6605 {
6606 if (i == hislen)
6607 i = 0;
6608 if (hist[i].hisstr != NULL
6609 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6610 {
6611 msg_putchar('\n');
6612 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6613 hist[i].hisnum);
6614 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6615 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006616 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 else
6618 STRCAT(IObuff, hist[i].hisstr);
6619 msg_outtrans(IObuff);
6620 out_flush();
6621 }
6622 if (i == idx)
6623 break;
6624 }
6625 }
6626}
6627#endif
6628
6629#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006630/*
6631 * Buffers for history read from a viminfo file. Only valid while reading.
6632 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006633static histentry_T *viminfo_history[HIST_COUNT] =
6634 {NULL, NULL, NULL, NULL, NULL};
6635static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6636static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637static int viminfo_add_at_front = FALSE;
6638
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006639static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
6641/*
6642 * Translate a history type number to the associated character.
6643 */
6644 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645hist_type2char(
6646 int type,
6647 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 if (type == HIST_CMD)
6650 return ':';
6651 if (type == HIST_SEARCH)
6652 {
6653 if (use_question)
6654 return '?';
6655 else
6656 return '/';
6657 }
6658 if (type == HIST_EXPR)
6659 return '=';
6660 return '@';
6661}
6662
6663/*
6664 * Prepare for reading the history from the viminfo file.
6665 * This allocates history arrays to store the read history lines.
6666 */
6667 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006668prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 int i;
6671 int num;
6672 int type;
6673 int len;
6674
6675 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006676 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (asklen > hislen)
6678 asklen = hislen;
6679
6680 for (type = 0; type < HIST_COUNT; ++type)
6681 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006682 /* Count the number of empty spaces in the history list. Entries read
6683 * from viminfo previously are also considered empty. If there are
6684 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006686 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 num++;
6688 len = asklen;
6689 if (num > len)
6690 len = num;
6691 if (len <= 0)
6692 viminfo_history[type] = NULL;
6693 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006694 viminfo_history[type] = (histentry_T *)lalloc(
6695 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if (viminfo_history[type] == NULL)
6697 len = 0;
6698 viminfo_hislen[type] = len;
6699 viminfo_hisidx[type] = 0;
6700 }
6701}
6702
6703/*
6704 * Accept a line from the viminfo, store it in the history array when it's
6705 * new.
6706 */
6707 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006708read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 int type;
6711 long_u len;
6712 char_u *val;
6713 char_u *p;
6714
6715 type = hist_char2type(virp->vir_line[0]);
6716 if (viminfo_hisidx[type] < viminfo_hislen[type])
6717 {
6718 val = viminfo_readstring(virp, 1, TRUE);
6719 if (val != NULL && *val != NUL)
6720 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006721 int sep = (*val == ' ' ? NUL : *val);
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006724 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {
6726 /* Need to re-allocate to append the separator byte. */
6727 len = STRLEN(val);
6728 p = lalloc(len + 2, TRUE);
6729 if (p != NULL)
6730 {
6731 if (type == HIST_SEARCH)
6732 {
6733 /* Search entry: Move the separator from the first
6734 * column to after the NUL. */
6735 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006736 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
6738 else
6739 {
6740 /* Not a search entry: No separator in the viminfo
6741 * file, add a NUL separator. */
6742 mch_memmove(p, val, (size_t)len + 1);
6743 p[len + 1] = NUL;
6744 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006745 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6746 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006747 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6748 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006749 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 }
6751 }
6752 }
6753 vim_free(val);
6754 }
6755 return viminfo_readline(virp);
6756}
6757
Bram Moolenaar07219f92013-04-14 23:19:36 +02006758/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006759 * Accept a new style history line from the viminfo, store it in the history
6760 * array when it's new.
6761 */
6762 void
6763handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006764 garray_T *values,
6765 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006766{
6767 int type;
6768 long_u len;
6769 char_u *val;
6770 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006771 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006772
6773 /* Check the format:
6774 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006775 if (values->ga_len < 4
6776 || vp[0].bv_type != BVAL_NR
6777 || vp[1].bv_type != BVAL_NR
6778 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6779 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006780 return;
6781
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006782 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006783 if (type >= HIST_COUNT)
6784 return;
6785 if (viminfo_hisidx[type] < viminfo_hislen[type])
6786 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006787 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006788 if (val != NULL && *val != NUL)
6789 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006790 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6791 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006792 int idx;
6793 int overwrite = FALSE;
6794
6795 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6796 {
6797 /* If lines were written by an older Vim we need to avoid
6798 * getting duplicates. See if the entry already exists. */
6799 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6800 {
6801 p = viminfo_history[type][idx].hisstr;
6802 if (STRCMP(val, p) == 0
6803 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6804 {
6805 overwrite = TRUE;
6806 break;
6807 }
6808 }
6809
6810 if (!overwrite)
6811 {
6812 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006813 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006814 p = lalloc(len + 2, TRUE);
6815 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006816 else
6817 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006818 if (p != NULL)
6819 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006820 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006821 if (!overwrite)
6822 {
6823 mch_memmove(p, val, (size_t)len + 1);
6824 /* Put the separator after the NUL. */
6825 p[len + 1] = sep;
6826 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006827 viminfo_history[type][idx].hisnum = 0;
6828 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006829 viminfo_hisidx[type]++;
6830 }
6831 }
6832 }
6833 }
6834 }
6835}
6836
6837/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006838 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006839 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006840 static void
6841concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 int idx;
6844 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006845
6846 idx = hisidx[type] + viminfo_hisidx[type];
6847 if (idx >= hislen)
6848 idx -= hislen;
6849 else if (idx < 0)
6850 idx = hislen - 1;
6851 if (viminfo_add_at_front)
6852 hisidx[type] = idx;
6853 else
6854 {
6855 if (hisidx[type] == -1)
6856 hisidx[type] = hislen - 1;
6857 do
6858 {
6859 if (history[type][idx].hisstr != NULL
6860 || history[type][idx].viminfo)
6861 break;
6862 if (++idx == hislen)
6863 idx = 0;
6864 } while (idx != hisidx[type]);
6865 if (idx != hisidx[type] && --idx < 0)
6866 idx = hislen - 1;
6867 }
6868 for (i = 0; i < viminfo_hisidx[type]; i++)
6869 {
6870 vim_free(history[type][idx].hisstr);
6871 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6872 history[type][idx].viminfo = TRUE;
6873 history[type][idx].time_set = viminfo_history[type][i].time_set;
6874 if (--idx < 0)
6875 idx = hislen - 1;
6876 }
6877 idx += 1;
6878 idx %= hislen;
6879 for (i = 0; i < viminfo_hisidx[type]; i++)
6880 {
6881 history[type][idx++].hisnum = ++hisnum[type];
6882 idx %= hislen;
6883 }
6884}
6885
6886#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6887 static int
6888#ifdef __BORLANDC__
6889_RTLENTRYF
6890#endif
6891sort_hist(const void *s1, const void *s2)
6892{
6893 histentry_T *p1 = *(histentry_T **)s1;
6894 histentry_T *p2 = *(histentry_T **)s2;
6895
6896 if (p1->time_set < p2->time_set) return -1;
6897 if (p1->time_set > p2->time_set) return 1;
6898 return 0;
6899}
6900#endif
6901
6902/*
6903 * Merge history lines from viminfo and lines typed in this Vim based on the
6904 * timestamp;
6905 */
6906 static void
6907merge_history(int type)
6908{
6909 int max_len;
6910 histentry_T **tot_hist;
6911 histentry_T *new_hist;
6912 int i;
6913 int len;
6914
6915 /* Make one long list with all entries. */
6916 max_len = hislen + viminfo_hisidx[type];
6917 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6918 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6919 if (tot_hist == NULL || new_hist == NULL)
6920 {
6921 vim_free(tot_hist);
6922 vim_free(new_hist);
6923 return;
6924 }
6925 for (i = 0; i < viminfo_hisidx[type]; i++)
6926 tot_hist[i] = &viminfo_history[type][i];
6927 len = i;
6928 for (i = 0; i < hislen; i++)
6929 if (history[type][i].hisstr != NULL)
6930 tot_hist[len++] = &history[type][i];
6931
6932 /* Sort the list on timestamp. */
6933 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6934
6935 /* Keep the newest ones. */
6936 for (i = 0; i < hislen; i++)
6937 {
6938 if (i < len)
6939 {
6940 new_hist[i] = *tot_hist[i];
6941 tot_hist[i]->hisstr = NULL;
6942 if (new_hist[i].hisnum == 0)
6943 new_hist[i].hisnum = ++hisnum[type];
6944 }
6945 else
6946 clear_hist_entry(&new_hist[i]);
6947 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006948 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006949
6950 /* Free what is not kept. */
6951 for (i = 0; i < viminfo_hisidx[type]; i++)
6952 vim_free(viminfo_history[type][i].hisstr);
6953 for (i = 0; i < hislen; i++)
6954 vim_free(history[type][i].hisstr);
6955 vim_free(history[type]);
6956 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006957 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006958}
6959
6960/*
6961 * Finish reading history lines from viminfo. Not used when writing viminfo.
6962 */
6963 void
6964finish_viminfo_history(vir_T *virp)
6965{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006967 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
6969 for (type = 0; type < HIST_COUNT; ++type)
6970 {
6971 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006972 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006973
6974 if (merge)
6975 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006977 concat_history(type);
6978
Bram Moolenaard23a8232018-02-10 18:45:26 +01006979 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006980 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 }
6982}
6983
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006984/*
6985 * Write history to viminfo file in "fp".
6986 * When "merge" is TRUE merge history lines with a previously read viminfo
6987 * file, data is in viminfo_history[].
6988 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006991write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 int i;
6994 int type;
6995 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006996 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 init_history();
6999 if (hislen == 0)
7000 return;
7001 for (type = 0; type < HIST_COUNT; ++type)
7002 {
7003 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7004 if (num_saved == 0)
7005 continue;
7006 if (num_saved < 0) /* Use default */
7007 num_saved = hislen;
7008 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7009 type == HIST_CMD ? _("Command Line") :
7010 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007011 type == HIST_EXPR ? _("Expression") :
7012 type == HIST_INPUT ? _("Input Line") :
7013 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (num_saved > hislen)
7015 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007016
7017 /*
7018 * Merge typed and viminfo history:
7019 * round 1: history of typed commands.
7020 * round 2: history from recently read viminfo.
7021 */
7022 for (round = 1; round <= 2; ++round)
7023 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007024 if (round == 1)
7025 /* start at newest entry, somewhere in the list */
7026 i = hisidx[type];
7027 else if (viminfo_hisidx[type] > 0)
7028 /* start at newest entry, first in the list */
7029 i = 0;
7030 else
7031 /* empty list */
7032 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007033 if (i >= 0)
7034 while (num_saved > 0
7035 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007037 char_u *p;
7038 time_t timestamp;
7039 int c = NUL;
7040
7041 if (round == 1)
7042 {
7043 p = history[type][i].hisstr;
7044 timestamp = history[type][i].time_set;
7045 }
7046 else
7047 {
7048 p = viminfo_history[type] == NULL ? NULL
7049 : viminfo_history[type][i].hisstr;
7050 timestamp = viminfo_history[type] == NULL ? 0
7051 : viminfo_history[type][i].time_set;
7052 }
7053
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007054 if (p != NULL && (round == 2
7055 || !merge
7056 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007058 --num_saved;
7059 fputc(hist_type2char(type, TRUE), fp);
7060 /* For the search history: put the separator in the
7061 * second column; use a space if there isn't one. */
7062 if (type == HIST_SEARCH)
7063 {
7064 c = p[STRLEN(p) + 1];
7065 putc(c == NUL ? ' ' : c, fp);
7066 }
7067 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007068
7069 {
7070 char cbuf[NUMBUFLEN];
7071
7072 /* New style history with a bar line. Format:
7073 * |{bartype},{histtype},{timestamp},{separator},"text" */
7074 if (c == NUL)
7075 cbuf[0] = NUL;
7076 else
7077 sprintf(cbuf, "%d", c);
7078 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7079 type, (long)timestamp, cbuf);
7080 barline_writestring(fp, p, LSIZE - 20);
7081 putc('\n', fp);
7082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007084 if (round == 1)
7085 {
7086 /* Decrement index, loop around and stop when back at
7087 * the start. */
7088 if (--i < 0)
7089 i = hislen - 1;
7090 if (i == hisidx[type])
7091 break;
7092 }
7093 else
7094 {
7095 /* Increment index. Stop at the end in the while. */
7096 ++i;
7097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007099 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007100 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007101 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007102 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007103 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007104 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
7106}
7107#endif /* FEAT_VIMINFO */
7108
7109#if defined(FEAT_FKMAP) || defined(PROTO)
7110/*
7111 * Write a character at the current cursor+offset position.
7112 * It is directly written into the command buffer block.
7113 */
7114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007115cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
7117 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7118 {
7119 EMSG(_("E198: cmd_pchar beyond the command length"));
7120 return;
7121 }
7122 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7123 ccline.cmdbuff[ccline.cmdlen] = NUL;
7124}
7125
7126 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007127cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7130 {
7131 /* EMSG(_("cmd_gchar beyond the command length")); */
7132 return NUL;
7133 }
7134 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7135}
7136#endif
7137
7138#if defined(FEAT_CMDWIN) || defined(PROTO)
7139/*
7140 * Open a window on the current command line and history. Allow editing in
7141 * the window. Returns when the window is closed.
7142 * Returns:
7143 * CR if the command is to be executed
7144 * Ctrl_C if it is to be abandoned
7145 * K_IGNORE if editing continues
7146 */
7147 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007148open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007151 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007153 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 win_T *wp;
7155 int i;
7156 linenr_T lnum;
7157 int histtype;
7158 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 int save_restart_edit = restart_edit;
7160 int save_State = State;
7161 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007162#ifdef FEAT_RIGHTLEFT
7163 int save_cmdmsg_rl = cmdmsg_rl;
7164#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007165#ifdef FEAT_FOLDING
7166 int save_KeyTyped;
7167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
7169 /* Can't do this recursively. Can't do it when typing a password. */
7170 if (cmdwin_type != 0
7171# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7172 || cmdline_star > 0
7173# endif
7174 )
7175 {
7176 beep_flush();
7177 return K_IGNORE;
7178 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007179 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
7181 /* Save current window sizes. */
7182 win_size_save(&winsizes);
7183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007185 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007186
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007187 /* don't use a new tab page */
7188 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007189 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 /* Create a window for the command-line buffer. */
7192 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7193 {
7194 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007195 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 return K_IGNORE;
7197 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007198 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
7200 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007201 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007202 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007205#ifdef FEAT_FOLDING
7206 curwin->w_p_fen = FALSE;
7207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007209 curwin->w_p_rl = cmdmsg_rl;
7210 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007212 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007215 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007216 /* But don't allow switching to another buffer. */
7217 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
Bram Moolenaar46152342005-09-07 21:18:43 +00007219 /* Showing the prompt may have set need_wait_return, reset it. */
7220 need_wait_return = FALSE;
7221
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007222 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7224 {
7225 if (p_wc == TAB)
7226 {
7227 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7228 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7229 }
7230 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7231 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007232 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007234 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7235 * sets 'textwidth' to 78). */
7236 curbuf->b_p_tw = 0;
7237
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 /* Fill the buffer with the history. */
7239 init_history();
7240 if (hislen > 0)
7241 {
7242 i = hisidx[histtype];
7243 if (i >= 0)
7244 {
7245 lnum = 0;
7246 do
7247 {
7248 if (++i == hislen)
7249 i = 0;
7250 if (history[histtype][i].hisstr != NULL)
7251 ml_append(lnum++, history[histtype][i].hisstr,
7252 (colnr_T)0, FALSE);
7253 }
7254 while (i != hisidx[histtype]);
7255 }
7256 }
7257
7258 /* Replace the empty last line with the current command-line and put the
7259 * cursor there. */
7260 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7261 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7262 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007263 changed_line_abv_curs();
7264 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007265 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266
7267 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007268 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 /* No Ex mode here! */
7271 exmode_active = 0;
7272
7273 State = NORMAL;
7274# ifdef FEAT_MOUSE
7275 setmouse();
7276# endif
7277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007279 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007280 if (restart_edit != 0) /* autocmd with ":startinsert" */
7281 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 i = RedrawingDisabled;
7284 RedrawingDisabled = 0;
7285
7286 /*
7287 * Call the main loop until <CR> or CTRL-C is typed.
7288 */
7289 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007290 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
7292 RedrawingDisabled = i;
7293
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007294# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007295 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007296# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007299 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007300
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007301# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007302 /* Restore KeyTyped in case it is modified by autocommands */
7303 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304# endif
7305
Bram Moolenaarccc18222007-05-10 18:25:20 +00007306 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007307 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 cmdwin_type = 0;
7309
7310 exmode_active = save_exmode;
7311
Bram Moolenaarf9821062008-06-20 16:31:07 +00007312 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007314 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
7316 cmdwin_result = Ctrl_C;
7317 EMSG(_("E199: Active window or buffer deleted"));
7318 }
7319 else
7320 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007321# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 /* autocmds may abort script processing */
7323 if (aborting() && cmdwin_result != K_IGNORE)
7324 cmdwin_result = Ctrl_C;
7325# endif
7326 /* Set the new command line from the cmdline buffer. */
7327 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007328 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007330 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7331
7332 if (histtype == HIST_CMD)
7333 {
7334 /* Execute the command directly. */
7335 ccline.cmdbuff = vim_strsave((char_u *)p);
7336 cmdwin_result = CAR;
7337 }
7338 else
7339 {
7340 /* First need to cancel what we were doing. */
7341 ccline.cmdbuff = NULL;
7342 stuffcharReadbuff(':');
7343 stuffReadbuff((char_u *)p);
7344 stuffcharReadbuff(CAR);
7345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
7347 else if (cmdwin_result == K_XF2) /* :qa typed */
7348 {
7349 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7350 cmdwin_result = CAR;
7351 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007352 else if (cmdwin_result == Ctrl_C)
7353 {
7354 /* :q or :close, don't execute any command
7355 * and don't modify the cmd window. */
7356 ccline.cmdbuff = NULL;
7357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 else
7359 ccline.cmdbuff = vim_strsave(ml_get_curline());
7360 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007361 {
7362 ccline.cmdbuff = vim_strsave((char_u *)"");
7363 ccline.cmdlen = 0;
7364 ccline.cmdbufflen = 1;
7365 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 else
7369 {
7370 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7371 ccline.cmdbufflen = ccline.cmdlen + 1;
7372 ccline.cmdpos = curwin->w_cursor.col;
7373 if (ccline.cmdpos > ccline.cmdlen)
7374 ccline.cmdpos = ccline.cmdlen;
7375 if (cmdwin_result == K_IGNORE)
7376 {
7377 set_cmdspos_cursor();
7378 redrawcmd();
7379 }
7380 }
7381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007383 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007384# ifdef FEAT_CONCEAL
7385 /* Avoid command-line window first character being concealed. */
7386 curwin->w_p_cole = 0;
7387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007389 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 win_goto(old_curwin);
7391 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007392
7393 /* win_close() may have already wiped the buffer when 'bh' is
7394 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007395 if (bufref_valid(&bufref))
7396 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
7398 /* Restore window sizes. */
7399 win_size_restore(&winsizes);
7400
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007401 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 }
7403
7404 ga_clear(&winsizes);
7405 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007406# ifdef FEAT_RIGHTLEFT
7407 cmdmsg_rl = save_cmdmsg_rl;
7408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
7410 State = save_State;
7411# ifdef FEAT_MOUSE
7412 setmouse();
7413# endif
7414
7415 return cmdwin_result;
7416}
7417#endif /* FEAT_CMDWIN */
7418
7419/*
7420 * Used for commands that either take a simple command string argument, or:
7421 * cmd << endmarker
7422 * {script}
7423 * endmarker
7424 * Returns a pointer to allocated memory with {script} or NULL.
7425 */
7426 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 char_u *theline;
7430 char *end_pattern = NULL;
7431 char dot[] = ".";
7432 garray_T ga;
7433
7434 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7435 return NULL;
7436
7437 ga_init2(&ga, 1, 0x400);
7438
7439 if (cmd[2] != NUL)
7440 end_pattern = (char *)skipwhite(cmd + 2);
7441 else
7442 end_pattern = dot;
7443
7444 for (;;)
7445 {
7446 theline = eap->getline(
7447#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007448 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449#endif
7450 NUL, eap->cookie, 0);
7451
7452 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007453 {
7454 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 ga_concat(&ga, theline);
7459 ga_append(&ga, '\n');
7460 vim_free(theline);
7461 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007462 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
7464 return (char_u *)ga.ga_data;
7465}