blob: 8765ac5b246ff00701b81d8488f3d514effdf31c [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 {
286 char_u *cmd = skip_range(ccline.cmdbuff, NULL);
287 char_u *p;
288 int delim;
289 char_u *end;
290
291 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
292 {
293 // Skip over "substitute" to find the pattern separator.
294 for (p = cmd; ASCII_ISALPHA(*p); ++p)
295 ;
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200296 if (*skipwhite(p) != NUL
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200297 && (STRNCMP(cmd, "substitute", p - cmd) == 0
298 || STRNCMP(cmd, "global", p - cmd) == 0
299 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200300 {
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200301 p = skipwhite(p);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200302 delim = *p++;
303 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200304 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200305 {
306 char_u *dummy;
307 exarg_T ea;
308 pos_T save_cursor = curwin->w_cursor;
309
310 // found a non-empty pattern
311 *skiplen = (int)(p - ccline.cmdbuff);
312 *patlen = (int)(end - p);
313
314 // parse the address range
315 vim_memset(&ea, 0, sizeof(ea));
316 ea.line1 = 1;
317 ea.line2 = 1;
318 ea.cmd = ccline.cmdbuff;
319 ea.addr_type = ADDR_LINES;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200320 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200321 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200322 if (ea.addr_count > 0)
323 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200324 // Allow for reverse match.
325 if (ea.line2 < ea.line1)
326 {
327 search_first_line = ea.line2;
328 search_last_line = ea.line1;
329 }
330 else
331 {
332 search_first_line = ea.line1;
333 search_last_line = ea.line2;
334 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200335 }
336 else if (*cmd == 's')
337 {
338 // :s defaults to the current line
339 search_first_line = curwin->w_cursor.lnum;
340 search_last_line = curwin->w_cursor.lnum;
341 }
342
343 curwin->w_cursor = save_cursor;
344 return TRUE;
345 }
346 }
347 }
348 }
349 }
350
351 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200352}
353
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200354 static void
355finish_incsearch_highlighting(
356 int gotesc,
357 incsearch_state_T *is_state,
358 int call_update_screen)
359{
360 if (is_state->did_incsearch)
361 {
362 is_state->did_incsearch = FALSE;
363 if (gotesc)
364 curwin->w_cursor = is_state->save_cursor;
365 else
366 {
367 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
368 {
369 // put the '" mark at the original position
370 curwin->w_cursor = is_state->save_cursor;
371 setpcmark();
372 }
373 curwin->w_cursor = is_state->search_start;
374 }
375 restore_viewstate(&is_state->old_viewstate);
376 highlight_match = FALSE;
377 validate_cursor(); /* needed for TAB */
378 if (call_update_screen)
379 update_screen(SOME_VALID);
380 else
381 redraw_all_later(SOME_VALID);
382 }
383}
384
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200385/*
386 * Do 'incsearch' highlighting if desired.
387 */
388 static void
389may_do_incsearch_highlighting(
390 int firstc,
391 long count,
392 incsearch_state_T *is_state)
393{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200394 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200395 int i;
396 pos_T end_pos;
397 struct cmdline_info save_ccline;
398#ifdef FEAT_RELTIME
399 proftime_T tm;
400#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200401 int next_char;
402 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200403
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200405 {
406 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200408 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200409
410 // If there is a character waiting, search and redraw later.
411 if (char_avail())
412 {
413 is_state->incsearch_postponed = TRUE;
414 return;
415 }
416 is_state->incsearch_postponed = FALSE;
417
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200418 if (search_first_line == 0)
419 // start at the original cursor position
420 curwin->w_cursor = is_state->search_start;
421 else
422 {
423 // start at the first line in the range
424 curwin->w_cursor.lnum = search_first_line;
425 curwin->w_cursor.col = 0;
426 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200427 save_last_search_pattern();
428
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200429 // Use the previous pattern for ":s//".
430 next_char = ccline.cmdbuff[skiplen + patlen];
431 use_last_pat = patlen == 0 && skiplen > 0
432 && ccline.cmdbuff[skiplen - 1] == next_char;
433
434 // If there is no pattern, don't do anything.
435 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200436 {
437 i = 0;
438 set_no_hlsearch(TRUE); // turn off previous highlight
439 redraw_all_later(SOME_VALID);
440 }
441 else
442 {
443 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
444
445 cursor_off(); // so the user knows we're busy
446 out_flush();
447 ++emsg_off; // so it doesn't beep if bad expr
448#ifdef FEAT_RELTIME
449 // Set the time limit to half a second.
450 profile_setlimit(500L, &tm);
451#endif
452 if (!p_hls)
453 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200454 if (search_first_line != 0)
455 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200456 ccline.cmdbuff[skiplen + patlen] = NUL;
457 i = do_search(NULL, firstc == ':' ? '/' : firstc,
458 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200459#ifdef FEAT_RELTIME
460 &tm, NULL
461#else
462 NULL, NULL
463#endif
464 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200465 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466 --emsg_off;
467
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200468 if (curwin->w_cursor.lnum < search_first_line
469 || curwin->w_cursor.lnum > search_last_line)
470 // match outside of address range
471 i = 0;
472
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473 // if interrupted while searching, behave like it failed
474 if (got_int)
475 {
476 (void)vpeekc(); // remove <C-C> from input stream
477 got_int = FALSE; // don't abandon the command line
478 i = 0;
479 }
480 else if (char_avail())
481 // cancelled searching because a char was typed
482 is_state->incsearch_postponed = TRUE;
483 }
484 if (i != 0)
485 highlight_match = TRUE; // highlight position
486 else
487 highlight_match = FALSE; // remove highlight
488
489 // First restore the old curwin values, so the screen is positioned in the
490 // same way as the actual search command.
491 restore_viewstate(&is_state->old_viewstate);
492 changed_cline_bef_curs();
493 update_topline();
494
495 if (i != 0)
496 {
497 pos_T save_pos = curwin->w_cursor;
498
499 is_state->match_start = curwin->w_cursor;
500 set_search_match(&curwin->w_cursor);
501 validate_cursor();
502 end_pos = curwin->w_cursor;
503 is_state->match_end = end_pos;
504 curwin->w_cursor = save_pos;
505 }
506 else
507 end_pos = curwin->w_cursor; // shutup gcc 4
508
509 // Disable 'hlsearch' highlighting if the pattern matches everything.
510 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200511 if (!use_last_pat)
512 {
513 next_char = ccline.cmdbuff[skiplen + patlen];
514 ccline.cmdbuff[skiplen + patlen] = NUL;
515 if (empty_pattern(ccline.cmdbuff))
516 set_no_hlsearch(TRUE);
517 ccline.cmdbuff[skiplen + patlen] = next_char;
518 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200519
520 validate_cursor();
521 // May redraw the status line to show the cursor position.
522 if (p_ru && curwin->w_status_height > 0)
523 curwin->w_redr_status = TRUE;
524
525 save_cmdline(&save_ccline);
526 update_screen(SOME_VALID);
527 restore_cmdline(&save_ccline);
528 restore_last_search_pattern();
529
530 // Leave it at the end to make CTRL-R CTRL-W work.
531 if (i != 0)
532 curwin->w_cursor = end_pos;
533
534 msg_starthere();
535 redrawcmdline();
536 is_state->did_incsearch = TRUE;
537}
538
539/*
540 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
541 * or previous match.
542 * Returns FAIL when jumping to cmdline_not_changed;
543 */
544 static int
545may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200546 int firstc,
547 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200548 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200549 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200550{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200551 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200552 pos_T t;
553 char_u *pat;
554 int search_flags = SEARCH_NOOF;
555 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200556 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200557
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200558 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200559 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561 return FAIL;
562
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200563 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200564 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200565 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200566 skiplen = 0;
567 patlen = STRLEN(pat);
568 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200569 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200570 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200571
572 save_last_search_pattern();
573 cursor_off();
574 out_flush();
575 if (c == Ctrl_G)
576 {
577 t = is_state->match_end;
578 if (LT_POS(is_state->match_start, is_state->match_end))
579 // Start searching at the end of the match not at the beginning of
580 // the next column.
581 (void)decl(&t);
582 search_flags += SEARCH_COL;
583 }
584 else
585 t = is_state->match_start;
586 if (!p_hls)
587 search_flags += SEARCH_KEEP;
588 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200589 save = pat[patlen];
590 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591 i = searchit(curwin, curbuf, &t,
592 c == Ctrl_G ? FORWARD : BACKWARD,
593 pat, count, search_flags,
594 RE_SEARCH, 0, NULL, NULL);
595 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200596 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 if (i)
598 {
599 is_state->search_start = is_state->match_start;
600 is_state->match_end = t;
601 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200602 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200603 {
604 // Move just before the current match, so that when nv_search
605 // finishes the cursor will be put back on the match.
606 is_state->search_start = t;
607 (void)decl(&is_state->search_start);
608 }
609 else if (c == Ctrl_G && firstc == '?')
610 {
611 // Move just after the current match, so that when nv_search
612 // finishes the cursor will be put back on the match.
613 is_state->search_start = t;
614 (void)incl(&is_state->search_start);
615 }
616 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
617 {
618 // wrap around
619 is_state->search_start = t;
620 if (firstc == '?')
621 (void)incl(&is_state->search_start);
622 else
623 (void)decl(&is_state->search_start);
624 }
625
626 set_search_match(&is_state->match_end);
627 curwin->w_cursor = is_state->match_start;
628 changed_cline_bef_curs();
629 update_topline();
630 validate_cursor();
631 highlight_match = TRUE;
632 save_viewstate(&is_state->old_viewstate);
633 update_screen(NOT_VALID);
634 redrawcmdline();
635 }
636 else
637 vim_beep(BO_ERROR);
638 restore_last_search_pattern();
639 return FAIL;
640}
641
642/*
643 * When CTRL-L typed: add character from the match to the pattern.
644 * May set "*c" to the added character.
645 * Return OK when jumping to cmdline_not_changed.
646 */
647 static int
648may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
649{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200650 int skiplen, patlen;
651
652 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653 return FAIL;
654
655 // Add a character from under the cursor for 'incsearch'.
656 if (is_state->did_incsearch)
657 {
658 curwin->w_cursor = is_state->match_end;
659 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
660 {
661 *c = gchar_cursor();
662
663 // If 'ignorecase' and 'smartcase' are set and the
664 // command line has no uppercase characters, convert
665 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200666 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200667 *c = MB_TOLOWER(*c);
668 if (*c != NUL)
669 {
670 if (*c == firstc || vim_strchr((char_u *)(
671 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
672 {
673 // put a backslash before special characters
674 stuffcharReadbuff(*c);
675 *c = '\\';
676 }
677 return FAIL;
678 }
679 }
680 }
681 return OK;
682}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200683#endif
684
Bram Moolenaar66216052017-12-16 16:33:44 +0100685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 * getcmdline() - accept a command line starting with firstc.
687 *
688 * firstc == ':' get ":" command line.
689 * firstc == '/' or '?' get search pattern
690 * firstc == '=' get expression
691 * firstc == '@' get text for input() function
692 * firstc == '>' get text for debug mode
693 * firstc == NUL get text for :insert command
694 * firstc == -1 like NUL, and break on CTRL-C
695 *
696 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
697 * command line.
698 *
699 * Careful: getcmdline() can be called recursively!
700 *
701 * Return pointer to allocated string if there is a commandline, NULL
702 * otherwise.
703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100705getcmdline(
706 int firstc,
707 long count UNUSED, /* only used for incremental search */
708 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 int c;
711 int i;
712 int j;
713 int gotesc = FALSE; /* TRUE when <ESC> just typed */
714 int do_abbr; /* when TRUE check for abbr. */
715#ifdef FEAT_CMDHIST
716 char_u *lookfor = NULL; /* string to match */
717 int hiscnt; /* current history line in use */
718 int histype; /* history type to be used */
719#endif
720#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200721 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#endif
723 int did_wild_list = FALSE; /* did wild_list() recently */
724 int wim_index = 0; /* index in wim_flags[] */
725 int res;
726 int save_msg_scroll = msg_scroll;
727 int save_State = State; /* remember State when called */
728 int some_key_typed = FALSE; /* one of the keys was typed */
729#ifdef FEAT_MOUSE
730 /* mouse drag and release events are ignored, unless they are
731 * preceded with a mouse down event */
732 int ignore_drag_release = TRUE;
733#endif
734#ifdef FEAT_EVAL
735 int break_ctrl_c = FALSE;
736#endif
737 expand_T xpc;
738 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200739#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000740 /* Everything that may work recursively should save and restore the
741 * current command line in save_ccline. That includes update_screen(), a
742 * custom status line may invoke ":normal". */
743 struct cmdline_info save_ccline;
744#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200745 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747#ifdef FEAT_EVAL
748 if (firstc == -1)
749 {
750 firstc = NUL;
751 break_ctrl_c = TRUE;
752 }
753#endif
754#ifdef FEAT_RIGHTLEFT
755 /* start without Hebrew mapping for a command line */
756 if (firstc == ':' || firstc == '=' || firstc == '>')
757 cmd_hkmap = 0;
758#endif
759
760 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200761
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200763 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764#endif
765
766 /*
767 * set some variables for redrawcmd()
768 */
769 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000770 ccline.cmdindent = (firstc > 0 ? indent : 0);
771
772 /* alloc initial ccline.cmdbuff */
773 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (ccline.cmdbuff == NULL)
775 return NULL; /* out of memory */
776 ccline.cmdlen = ccline.cmdpos = 0;
777 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100778 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000780 /* autoindent for :insert and :append */
781 if (firstc <= 0)
782 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200783 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000784 ccline.cmdbuff[indent] = NUL;
785 ccline.cmdpos = indent;
786 ccline.cmdspos = indent;
787 ccline.cmdlen = indent;
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000791 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793#ifdef FEAT_RIGHTLEFT
794 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
795 && (firstc == '/' || firstc == '?'))
796 cmdmsg_rl = TRUE;
797 else
798 cmdmsg_rl = FALSE;
799#endif
800
801 redir_off = TRUE; /* don't redirect the typed command */
802 if (!cmd_silent)
803 {
804 i = msg_scrolled;
805 msg_scrolled = 0; /* avoid wait_return message */
806 gotocmdline(TRUE);
807 msg_scrolled += i;
808 redrawcmdprompt(); /* draw prompt or indent */
809 set_cmdspos();
810 }
811 xpc.xp_context = EXPAND_NOTHING;
812 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000813#ifndef BACKSLASH_IN_FILENAME
814 xpc.xp_shell = FALSE;
815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000817#if defined(FEAT_EVAL)
818 if (ccline.input_fn)
819 {
820 xpc.xp_context = ccline.xp_context;
821 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000822# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000823 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000824# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000825 }
826#endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 /*
829 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
830 * doing ":@0" when register 0 doesn't contain a CR.
831 */
832 msg_scroll = FALSE;
833
834 State = CMDLINE;
835
836 if (firstc == '/' || firstc == '?' || firstc == '@')
837 {
838 /* Use ":lmap" mappings for search pattern and input(). */
839 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
840 b_im_ptr = &curbuf->b_p_iminsert;
841 else
842 b_im_ptr = &curbuf->b_p_imsearch;
843 if (*b_im_ptr == B_IMODE_LMAP)
844 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100845#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 im_set_active(*b_im_ptr == B_IMODE_IM);
847#endif
848 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100849#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 else if (p_imcmdline)
851 im_set_active(TRUE);
852#endif
853
854#ifdef FEAT_MOUSE
855 setmouse();
856#endif
857#ifdef CURSOR_SHAPE
858 ui_cursor_shape(); /* may show different cursor shape */
859#endif
860
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000861 /* When inside an autocommand for writing "exiting" may be set and
862 * terminal mode set to cooked. Need to set raw mode here then. */
863 settmode(TMODE_RAW);
864
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200865 /* Trigger CmdlineEnter autocommands. */
866 cmdline_type = firstc == NUL ? '-' : firstc;
867 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_CMDHIST
870 init_history();
871 hiscnt = hislen; /* set hiscnt to impossible history value */
872 histype = hist_char2type(firstc);
873#endif
874
875#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000876 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#endif
878
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200879 /* If something above caused an error, reset the flags, we do want to type
880 * and execute commands. Display may be messed up a bit. */
881 if (did_emsg)
882 redrawcmd();
883 did_emsg = FALSE;
884 got_int = FALSE;
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 /*
887 * Collect the command string, handling editing keys.
888 */
889 for (;;)
890 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000891 redir_off = TRUE; /* Don't redirect the typed command.
892 Repeated, because a ":redir" inside
893 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#ifdef USE_ON_FLY_SCROLL
895 dont_scroll = FALSE; /* allow scrolling here */
896#endif
897 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
898
Bram Moolenaar72532d32018-04-07 19:09:09 +0200899 did_emsg = FALSE; /* There can't really be a reason why an error
900 that occurs while typing a command should
901 cause the command not to be executed. */
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000904
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100905 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
906 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000907 do
908 {
909 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100910 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (KeyTyped)
913 {
914 some_key_typed = TRUE;
915#ifdef FEAT_RIGHTLEFT
916 if (cmd_hkmap)
917 c = hkmap(c);
918# ifdef FEAT_FKMAP
919 if (cmd_fkmap)
920 c = cmdl_fkmap(c);
921# endif
922 if (cmdmsg_rl && !KeyStuffed)
923 {
924 /* Invert horizontal movements and operations. Only when
925 * typed by the user directly, not when the result of a
926 * mapping. */
927 switch (c)
928 {
929 case K_RIGHT: c = K_LEFT; break;
930 case K_S_RIGHT: c = K_S_LEFT; break;
931 case K_C_RIGHT: c = K_C_LEFT; break;
932 case K_LEFT: c = K_RIGHT; break;
933 case K_S_LEFT: c = K_S_RIGHT; break;
934 case K_C_LEFT: c = K_C_RIGHT; break;
935 }
936 }
937#endif
938 }
939
940 /*
941 * Ignore got_int when CTRL-C was typed here.
942 * Don't ignore it in :global, we really need to break then, e.g., for
943 * ":g/pat/normal /pat" (without the <CR>).
944 * Don't ignore it for the input() function.
945 */
946 if ((c == Ctrl_C
947#ifdef UNIX
948 || c == intr_char
949#endif
950 )
951#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
952 && firstc != '@'
953#endif
954#ifdef FEAT_EVAL
955 && !break_ctrl_c
956#endif
957 && !global_busy)
958 got_int = FALSE;
959
960#ifdef FEAT_CMDHIST
961 /* free old command line when finished moving around in the history
962 * list */
963 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000964 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000965 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 && c != K_PAGEDOWN && c != K_PAGEUP
967 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000968 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100970 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#endif
972
973 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000974 * When there are matching completions to select <S-Tab> works like
975 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000977 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 c = Ctrl_P;
979
980#ifdef FEAT_WILDMENU
981 /* Special translations for 'wildmenu' */
982 if (did_wild_list && p_wmnu)
983 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000984 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000986 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 c = Ctrl_N;
988 }
989 /* Hitting CR after "emenu Name.": complete submenu */
990 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
991 && ccline.cmdpos > 1
992 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
993 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
994 && (c == '\n' || c == '\r' || c == K_KENTER))
995 c = K_DOWN;
996#endif
997
998 /* free expanded names when finished walking through matches */
999 if (xpc.xp_numfiles != -1
1000 && !(c == p_wc && KeyTyped) && c != p_wcm
1001 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1002 && c != Ctrl_L)
1003 {
1004 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1005 did_wild_list = FALSE;
1006#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001007 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#endif
1009 xpc.xp_context = EXPAND_NOTHING;
1010 wim_index = 0;
1011#ifdef FEAT_WILDMENU
1012 if (p_wmnu && wild_menu_showing != 0)
1013 {
1014 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001015 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001016
1017 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001018 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020 if (wild_menu_showing == WM_SCROLLED)
1021 {
1022 /* Entered command line, move it up */
1023 cmdline_row--;
1024 redrawcmd();
1025 }
1026 else if (save_p_ls != -1)
1027 {
1028 /* restore 'laststatus' and 'winminheight' */
1029 p_ls = save_p_ls;
1030 p_wmh = save_p_wmh;
1031 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001032 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001034 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 redrawcmd();
1036 save_p_ls = -1;
1037 }
1038 else
1039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 redraw_statuslines();
1042 }
1043 KeyTyped = skt;
1044 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001045 if (ccline.input_fn)
1046 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048#endif
1049 }
1050
1051#ifdef FEAT_WILDMENU
1052 /* Special translations for 'wildmenu' */
1053 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1054 {
1055 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001056 if (c == K_DOWN && ccline.cmdpos > 0
1057 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001059 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
1061 /* Hitting <Up>: Remove one submenu name in front of the
1062 * cursor */
1063 int found = FALSE;
1064
1065 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1066 i = 0;
1067 while (--j > 0)
1068 {
1069 /* check for start of menu name */
1070 if (ccline.cmdbuff[j] == ' '
1071 && ccline.cmdbuff[j - 1] != '\\')
1072 {
1073 i = j + 1;
1074 break;
1075 }
1076 /* check for start of submenu name */
1077 if (ccline.cmdbuff[j] == '.'
1078 && ccline.cmdbuff[j - 1] != '\\')
1079 {
1080 if (found)
1081 {
1082 i = j + 1;
1083 break;
1084 }
1085 else
1086 found = TRUE;
1087 }
1088 }
1089 if (i > 0)
1090 cmdline_del(i);
1091 c = p_wc;
1092 xpc.xp_context = EXPAND_NOTHING;
1093 }
1094 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001095 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001096 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001097 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 char_u upseg[5];
1100
1101 upseg[0] = PATHSEP;
1102 upseg[1] = '.';
1103 upseg[2] = '.';
1104 upseg[3] = PATHSEP;
1105 upseg[4] = NUL;
1106
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001107 if (c == K_DOWN
1108 && ccline.cmdpos > 0
1109 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1110 && (ccline.cmdpos < 3
1111 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1113 {
1114 /* go down a directory */
1115 c = p_wc;
1116 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001117 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 /* If in a direct ancestor, strip off one ../ to go down */
1120 int found = FALSE;
1121
1122 j = ccline.cmdpos;
1123 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1124 while (--j > i)
1125 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001126#ifdef FEAT_MBYTE
1127 if (has_mbyte)
1128 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (vim_ispathsep(ccline.cmdbuff[j]))
1131 {
1132 found = TRUE;
1133 break;
1134 }
1135 }
1136 if (found
1137 && ccline.cmdbuff[j - 1] == '.'
1138 && ccline.cmdbuff[j - 2] == '.'
1139 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1140 {
1141 cmdline_del(j - 2);
1142 c = p_wc;
1143 }
1144 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001145 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
1147 /* go up a directory */
1148 int found = FALSE;
1149
1150 j = ccline.cmdpos - 1;
1151 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1152 while (--j > i)
1153 {
1154#ifdef FEAT_MBYTE
1155 if (has_mbyte)
1156 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1157#endif
1158 if (vim_ispathsep(ccline.cmdbuff[j])
1159#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001160 && vim_strchr((char_u *)" *?[{`$%#",
1161 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#endif
1163 )
1164 {
1165 if (found)
1166 {
1167 i = j + 1;
1168 break;
1169 }
1170 else
1171 found = TRUE;
1172 }
1173 }
1174
1175 if (!found)
1176 j = i;
1177 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1178 j += 4;
1179 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1180 && j == i)
1181 j += 3;
1182 else
1183 j = 0;
1184 if (j > 0)
1185 {
1186 /* TODO this is only for DOS/UNIX systems - need to put in
1187 * machine-specific stuff here and in upseg init */
1188 cmdline_del(j);
1189 put_on_cmdline(upseg + 1, 3, FALSE);
1190 }
1191 else if (ccline.cmdpos > i)
1192 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001193
1194 /* Now complete in the new directory. Set KeyTyped in case the
1195 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001197 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201#endif /* FEAT_WILDMENU */
1202
1203 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1204 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1205 if (c == Ctrl_BSL)
1206 {
1207 ++no_mapping;
1208 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001209 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 --no_mapping;
1211 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001212 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1213 * is in a mapping. */
1214 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1215 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
1217 vungetc(c);
1218 c = Ctrl_BSL;
1219 }
1220#ifdef FEAT_EVAL
1221 else if (c == 'e')
1222 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001223 char_u *p = NULL;
1224 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 /*
1227 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001228 * Need to save and restore the current command line, to be
1229 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
1231 if (ccline.cmdpos == ccline.cmdlen)
1232 new_cmdpos = 99999; /* keep it at the end */
1233 else
1234 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001235
1236 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001238 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (c == '=')
1240 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001241 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001242 * to avoid nasty things like going to another buffer when
1243 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001244 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001245 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001247 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001248 restore_cmdline(&save_ccline);
1249
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001250 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001252 len = (int)STRLEN(p);
1253 if (realloc_cmdbuff(len + 1) == OK)
1254 {
1255 ccline.cmdlen = len;
1256 STRCPY(ccline.cmdbuff, p);
1257 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001259 /* Restore the cursor or use the position set with
1260 * set_cmdline_pos(). */
1261 if (new_cmdpos > ccline.cmdlen)
1262 ccline.cmdpos = ccline.cmdlen;
1263 else
1264 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001266 KeyTyped = FALSE; /* Don't do p_wc completion. */
1267 redrawcmd();
1268 goto cmdline_changed;
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 }
1272 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001273 got_int = FALSE; /* don't abandon the command line */
1274 did_emsg = FALSE;
1275 emsg_on_display = FALSE;
1276 redrawcmd();
1277 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279#endif
1280 else
1281 {
1282 if (c == Ctrl_G && p_im && restart_edit == 0)
1283 restart_edit = 'a';
1284 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1285 in history */
1286 goto returncmd; /* back to Normal mode */
1287 }
1288 }
1289
1290#ifdef FEAT_CMDWIN
1291 if (c == cedit_key || c == K_CMDWIN)
1292 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001293 if (ex_normal_busy == 0 && got_int == FALSE)
1294 {
1295 /*
1296 * Open a window to edit the command line (and history).
1297 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001298 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001299 some_key_typed = TRUE;
1300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302# ifdef FEAT_DIGRAPHS
1303 else
1304# endif
1305#endif
1306#ifdef FEAT_DIGRAPHS
1307 c = do_digraph(c);
1308#endif
1309
1310 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1311 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1312 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001313 /* In Ex mode a backslash escapes a newline. */
1314 if (exmode_active
1315 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001316 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001317 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001318 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001320 if (c == K_KENTER)
1321 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001323 else
1324 {
1325 gotesc = FALSE; /* Might have typed ESC previously, don't
1326 truncate the cmdline now. */
1327 if (ccheck_abbr(c + ABBR_OFF))
1328 goto cmdline_changed;
1329 if (!cmd_silent)
1330 {
1331 windgoto(msg_row, 0);
1332 out_flush();
1333 }
1334 break;
1335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337
1338 /*
1339 * Completion for 'wildchar' or 'wildcharm' key.
1340 * - hitting <ESC> twice means: abandon command line.
1341 * - wildcard expansion is only done when the 'wildchar' key is really
1342 * typed, not when it comes from a macro
1343 */
1344 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1345 {
1346 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1347 {
1348 /* if 'wildmode' contains "list" may still need to list */
1349 if (xpc.xp_numfiles > 1
1350 && !did_wild_list
1351 && (wim_flags[wim_index] & WIM_LIST))
1352 {
1353 (void)showmatches(&xpc, FALSE);
1354 redrawcmd();
1355 did_wild_list = TRUE;
1356 }
1357 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001358 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1359 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001361 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1362 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 else
1364 res = OK; /* don't insert 'wildchar' now */
1365 }
1366 else /* typed p_wc first time */
1367 {
1368 wim_index = 0;
1369 j = ccline.cmdpos;
1370 /* if 'wildmode' first contains "longest", get longest
1371 * common part */
1372 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001373 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1374 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001376 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1377 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 /* if interrupted while completing, behave like it failed */
1380 if (got_int)
1381 {
1382 (void)vpeekc(); /* remove <C-C> from input stream */
1383 got_int = FALSE; /* don't abandon the command line */
1384 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1385#ifdef FEAT_WILDMENU
1386 xpc.xp_context = EXPAND_NOTHING;
1387#endif
1388 goto cmdline_changed;
1389 }
1390
1391 /* when more than one match, and 'wildmode' first contains
1392 * "list", or no change and 'wildmode' contains "longest,list",
1393 * list all matches */
1394 if (res == OK && xpc.xp_numfiles > 1)
1395 {
1396 /* a "longest" that didn't do anything is skipped (but not
1397 * "list:longest") */
1398 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1399 wim_index = 1;
1400 if ((wim_flags[wim_index] & WIM_LIST)
1401#ifdef FEAT_WILDMENU
1402 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1403#endif
1404 )
1405 {
1406 if (!(wim_flags[0] & WIM_LONGEST))
1407 {
1408#ifdef FEAT_WILDMENU
1409 int p_wmnu_save = p_wmnu;
1410 p_wmnu = 0;
1411#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001412 /* remove match */
1413 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#ifdef FEAT_WILDMENU
1415 p_wmnu = p_wmnu_save;
1416#endif
1417 }
1418#ifdef FEAT_WILDMENU
1419 (void)showmatches(&xpc, p_wmnu
1420 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1421#else
1422 (void)showmatches(&xpc, FALSE);
1423#endif
1424 redrawcmd();
1425 did_wild_list = TRUE;
1426 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001427 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1428 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001430 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1431 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001434 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436#ifdef FEAT_WILDMENU
1437 else if (xpc.xp_numfiles == -1)
1438 xpc.xp_context = EXPAND_NOTHING;
1439#endif
1440 }
1441 if (wim_index < 3)
1442 ++wim_index;
1443 if (c == ESC)
1444 gotesc = TRUE;
1445 if (res == OK)
1446 goto cmdline_changed;
1447 }
1448
1449 gotesc = FALSE;
1450
1451 /* <S-Tab> goes to last match, in a clumsy way */
1452 if (c == K_S_TAB && KeyTyped)
1453 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001454 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1455 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1456 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 goto cmdline_changed;
1458 }
1459
1460 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1461 c = NL;
1462
1463 do_abbr = TRUE; /* default: check for abbreviation */
1464
1465 /*
1466 * Big switch for a typed command line character.
1467 */
1468 switch (c)
1469 {
1470 case K_BS:
1471 case Ctrl_H:
1472 case K_DEL:
1473 case K_KDEL:
1474 case Ctrl_W:
1475#ifdef FEAT_FKMAP
1476 if (cmd_fkmap && c == K_BS)
1477 c = K_DEL;
1478#endif
1479 if (c == K_KDEL)
1480 c = K_DEL;
1481
1482 /*
1483 * delete current character is the same as backspace on next
1484 * character, except at end of line
1485 */
1486 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1487 ++ccline.cmdpos;
1488#ifdef FEAT_MBYTE
1489 if (has_mbyte && c == K_DEL)
1490 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1491 ccline.cmdbuff + ccline.cmdpos);
1492#endif
1493 if (ccline.cmdpos > 0)
1494 {
1495 char_u *p;
1496
1497 j = ccline.cmdpos;
1498 p = ccline.cmdbuff + j;
1499#ifdef FEAT_MBYTE
1500 if (has_mbyte)
1501 {
1502 p = mb_prevptr(ccline.cmdbuff, p);
1503 if (c == Ctrl_W)
1504 {
1505 while (p > ccline.cmdbuff && vim_isspace(*p))
1506 p = mb_prevptr(ccline.cmdbuff, p);
1507 i = mb_get_class(p);
1508 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1509 p = mb_prevptr(ccline.cmdbuff, p);
1510 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001511 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 }
1514 else
1515#endif
1516 if (c == Ctrl_W)
1517 {
1518 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1519 --p;
1520 i = vim_iswordc(p[-1]);
1521 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1522 && vim_iswordc(p[-1]) == i)
1523 --p;
1524 }
1525 else
1526 --p;
1527 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1528 ccline.cmdlen -= j - ccline.cmdpos;
1529 i = ccline.cmdpos;
1530 while (i < ccline.cmdlen)
1531 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1532
1533 /* Truncate at the end, required for multi-byte chars. */
1534 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001535#ifdef FEAT_SEARCH_EXTRA
1536 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001537 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001538 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001539 /* save view settings, so that the screen
1540 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001541 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001542 }
1543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 redrawcmd();
1545 }
1546 else if (ccline.cmdlen == 0 && c != Ctrl_W
1547 && ccline.cmdprompt == NULL && indent == 0)
1548 {
1549 /* In ex and debug mode it doesn't make sense to return. */
1550 if (exmode_active
1551#ifdef FEAT_EVAL
1552 || ccline.cmdfirstc == '>'
1553#endif
1554 )
1555 goto cmdline_not_changed;
1556
Bram Moolenaard23a8232018-02-10 18:45:26 +01001557 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (!cmd_silent)
1559 {
1560#ifdef FEAT_RIGHTLEFT
1561 if (cmdmsg_rl)
1562 msg_col = Columns;
1563 else
1564#endif
1565 msg_col = 0;
1566 msg_putchar(' '); /* delete ':' */
1567 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001568#ifdef FEAT_SEARCH_EXTRA
1569 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001570 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 redraw_cmdline = TRUE;
1573 goto returncmd; /* back to cmd mode */
1574 }
1575 goto cmdline_changed;
1576
1577 case K_INS:
1578 case K_KINS:
1579#ifdef FEAT_FKMAP
1580 /* if Farsi mode set, we are in reverse insert mode -
1581 Do not change the mode */
1582 if (cmd_fkmap)
1583 beep_flush();
1584 else
1585#endif
1586 ccline.overstrike = !ccline.overstrike;
1587#ifdef CURSOR_SHAPE
1588 ui_cursor_shape(); /* may show different cursor shape */
1589#endif
1590 goto cmdline_not_changed;
1591
1592 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001593 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 /* ":lmap" mappings exists, toggle use of mappings. */
1596 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001597#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 im_set_active(FALSE); /* Disable input method */
1599#endif
1600 if (b_im_ptr != NULL)
1601 {
1602 if (State & LANGMAP)
1603 *b_im_ptr = B_IMODE_LMAP;
1604 else
1605 *b_im_ptr = B_IMODE_NONE;
1606 }
1607 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001608#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 else
1610 {
1611 /* There are no ":lmap" mappings, toggle IM. When
1612 * 'imdisable' is set don't try getting the status, it's
1613 * always off. */
1614 if ((p_imdisable && b_im_ptr != NULL)
1615 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1616 {
1617 im_set_active(FALSE); /* Disable input method */
1618 if (b_im_ptr != NULL)
1619 *b_im_ptr = B_IMODE_NONE;
1620 }
1621 else
1622 {
1623 im_set_active(TRUE); /* Enable input method */
1624 if (b_im_ptr != NULL)
1625 *b_im_ptr = B_IMODE_IM;
1626 }
1627 }
1628#endif
1629 if (b_im_ptr != NULL)
1630 {
1631 if (b_im_ptr == &curbuf->b_p_iminsert)
1632 set_iminsert_global();
1633 else
1634 set_imsearch_global();
1635 }
1636#ifdef CURSOR_SHAPE
1637 ui_cursor_shape(); /* may show different cursor shape */
1638#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001639#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 /* Show/unshow value of 'keymap' in status lines later. */
1641 status_redraw_curbuf();
1642#endif
1643 goto cmdline_not_changed;
1644
1645/* case '@': only in very old vi */
1646 case Ctrl_U:
1647 /* delete all characters left of the cursor */
1648 j = ccline.cmdpos;
1649 ccline.cmdlen -= j;
1650 i = ccline.cmdpos = 0;
1651 while (i < ccline.cmdlen)
1652 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1653 /* Truncate at the end, required for multi-byte chars. */
1654 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001655#ifdef FEAT_SEARCH_EXTRA
1656 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001657 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 redrawcmd();
1660 goto cmdline_changed;
1661
1662#ifdef FEAT_CLIPBOARD
1663 case Ctrl_Y:
1664 /* Copy the modeless selection, if there is one. */
1665 if (clip_star.state != SELECT_CLEARED)
1666 {
1667 if (clip_star.state == SELECT_DONE)
1668 clip_copy_modeless_selection(TRUE);
1669 goto cmdline_not_changed;
1670 }
1671 break;
1672#endif
1673
1674 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1675 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001676 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001677 * ":normal" runs out of characters. */
1678 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001679 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 goto cmdline_not_changed;
1681
1682 gotesc = TRUE; /* will free ccline.cmdbuff after
1683 putting it in history */
1684 goto returncmd; /* back to cmd mode */
1685
1686 case Ctrl_R: /* insert register */
1687#ifdef USE_ON_FLY_SCROLL
1688 dont_scroll = TRUE; /* disallow scrolling here */
1689#endif
1690 putcmdline('"', TRUE);
1691 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001692 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (i == Ctrl_O)
1694 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1695 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001696 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001697 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 --no_mapping;
1699#ifdef FEAT_EVAL
1700 /*
1701 * Insert the result of an expression.
1702 * Need to save the current command line, to be able to enter
1703 * a new one...
1704 */
1705 new_cmdpos = -1;
1706 if (c == '=')
1707 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1709 {
1710 beep_flush();
1711 c = ESC;
1712 }
1713 else
1714 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001715 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001717 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719 }
1720#endif
1721 if (c != ESC) /* use ESC to cancel inserting register */
1722 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001723 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001724
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001725#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001726 /* When there was a serious error abort getting the
1727 * command line. */
1728 if (aborting())
1729 {
1730 gotesc = TRUE; /* will free ccline.cmdbuff after
1731 putting it in history */
1732 goto returncmd; /* back to cmd mode */
1733 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 KeyTyped = FALSE; /* Don't do p_wc completion. */
1736#ifdef FEAT_EVAL
1737 if (new_cmdpos >= 0)
1738 {
1739 /* set_cmdline_pos() was used */
1740 if (new_cmdpos > ccline.cmdlen)
1741 ccline.cmdpos = ccline.cmdlen;
1742 else
1743 ccline.cmdpos = new_cmdpos;
1744 }
1745#endif
1746 }
1747 redrawcmd();
1748 goto cmdline_changed;
1749
1750 case Ctrl_D:
1751 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1752 break; /* Use ^D as normal char instead */
1753
1754 redrawcmd();
1755 continue; /* don't do incremental search now */
1756
1757 case K_RIGHT:
1758 case K_S_RIGHT:
1759 case K_C_RIGHT:
1760 do
1761 {
1762 if (ccline.cmdpos >= ccline.cmdlen)
1763 break;
1764 i = cmdline_charsize(ccline.cmdpos);
1765 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1766 break;
1767 ccline.cmdspos += i;
1768#ifdef FEAT_MBYTE
1769 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001770 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 + ccline.cmdpos);
1772 else
1773#endif
1774 ++ccline.cmdpos;
1775 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001776 while ((c == K_S_RIGHT || c == K_C_RIGHT
1777 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1779#ifdef FEAT_MBYTE
1780 if (has_mbyte)
1781 set_cmdspos_cursor();
1782#endif
1783 goto cmdline_not_changed;
1784
1785 case K_LEFT:
1786 case K_S_LEFT:
1787 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001788 if (ccline.cmdpos == 0)
1789 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 do
1791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 --ccline.cmdpos;
1793#ifdef FEAT_MBYTE
1794 if (has_mbyte) /* move to first byte of char */
1795 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1796 ccline.cmdbuff + ccline.cmdpos);
1797#endif
1798 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1799 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001800 while (ccline.cmdpos > 0
1801 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001802 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1804#ifdef FEAT_MBYTE
1805 if (has_mbyte)
1806 set_cmdspos_cursor();
1807#endif
1808 goto cmdline_not_changed;
1809
1810 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001811 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001812 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar4770d092006-01-12 23:22:24 +00001814#ifdef FEAT_GUI_W32
1815 /* On Win32 ignore <M-F4>, we get it when closing the window was
1816 * cancelled. */
1817 case K_F4:
1818 if (mod_mask == MOD_MASK_ALT)
1819 {
1820 redrawcmd(); /* somehow the cmdline is cleared */
1821 goto cmdline_not_changed;
1822 }
1823 break;
1824#endif
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826#ifdef FEAT_MOUSE
1827 case K_MIDDLEDRAG:
1828 case K_MIDDLERELEASE:
1829 goto cmdline_not_changed; /* Ignore mouse */
1830
1831 case K_MIDDLEMOUSE:
1832# ifdef FEAT_GUI
1833 /* When GUI is active, also paste when 'mouse' is empty */
1834 if (!gui.in_use)
1835# endif
1836 if (!mouse_has(MOUSE_COMMAND))
1837 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001838# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001840 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001842# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001843 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 redrawcmd();
1845 goto cmdline_changed;
1846
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001847# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001849 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 redrawcmd();
1851 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 case K_LEFTDRAG:
1855 case K_LEFTRELEASE:
1856 case K_RIGHTDRAG:
1857 case K_RIGHTRELEASE:
1858 /* Ignore drag and release events when the button-down wasn't
1859 * seen before. */
1860 if (ignore_drag_release)
1861 goto cmdline_not_changed;
1862 /* FALLTHROUGH */
1863 case K_LEFTMOUSE:
1864 case K_RIGHTMOUSE:
1865 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1866 ignore_drag_release = TRUE;
1867 else
1868 ignore_drag_release = FALSE;
1869# ifdef FEAT_GUI
1870 /* When GUI is active, also move when 'mouse' is empty */
1871 if (!gui.in_use)
1872# endif
1873 if (!mouse_has(MOUSE_COMMAND))
1874 goto cmdline_not_changed; /* Ignore mouse */
1875# ifdef FEAT_CLIPBOARD
1876 if (mouse_row < cmdline_row && clip_star.available)
1877 {
1878 int button, is_click, is_drag;
1879
1880 /*
1881 * Handle modeless selection.
1882 */
1883 button = get_mouse_button(KEY2TERMCAP1(c),
1884 &is_click, &is_drag);
1885 if (mouse_model_popup() && button == MOUSE_LEFT
1886 && (mod_mask & MOD_MASK_SHIFT))
1887 {
1888 /* Translate shift-left to right button. */
1889 button = MOUSE_RIGHT;
1890 mod_mask &= ~MOD_MASK_SHIFT;
1891 }
1892 clip_modeless(button, is_click, is_drag);
1893 goto cmdline_not_changed;
1894 }
1895# endif
1896
1897 set_cmdspos();
1898 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1899 ++ccline.cmdpos)
1900 {
1901 i = cmdline_charsize(ccline.cmdpos);
1902 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1903 && mouse_col < ccline.cmdspos % Columns + i)
1904 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001905# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (has_mbyte)
1907 {
1908 /* Count ">" for double-wide char that doesn't fit. */
1909 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001910 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 + ccline.cmdpos) - 1;
1912 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 ccline.cmdspos += i;
1915 }
1916 goto cmdline_not_changed;
1917
1918 /* Mouse scroll wheel: ignored here */
1919 case K_MOUSEDOWN:
1920 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001921 case K_MOUSELEFT:
1922 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 /* Alternate buttons ignored here */
1924 case K_X1MOUSE:
1925 case K_X1DRAG:
1926 case K_X1RELEASE:
1927 case K_X2MOUSE:
1928 case K_X2DRAG:
1929 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001930 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 goto cmdline_not_changed;
1932
1933#endif /* FEAT_MOUSE */
1934
1935#ifdef FEAT_GUI
1936 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1937 case K_LEFTRELEASE_NM:
1938 goto cmdline_not_changed;
1939
1940 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001941 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
1943 gui_do_scroll();
1944 redrawcmd();
1945 }
1946 goto cmdline_not_changed;
1947
1948 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001949 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001951 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 redrawcmd();
1953 }
1954 goto cmdline_not_changed;
1955#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001956#ifdef FEAT_GUI_TABLINE
1957 case K_TABLINE:
1958 case K_TABMENU:
1959 /* Don't want to change any tabs here. Make sure the same tab
1960 * is still selected. */
1961 if (gui_use_tabline())
1962 gui_mch_set_curtab(tabpage_index(curtab));
1963 goto cmdline_not_changed;
1964#endif
1965
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 case K_SELECT: /* end of Select mode mapping - ignore */
1967 goto cmdline_not_changed;
1968
1969 case Ctrl_B: /* begin of command line */
1970 case K_HOME:
1971 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 case K_S_HOME:
1973 case K_C_HOME:
1974 ccline.cmdpos = 0;
1975 set_cmdspos();
1976 goto cmdline_not_changed;
1977
1978 case Ctrl_E: /* end of command line */
1979 case K_END:
1980 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 case K_S_END:
1982 case K_C_END:
1983 ccline.cmdpos = ccline.cmdlen;
1984 set_cmdspos_cursor();
1985 goto cmdline_not_changed;
1986
1987 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001988 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 break;
1990 goto cmdline_changed;
1991
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001992 case Ctrl_L:
1993#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001994 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001995 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001996#endif
1997
1998 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001999 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 break;
2001 goto cmdline_changed;
2002
2003 case Ctrl_N: /* next match */
2004 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002005 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002007 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2008 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002010 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002013 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 case K_UP:
2015 case K_DOWN:
2016 case K_S_UP:
2017 case K_S_DOWN:
2018 case K_PAGEUP:
2019 case K_KPAGEUP:
2020 case K_PAGEDOWN:
2021 case K_KPAGEDOWN:
2022 if (hislen == 0 || firstc == NUL) /* no history */
2023 goto cmdline_not_changed;
2024
2025 i = hiscnt;
2026
2027 /* save current command string so it can be restored later */
2028 if (lookfor == NULL)
2029 {
2030 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2031 goto cmdline_not_changed;
2032 lookfor[ccline.cmdpos] = NUL;
2033 }
2034
2035 j = (int)STRLEN(lookfor);
2036 for (;;)
2037 {
2038 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002039 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002040 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 if (hiscnt == hislen) /* first time */
2043 hiscnt = hisidx[histype];
2044 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2045 hiscnt = hislen - 1;
2046 else if (hiscnt != hisidx[histype] + 1)
2047 --hiscnt;
2048 else /* at top of list */
2049 {
2050 hiscnt = i;
2051 break;
2052 }
2053 }
2054 else /* one step forwards */
2055 {
2056 /* on last entry, clear the line */
2057 if (hiscnt == hisidx[histype])
2058 {
2059 hiscnt = hislen;
2060 break;
2061 }
2062
2063 /* not on a history line, nothing to do */
2064 if (hiscnt == hislen)
2065 break;
2066 if (hiscnt == hislen - 1) /* wrap around */
2067 hiscnt = 0;
2068 else
2069 ++hiscnt;
2070 }
2071 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2072 {
2073 hiscnt = i;
2074 break;
2075 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002076 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002077 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 || STRNCMP(history[histype][hiscnt].hisstr,
2079 lookfor, (size_t)j) == 0)
2080 break;
2081 }
2082
2083 if (hiscnt != i) /* jumped to other entry */
2084 {
2085 char_u *p;
2086 int len;
2087 int old_firstc;
2088
2089 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002090 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 if (hiscnt == hislen)
2092 p = lookfor; /* back to the old one */
2093 else
2094 p = history[histype][hiscnt].hisstr;
2095
2096 if (histype == HIST_SEARCH
2097 && p != lookfor
2098 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2099 {
2100 /* Correct for the separator character used when
2101 * adding the history entry vs the one used now.
2102 * First loop: count length.
2103 * Second loop: copy the characters. */
2104 for (i = 0; i <= 1; ++i)
2105 {
2106 len = 0;
2107 for (j = 0; p[j] != NUL; ++j)
2108 {
2109 /* Replace old sep with new sep, unless it is
2110 * escaped. */
2111 if (p[j] == old_firstc
2112 && (j == 0 || p[j - 1] != '\\'))
2113 {
2114 if (i > 0)
2115 ccline.cmdbuff[len] = firstc;
2116 }
2117 else
2118 {
2119 /* Escape new sep, unless it is already
2120 * escaped. */
2121 if (p[j] == firstc
2122 && (j == 0 || p[j - 1] != '\\'))
2123 {
2124 if (i > 0)
2125 ccline.cmdbuff[len] = '\\';
2126 ++len;
2127 }
2128 if (i > 0)
2129 ccline.cmdbuff[len] = p[j];
2130 }
2131 ++len;
2132 }
2133 if (i == 0)
2134 {
2135 alloc_cmdbuff(len);
2136 if (ccline.cmdbuff == NULL)
2137 goto returncmd;
2138 }
2139 }
2140 ccline.cmdbuff[len] = NUL;
2141 }
2142 else
2143 {
2144 alloc_cmdbuff((int)STRLEN(p));
2145 if (ccline.cmdbuff == NULL)
2146 goto returncmd;
2147 STRCPY(ccline.cmdbuff, p);
2148 }
2149
2150 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2151 redrawcmd();
2152 goto cmdline_changed;
2153 }
2154 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002155#endif
2156 goto cmdline_not_changed;
2157
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002158#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002159 case Ctrl_G: /* next match */
2160 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002161 if (may_adjust_incsearch_highlighting(
2162 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002163 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002164 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165#endif
2166
2167 case Ctrl_V:
2168 case Ctrl_Q:
2169#ifdef FEAT_MOUSE
2170 ignore_drag_release = TRUE;
2171#endif
2172 putcmdline('^', TRUE);
2173 c = get_literal(); /* get next (two) character(s) */
2174 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002175 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176#ifdef FEAT_MBYTE
2177 /* may need to remove ^ when composing char was typed */
2178 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2179 {
2180 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2181 msg_putchar(' ');
2182 cursorcmd();
2183 }
2184#endif
2185 break;
2186
2187#ifdef FEAT_DIGRAPHS
2188 case Ctrl_K:
2189#ifdef FEAT_MOUSE
2190 ignore_drag_release = TRUE;
2191#endif
2192 putcmdline('?', TRUE);
2193#ifdef USE_ON_FLY_SCROLL
2194 dont_scroll = TRUE; /* disallow scrolling here */
2195#endif
2196 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002197 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (c != NUL)
2199 break;
2200
2201 redrawcmd();
2202 goto cmdline_not_changed;
2203#endif /* FEAT_DIGRAPHS */
2204
2205#ifdef FEAT_RIGHTLEFT
2206 case Ctrl__: /* CTRL-_: switch language mode */
2207 if (!p_ari)
2208 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002209# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (p_altkeymap)
2211 {
2212 cmd_fkmap = !cmd_fkmap;
2213 if (cmd_fkmap) /* in Farsi always in Insert mode */
2214 ccline.overstrike = FALSE;
2215 }
2216 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 cmd_hkmap = !cmd_hkmap;
2219 goto cmdline_not_changed;
2220#endif
2221
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002222 case K_PS:
2223 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2224 goto cmdline_changed;
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 default:
2227#ifdef UNIX
2228 if (c == intr_char)
2229 {
2230 gotesc = TRUE; /* will free ccline.cmdbuff after
2231 putting it in history */
2232 goto returncmd; /* back to Normal mode */
2233 }
2234#endif
2235 /*
2236 * Normal character with no special meaning. Just set mod_mask
2237 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2238 * the string <S-Space>. This should only happen after ^V.
2239 */
2240 if (!IS_SPECIAL(c))
2241 mod_mask = 0x0;
2242 break;
2243 }
2244 /*
2245 * End of switch on command line character.
2246 * We come here if we have a normal character.
2247 */
2248
Bram Moolenaarede3e632013-06-23 16:16:19 +02002249 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#ifdef FEAT_MBYTE
2251 /* Add ABBR_OFF for characters above 0x100, this is
2252 * what check_abbr() expects. */
2253 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2254#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002255 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 goto cmdline_changed;
2257
2258 /*
2259 * put the character in the command line
2260 */
2261 if (IS_SPECIAL(c) || mod_mask != 0)
2262 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2263 else
2264 {
2265#ifdef FEAT_MBYTE
2266 if (has_mbyte)
2267 {
2268 j = (*mb_char2bytes)(c, IObuff);
2269 IObuff[j] = NUL; /* exclude composing chars */
2270 put_on_cmdline(IObuff, j, TRUE);
2271 }
2272 else
2273#endif
2274 {
2275 IObuff[0] = c;
2276 put_on_cmdline(IObuff, 1, TRUE);
2277 }
2278 }
2279 goto cmdline_changed;
2280
2281/*
2282 * This part implements incremental searches for "/" and "?"
2283 * Jump to cmdline_not_changed when a character has been read but the command
2284 * line did not change. Then we only search and redraw if something changed in
2285 * the past.
2286 * Jump to cmdline_changed when the command line did change.
2287 * (Sorry for the goto's, I know it is ugly).
2288 */
2289cmdline_not_changed:
2290#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002291 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 continue;
2293#endif
2294
2295cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002296 /* Trigger CmdlineChanged autocommands. */
2297 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002300 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#endif
2302
2303#ifdef FEAT_RIGHTLEFT
2304 if (cmdmsg_rl
2305# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002306 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307# endif
2308 )
2309 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002310 * right-left typing. Not efficient, but it works.
2311 * Do it only when there are no characters left to read
2312 * to avoid useless intermediate redraws. */
2313 if (vpeekc() == NUL)
2314 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#endif
2316 }
2317
2318returncmd:
2319
2320#ifdef FEAT_RIGHTLEFT
2321 cmdmsg_rl = FALSE;
2322#endif
2323
2324#ifdef FEAT_FKMAP
2325 cmd_fkmap = 0;
2326#endif
2327
2328 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002329 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002332 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#endif
2334
2335 if (ccline.cmdbuff != NULL)
2336 {
2337 /*
2338 * Put line in history buffer (":" and "=" only when it was typed).
2339 */
2340#ifdef FEAT_CMDHIST
2341 if (ccline.cmdlen && firstc != NUL
2342 && (some_key_typed || histype == HIST_SEARCH))
2343 {
2344 add_to_history(histype, ccline.cmdbuff, TRUE,
2345 histype == HIST_SEARCH ? firstc : NUL);
2346 if (firstc == ':')
2347 {
2348 vim_free(new_last_cmdline);
2349 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2350 }
2351 }
2352#endif
2353
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002354 if (gotesc)
2355 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357
2358 /*
2359 * If the screen was shifted up, redraw the whole screen (later).
2360 * If the line is too long, clear it, so ruler and shown command do
2361 * not get printed in the middle of it.
2362 */
2363 msg_check();
2364 msg_scroll = save_msg_scroll;
2365 redir_off = FALSE;
2366
2367 /* When the command line was typed, no need for a wait-return prompt. */
2368 if (some_key_typed)
2369 need_wait_return = FALSE;
2370
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002371 /* Trigger CmdlineLeave autocommands. */
2372 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002375#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2377 im_save_status(b_im_ptr);
2378 im_set_active(FALSE);
2379#endif
2380#ifdef FEAT_MOUSE
2381 setmouse();
2382#endif
2383#ifdef CURSOR_SHAPE
2384 ui_cursor_shape(); /* may show different cursor shape */
2385#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002386 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002388 {
2389 char_u *p = ccline.cmdbuff;
2390
2391 /* Make ccline empty, getcmdline() may try to use it. */
2392 ccline.cmdbuff = NULL;
2393 return p;
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395}
2396
2397#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2398/*
2399 * Get a command line with a prompt.
2400 * This is prepared to be called recursively from getcmdline() (e.g. by
2401 * f_input() when evaluating an expression from CTRL-R =).
2402 * Returns the command line in allocated memory, or NULL.
2403 */
2404 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002405getcmdline_prompt(
2406 int firstc,
2407 char_u *prompt, /* command line prompt */
2408 int attr, /* attributes for prompt */
2409 int xp_context, /* type of expansion */
2410 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 char_u *s;
2413 struct cmdline_info save_ccline;
2414 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002415 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002417 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 ccline.cmdprompt = prompt;
2419 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002420# ifdef FEAT_EVAL
2421 ccline.xp_context = xp_context;
2422 ccline.xp_arg = xp_arg;
2423 ccline.input_fn = (firstc == '@');
2424# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002425 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002427 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002428 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002429 /* Restore msg_col, the prompt from input() may have changed it.
2430 * But only if called recursively and the commandline is therefore being
2431 * restored to an old one; if not, the input() prompt stays on the screen,
2432 * so we need its modified msg_col left intact. */
2433 if (ccline.cmdbuff != NULL)
2434 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 return s;
2437}
2438#endif
2439
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002440/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002441 * Return TRUE when the text must not be changed and we can't switch to
2442 * another window or buffer. Used when editing the command line, evaluating
2443 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002444 */
2445 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002446text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002447{
2448#ifdef FEAT_CMDWIN
2449 if (cmdwin_type != 0)
2450 return TRUE;
2451#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002452 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002453}
2454
2455/*
2456 * Give an error message for a command that isn't allowed while the cmdline
2457 * window is open or editing the cmdline in another way.
2458 */
2459 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002460text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002461{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002462 EMSG(_(get_text_locked_msg()));
2463}
2464
2465 char_u *
2466get_text_locked_msg(void)
2467{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002468#ifdef FEAT_CMDWIN
2469 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002470 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002471#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002472 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002473}
2474
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002475/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002476 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2477 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002478 */
2479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002480curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002481{
2482 if (curbuf_lock > 0)
2483 {
2484 EMSG(_("E788: Not allowed to edit another buffer now"));
2485 return TRUE;
2486 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002487 return allbuf_locked();
2488}
2489
2490/*
2491 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2492 * message.
2493 */
2494 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002495allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002496{
2497 if (allbuf_lock > 0)
2498 {
2499 EMSG(_("E811: Not allowed to change buffer information now"));
2500 return TRUE;
2501 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002502 return FALSE;
2503}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002506cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2509 if (cmdline_star > 0) /* showing '*', always 1 position */
2510 return 1;
2511#endif
2512 return ptr2cells(ccline.cmdbuff + idx);
2513}
2514
2515/*
2516 * Compute the offset of the cursor on the command line for the prompt and
2517 * indent.
2518 */
2519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002520set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002522 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 ccline.cmdspos = 1 + ccline.cmdindent;
2524 else
2525 ccline.cmdspos = 0 + ccline.cmdindent;
2526}
2527
2528/*
2529 * Compute the screen position for the cursor on the command line.
2530 */
2531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002532set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534 int i, m, c;
2535
2536 set_cmdspos();
2537 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002540 if (m < 0) /* overflow, Columns or Rows at weird value */
2541 m = MAXCOL;
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 else
2544 m = MAXCOL;
2545 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2546 {
2547 c = cmdline_charsize(i);
2548#ifdef FEAT_MBYTE
2549 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2550 if (has_mbyte)
2551 correct_cmdspos(i, c);
2552#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002553 /* If the cmdline doesn't fit, show cursor on last visible char.
2554 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if ((ccline.cmdspos += c) >= m)
2556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 ccline.cmdspos -= c;
2558 break;
2559 }
2560#ifdef FEAT_MBYTE
2561 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002562 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#endif
2564 }
2565}
2566
2567#ifdef FEAT_MBYTE
2568/*
2569 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2570 * character that doesn't fit, so that a ">" must be displayed.
2571 */
2572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002573correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002575 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2577 && ccline.cmdspos % Columns + cells > Columns)
2578 ccline.cmdspos++;
2579}
2580#endif
2581
2582/*
2583 * Get an Ex command line for the ":" command.
2584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002586getexline(
2587 int c, /* normally ':', NUL for ":append" */
2588 void *cookie UNUSED,
2589 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591 /* When executing a register, remove ':' that's in front of each line. */
2592 if (exec_from_reg && vpeekc() == ':')
2593 (void)vgetc();
2594 return getcmdline(c, 1L, indent);
2595}
2596
2597/*
2598 * Get an Ex command line for Ex mode.
2599 * In Ex mode we only use the OS supplied line editing features and no
2600 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002601 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002604getexmodeline(
2605 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002606 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002607 void *cookie UNUSED,
2608 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002610 garray_T line_ga;
2611 char_u *pend;
2612 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002613 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002614 int escaped = FALSE; /* CTRL-V typed */
2615 int vcol = 0;
2616 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002617 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002618 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 /* Switch cursor on now. This avoids that it happens after the "\n", which
2621 * confuses the system function that computes tabstops. */
2622 cursor_on();
2623
2624 /* always start in column 0; write a newline if necessary */
2625 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002628 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002630 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 if (p_prompt)
2632 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 while (indent-- > 0)
2634 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637
2638 ga_init2(&line_ga, 1, 30);
2639
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002640 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002642 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002643 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002644 while (indent >= 8)
2645 {
2646 ga_append(&line_ga, TAB);
2647 msg_puts((char_u *)" ");
2648 indent -= 8;
2649 }
2650 while (indent-- > 0)
2651 {
2652 ga_append(&line_ga, ' ');
2653 msg_putchar(' ');
2654 }
2655 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002656 ++no_mapping;
2657 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 /*
2660 * Get the line, one character at a time.
2661 */
2662 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002663 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002665 long sw;
2666 char_u *s;
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (ga_grow(&line_ga, 40) == FAIL)
2669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
Bram Moolenaarba748c82017-02-26 14:00:07 +01002671 /*
2672 * Get one character at a time.
2673 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002674 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002675
2676 /* Check for a ":normal" command and no more characters left. */
2677 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2678 c1 = '\n';
2679 else
2680 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
2682 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002683 * Handle line editing.
2684 * Previously this was left to the system, putting the terminal in
2685 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002687 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002689 msg_putchar('\n');
2690 break;
2691 }
2692
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002693 if (c1 == K_PS)
2694 {
2695 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2696 goto redraw;
2697 }
2698
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002699 if (!escaped)
2700 {
2701 /* CR typed means "enter", which is NL */
2702 if (c1 == '\r')
2703 c1 = '\n';
2704
2705 if (c1 == BS || c1 == K_BS
2706 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708 if (line_ga.ga_len > 0)
2709 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002710#ifdef FEAT_MBYTE
2711 if (has_mbyte)
2712 {
2713 p = (char_u *)line_ga.ga_data;
2714 p[line_ga.ga_len] = NUL;
2715 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2716 line_ga.ga_len -= len;
2717 }
2718 else
2719#endif
2720 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002721 goto redraw;
2722 }
2723 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002726 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 msg_col = startcol;
2729 msg_clr_eos();
2730 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002731 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732 }
2733
2734 if (c1 == Ctrl_T)
2735 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002736 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 p = (char_u *)line_ga.ga_data;
2738 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002739 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002740 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002741add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002742 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002744 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002745 p = (char_u *)line_ga.ga_data;
2746 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2748 *s = ' ';
2749 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002751redraw:
2752 /* redraw the line */
2753 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002755 p = (char_u *)line_ga.ga_data;
2756 p[line_ga.ga_len] = NUL;
2757 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002761 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 msg_putchar(' ');
2764 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002765 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002769 len = MB_PTR2LEN(p);
2770 msg_outtrans_len(p, len);
2771 vcol += ptr2cells(p);
2772 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
2774 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002775 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002776 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002777 continue;
2778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 if (c1 == Ctrl_D)
2781 {
2782 /* Delete one shiftwidth. */
2783 p = (char_u *)line_ga.ga_data;
2784 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002786 if (prev_char == '^')
2787 ex_keep_indent = TRUE;
2788 indent = 0;
2789 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 else
2792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002794 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002795 if (indent > 0)
2796 {
2797 --indent;
2798 indent -= indent % get_sw_value(curbuf);
2799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002801 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002803 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2805 --line_ga.ga_len;
2806 }
2807 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809
2810 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2811 {
2812 escaped = TRUE;
2813 continue;
2814 }
2815
2816 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2817 if (IS_SPECIAL(c1))
2818 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820
2821 if (IS_SPECIAL(c1))
2822 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002823#ifdef FEAT_MBYTE
2824 if (has_mbyte)
2825 len = (*mb_char2bytes)(c1,
2826 (char_u *)line_ga.ga_data + line_ga.ga_len);
2827 else
2828#endif
2829 {
2830 len = 1;
2831 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2832 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 if (c1 == '\n')
2834 msg_putchar('\n');
2835 else if (c1 == TAB)
2836 {
2837 /* Don't use chartabsize(), 'ts' can be different */
2838 do
2839 {
2840 msg_putchar(' ');
2841 } while (++vcol % 8);
2842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002846 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002847 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002849 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 escaped = FALSE;
2851
2852 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002853 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002855 /* We are done when a NL is entered, but not when it comes after an
2856 * odd number of backslashes, that results in a NUL. */
2857 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002859 int bcount = 0;
2860
2861 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2862 ++bcount;
2863
2864 if (bcount > 0)
2865 {
2866 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2867 * "\NL", etc. */
2868 line_ga.ga_len -= (bcount + 1) / 2;
2869 pend -= (bcount + 1) / 2;
2870 pend[-1] = '\n';
2871 }
2872
2873 if ((bcount & 1) == 0)
2874 {
2875 --line_ga.ga_len;
2876 --pend;
2877 *pend = NUL;
2878 break;
2879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881 }
2882
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883 --no_mapping;
2884 --allow_keys;
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 /* make following messages go to the next line */
2887 msg_didout = FALSE;
2888 msg_col = 0;
2889 if (msg_row < Rows - 1)
2890 ++msg_row;
2891 emsg_on_display = FALSE; /* don't want ui_delay() */
2892
2893 if (got_int)
2894 ga_clear(&line_ga);
2895
2896 return (char_u *)line_ga.ga_data;
2897}
2898
Bram Moolenaare344bea2005-09-01 20:46:49 +00002899# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2900 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901/*
2902 * Return TRUE if ccline.overstrike is on.
2903 */
2904 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002905cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 return ccline.overstrike;
2908}
2909
2910/*
2911 * Return TRUE if the cursor is at the end of the cmdline.
2912 */
2913 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002914cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
2916 return (ccline.cmdpos >= ccline.cmdlen);
2917}
2918#endif
2919
Bram Moolenaar9372a112005-12-06 19:59:18 +00002920#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921/*
2922 * Return the virtual column number at the current cursor position.
2923 * This is used by the IM code to obtain the start of the preedit string.
2924 */
2925 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002926cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2929 return MAXCOL;
2930
2931# ifdef FEAT_MBYTE
2932 if (has_mbyte)
2933 {
2934 colnr_T col;
2935 int i = 0;
2936
2937 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002938 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
2940 return col;
2941 }
2942 else
2943# endif
2944 return ccline.cmdpos;
2945}
2946#endif
2947
2948#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2949/*
2950 * If part of the command line is an IM preedit string, redraw it with
2951 * IM feedback attributes. The cursor position is restored after drawing.
2952 */
2953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002954redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 if ((State & CMDLINE)
2957 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002958 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 && !p_imdisable
2960 && im_is_preediting())
2961 {
2962 int cmdpos = 0;
2963 int cmdspos;
2964 int old_row;
2965 int old_col;
2966 colnr_T col;
2967
2968 old_row = msg_row;
2969 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002970 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972# ifdef FEAT_MBYTE
2973 if (has_mbyte)
2974 {
2975 for (col = 0; col < preedit_start_col
2976 && cmdpos < ccline.cmdlen; ++col)
2977 {
2978 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002979 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 }
2982 else
2983# endif
2984 {
2985 cmdspos += preedit_start_col;
2986 cmdpos += preedit_start_col;
2987 }
2988
2989 msg_row = cmdline_row + (cmdspos / (int)Columns);
2990 msg_col = cmdspos % (int)Columns;
2991 if (msg_row >= Rows)
2992 msg_row = Rows - 1;
2993
2994 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2995 {
2996 int char_len;
2997 int char_attr;
2998
2999 char_attr = im_get_feedback_attr(col);
3000 if (char_attr < 0)
3001 break; /* end of preedit string */
3002
3003# ifdef FEAT_MBYTE
3004 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003005 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 else
3007# endif
3008 char_len = 1;
3009
3010 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3011 cmdpos += char_len;
3012 }
3013
3014 msg_row = old_row;
3015 msg_col = old_col;
3016 }
3017}
3018#endif /* FEAT_XIM && FEAT_GUI_GTK */
3019
3020/*
3021 * Allocate a new command line buffer.
3022 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3023 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3024 */
3025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003026alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 /*
3029 * give some extra space to avoid having to allocate all the time
3030 */
3031 if (len < 80)
3032 len = 100;
3033 else
3034 len += 20;
3035
3036 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3037 ccline.cmdbufflen = len;
3038}
3039
3040/*
3041 * Re-allocate the command line to length len + something extra.
3042 * return FAIL for failure, OK otherwise
3043 */
3044 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003045realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 char_u *p;
3048
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003049 if (len < ccline.cmdbufflen)
3050 return OK; /* no need to resize */
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 p = ccline.cmdbuff;
3053 alloc_cmdbuff(len); /* will get some more */
3054 if (ccline.cmdbuff == NULL) /* out of memory */
3055 {
3056 ccline.cmdbuff = p; /* keep the old one */
3057 return FAIL;
3058 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003059 /* There isn't always a NUL after the command, but it may need to be
3060 * there, thus copy up to the NUL and add a NUL. */
3061 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3062 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003064
3065 if (ccline.xpc != NULL
3066 && ccline.xpc->xp_pattern != NULL
3067 && ccline.xpc->xp_context != EXPAND_NOTHING
3068 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3069 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003070 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003071
3072 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3073 * to point into the newly allocated memory. */
3074 if (i >= 0 && i <= ccline.cmdlen)
3075 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3076 }
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 return OK;
3079}
3080
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003081#if defined(FEAT_ARABIC) || defined(PROTO)
3082static char_u *arshape_buf = NULL;
3083
3084# if defined(EXITFREE) || defined(PROTO)
3085 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003086free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003087{
3088 vim_free(arshape_buf);
3089}
3090# endif
3091#endif
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093/*
3094 * Draw part of the cmdline at the current cursor position. But draw stars
3095 * when cmdline_star is TRUE.
3096 */
3097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003098draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3101 int i;
3102
3103 if (cmdline_star > 0)
3104 for (i = 0; i < len; ++i)
3105 {
3106 msg_putchar('*');
3107# ifdef FEAT_MBYTE
3108 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003109 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110# endif
3111 }
3112 else
3113#endif
3114#ifdef FEAT_ARABIC
3115 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 static int buflen = 0;
3118 char_u *p;
3119 int j;
3120 int newlen = 0;
3121 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003122 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 int prev_c = 0;
3124 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003125 int u8c;
3126 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
3129 /*
3130 * Do arabic shaping into a temporary buffer. This is very
3131 * inefficient!
3132 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003133 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
3135 /* Re-allocate the buffer. We keep it around to avoid a lot of
3136 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003137 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003138 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003139 arshape_buf = alloc(buflen);
3140 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 return; /* out of memory */
3142 }
3143
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003144 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3145 {
3146 /* Prepend a space to draw the leading composing char on. */
3147 arshape_buf[0] = ' ';
3148 newlen = 1;
3149 }
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 for (j = start; j < start + len; j += mb_l)
3152 {
3153 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003154 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003155 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (ARABIC_CHAR(u8c))
3157 {
3158 /* Do Arabic shaping. */
3159 if (cmdmsg_rl)
3160 {
3161 /* displaying from right to left */
3162 pc = prev_c;
3163 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003164 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (j + mb_l >= start + len)
3166 nc = NUL;
3167 else
3168 nc = utf_ptr2char(p + mb_l);
3169 }
3170 else
3171 {
3172 /* displaying from left to right */
3173 if (j + mb_l >= start + len)
3174 pc = NUL;
3175 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003176 {
3177 int pcc[MAX_MCO];
3178
3179 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003181 pc1 = pcc[0];
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 nc = prev_c;
3184 }
3185 prev_c = u8c;
3186
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003187 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003189 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003190 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003192 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3193 if (u8cc[1] != 0)
3194 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003195 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197 }
3198 else
3199 {
3200 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 newlen += mb_l;
3203 }
3204 }
3205
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003206 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208 else
3209#endif
3210 msg_outtrans_len(ccline.cmdbuff + start, len);
3211}
3212
3213/*
3214 * Put a character on the command line. Shifts the following text to the
3215 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3216 * "c" must be printable (fit in one display cell)!
3217 */
3218 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003219putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221 if (cmd_silent)
3222 return;
3223 msg_no_more = TRUE;
3224 msg_putchar(c);
3225 if (shift)
3226 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3227 msg_no_more = FALSE;
3228 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003229 extra_char = c;
3230 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231}
3232
3233/*
3234 * Undo a putcmdline(c, FALSE).
3235 */
3236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003237unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 if (cmd_silent)
3240 return;
3241 msg_no_more = TRUE;
3242 if (ccline.cmdlen == ccline.cmdpos)
3243 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003244#ifdef FEAT_MBYTE
3245 else if (has_mbyte)
3246 draw_cmdline(ccline.cmdpos,
3247 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 else
3250 draw_cmdline(ccline.cmdpos, 1);
3251 msg_no_more = FALSE;
3252 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003253 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254}
3255
3256/*
3257 * Put the given string, of the given length, onto the command line.
3258 * If len is -1, then STRLEN() is used to calculate the length.
3259 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3260 * part will be redrawn, otherwise it will not. If this function is called
3261 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3262 * called afterwards.
3263 */
3264 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003265put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
3267 int retval;
3268 int i;
3269 int m;
3270 int c;
3271
3272 if (len < 0)
3273 len = (int)STRLEN(str);
3274
3275 /* Check if ccline.cmdbuff needs to be longer */
3276 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003277 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else
3279 retval = OK;
3280 if (retval == OK)
3281 {
3282 if (!ccline.overstrike)
3283 {
3284 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3285 ccline.cmdbuff + ccline.cmdpos,
3286 (size_t)(ccline.cmdlen - ccline.cmdpos));
3287 ccline.cmdlen += len;
3288 }
3289 else
3290 {
3291#ifdef FEAT_MBYTE
3292 if (has_mbyte)
3293 {
3294 /* Count nr of characters in the new string. */
3295 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003296 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 ++m;
3298 /* Count nr of bytes in cmdline that are overwritten by these
3299 * characters. */
3300 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003301 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 --m;
3303 if (i < ccline.cmdlen)
3304 {
3305 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3306 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3307 ccline.cmdlen += ccline.cmdpos + len - i;
3308 }
3309 else
3310 ccline.cmdlen = ccline.cmdpos + len;
3311 }
3312 else
3313#endif
3314 if (ccline.cmdpos + len > ccline.cmdlen)
3315 ccline.cmdlen = ccline.cmdpos + len;
3316 }
3317 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3318 ccline.cmdbuff[ccline.cmdlen] = NUL;
3319
3320#ifdef FEAT_MBYTE
3321 if (enc_utf8)
3322 {
3323 /* When the inserted text starts with a composing character,
3324 * backup to the character before it. There could be two of them.
3325 */
3326 i = 0;
3327 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3328 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3329 {
3330 i = (*mb_head_off)(ccline.cmdbuff,
3331 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3332 ccline.cmdpos -= i;
3333 len += i;
3334 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3335 }
3336# ifdef FEAT_ARABIC
3337 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3338 {
3339 /* Check the previous character for Arabic combining pair. */
3340 i = (*mb_head_off)(ccline.cmdbuff,
3341 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3342 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3343 + ccline.cmdpos - i), c))
3344 {
3345 ccline.cmdpos -= i;
3346 len += i;
3347 }
3348 else
3349 i = 0;
3350 }
3351# endif
3352 if (i != 0)
3353 {
3354 /* Also backup the cursor position. */
3355 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3356 ccline.cmdspos -= i;
3357 msg_col -= i;
3358 if (msg_col < 0)
3359 {
3360 msg_col += Columns;
3361 --msg_row;
3362 }
3363 }
3364 }
3365#endif
3366
3367 if (redraw && !cmd_silent)
3368 {
3369 msg_no_more = TRUE;
3370 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003371 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3373 /* Avoid clearing the rest of the line too often. */
3374 if (cmdline_row != i || ccline.overstrike)
3375 msg_clr_eos();
3376 msg_no_more = FALSE;
3377 }
3378#ifdef FEAT_FKMAP
3379 /*
3380 * If we are in Farsi command mode, the character input must be in
3381 * Insert mode. So do not advance the cmdpos.
3382 */
3383 if (!cmd_fkmap)
3384#endif
3385 {
3386 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003389 if (m < 0) /* overflow, Columns or Rows at weird value */
3390 m = MAXCOL;
3391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 else
3393 m = MAXCOL;
3394 for (i = 0; i < len; ++i)
3395 {
3396 c = cmdline_charsize(ccline.cmdpos);
3397#ifdef FEAT_MBYTE
3398 /* count ">" for a double-wide char that doesn't fit. */
3399 if (has_mbyte)
3400 correct_cmdspos(ccline.cmdpos, c);
3401#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003402 /* Stop cursor at the end of the screen, but do increment the
3403 * insert position, so that entering a very long command
3404 * works, even though you can't see it. */
3405 if (ccline.cmdspos + c < m)
3406 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#ifdef FEAT_MBYTE
3408 if (has_mbyte)
3409 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003410 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (c > len - i - 1)
3412 c = len - i - 1;
3413 ccline.cmdpos += c;
3414 i += c;
3415 }
3416#endif
3417 ++ccline.cmdpos;
3418 }
3419 }
3420 }
3421 if (redraw)
3422 msg_check();
3423 return retval;
3424}
3425
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003426static struct cmdline_info prev_ccline;
3427static int prev_ccline_used = FALSE;
3428
3429/*
3430 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3431 * and overwrite it. But get_cmdline_str() may need it, thus make it
3432 * available globally in prev_ccline.
3433 */
3434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003435save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003436{
3437 if (!prev_ccline_used)
3438 {
3439 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3440 prev_ccline_used = TRUE;
3441 }
3442 *ccp = prev_ccline;
3443 prev_ccline = ccline;
3444 ccline.cmdbuff = NULL;
3445 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003446 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003447}
3448
3449/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003450 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003451 */
3452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003453restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003454{
3455 ccline = prev_ccline;
3456 prev_ccline = *ccp;
3457}
3458
Bram Moolenaar5a305422006-04-28 22:38:25 +00003459#if defined(FEAT_EVAL) || defined(PROTO)
3460/*
3461 * Save the command line into allocated memory. Returns a pointer to be
3462 * passed to restore_cmdline_alloc() later.
3463 * Returns NULL when failed.
3464 */
3465 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003466save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003467{
3468 struct cmdline_info *p;
3469
3470 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3471 if (p != NULL)
3472 save_cmdline(p);
3473 return (char_u *)p;
3474}
3475
3476/*
3477 * Restore the command line from the return value of save_cmdline_alloc().
3478 */
3479 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003480restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003481{
3482 if (p != NULL)
3483 {
3484 restore_cmdline((struct cmdline_info *)p);
3485 vim_free(p);
3486 }
3487}
3488#endif
3489
Bram Moolenaar8299df92004-07-10 09:47:34 +00003490/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003491 * Paste a yank register into the command line.
3492 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003493 * insert_reg() can't be used here, because special characters from the
3494 * register contents will be interpreted as commands.
3495 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003496 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003497 */
3498 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003499cmdline_paste(
3500 int regname,
3501 int literally, /* Insert text literally instead of "as typed" */
3502 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003503{
3504 long i;
3505 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003506 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003507 int allocated;
3508 struct cmdline_info save_ccline;
3509
3510 /* check for valid regname; also accept special characters for CTRL-R in
3511 * the command line */
3512 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003513 && regname != Ctrl_A && regname != Ctrl_L
3514 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003515 return FAIL;
3516
3517 /* A register containing CTRL-R can cause an endless loop. Allow using
3518 * CTRL-C to break the loop. */
3519 line_breakcheck();
3520 if (got_int)
3521 return FAIL;
3522
3523#ifdef FEAT_CLIPBOARD
3524 regname = may_get_selection(regname);
3525#endif
3526
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003527 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003528 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003529 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003530 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003532 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003533 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003534
3535 if (i)
3536 {
3537 /* Got the value of a special register in "arg". */
3538 if (arg == NULL)
3539 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003540
3541 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3542 * part of the word. */
3543 p = arg;
3544 if (p_is && regname == Ctrl_W)
3545 {
3546 char_u *w;
3547 int len;
3548
3549 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003550 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003551 {
3552#ifdef FEAT_MBYTE
3553 if (has_mbyte)
3554 {
3555 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3556 if (!vim_iswordc(mb_ptr2char(w - len)))
3557 break;
3558 w -= len;
3559 }
3560 else
3561#endif
3562 {
3563 if (!vim_iswordc(w[-1]))
3564 break;
3565 --w;
3566 }
3567 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003568 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003569 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3570 p += len;
3571 }
3572
3573 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003574 if (allocated)
3575 vim_free(arg);
3576 return OK;
3577 }
3578
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003579 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003580}
3581
3582/*
3583 * Put a string on the command line.
3584 * When "literally" is TRUE, insert literally.
3585 * When "literally" is FALSE, insert as typed, but don't leave the command
3586 * line.
3587 */
3588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003589cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003590{
3591 int c, cv;
3592
3593 if (literally)
3594 put_on_cmdline(s, -1, TRUE);
3595 else
3596 while (*s != NUL)
3597 {
3598 cv = *s;
3599 if (cv == Ctrl_V && s[1])
3600 ++s;
3601#ifdef FEAT_MBYTE
3602 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003603 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003604 else
3605#endif
3606 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003607 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3608 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003609#ifdef UNIX
3610 || c == intr_char
3611#endif
3612 || (c == Ctrl_BSL && *s == Ctrl_N))
3613 stuffcharReadbuff(Ctrl_V);
3614 stuffcharReadbuff(c);
3615 }
3616}
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#ifdef FEAT_WILDMENU
3619/*
3620 * Delete characters on the command line, from "from" to the current
3621 * position.
3622 */
3623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003624cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
3626 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3627 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3628 ccline.cmdlen -= ccline.cmdpos - from;
3629 ccline.cmdpos = from;
3630}
3631#endif
3632
3633/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003634 * This function is called when the screen size changes and with incremental
3635 * search and in other situations where the command line may have been
3636 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 */
3638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003639redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003641 redrawcmdline_ex(TRUE);
3642}
3643
3644 void
3645redrawcmdline_ex(int do_compute_cmdrow)
3646{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (cmd_silent)
3648 return;
3649 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003650 if (do_compute_cmdrow)
3651 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 redrawcmd();
3653 cursorcmd();
3654}
3655
3656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003657redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 int i;
3660
3661 if (cmd_silent)
3662 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003663 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 msg_putchar(ccline.cmdfirstc);
3665 if (ccline.cmdprompt != NULL)
3666 {
3667 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3668 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3669 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003670 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 --ccline.cmdindent;
3672 }
3673 else
3674 for (i = ccline.cmdindent; i > 0; --i)
3675 msg_putchar(' ');
3676}
3677
3678/*
3679 * Redraw what is currently on the command line.
3680 */
3681 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003682redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 if (cmd_silent)
3685 return;
3686
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003687 /* when 'incsearch' is set there may be no command line while redrawing */
3688 if (ccline.cmdbuff == NULL)
3689 {
3690 windgoto(cmdline_row, 0);
3691 msg_clr_eos();
3692 return;
3693 }
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 msg_start();
3696 redrawcmdprompt();
3697
3698 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3699 msg_no_more = TRUE;
3700 draw_cmdline(0, ccline.cmdlen);
3701 msg_clr_eos();
3702 msg_no_more = FALSE;
3703
3704 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003705 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003706 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 /*
3709 * An emsg() before may have set msg_scroll. This is used in normal mode,
3710 * in cmdline mode we can reset them now.
3711 */
3712 msg_scroll = FALSE; /* next message overwrites cmdline */
3713
3714 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3715 * in cmdline mode */
3716 skip_redraw = FALSE;
3717}
3718
3719 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003720compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003722 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 cmdline_row = Rows - 1;
3724 else
3725 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003726 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727}
3728
3729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003730cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
3732 if (cmd_silent)
3733 return;
3734
3735#ifdef FEAT_RIGHTLEFT
3736 if (cmdmsg_rl)
3737 {
3738 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3739 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3740 if (msg_row <= 0)
3741 msg_row = Rows - 1;
3742 }
3743 else
3744#endif
3745 {
3746 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3747 msg_col = ccline.cmdspos % (int)Columns;
3748 if (msg_row >= Rows)
3749 msg_row = Rows - 1;
3750 }
3751
3752 windgoto(msg_row, msg_col);
3753#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003754 if (p_imst == IM_ON_THE_SPOT)
3755 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#endif
3757#ifdef MCH_CURSOR_SHAPE
3758 mch_update_cursor();
3759#endif
3760}
3761
3762 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003763gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 msg_start();
3766#ifdef FEAT_RIGHTLEFT
3767 if (cmdmsg_rl)
3768 msg_col = Columns - 1;
3769 else
3770#endif
3771 msg_col = 0; /* always start in column 0 */
3772 if (clr) /* clear the bottom line(s) */
3773 msg_clr_eos(); /* will reset clear_cmdline */
3774 windgoto(cmdline_row, 0);
3775}
3776
3777/*
3778 * Check the word in front of the cursor for an abbreviation.
3779 * Called when the non-id character "c" has been entered.
3780 * When an abbreviation is recognized it is removed from the text with
3781 * backspaces and the replacement string is inserted, followed by "c".
3782 */
3783 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003784ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003786 int spos = 0;
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3789 return FALSE;
3790
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003791 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3792 * Actually accepts any mark. */
3793 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3794 spos++;
3795 if (ccline.cmdlen - spos > 5
3796 && ccline.cmdbuff[spos] == '\''
3797 && ccline.cmdbuff[spos + 2] == ','
3798 && ccline.cmdbuff[spos + 3] == '\'')
3799 spos += 5;
3800 else
3801 /* check abbreviation from the beginning of the commandline */
3802 spos = 0;
3803
3804 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805}
3806
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003807#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3808 static int
3809#ifdef __BORLANDC__
3810_RTLENTRYF
3811#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003812sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003813{
3814 char_u *p1 = *(char_u **)s1;
3815 char_u *p2 = *(char_u **)s2;
3816
3817 if (*p1 != '<' && *p2 == '<') return -1;
3818 if (*p1 == '<' && *p2 != '<') return 1;
3819 return STRCMP(p1, p2);
3820}
3821#endif
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823/*
3824 * Return FAIL if this is not an appropriate context in which to do
3825 * completion of anything, return OK if it is (even if there are no matches).
3826 * For the caller, this means that the character is just passed through like a
3827 * normal character (instead of being expanded). This allows :s/^I^D etc.
3828 */
3829 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003830nextwild(
3831 expand_T *xp,
3832 int type,
3833 int options, /* extra options for ExpandOne() */
3834 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
3836 int i, j;
3837 char_u *p1;
3838 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 int difflen;
3840 int v;
3841
3842 if (xp->xp_numfiles == -1)
3843 {
3844 set_expand_context(xp);
3845 cmd_showtail = expand_showtail(xp);
3846 }
3847
3848 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3849 {
3850 beep_flush();
3851 return OK; /* Something illegal on command line */
3852 }
3853 if (xp->xp_context == EXPAND_NOTHING)
3854 {
3855 /* Caller can use the character as a normal char instead */
3856 return FAIL;
3857 }
3858
3859 MSG_PUTS("..."); /* show that we are busy */
3860 out_flush();
3861
3862 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003863 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
3865 if (type == WILD_NEXT || type == WILD_PREV)
3866 {
3867 /*
3868 * Get next/previous match for a previous expanded pattern.
3869 */
3870 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3871 }
3872 else
3873 {
3874 /*
3875 * Translate string into pattern and expand it.
3876 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003877 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3878 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 p2 = NULL;
3880 else
3881 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003882 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003883 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3884 if (escape)
3885 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003886
3887 if (p_wic)
3888 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003889 p2 = ExpandOne(xp, p1,
3890 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003891 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003893 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (p2 != NULL && type == WILD_LONGEST)
3895 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003896 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (ccline.cmdbuff[i + j] == '*'
3898 || ccline.cmdbuff[i + j] == '?')
3899 break;
3900 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003901 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 }
3904 }
3905
3906 if (p2 != NULL && !got_int)
3907 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003908 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003909 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003911 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 xp->xp_pattern = ccline.cmdbuff + i;
3913 }
3914 else
3915 v = OK;
3916 if (v == OK)
3917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003918 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3919 &ccline.cmdbuff[ccline.cmdpos],
3920 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3921 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 ccline.cmdlen += difflen;
3923 ccline.cmdpos += difflen;
3924 }
3925 }
3926 vim_free(p2);
3927
3928 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003929 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 /* When expanding a ":map" command and no matches are found, assume that
3932 * the key is supposed to be inserted literally */
3933 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3934 return FAIL;
3935
3936 if (xp->xp_numfiles <= 0 && p2 == NULL)
3937 beep_flush();
3938 else if (xp->xp_numfiles == 1)
3939 /* free expanded pattern */
3940 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3941
3942 return OK;
3943}
3944
3945/*
3946 * Do wildcard expansion on the string 'str'.
3947 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003948 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * Return NULL for failure.
3950 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003951 * "orig" is the originally expanded string, copied to allocated memory. It
3952 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3953 * WILD_PREV "orig" should be NULL.
3954 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003955 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3956 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 *
3958 * mode = WILD_FREE: just free previously expanded matches
3959 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3960 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3961 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3962 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3963 * mode = WILD_ALL: return all matches concatenated
3964 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003965 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 *
3967 * options = WILD_LIST_NOTFOUND: list entries without a match
3968 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3969 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3970 * options = WILD_NO_BEEP: Don't beep for multiple matches
3971 * options = WILD_ADD_SLASH: add a slash after directory names
3972 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3973 * options = WILD_SILENT: don't print warning messages
3974 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003975 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 *
3977 * The variables xp->xp_context and xp->xp_backslash must have been set!
3978 */
3979 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003980ExpandOne(
3981 expand_T *xp,
3982 char_u *str,
3983 char_u *orig, /* allocated copy of original of expanded string */
3984 int options,
3985 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 char_u *ss = NULL;
3988 static int findex;
3989 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003990 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int i;
3992 long_u len;
3993 int non_suf_match; /* number without matching suffix */
3994
3995 /*
3996 * first handle the case of using an old match
3997 */
3998 if (mode == WILD_NEXT || mode == WILD_PREV)
3999 {
4000 if (xp->xp_numfiles > 0)
4001 {
4002 if (mode == WILD_PREV)
4003 {
4004 if (findex == -1)
4005 findex = xp->xp_numfiles;
4006 --findex;
4007 }
4008 else /* mode == WILD_NEXT */
4009 ++findex;
4010
4011 /*
4012 * When wrapping around, return the original string, set findex to
4013 * -1.
4014 */
4015 if (findex < 0)
4016 {
4017 if (orig_save == NULL)
4018 findex = xp->xp_numfiles - 1;
4019 else
4020 findex = -1;
4021 }
4022 if (findex >= xp->xp_numfiles)
4023 {
4024 if (orig_save == NULL)
4025 findex = 0;
4026 else
4027 findex = -1;
4028 }
4029#ifdef FEAT_WILDMENU
4030 if (p_wmnu)
4031 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4032 findex, cmd_showtail);
4033#endif
4034 if (findex == -1)
4035 return vim_strsave(orig_save);
4036 return vim_strsave(xp->xp_files[findex]);
4037 }
4038 else
4039 return NULL;
4040 }
4041
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004042 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4044 {
4045 FreeWild(xp->xp_numfiles, xp->xp_files);
4046 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004047 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 findex = 0;
4050
4051 if (mode == WILD_FREE) /* only release file name */
4052 return NULL;
4053
4054 if (xp->xp_numfiles == -1)
4055 {
4056 vim_free(orig_save);
4057 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004058 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 /*
4061 * Do the expansion.
4062 */
4063 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4064 options) == FAIL)
4065 {
4066#ifdef FNAME_ILLEGAL
4067 /* Illegal file name has been silently skipped. But when there
4068 * are wildcards, the real problem is that there was no match,
4069 * causing the pattern to be added, which has illegal characters.
4070 */
4071 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4072 EMSG2(_(e_nomatch2), str);
4073#endif
4074 }
4075 else if (xp->xp_numfiles == 0)
4076 {
4077 if (!(options & WILD_SILENT))
4078 EMSG2(_(e_nomatch2), str);
4079 }
4080 else
4081 {
4082 /* Escape the matches for use on the command line. */
4083 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4084
4085 /*
4086 * Check for matching suffixes in file names.
4087 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004088 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4089 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
4091 if (xp->xp_numfiles)
4092 non_suf_match = xp->xp_numfiles;
4093 else
4094 non_suf_match = 1;
4095 if ((xp->xp_context == EXPAND_FILES
4096 || xp->xp_context == EXPAND_DIRECTORIES)
4097 && xp->xp_numfiles > 1)
4098 {
4099 /*
4100 * More than one match; check suffix.
4101 * The files will have been sorted on matching suffix in
4102 * expand_wildcards, only need to check the first two.
4103 */
4104 non_suf_match = 0;
4105 for (i = 0; i < 2; ++i)
4106 if (match_suffix(xp->xp_files[i]))
4107 ++non_suf_match;
4108 }
4109 if (non_suf_match != 1)
4110 {
4111 /* Can we ever get here unless it's while expanding
4112 * interactively? If not, we can get rid of this all
4113 * together. Don't really want to wait for this message
4114 * (and possibly have to hit return to continue!).
4115 */
4116 if (!(options & WILD_SILENT))
4117 EMSG(_(e_toomany));
4118 else if (!(options & WILD_NO_BEEP))
4119 beep_flush();
4120 }
4121 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4122 ss = vim_strsave(xp->xp_files[0]);
4123 }
4124 }
4125 }
4126
4127 /* Find longest common part */
4128 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4129 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004130 int mb_len = 1;
4131 int c0, ci;
4132
4133 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004135#ifdef FEAT_MBYTE
4136 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004138 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4139 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4140 }
4141 else
4142#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004143 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004144 for (i = 1; i < xp->xp_numfiles; ++i)
4145 {
4146#ifdef FEAT_MBYTE
4147 if (has_mbyte)
4148 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4149 else
4150#endif
4151 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004152 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004154 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004155 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004157 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004160 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162 }
4163 if (i < xp->xp_numfiles)
4164 {
4165 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004166 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168 }
4169 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 ss = alloc((unsigned)len + 1);
4172 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004173 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 findex = -1; /* next p_wc gets first one */
4175 }
4176
4177 /* Concatenate all matching names */
4178 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4179 {
4180 len = 0;
4181 for (i = 0; i < xp->xp_numfiles; ++i)
4182 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4183 ss = lalloc(len, TRUE);
4184 if (ss != NULL)
4185 {
4186 *ss = NUL;
4187 for (i = 0; i < xp->xp_numfiles; ++i)
4188 {
4189 STRCAT(ss, xp->xp_files[i]);
4190 if (i != xp->xp_numfiles - 1)
4191 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4192 }
4193 }
4194 }
4195
4196 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4197 ExpandCleanup(xp);
4198
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004199 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004200 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004201 vim_free(orig);
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return ss;
4204}
4205
4206/*
4207 * Prepare an expand structure for use.
4208 */
4209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004210ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004212 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004213 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004215#ifndef BACKSLASH_IN_FILENAME
4216 xp->xp_shell = FALSE;
4217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 xp->xp_numfiles = -1;
4219 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004220#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4221 xp->xp_arg = NULL;
4222#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004223 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224}
4225
4226/*
4227 * Cleanup an expand structure after use.
4228 */
4229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004230ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
4232 if (xp->xp_numfiles >= 0)
4233 {
4234 FreeWild(xp->xp_numfiles, xp->xp_files);
4235 xp->xp_numfiles = -1;
4236 }
4237}
4238
4239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004240ExpandEscape(
4241 expand_T *xp,
4242 char_u *str,
4243 int numfiles,
4244 char_u **files,
4245 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 int i;
4248 char_u *p;
4249
4250 /*
4251 * May change home directory back to "~"
4252 */
4253 if (options & WILD_HOME_REPLACE)
4254 tilde_replace(str, numfiles, files);
4255
4256 if (options & WILD_ESCAPE)
4257 {
4258 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004259 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004260 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 || xp->xp_context == EXPAND_BUFFERS
4262 || xp->xp_context == EXPAND_DIRECTORIES)
4263 {
4264 /*
4265 * Insert a backslash into a file name before a space, \, %, #
4266 * and wildmatch characters, except '~'.
4267 */
4268 for (i = 0; i < numfiles; ++i)
4269 {
4270 /* for ":set path=" we need to escape spaces twice */
4271 if (xp->xp_backslash == XP_BS_THREE)
4272 {
4273 p = vim_strsave_escaped(files[i], (char_u *)" ");
4274 if (p != NULL)
4275 {
4276 vim_free(files[i]);
4277 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004278#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 p = vim_strsave_escaped(files[i], (char_u *)" ");
4280 if (p != NULL)
4281 {
4282 vim_free(files[i]);
4283 files[i] = p;
4284 }
4285#endif
4286 }
4287 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004288#ifdef BACKSLASH_IN_FILENAME
4289 p = vim_strsave_fnameescape(files[i], FALSE);
4290#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004291 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 if (p != NULL)
4294 {
4295 vim_free(files[i]);
4296 files[i] = p;
4297 }
4298
4299 /* If 'str' starts with "\~", replace "~" at start of
4300 * files[i] with "\~". */
4301 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004302 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004305
4306 /* If the first file starts with a '+' escape it. Otherwise it
4307 * could be seen as "+cmd". */
4308 if (*files[0] == '+')
4309 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 else if (xp->xp_context == EXPAND_TAGS)
4312 {
4313 /*
4314 * Insert a backslash before characters in a tag name that
4315 * would terminate the ":tag" command.
4316 */
4317 for (i = 0; i < numfiles; ++i)
4318 {
4319 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4320 if (p != NULL)
4321 {
4322 vim_free(files[i]);
4323 files[i] = p;
4324 }
4325 }
4326 }
4327 }
4328}
4329
4330/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004331 * Escape special characters in "fname" for when used as a file name argument
4332 * after a Vim command, or, when "shell" is non-zero, a shell command.
4333 * Returns the result in allocated memory.
4334 */
4335 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004336vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004337{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004338 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004339#ifdef BACKSLASH_IN_FILENAME
4340 char_u buf[20];
4341 int j = 0;
4342
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004343 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004344 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004345 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004346 buf[j++] = *p;
4347 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004348 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004349#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004350 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4351 if (shell && csh_like_shell() && p != NULL)
4352 {
4353 char_u *s;
4354
4355 /* For csh and similar shells need to put two backslashes before '!'.
4356 * One is taken by Vim, one by the shell. */
4357 s = vim_strsave_escaped(p, (char_u *)"!");
4358 vim_free(p);
4359 p = s;
4360 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004361#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004362
4363 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4364 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004365 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004366 escape_fname(&p);
4367
4368 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004369}
4370
4371/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004372 * Put a backslash before the file name in "pp", which is in allocated memory.
4373 */
4374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004375escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004376{
4377 char_u *p;
4378
4379 p = alloc((unsigned)(STRLEN(*pp) + 2));
4380 if (p != NULL)
4381 {
4382 p[0] = '\\';
4383 STRCPY(p + 1, *pp);
4384 vim_free(*pp);
4385 *pp = p;
4386 }
4387}
4388
4389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 * For each file name in files[num_files]:
4391 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4392 */
4393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394tilde_replace(
4395 char_u *orig_pat,
4396 int num_files,
4397 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 int i;
4400 char_u *p;
4401
4402 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4403 {
4404 for (i = 0; i < num_files; ++i)
4405 {
4406 p = home_replace_save(NULL, files[i]);
4407 if (p != NULL)
4408 {
4409 vim_free(files[i]);
4410 files[i] = p;
4411 }
4412 }
4413 }
4414}
4415
4416/*
4417 * Show all matches for completion on the command line.
4418 * Returns EXPAND_NOTHING when the character that triggered expansion should
4419 * be inserted like a normal character.
4420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004422showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4425 int num_files;
4426 char_u **files_found;
4427 int i, j, k;
4428 int maxlen;
4429 int lines;
4430 int columns;
4431 char_u *p;
4432 int lastlen;
4433 int attr;
4434 int showtail;
4435
4436 if (xp->xp_numfiles == -1)
4437 {
4438 set_expand_context(xp);
4439 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4440 &num_files, &files_found);
4441 showtail = expand_showtail(xp);
4442 if (i != EXPAND_OK)
4443 return i;
4444
4445 }
4446 else
4447 {
4448 num_files = xp->xp_numfiles;
4449 files_found = xp->xp_files;
4450 showtail = cmd_showtail;
4451 }
4452
4453#ifdef FEAT_WILDMENU
4454 if (!wildmenu)
4455 {
4456#endif
4457 msg_didany = FALSE; /* lines_left will be set */
4458 msg_start(); /* prepare for paging */
4459 msg_putchar('\n');
4460 out_flush();
4461 cmdline_row = msg_row;
4462 msg_didany = FALSE; /* lines_left will be set again */
4463 msg_start(); /* prepare for paging */
4464#ifdef FEAT_WILDMENU
4465 }
4466#endif
4467
4468 if (got_int)
4469 got_int = FALSE; /* only int. the completion, not the cmd line */
4470#ifdef FEAT_WILDMENU
4471 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004472 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
4474 else
4475 {
4476 /* find the length of the longest file name */
4477 maxlen = 0;
4478 for (i = 0; i < num_files; ++i)
4479 {
4480 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 || xp->xp_context == EXPAND_BUFFERS))
4483 {
4484 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4485 j = vim_strsize(NameBuff);
4486 }
4487 else
4488 j = vim_strsize(L_SHOWFILE(i));
4489 if (j > maxlen)
4490 maxlen = j;
4491 }
4492
4493 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4494 lines = num_files;
4495 else
4496 {
4497 /* compute the number of columns and lines for the listing */
4498 maxlen += 2; /* two spaces between file names */
4499 columns = ((int)Columns + 2) / maxlen;
4500 if (columns < 1)
4501 columns = 1;
4502 lines = (num_files + columns - 1) / columns;
4503 }
4504
Bram Moolenaar8820b482017-03-16 17:23:31 +01004505 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4508 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004509 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 msg_clr_eos();
4511 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004512 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514
4515 /* list the files line by line */
4516 for (i = 0; i < lines; ++i)
4517 {
4518 lastlen = 999;
4519 for (k = i; k < num_files; k += lines)
4520 {
4521 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4522 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004523 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 p = files_found[k] + STRLEN(files_found[k]) + 1;
4525 msg_advance(maxlen + 1);
4526 msg_puts(p);
4527 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004528 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530 }
4531 for (j = maxlen - lastlen; --j >= 0; )
4532 msg_putchar(' ');
4533 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004534 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 || xp->xp_context == EXPAND_BUFFERS)
4536 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004537 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004538 if (xp->xp_numfiles != -1)
4539 {
4540 char_u *halved_slash;
4541 char_u *exp_path;
4542
4543 /* Expansion was done before and special characters
4544 * were escaped, need to halve backslashes. Also
4545 * $HOME has been replaced with ~/. */
4546 exp_path = expand_env_save_opt(files_found[k], TRUE);
4547 halved_slash = backslash_halve_save(
4548 exp_path != NULL ? exp_path : files_found[k]);
4549 j = mch_isdir(halved_slash != NULL ? halved_slash
4550 : files_found[k]);
4551 vim_free(exp_path);
4552 vim_free(halved_slash);
4553 }
4554 else
4555 /* Expansion was done here, file names are literal. */
4556 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (showtail)
4558 p = L_SHOWFILE(k);
4559 else
4560 {
4561 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4562 TRUE);
4563 p = NameBuff;
4564 }
4565 }
4566 else
4567 {
4568 j = FALSE;
4569 p = L_SHOWFILE(k);
4570 }
4571 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4572 }
4573 if (msg_col > 0) /* when not wrapped around */
4574 {
4575 msg_clr_eos();
4576 msg_putchar('\n');
4577 }
4578 out_flush(); /* show one line at a time */
4579 if (got_int)
4580 {
4581 got_int = FALSE;
4582 break;
4583 }
4584 }
4585
4586 /*
4587 * we redraw the command below the lines that we have just listed
4588 * This is a bit tricky, but it saves a lot of screen updating.
4589 */
4590 cmdline_row = msg_row; /* will put it back later */
4591 }
4592
4593 if (xp->xp_numfiles == -1)
4594 FreeWild(num_files, files_found);
4595
4596 return EXPAND_OK;
4597}
4598
4599/*
4600 * Private gettail for showmatches() (and win_redr_status_matches()):
4601 * Find tail of file name path, but ignore trailing "/".
4602 */
4603 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 char_u *p;
4607 char_u *t = s;
4608 int had_sep = FALSE;
4609
4610 for (p = s; *p != NUL; )
4611 {
4612 if (vim_ispathsep(*p)
4613#ifdef BACKSLASH_IN_FILENAME
4614 && !rem_backslash(p)
4615#endif
4616 )
4617 had_sep = TRUE;
4618 else if (had_sep)
4619 {
4620 t = p;
4621 had_sep = FALSE;
4622 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004623 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625 return t;
4626}
4627
4628/*
4629 * Return TRUE if we only need to show the tail of completion matches.
4630 * When not completing file names or there is a wildcard in the path FALSE is
4631 * returned.
4632 */
4633 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 char_u *s;
4637 char_u *end;
4638
4639 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 if (xp->xp_context != EXPAND_FILES
4641 && xp->xp_context != EXPAND_SHELLCMD
4642 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FALSE;
4644
4645 end = gettail(xp->xp_pattern);
4646 if (end == xp->xp_pattern) /* there is no path separator */
4647 return FALSE;
4648
4649 for (s = xp->xp_pattern; s < end; s++)
4650 {
4651 /* Skip escaped wildcards. Only when the backslash is not a path
4652 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4653 if (rem_backslash(s))
4654 ++s;
4655 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4656 return FALSE;
4657 }
4658 return TRUE;
4659}
4660
4661/*
4662 * Prepare a string for expansion.
4663 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004664 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 * When expanding other names: The string will be used with regcomp(). Copy
4666 * the name into allocated memory and prepend "^".
4667 */
4668 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004669addstar(
4670 char_u *fname,
4671 int len,
4672 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
4674 char_u *retval;
4675 int i, j;
4676 int new_len;
4677 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004678 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004680 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004681 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004682 && context != EXPAND_SHELLCMD
4683 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
4685 /*
4686 * Matching will be done internally (on something other than files).
4687 * So we convert the file-matching-type wildcards into our kind for
4688 * use with vim_regcomp(). First work out how long it will be:
4689 */
4690
4691 /* For help tags the translation is done in find_help_tags().
4692 * For a tag pattern starting with "/" no translation is needed. */
4693 if (context == EXPAND_HELP
4694 || context == EXPAND_COLORS
4695 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004696 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004697 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004698 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004699 || ((context == EXPAND_TAGS_LISTFILES
4700 || context == EXPAND_TAGS)
4701 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 retval = vim_strnsave(fname, len);
4703 else
4704 {
4705 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4706 for (i = 0; i < len; i++)
4707 {
4708 if (fname[i] == '*' || fname[i] == '~')
4709 new_len++; /* '*' needs to be replaced by ".*"
4710 '~' needs to be replaced by "\~" */
4711
4712 /* Buffer names are like file names. "." should be literal */
4713 if (context == EXPAND_BUFFERS && fname[i] == '.')
4714 new_len++; /* "." becomes "\." */
4715
4716 /* Custom expansion takes care of special things, match
4717 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004718 if ((context == EXPAND_USER_DEFINED
4719 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 new_len++; /* '\' becomes "\\" */
4721 }
4722 retval = alloc(new_len);
4723 if (retval != NULL)
4724 {
4725 retval[0] = '^';
4726 j = 1;
4727 for (i = 0; i < len; i++, j++)
4728 {
4729 /* Skip backslash. But why? At least keep it for custom
4730 * expansion. */
4731 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004732 && context != EXPAND_USER_LIST
4733 && fname[i] == '\\'
4734 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 break;
4736
4737 switch (fname[i])
4738 {
4739 case '*': retval[j++] = '.';
4740 break;
4741 case '~': retval[j++] = '\\';
4742 break;
4743 case '?': retval[j] = '.';
4744 continue;
4745 case '.': if (context == EXPAND_BUFFERS)
4746 retval[j++] = '\\';
4747 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004748 case '\\': if (context == EXPAND_USER_DEFINED
4749 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 retval[j++] = '\\';
4751 break;
4752 }
4753 retval[j] = fname[i];
4754 }
4755 retval[j] = NUL;
4756 }
4757 }
4758 }
4759 else
4760 {
4761 retval = alloc(len + 4);
4762 if (retval != NULL)
4763 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004764 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004767 * Don't add a star to *, ~, ~user, $var or `cmd`.
4768 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 * ~ would be at the start of the file name, but not the tail.
4770 * $ could be anywhere in the tail.
4771 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004772 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
4774 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004775 ends_in_star = (len > 0 && retval[len - 1] == '*');
4776#ifndef BACKSLASH_IN_FILENAME
4777 for (i = len - 2; i >= 0; --i)
4778 {
4779 if (retval[i] != '\\')
4780 break;
4781 ends_in_star = !ends_in_star;
4782 }
4783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004785 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 && vim_strchr(tail, '$') == NULL
4787 && vim_strchr(retval, '`') == NULL)
4788 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004789 else if (len > 0 && retval[len - 1] == '$')
4790 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 retval[len] = NUL;
4792 }
4793 }
4794 return retval;
4795}
4796
4797/*
4798 * Must parse the command line so far to work out what context we are in.
4799 * Completion can then be done based on that context.
4800 * This routine sets the variables:
4801 * xp->xp_pattern The start of the pattern to be expanded within
4802 * the command line (ends at the cursor).
4803 * xp->xp_context The type of thing to expand. Will be one of:
4804 *
4805 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4806 * the command line, like an unknown command. Caller
4807 * should beep.
4808 * EXPAND_NOTHING Unrecognised context for completion, use char like
4809 * a normal char, rather than for completion. eg
4810 * :s/^I/
4811 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4812 * it.
4813 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4814 * EXPAND_FILES After command with XFILE set, or after setting
4815 * with P_EXPAND set. eg :e ^I, :w>>^I
4816 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4817 * when we know only directories are of interest. eg
4818 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004819 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4821 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4822 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4823 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4824 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4825 * EXPAND_EVENTS Complete event names
4826 * EXPAND_SYNTAX Complete :syntax command arguments
4827 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4828 * EXPAND_AUGROUP Complete autocommand group names
4829 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4830 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4831 * eg :unmap a^I , :cunab x^I
4832 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4833 * eg :call sub^I
4834 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4835 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4836 * names in expressions, eg :while s^I
4837 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004838 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
4840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004841set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004843 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (ccline.cmdfirstc != ':'
4845#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004846 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004847 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#endif
4849 )
4850 {
4851 xp->xp_context = EXPAND_NOTHING;
4852 return;
4853 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004854 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855}
4856
4857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004858set_cmd_context(
4859 expand_T *xp,
4860 char_u *str, /* start of command line */
4861 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004862 int col, /* position of cursor */
4863 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 int old_char = NUL;
4866 char_u *nextcomm;
4867
4868 /*
4869 * Avoid a UMR warning from Purify, only save the character if it has been
4870 * written before.
4871 */
4872 if (col < len)
4873 old_char = str[col];
4874 str[col] = NUL;
4875 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004876
4877#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004878 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004879 {
4880# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004881 /* pass CMD_SIZE because there is no real command */
4882 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004883# endif
4884 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004885 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004886 {
4887 xp->xp_context = ccline.xp_context;
4888 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004889# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004890 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004891# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004892 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004893 else
4894#endif
4895 while (nextcomm != NULL)
4896 nextcomm = set_one_cmd_context(xp, nextcomm);
4897
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004898 /* Store the string here so that call_user_expand_func() can get to them
4899 * easily. */
4900 xp->xp_line = str;
4901 xp->xp_col = col;
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 str[col] = old_char;
4904}
4905
4906/*
4907 * Expand the command line "str" from context "xp".
4908 * "xp" must have been set by set_cmd_context().
4909 * xp->xp_pattern points into "str", to where the text that is to be expanded
4910 * starts.
4911 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4912 * cursor.
4913 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4914 * key that triggered expansion literally.
4915 * Returns EXPAND_OK otherwise.
4916 */
4917 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004918expand_cmdline(
4919 expand_T *xp,
4920 char_u *str, /* start of command line */
4921 int col, /* position of cursor */
4922 int *matchcount, /* return: nr of matches */
4923 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924{
4925 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004926 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4929 {
4930 beep_flush();
4931 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4932 }
4933 if (xp->xp_context == EXPAND_NOTHING)
4934 {
4935 /* Caller can use the character as a normal char instead */
4936 return EXPAND_NOTHING;
4937 }
4938
4939 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004940 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4941 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (file_str == NULL)
4943 return EXPAND_UNSUCCESSFUL;
4944
Bram Moolenaar94950a92010-12-02 16:01:29 +01004945 if (p_wic)
4946 options += WILD_ICASE;
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004949 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 *matchcount = 0;
4952 *matches = NULL;
4953 }
4954 vim_free(file_str);
4955
4956 return EXPAND_OK;
4957}
4958
4959#ifdef FEAT_MULTI_LANG
4960/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004961 * Cleanup matches for help tags:
4962 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4963 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004965static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004968cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 int i, j;
4971 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004972 char_u buf[4];
4973 char_u *p = buf;
4974
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004975 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004976 {
4977 *p++ = '@';
4978 *p++ = p_hlg[0];
4979 *p++ = p_hlg[1];
4980 }
4981 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 for (i = 0; i < num_file; ++i)
4984 {
4985 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004986 if (len <= 0)
4987 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004988 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 /* Sorting on priority means the same item in another language may
4991 * be anywhere. Search all items for a match up to the "@en". */
4992 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004993 if (j != i && (int)STRLEN(file[j]) == len + 3
4994 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 break;
4996 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004997 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 file[i][len] = NUL;
4999 }
5000 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005001
5002 if (*buf != NUL)
5003 for (i = 0; i < num_file; ++i)
5004 {
5005 len = (int)STRLEN(file[i]) - 3;
5006 if (len <= 0)
5007 continue;
5008 if (STRCMP(file[i] + len, buf) == 0)
5009 {
5010 /* remove the default language */
5011 file[i][len] = NUL;
5012 }
5013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014}
5015#endif
5016
5017/*
5018 * Do the expansion based on xp->xp_context and "pat".
5019 */
5020 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005021ExpandFromContext(
5022 expand_T *xp,
5023 char_u *pat,
5024 int *num_file,
5025 char_u ***file,
5026 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028#ifdef FEAT_CMDL_COMPL
5029 regmatch_T regmatch;
5030#endif
5031 int ret;
5032 int flags;
5033
5034 flags = EW_DIR; /* include directories */
5035 if (options & WILD_LIST_NOTFOUND)
5036 flags |= EW_NOTFOUND;
5037 if (options & WILD_ADD_SLASH)
5038 flags |= EW_ADDSLASH;
5039 if (options & WILD_KEEP_ALL)
5040 flags |= EW_KEEPALL;
5041 if (options & WILD_SILENT)
5042 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005043 if (options & WILD_ALLLINKS)
5044 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005046 if (xp->xp_context == EXPAND_FILES
5047 || xp->xp_context == EXPAND_DIRECTORIES
5048 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
5050 /*
5051 * Expand file or directory names.
5052 */
5053 int free_pat = FALSE;
5054 int i;
5055
5056 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5057 * space */
5058 if (xp->xp_backslash != XP_BS_NONE)
5059 {
5060 free_pat = TRUE;
5061 pat = vim_strsave(pat);
5062 for (i = 0; pat[i]; ++i)
5063 if (pat[i] == '\\')
5064 {
5065 if (xp->xp_backslash == XP_BS_THREE
5066 && pat[i + 1] == '\\'
5067 && pat[i + 2] == '\\'
5068 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005069 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (xp->xp_backslash == XP_BS_ONE
5071 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005072 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 }
5075
5076 if (xp->xp_context == EXPAND_FILES)
5077 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005078 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5079 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 else
5081 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005082 if (options & WILD_ICASE)
5083 flags |= EW_ICASE;
5084
Bram Moolenaard7834d32009-12-02 16:14:36 +00005085 /* Expand wildcards, supporting %:h and the like. */
5086 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (free_pat)
5088 vim_free(pat);
5089 return ret;
5090 }
5091
5092 *file = (char_u **)"";
5093 *num_file = 0;
5094 if (xp->xp_context == EXPAND_HELP)
5095 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005096 /* With an empty argument we would get all the help tags, which is
5097 * very slow. Get matches for "help" instead. */
5098 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5099 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101#ifdef FEAT_MULTI_LANG
5102 cleanup_help_tags(*num_file, *file);
5103#endif
5104 return OK;
5105 }
5106 return FAIL;
5107 }
5108
5109#ifndef FEAT_CMDL_COMPL
5110 return FAIL;
5111#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005112 if (xp->xp_context == EXPAND_SHELLCMD)
5113 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (xp->xp_context == EXPAND_OLD_SETTING)
5115 return ExpandOldSetting(num_file, file);
5116 if (xp->xp_context == EXPAND_BUFFERS)
5117 return ExpandBufnames(pat, num_file, file, options);
5118 if (xp->xp_context == EXPAND_TAGS
5119 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5120 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5121 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005122 {
5123 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005124 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5125 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005128 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005129 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005130 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005131 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005132 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005133 {
5134 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005135 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005136 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005137 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005138 {
5139 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005140 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005141 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005142# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5143 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005144 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005145# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005146 if (xp->xp_context == EXPAND_PACKADD)
5147 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5150 if (regmatch.regprog == NULL)
5151 return FAIL;
5152
5153 /* set ignore-case according to p_ic, p_scs and pat */
5154 regmatch.rm_ic = ignorecase(pat);
5155
5156 if (xp->xp_context == EXPAND_SETTINGS
5157 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5158 ret = ExpandSettings(xp, &regmatch, num_file, file);
5159 else if (xp->xp_context == EXPAND_MAPPINGS)
5160 ret = ExpandMappings(&regmatch, num_file, file);
5161# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5162 else if (xp->xp_context == EXPAND_USER_DEFINED)
5163 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5164# endif
5165 else
5166 {
5167 static struct expgen
5168 {
5169 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005170 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005172 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 } tab[] =
5174 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005175 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5176 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005177 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005178 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005179#ifdef FEAT_CMDHIST
5180 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005183 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005184 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005185 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5186 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5187 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#endif
5189#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005190 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5191 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5192 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5193 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194#endif
5195#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005196 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5197 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#endif
5199#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005200 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005202#ifdef FEAT_PROFILE
5203 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5204#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005205 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005206 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5207 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005208#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005209 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005210#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005211#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005212 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005213#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005214#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005215 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5218 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005219 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5220 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005223 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005224 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 };
5226 int i;
5227
5228 /*
5229 * Find a context in the table and call the ExpandGeneric() with the
5230 * right function to do the expansion.
5231 */
5232 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005233 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 if (xp->xp_context == tab[i].context)
5235 {
5236 if (tab[i].ic)
5237 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005238 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005239 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 break;
5241 }
5242 }
5243
Bram Moolenaar473de612013-06-08 18:19:48 +02005244 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 return ret;
5247#endif /* FEAT_CMDL_COMPL */
5248}
5249
5250#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5251/*
5252 * Expand a list of names.
5253 *
5254 * Generic function for command line completion. It calls a function to
5255 * obtain strings, one by one. The strings are matched against a regexp
5256 * program. Matching strings are copied into an array, which is returned.
5257 *
5258 * Returns OK when no problems encountered, FAIL for error (out of memory).
5259 */
5260 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005261ExpandGeneric(
5262 expand_T *xp,
5263 regmatch_T *regmatch,
5264 int *num_file,
5265 char_u ***file,
5266 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005268 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
5270 int i;
5271 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005272 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 char_u *str;
5274
5275 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005276 * round == 0: count the number of matching names
5277 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005279 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
5281 for (i = 0; ; ++i)
5282 {
5283 str = (*func)(xp, i);
5284 if (str == NULL) /* end of list */
5285 break;
5286 if (*str == NUL) /* skip empty strings */
5287 continue;
5288
5289 if (vim_regexec(regmatch, str, (colnr_T)0))
5290 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005291 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005293 if (escaped)
5294 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5295 else
5296 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 (*file)[count] = str;
5298#ifdef FEAT_MENU
5299 if (func == get_menu_names && str != NULL)
5300 {
5301 /* test for separator added by get_menu_names() */
5302 str += STRLEN(str) - 1;
5303 if (*str == '\001')
5304 *str = '.';
5305 }
5306#endif
5307 }
5308 ++count;
5309 }
5310 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005311 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
5313 if (count == 0)
5314 return OK;
5315 *num_file = count;
5316 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5317 if (*file == NULL)
5318 {
5319 *file = (char_u **)"";
5320 return FAIL;
5321 }
5322 count = 0;
5323 }
5324 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005325
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005326 /* Sort the results. Keep menu's in the specified order. */
5327 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005328 {
5329 if (xp->xp_context == EXPAND_EXPRESSION
5330 || xp->xp_context == EXPAND_FUNCTIONS
5331 || xp->xp_context == EXPAND_USER_FUNC)
5332 /* <SNR> functions should be sorted to the end. */
5333 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5334 sort_func_compare);
5335 else
5336 sort_strings(*file, *num_file);
5337 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005338
Bram Moolenaar4f688582007-07-24 12:34:30 +00005339#ifdef FEAT_CMDL_COMPL
5340 /* Reset the variables used for special highlight names expansion, so that
5341 * they don't show up when getting normal highlight names by ID. */
5342 reset_expand_highlight();
5343#endif
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 return OK;
5346}
5347
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005348/*
5349 * Complete a shell command.
5350 * Returns FAIL or OK;
5351 */
5352 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005353expand_shellcmd(
5354 char_u *filepat, /* pattern to match with command names */
5355 int *num_file, /* return: number of matches */
5356 char_u ***file, /* return: array with matches */
5357 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005358{
5359 char_u *pat;
5360 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005361 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005362 int mustfree = FALSE;
5363 garray_T ga;
5364 char_u *buf = alloc(MAXPATHL);
5365 size_t l;
5366 char_u *s, *e;
5367 int flags = flagsarg;
5368 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005369 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005370 hashtab_T found_ht;
5371 hashitem_T *hi;
5372 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005373
5374 if (buf == NULL)
5375 return FAIL;
5376
5377 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5378 * space */
5379 pat = vim_strsave(filepat);
5380 for (i = 0; pat[i]; ++i)
5381 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005382 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005383
Bram Moolenaarb5971142015-03-21 17:32:19 +01005384 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005385
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005386 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5387 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005388 path = (char_u *)".";
5389 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005390 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005391 /* For an absolute name we don't use $PATH. */
5392 if (!mch_isFullName(pat))
5393 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005394 if (path == NULL)
5395 path = (char_u *)"";
5396 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005397
5398 /*
5399 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005400 * collect them in "ga". When "." is not in $PATH also expand for the
5401 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005402 */
5403 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005404 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005405 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005406 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005407#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005408 e = vim_strchr(s, ';');
5409#else
5410 e = vim_strchr(s, ':');
5411#endif
5412 if (e == NULL)
5413 e = s + STRLEN(s);
5414
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005415 if (*s == NUL)
5416 {
5417 if (did_curdir)
5418 break;
5419 // Find directories in the current directory, path is empty.
5420 did_curdir = TRUE;
5421 flags |= EW_DIR;
5422 }
5423 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5424 {
5425 did_curdir = TRUE;
5426 flags |= EW_DIR;
5427 }
5428 else
5429 // Do not match directories inside a $PATH item.
5430 flags &= ~EW_DIR;
5431
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005432 l = e - s;
5433 if (l > MAXPATHL - 5)
5434 break;
5435 vim_strncpy(buf, s, l);
5436 add_pathsep(buf);
5437 l = STRLEN(buf);
5438 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5439
5440 /* Expand matches in one directory of $PATH. */
5441 ret = expand_wildcards(1, &buf, num_file, file, flags);
5442 if (ret == OK)
5443 {
5444 if (ga_grow(&ga, *num_file) == FAIL)
5445 FreeWild(*num_file, *file);
5446 else
5447 {
5448 for (i = 0; i < *num_file; ++i)
5449 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005450 char_u *name = (*file)[i];
5451
5452 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005453 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005454 // Check if this name was already found.
5455 hash = hash_hash(name + l);
5456 hi = hash_lookup(&found_ht, name + l, hash);
5457 if (HASHITEM_EMPTY(hi))
5458 {
5459 // Remove the path that was prepended.
5460 STRMOVE(name, name + l);
5461 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5462 hash_add_item(&found_ht, hi, name, hash);
5463 name = NULL;
5464 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005465 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005466 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005467 }
5468 vim_free(*file);
5469 }
5470 }
5471 if (*e != NUL)
5472 ++e;
5473 }
5474 *file = ga.ga_data;
5475 *num_file = ga.ga_len;
5476
5477 vim_free(buf);
5478 vim_free(pat);
5479 if (mustfree)
5480 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005481 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005482 return OK;
5483}
5484
5485
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5487/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005488 * Call "user_expand_func()" to invoke a user defined Vim script function and
5489 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005491 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005492call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005493 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494 expand_T *xp,
5495 int *num_file,
5496 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005498 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005499 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005501 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005502 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005503 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
Bram Moolenaarb7515462013-06-29 12:58:33 +02005505 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005506 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 *num_file = 0;
5508 *file = NULL;
5509
Bram Moolenaarb7515462013-06-29 12:58:33 +02005510 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005511 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005512 keep = ccline.cmdbuff[ccline.cmdlen];
5513 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005514 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005515
Bram Moolenaarffa96842018-06-12 22:05:14 +02005516 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5517
5518 args[0].v_type = VAR_STRING;
5519 args[0].vval.v_string = pat;
5520 args[1].v_type = VAR_STRING;
5521 args[1].vval.v_string = xp->xp_line;
5522 args[2].v_type = VAR_NUMBER;
5523 args[2].vval.v_number = xp->xp_col;
5524 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005526 /* Save the cmdline, we don't know what the function may do. */
5527 save_ccline = ccline;
5528 ccline.cmdbuff = NULL;
5529 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005531
Bram Moolenaarded27a12018-08-01 19:06:03 +02005532 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005533
5534 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005536 if (ccline.cmdbuff != NULL)
5537 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005538
Bram Moolenaarffa96842018-06-12 22:05:14 +02005539 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005540 return ret;
5541}
5542
5543/*
5544 * Expand names with a function defined by the user.
5545 */
5546 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547ExpandUserDefined(
5548 expand_T *xp,
5549 regmatch_T *regmatch,
5550 int *num_file,
5551 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005552{
5553 char_u *retstr;
5554 char_u *s;
5555 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005556 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005557 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005558 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005559
5560 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5561 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return FAIL;
5563
5564 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005565 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
5567 e = vim_strchr(s, '\n');
5568 if (e == NULL)
5569 e = s + STRLEN(s);
5570 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005571 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005573 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5574 *e = keep;
5575
5576 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005578 if (ga_grow(&ga, 1) == FAIL)
5579 break;
5580 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5581 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (*e != NUL)
5585 ++e;
5586 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005587 vim_free(retstr);
5588 *file = ga.ga_data;
5589 *num_file = ga.ga_len;
5590 return OK;
5591}
5592
5593/*
5594 * Expand names with a list returned by a function defined by the user.
5595 */
5596 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005597ExpandUserList(
5598 expand_T *xp,
5599 int *num_file,
5600 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005601{
5602 list_T *retlist;
5603 listitem_T *li;
5604 garray_T ga;
5605
5606 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5607 if (retlist == NULL)
5608 return FAIL;
5609
5610 ga_init2(&ga, (int)sizeof(char *), 3);
5611 /* Loop over the items in the list. */
5612 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5613 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005614 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5615 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005616
5617 if (ga_grow(&ga, 1) == FAIL)
5618 break;
5619
5620 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005621 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005622 ++ga.ga_len;
5623 }
5624 list_unref(retlist);
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 *file = ga.ga_data;
5627 *num_file = ga.ga_len;
5628 return OK;
5629}
5630#endif
5631
5632/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005633 * Expand color scheme, compiler or filetype names.
5634 * Search from 'runtimepath':
5635 * 'runtimepath'/{dirnames}/{pat}.vim
5636 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5637 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5638 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5639 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005640 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 */
5642 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643ExpandRTDir(
5644 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005645 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646 int *num_file,
5647 char_u ***file,
5648 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 char_u *s;
5651 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005652 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005654 int i;
5655 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 *num_file = 0;
5658 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005659 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005660 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005662 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005664 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5665 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005667 ga_clear_strings(&ga);
5668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005670 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005671 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005672 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005674
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005675 if (flags & DIP_START) {
5676 for (i = 0; dirnames[i] != NULL; ++i)
5677 {
5678 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5679 if (s == NULL)
5680 {
5681 ga_clear_strings(&ga);
5682 return FAIL;
5683 }
5684 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5685 globpath(p_pp, s, &ga, 0);
5686 vim_free(s);
5687 }
5688 }
5689
5690 if (flags & DIP_OPT) {
5691 for (i = 0; dirnames[i] != NULL; ++i)
5692 {
5693 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5694 if (s == NULL)
5695 {
5696 ga_clear_strings(&ga);
5697 return FAIL;
5698 }
5699 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5700 globpath(p_pp, s, &ga, 0);
5701 vim_free(s);
5702 }
5703 }
5704
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005705 for (i = 0; i < ga.ga_len; ++i)
5706 {
5707 match = ((char_u **)ga.ga_data)[i];
5708 s = match;
5709 e = s + STRLEN(s);
5710 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5711 {
5712 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005713 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005714 if (s < match || vim_ispathsep(*s))
5715 break;
5716 ++s;
5717 *e = NUL;
5718 mch_memmove(match, s, e - s + 1);
5719 }
5720 }
5721
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005722 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005723 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005724
5725 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005726 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005727 remove_duplicates(&ga);
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 *file = ga.ga_data;
5730 *num_file = ga.ga_len;
5731 return OK;
5732}
5733
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005734/*
5735 * Expand loadplugin names:
5736 * 'packpath'/pack/ * /opt/{pat}
5737 */
5738 static int
5739ExpandPackAddDir(
5740 char_u *pat,
5741 int *num_file,
5742 char_u ***file)
5743{
5744 char_u *s;
5745 char_u *e;
5746 char_u *match;
5747 garray_T ga;
5748 int i;
5749 int pat_len;
5750
5751 *num_file = 0;
5752 *file = NULL;
5753 pat_len = (int)STRLEN(pat);
5754 ga_init2(&ga, (int)sizeof(char *), 10);
5755
5756 s = alloc((unsigned)(pat_len + 26));
5757 if (s == NULL)
5758 {
5759 ga_clear_strings(&ga);
5760 return FAIL;
5761 }
5762 sprintf((char *)s, "pack/*/opt/%s*", pat);
5763 globpath(p_pp, s, &ga, 0);
5764 vim_free(s);
5765
5766 for (i = 0; i < ga.ga_len; ++i)
5767 {
5768 match = ((char_u **)ga.ga_data)[i];
5769 s = gettail(match);
5770 e = s + STRLEN(s);
5771 mch_memmove(match, s, e - s + 1);
5772 }
5773
5774 if (ga.ga_len == 0)
5775 return FAIL;
5776
5777 /* Sort and remove duplicates which can happen when specifying multiple
5778 * directories in dirnames. */
5779 remove_duplicates(&ga);
5780
5781 *file = ga.ga_data;
5782 *num_file = ga.ga_len;
5783 return OK;
5784}
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#endif
5787
5788#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5789/*
5790 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005791 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794globpath(
5795 char_u *path,
5796 char_u *file,
5797 garray_T *ga,
5798 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 expand_T xpc;
5801 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 int num_p;
5804 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 buf = alloc(MAXPATHL);
5807 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005808 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005810 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 /* Loop over all entries in {path}. */
5814 while (*path != NUL)
5815 {
5816 /* Copy one item of the path to buf[] and concatenate the file name. */
5817 copy_option_part(&path, buf, MAXPATHL, ",");
5818 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5819 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005820# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005821 /* Using the platform's path separator (\) makes vim incorrectly
5822 * treat it as an escape character, use '/' instead. */
5823 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5824 STRCAT(buf, "/");
5825# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005829 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5830 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005832 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005834 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 for (i = 0; i < num_p; ++i)
5837 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005838 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005839 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005840 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 FreeWild(num_p, p);
5845 }
5846 }
5847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850}
5851
5852#endif
5853
5854#if defined(FEAT_CMDHIST) || defined(PROTO)
5855
5856/*********************************
5857 * Command line history stuff *
5858 *********************************/
5859
5860/*
5861 * Translate a history character to the associated type number.
5862 */
5863 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005864hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 if (c == ':')
5867 return HIST_CMD;
5868 if (c == '=')
5869 return HIST_EXPR;
5870 if (c == '@')
5871 return HIST_INPUT;
5872 if (c == '>')
5873 return HIST_DEBUG;
5874 return HIST_SEARCH; /* must be '?' or '/' */
5875}
5876
5877/*
5878 * Table of history names.
5879 * These names are used in :history and various hist...() functions.
5880 * It is sufficient to give the significant prefix of a history name.
5881 */
5882
5883static char *(history_names[]) =
5884{
5885 "cmd",
5886 "search",
5887 "expr",
5888 "input",
5889 "debug",
5890 NULL
5891};
5892
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005893#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5894/*
5895 * Function given to ExpandGeneric() to obtain the possible first
5896 * arguments of the ":history command.
5897 */
5898 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005899get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005900{
5901 static char_u compl[2] = { NUL, NUL };
5902 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005903 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005904 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5905
5906 if (idx < short_names_count)
5907 {
5908 compl[0] = (char_u)short_names[idx];
5909 return compl;
5910 }
5911 if (idx < short_names_count + history_name_count)
5912 return (char_u *)history_names[idx - short_names_count];
5913 if (idx == short_names_count + history_name_count)
5914 return (char_u *)"all";
5915 return NULL;
5916}
5917#endif
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919/*
5920 * init_history() - Initialize the command line history.
5921 * Also used to re-allocate the history when the size changes.
5922 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005923 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005924init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
5926 int newlen; /* new length of history table */
5927 histentry_T *temp;
5928 int i;
5929 int j;
5930 int type;
5931
5932 /*
5933 * If size of history table changed, reallocate it
5934 */
5935 newlen = (int)p_hi;
5936 if (newlen != hislen) /* history length changed */
5937 {
5938 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5939 {
5940 if (newlen)
5941 {
5942 temp = (histentry_T *)lalloc(
5943 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5944 if (temp == NULL) /* out of memory! */
5945 {
5946 if (type == 0) /* first one: just keep the old length */
5947 {
5948 newlen = hislen;
5949 break;
5950 }
5951 /* Already changed one table, now we can only have zero
5952 * length for all tables. */
5953 newlen = 0;
5954 type = -1;
5955 continue;
5956 }
5957 }
5958 else
5959 temp = NULL;
5960 if (newlen == 0 || temp != NULL)
5961 {
5962 if (hisidx[type] < 0) /* there are no entries yet */
5963 {
5964 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005965 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967 else if (newlen > hislen) /* array becomes bigger */
5968 {
5969 for (i = 0; i <= hisidx[type]; ++i)
5970 temp[i] = history[type][i];
5971 j = i;
5972 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005973 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 for ( ; j < hislen; ++i, ++j)
5975 temp[i] = history[type][j];
5976 }
5977 else /* array becomes smaller or 0 */
5978 {
5979 j = hisidx[type];
5980 for (i = newlen - 1; ; --i)
5981 {
5982 if (i >= 0) /* copy newest entries */
5983 temp[i] = history[type][j];
5984 else /* remove older entries */
5985 vim_free(history[type][j].hisstr);
5986 if (--j < 0)
5987 j = hislen - 1;
5988 if (j == hisidx[type])
5989 break;
5990 }
5991 hisidx[type] = newlen - 1;
5992 }
5993 vim_free(history[type]);
5994 history[type] = temp;
5995 }
5996 }
5997 hislen = newlen;
5998 }
5999}
6000
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006002clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006003{
6004 hisptr->hisnum = 0;
6005 hisptr->viminfo = FALSE;
6006 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006007 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006008}
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010/*
6011 * Check if command line 'str' is already in history.
6012 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6013 */
6014 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015in_history(
6016 int type,
6017 char_u *str,
6018 int move_to_front, /* Move the entry to the front if it exists */
6019 int sep,
6020 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 int i;
6023 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006024 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025
6026 if (hisidx[type] < 0)
6027 return FALSE;
6028 i = hisidx[type];
6029 do
6030 {
6031 if (history[type][i].hisstr == NULL)
6032 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006033
6034 /* For search history, check that the separator character matches as
6035 * well. */
6036 p = history[type][i].hisstr;
6037 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006038 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006039 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 if (!move_to_front)
6042 return TRUE;
6043 last_i = i;
6044 break;
6045 }
6046 if (--i < 0)
6047 i = hislen - 1;
6048 } while (i != hisidx[type]);
6049
6050 if (last_i >= 0)
6051 {
6052 str = history[type][i].hisstr;
6053 while (i != hisidx[type])
6054 {
6055 if (++i >= hislen)
6056 i = 0;
6057 history[type][last_i] = history[type][i];
6058 last_i = i;
6059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006061 history[type][i].viminfo = FALSE;
6062 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006063 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 return TRUE;
6065 }
6066 return FALSE;
6067}
6068
6069/*
6070 * Convert history name (from table above) to its HIST_ equivalent.
6071 * When "name" is empty, return "cmd" history.
6072 * Returns -1 for unknown history name.
6073 */
6074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006075get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 int i;
6078 int len = (int)STRLEN(name);
6079
6080 /* No argument: use current history. */
6081 if (len == 0)
6082 return hist_char2type(ccline.cmdfirstc);
6083
6084 for (i = 0; history_names[i] != NULL; ++i)
6085 if (STRNICMP(name, history_names[i], len) == 0)
6086 return i;
6087
6088 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6089 return hist_char2type(name[0]);
6090
6091 return -1;
6092}
6093
6094static int last_maptick = -1; /* last seen maptick */
6095
6096/*
6097 * Add the given string to the given history. If the string is already in the
6098 * history then it is moved to the front. "histype" may be one of he HIST_
6099 * values.
6100 */
6101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006102add_to_history(
6103 int histype,
6104 char_u *new_entry,
6105 int in_map, /* consider maptick when inside a mapping */
6106 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 histentry_T *hisptr;
6109 int len;
6110
6111 if (hislen == 0) /* no history */
6112 return;
6113
Bram Moolenaara939e432013-11-09 05:30:26 +01006114 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6115 return;
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 /*
6118 * Searches inside the same mapping overwrite each other, so that only
6119 * the last line is kept. Be careful not to remove a line that was moved
6120 * down, only lines that were added.
6121 */
6122 if (histype == HIST_SEARCH && in_map)
6123 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006124 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
6126 /* Current line is from the same mapping, remove it */
6127 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6128 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006129 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 --hisnum[histype];
6131 if (--hisidx[HIST_SEARCH] < 0)
6132 hisidx[HIST_SEARCH] = hislen - 1;
6133 }
6134 last_maptick = -1;
6135 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006136 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
6138 if (++hisidx[histype] == hislen)
6139 hisidx[histype] = 0;
6140 hisptr = &history[histype][hisidx[histype]];
6141 vim_free(hisptr->hisstr);
6142
6143 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006144 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6146 if (hisptr->hisstr != NULL)
6147 hisptr->hisstr[len + 1] = sep;
6148
6149 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006150 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006151 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (histype == HIST_SEARCH && in_map)
6153 last_maptick = maptick;
6154 }
6155}
6156
6157#if defined(FEAT_EVAL) || defined(PROTO)
6158
6159/*
6160 * Get identifier of newest history entry.
6161 * "histype" may be one of the HIST_ values.
6162 */
6163 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006164get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
6166 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6167 || hisidx[histype] < 0)
6168 return -1;
6169
6170 return history[histype][hisidx[histype]].hisnum;
6171}
6172
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 * Calculate history index from a number:
6175 * num > 0: seen as identifying number of a history entry
6176 * num < 0: relative position in history wrt newest entry
6177 * "histype" may be one of the HIST_ values.
6178 */
6179 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 int i;
6183 histentry_T *hist;
6184 int wrapped = FALSE;
6185
6186 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6187 || (i = hisidx[histype]) < 0 || num == 0)
6188 return -1;
6189
6190 hist = history[histype];
6191 if (num > 0)
6192 {
6193 while (hist[i].hisnum > num)
6194 if (--i < 0)
6195 {
6196 if (wrapped)
6197 break;
6198 i += hislen;
6199 wrapped = TRUE;
6200 }
6201 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6202 return i;
6203 }
6204 else if (-num <= hislen)
6205 {
6206 i += num + 1;
6207 if (i < 0)
6208 i += hislen;
6209 if (hist[i].hisstr != NULL)
6210 return i;
6211 }
6212 return -1;
6213}
6214
6215/*
6216 * Get a history entry by its index.
6217 * "histype" may be one of the HIST_ values.
6218 */
6219 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006220get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
6222 idx = calc_hist_idx(histype, idx);
6223 if (idx >= 0)
6224 return history[histype][idx].hisstr;
6225 else
6226 return (char_u *)"";
6227}
6228
6229/*
6230 * Clear all entries of a history.
6231 * "histype" may be one of the HIST_ values.
6232 */
6233 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006234clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 int i;
6237 histentry_T *hisptr;
6238
6239 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6240 {
6241 hisptr = history[histype];
6242 for (i = hislen; i--;)
6243 {
6244 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006245 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006246 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
6248 hisidx[histype] = -1; /* mark history as cleared */
6249 hisnum[histype] = 0; /* reset identifier counter */
6250 return OK;
6251 }
6252 return FAIL;
6253}
6254
6255/*
6256 * Remove all entries matching {str} from a history.
6257 * "histype" may be one of the HIST_ values.
6258 */
6259 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 regmatch_T regmatch;
6263 histentry_T *hisptr;
6264 int idx;
6265 int i;
6266 int last;
6267 int found = FALSE;
6268
6269 regmatch.regprog = NULL;
6270 regmatch.rm_ic = FALSE; /* always match case */
6271 if (hislen != 0
6272 && histype >= 0
6273 && histype < HIST_COUNT
6274 && *str != NUL
6275 && (idx = hisidx[histype]) >= 0
6276 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6277 != NULL)
6278 {
6279 i = last = idx;
6280 do
6281 {
6282 hisptr = &history[histype][i];
6283 if (hisptr->hisstr == NULL)
6284 break;
6285 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6286 {
6287 found = TRUE;
6288 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006289 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 }
6291 else
6292 {
6293 if (i != last)
6294 {
6295 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006296 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298 if (--last < 0)
6299 last += hislen;
6300 }
6301 if (--i < 0)
6302 i += hislen;
6303 } while (i != idx);
6304 if (history[histype][idx].hisstr == NULL)
6305 hisidx[histype] = -1;
6306 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006307 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 return found;
6309}
6310
6311/*
6312 * Remove an indexed entry from a history.
6313 * "histype" may be one of the HIST_ values.
6314 */
6315 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006316del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 int i, j;
6319
6320 i = calc_hist_idx(histype, idx);
6321 if (i < 0)
6322 return FALSE;
6323 idx = hisidx[histype];
6324 vim_free(history[histype][i].hisstr);
6325
6326 /* When deleting the last added search string in a mapping, reset
6327 * last_maptick, so that the last added search string isn't deleted again.
6328 */
6329 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6330 last_maptick = -1;
6331
6332 while (i != idx)
6333 {
6334 j = (i + 1) % hislen;
6335 history[histype][i] = history[histype][j];
6336 i = j;
6337 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006338 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (--i < 0)
6340 i += hislen;
6341 hisidx[histype] = i;
6342 return TRUE;
6343}
6344
6345#endif /* FEAT_EVAL */
6346
6347#if defined(FEAT_CRYPT) || defined(PROTO)
6348/*
6349 * Very specific function to remove the value in ":set key=val" from the
6350 * history.
6351 */
6352 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006353remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
6355 char_u *p;
6356 int i;
6357
6358 i = hisidx[HIST_CMD];
6359 if (i < 0)
6360 return;
6361 p = history[HIST_CMD][i].hisstr;
6362 if (p != NULL)
6363 for ( ; *p; ++p)
6364 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6365 {
6366 p = vim_strchr(p + 3, '=');
6367 if (p == NULL)
6368 break;
6369 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006370 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (p[i] == '\\' && p[i + 1])
6372 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006373 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 --p;
6375 }
6376}
6377#endif
6378
6379#endif /* FEAT_CMDHIST */
6380
Bram Moolenaar064154c2016-03-19 22:50:43 +01006381#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006382/*
6383 * Get pointer to the command line info to use. cmdline_paste() may clear
6384 * ccline and put the previous value in prev_ccline.
6385 */
6386 static struct cmdline_info *
6387get_ccline_ptr(void)
6388{
6389 if ((State & CMDLINE) == 0)
6390 return NULL;
6391 if (ccline.cmdbuff != NULL)
6392 return &ccline;
6393 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6394 return &prev_ccline;
6395 return NULL;
6396}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006397#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006398
Bram Moolenaar064154c2016-03-19 22:50:43 +01006399#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006400/*
6401 * Get the current command line in allocated memory.
6402 * Only works when the command line is being edited.
6403 * Returns NULL when something is wrong.
6404 */
6405 char_u *
6406get_cmdline_str(void)
6407{
6408 struct cmdline_info *p = get_ccline_ptr();
6409
6410 if (p == NULL)
6411 return NULL;
6412 return vim_strnsave(p->cmdbuff, p->cmdlen);
6413}
6414
6415/*
6416 * Get the current command line position, counted in bytes.
6417 * Zero is the first position.
6418 * Only works when the command line is being edited.
6419 * Returns -1 when something is wrong.
6420 */
6421 int
6422get_cmdline_pos(void)
6423{
6424 struct cmdline_info *p = get_ccline_ptr();
6425
6426 if (p == NULL)
6427 return -1;
6428 return p->cmdpos;
6429}
6430
6431/*
6432 * Set the command line byte position to "pos". Zero is the first position.
6433 * Only works when the command line is being edited.
6434 * Returns 1 when failed, 0 when OK.
6435 */
6436 int
6437set_cmdline_pos(
6438 int pos)
6439{
6440 struct cmdline_info *p = get_ccline_ptr();
6441
6442 if (p == NULL)
6443 return 1;
6444
6445 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6446 * changed the command line. */
6447 if (pos < 0)
6448 new_cmdpos = 0;
6449 else
6450 new_cmdpos = pos;
6451 return 0;
6452}
6453#endif
6454
6455#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6456/*
6457 * Get the current command-line type.
6458 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6459 * Only works when the command line is being edited.
6460 * Returns NUL when something is wrong.
6461 */
6462 int
6463get_cmdline_type(void)
6464{
6465 struct cmdline_info *p = get_ccline_ptr();
6466
6467 if (p == NULL)
6468 return NUL;
6469 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006470 return
6471# ifdef FEAT_EVAL
6472 (p->input_fn) ? '@' :
6473# endif
6474 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006475 return p->cmdfirstc;
6476}
6477#endif
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6480/*
6481 * Get indices "num1,num2" that specify a range within a list (not a range of
6482 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6483 * Returns OK if parsed successfully, otherwise FAIL.
6484 */
6485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006486get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 int len;
6489 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006490 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492 *str = skipwhite(*str);
6493 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6494 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006495 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 *str += len;
6497 *num1 = (int)num;
6498 first = TRUE;
6499 }
6500 *str = skipwhite(*str);
6501 if (**str == ',') /* parse "to" part of range */
6502 {
6503 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006504 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (len > 0)
6506 {
6507 *num2 = (int)num;
6508 *str = skipwhite(*str + len);
6509 }
6510 else if (!first) /* no number given at all */
6511 return FAIL;
6512 }
6513 else if (first) /* only one number given */
6514 *num2 = *num1;
6515 return OK;
6516}
6517#endif
6518
6519#if defined(FEAT_CMDHIST) || defined(PROTO)
6520/*
6521 * :history command - print a history
6522 */
6523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006524ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 histentry_T *hist;
6527 int histype1 = HIST_CMD;
6528 int histype2 = HIST_CMD;
6529 int hisidx1 = 1;
6530 int hisidx2 = -1;
6531 int idx;
6532 int i, j, k;
6533 char_u *end;
6534 char_u *arg = eap->arg;
6535
6536 if (hislen == 0)
6537 {
6538 MSG(_("'history' option is zero"));
6539 return;
6540 }
6541
6542 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6543 {
6544 end = arg;
6545 while (ASCII_ISALPHA(*end)
6546 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6547 end++;
6548 i = *end;
6549 *end = NUL;
6550 histype1 = get_histtype(arg);
6551 if (histype1 == -1)
6552 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006553 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
6555 histype1 = 0;
6556 histype2 = HIST_COUNT-1;
6557 }
6558 else
6559 {
6560 *end = i;
6561 EMSG(_(e_trailing));
6562 return;
6563 }
6564 }
6565 else
6566 histype2 = histype1;
6567 *end = i;
6568 }
6569 else
6570 end = arg;
6571 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6572 {
6573 EMSG(_(e_trailing));
6574 return;
6575 }
6576
6577 for (; !got_int && histype1 <= histype2; ++histype1)
6578 {
6579 STRCPY(IObuff, "\n # ");
6580 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6581 MSG_PUTS_TITLE(IObuff);
6582 idx = hisidx[histype1];
6583 hist = history[histype1];
6584 j = hisidx1;
6585 k = hisidx2;
6586 if (j < 0)
6587 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6588 if (k < 0)
6589 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6590 if (idx >= 0 && j <= k)
6591 for (i = idx + 1; !got_int; ++i)
6592 {
6593 if (i == hislen)
6594 i = 0;
6595 if (hist[i].hisstr != NULL
6596 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6597 {
6598 msg_putchar('\n');
6599 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6600 hist[i].hisnum);
6601 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6602 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006603 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 else
6605 STRCAT(IObuff, hist[i].hisstr);
6606 msg_outtrans(IObuff);
6607 out_flush();
6608 }
6609 if (i == idx)
6610 break;
6611 }
6612 }
6613}
6614#endif
6615
6616#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006617/*
6618 * Buffers for history read from a viminfo file. Only valid while reading.
6619 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006620static histentry_T *viminfo_history[HIST_COUNT] =
6621 {NULL, NULL, NULL, NULL, NULL};
6622static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6623static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624static int viminfo_add_at_front = FALSE;
6625
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006626static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
6628/*
6629 * Translate a history type number to the associated character.
6630 */
6631 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006632hist_type2char(
6633 int type,
6634 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 if (type == HIST_CMD)
6637 return ':';
6638 if (type == HIST_SEARCH)
6639 {
6640 if (use_question)
6641 return '?';
6642 else
6643 return '/';
6644 }
6645 if (type == HIST_EXPR)
6646 return '=';
6647 return '@';
6648}
6649
6650/*
6651 * Prepare for reading the history from the viminfo file.
6652 * This allocates history arrays to store the read history lines.
6653 */
6654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006655prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 int i;
6658 int num;
6659 int type;
6660 int len;
6661
6662 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006663 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 if (asklen > hislen)
6665 asklen = hislen;
6666
6667 for (type = 0; type < HIST_COUNT; ++type)
6668 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006669 /* Count the number of empty spaces in the history list. Entries read
6670 * from viminfo previously are also considered empty. If there are
6671 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006673 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 num++;
6675 len = asklen;
6676 if (num > len)
6677 len = num;
6678 if (len <= 0)
6679 viminfo_history[type] = NULL;
6680 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006681 viminfo_history[type] = (histentry_T *)lalloc(
6682 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (viminfo_history[type] == NULL)
6684 len = 0;
6685 viminfo_hislen[type] = len;
6686 viminfo_hisidx[type] = 0;
6687 }
6688}
6689
6690/*
6691 * Accept a line from the viminfo, store it in the history array when it's
6692 * new.
6693 */
6694 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006695read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 int type;
6698 long_u len;
6699 char_u *val;
6700 char_u *p;
6701
6702 type = hist_char2type(virp->vir_line[0]);
6703 if (viminfo_hisidx[type] < viminfo_hislen[type])
6704 {
6705 val = viminfo_readstring(virp, 1, TRUE);
6706 if (val != NULL && *val != NUL)
6707 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006708 int sep = (*val == ' ' ? NUL : *val);
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006711 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {
6713 /* Need to re-allocate to append the separator byte. */
6714 len = STRLEN(val);
6715 p = lalloc(len + 2, TRUE);
6716 if (p != NULL)
6717 {
6718 if (type == HIST_SEARCH)
6719 {
6720 /* Search entry: Move the separator from the first
6721 * column to after the NUL. */
6722 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006723 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
6725 else
6726 {
6727 /* Not a search entry: No separator in the viminfo
6728 * file, add a NUL separator. */
6729 mch_memmove(p, val, (size_t)len + 1);
6730 p[len + 1] = NUL;
6731 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006732 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6733 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006734 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6735 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006736 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
6738 }
6739 }
6740 vim_free(val);
6741 }
6742 return viminfo_readline(virp);
6743}
6744
Bram Moolenaar07219f92013-04-14 23:19:36 +02006745/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006746 * Accept a new style history line from the viminfo, store it in the history
6747 * array when it's new.
6748 */
6749 void
6750handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006751 garray_T *values,
6752 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006753{
6754 int type;
6755 long_u len;
6756 char_u *val;
6757 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006758 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006759
6760 /* Check the format:
6761 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006762 if (values->ga_len < 4
6763 || vp[0].bv_type != BVAL_NR
6764 || vp[1].bv_type != BVAL_NR
6765 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6766 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006767 return;
6768
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006769 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006770 if (type >= HIST_COUNT)
6771 return;
6772 if (viminfo_hisidx[type] < viminfo_hislen[type])
6773 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006774 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006775 if (val != NULL && *val != NUL)
6776 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006777 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6778 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006779 int idx;
6780 int overwrite = FALSE;
6781
6782 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6783 {
6784 /* If lines were written by an older Vim we need to avoid
6785 * getting duplicates. See if the entry already exists. */
6786 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6787 {
6788 p = viminfo_history[type][idx].hisstr;
6789 if (STRCMP(val, p) == 0
6790 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6791 {
6792 overwrite = TRUE;
6793 break;
6794 }
6795 }
6796
6797 if (!overwrite)
6798 {
6799 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006800 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006801 p = lalloc(len + 2, TRUE);
6802 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006803 else
6804 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006805 if (p != NULL)
6806 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006807 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808 if (!overwrite)
6809 {
6810 mch_memmove(p, val, (size_t)len + 1);
6811 /* Put the separator after the NUL. */
6812 p[len + 1] = sep;
6813 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006814 viminfo_history[type][idx].hisnum = 0;
6815 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006816 viminfo_hisidx[type]++;
6817 }
6818 }
6819 }
6820 }
6821 }
6822}
6823
6824/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006825 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006826 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006827 static void
6828concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 int idx;
6831 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006832
6833 idx = hisidx[type] + viminfo_hisidx[type];
6834 if (idx >= hislen)
6835 idx -= hislen;
6836 else if (idx < 0)
6837 idx = hislen - 1;
6838 if (viminfo_add_at_front)
6839 hisidx[type] = idx;
6840 else
6841 {
6842 if (hisidx[type] == -1)
6843 hisidx[type] = hislen - 1;
6844 do
6845 {
6846 if (history[type][idx].hisstr != NULL
6847 || history[type][idx].viminfo)
6848 break;
6849 if (++idx == hislen)
6850 idx = 0;
6851 } while (idx != hisidx[type]);
6852 if (idx != hisidx[type] && --idx < 0)
6853 idx = hislen - 1;
6854 }
6855 for (i = 0; i < viminfo_hisidx[type]; i++)
6856 {
6857 vim_free(history[type][idx].hisstr);
6858 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6859 history[type][idx].viminfo = TRUE;
6860 history[type][idx].time_set = viminfo_history[type][i].time_set;
6861 if (--idx < 0)
6862 idx = hislen - 1;
6863 }
6864 idx += 1;
6865 idx %= hislen;
6866 for (i = 0; i < viminfo_hisidx[type]; i++)
6867 {
6868 history[type][idx++].hisnum = ++hisnum[type];
6869 idx %= hislen;
6870 }
6871}
6872
6873#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6874 static int
6875#ifdef __BORLANDC__
6876_RTLENTRYF
6877#endif
6878sort_hist(const void *s1, const void *s2)
6879{
6880 histentry_T *p1 = *(histentry_T **)s1;
6881 histentry_T *p2 = *(histentry_T **)s2;
6882
6883 if (p1->time_set < p2->time_set) return -1;
6884 if (p1->time_set > p2->time_set) return 1;
6885 return 0;
6886}
6887#endif
6888
6889/*
6890 * Merge history lines from viminfo and lines typed in this Vim based on the
6891 * timestamp;
6892 */
6893 static void
6894merge_history(int type)
6895{
6896 int max_len;
6897 histentry_T **tot_hist;
6898 histentry_T *new_hist;
6899 int i;
6900 int len;
6901
6902 /* Make one long list with all entries. */
6903 max_len = hislen + viminfo_hisidx[type];
6904 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6905 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6906 if (tot_hist == NULL || new_hist == NULL)
6907 {
6908 vim_free(tot_hist);
6909 vim_free(new_hist);
6910 return;
6911 }
6912 for (i = 0; i < viminfo_hisidx[type]; i++)
6913 tot_hist[i] = &viminfo_history[type][i];
6914 len = i;
6915 for (i = 0; i < hislen; i++)
6916 if (history[type][i].hisstr != NULL)
6917 tot_hist[len++] = &history[type][i];
6918
6919 /* Sort the list on timestamp. */
6920 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6921
6922 /* Keep the newest ones. */
6923 for (i = 0; i < hislen; i++)
6924 {
6925 if (i < len)
6926 {
6927 new_hist[i] = *tot_hist[i];
6928 tot_hist[i]->hisstr = NULL;
6929 if (new_hist[i].hisnum == 0)
6930 new_hist[i].hisnum = ++hisnum[type];
6931 }
6932 else
6933 clear_hist_entry(&new_hist[i]);
6934 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006935 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006936
6937 /* Free what is not kept. */
6938 for (i = 0; i < viminfo_hisidx[type]; i++)
6939 vim_free(viminfo_history[type][i].hisstr);
6940 for (i = 0; i < hislen; i++)
6941 vim_free(history[type][i].hisstr);
6942 vim_free(history[type]);
6943 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006944 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006945}
6946
6947/*
6948 * Finish reading history lines from viminfo. Not used when writing viminfo.
6949 */
6950 void
6951finish_viminfo_history(vir_T *virp)
6952{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006954 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 for (type = 0; type < HIST_COUNT; ++type)
6957 {
6958 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006959 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006960
6961 if (merge)
6962 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006964 concat_history(type);
6965
Bram Moolenaard23a8232018-02-10 18:45:26 +01006966 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006967 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969}
6970
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006971/*
6972 * Write history to viminfo file in "fp".
6973 * When "merge" is TRUE merge history lines with a previously read viminfo
6974 * file, data is in viminfo_history[].
6975 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006978write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980 int i;
6981 int type;
6982 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006983 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 init_history();
6986 if (hislen == 0)
6987 return;
6988 for (type = 0; type < HIST_COUNT; ++type)
6989 {
6990 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6991 if (num_saved == 0)
6992 continue;
6993 if (num_saved < 0) /* Use default */
6994 num_saved = hislen;
6995 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6996 type == HIST_CMD ? _("Command Line") :
6997 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006998 type == HIST_EXPR ? _("Expression") :
6999 type == HIST_INPUT ? _("Input Line") :
7000 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (num_saved > hislen)
7002 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007003
7004 /*
7005 * Merge typed and viminfo history:
7006 * round 1: history of typed commands.
7007 * round 2: history from recently read viminfo.
7008 */
7009 for (round = 1; round <= 2; ++round)
7010 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007011 if (round == 1)
7012 /* start at newest entry, somewhere in the list */
7013 i = hisidx[type];
7014 else if (viminfo_hisidx[type] > 0)
7015 /* start at newest entry, first in the list */
7016 i = 0;
7017 else
7018 /* empty list */
7019 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007020 if (i >= 0)
7021 while (num_saved > 0
7022 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007024 char_u *p;
7025 time_t timestamp;
7026 int c = NUL;
7027
7028 if (round == 1)
7029 {
7030 p = history[type][i].hisstr;
7031 timestamp = history[type][i].time_set;
7032 }
7033 else
7034 {
7035 p = viminfo_history[type] == NULL ? NULL
7036 : viminfo_history[type][i].hisstr;
7037 timestamp = viminfo_history[type] == NULL ? 0
7038 : viminfo_history[type][i].time_set;
7039 }
7040
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007041 if (p != NULL && (round == 2
7042 || !merge
7043 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007045 --num_saved;
7046 fputc(hist_type2char(type, TRUE), fp);
7047 /* For the search history: put the separator in the
7048 * second column; use a space if there isn't one. */
7049 if (type == HIST_SEARCH)
7050 {
7051 c = p[STRLEN(p) + 1];
7052 putc(c == NUL ? ' ' : c, fp);
7053 }
7054 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007055
7056 {
7057 char cbuf[NUMBUFLEN];
7058
7059 /* New style history with a bar line. Format:
7060 * |{bartype},{histtype},{timestamp},{separator},"text" */
7061 if (c == NUL)
7062 cbuf[0] = NUL;
7063 else
7064 sprintf(cbuf, "%d", c);
7065 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7066 type, (long)timestamp, cbuf);
7067 barline_writestring(fp, p, LSIZE - 20);
7068 putc('\n', fp);
7069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007071 if (round == 1)
7072 {
7073 /* Decrement index, loop around and stop when back at
7074 * the start. */
7075 if (--i < 0)
7076 i = hislen - 1;
7077 if (i == hisidx[type])
7078 break;
7079 }
7080 else
7081 {
7082 /* Increment index. Stop at the end in the while. */
7083 ++i;
7084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007086 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007087 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007088 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007089 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007090 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007091 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093}
7094#endif /* FEAT_VIMINFO */
7095
7096#if defined(FEAT_FKMAP) || defined(PROTO)
7097/*
7098 * Write a character at the current cursor+offset position.
7099 * It is directly written into the command buffer block.
7100 */
7101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007102cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7105 {
7106 EMSG(_("E198: cmd_pchar beyond the command length"));
7107 return;
7108 }
7109 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7110 ccline.cmdbuff[ccline.cmdlen] = NUL;
7111}
7112
7113 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007114cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7117 {
7118 /* EMSG(_("cmd_gchar beyond the command length")); */
7119 return NUL;
7120 }
7121 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7122}
7123#endif
7124
7125#if defined(FEAT_CMDWIN) || defined(PROTO)
7126/*
7127 * Open a window on the current command line and history. Allow editing in
7128 * the window. Returns when the window is closed.
7129 * Returns:
7130 * CR if the command is to be executed
7131 * Ctrl_C if it is to be abandoned
7132 * K_IGNORE if editing continues
7133 */
7134 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007135open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
7137 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007138 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007140 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 win_T *wp;
7142 int i;
7143 linenr_T lnum;
7144 int histtype;
7145 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 int save_restart_edit = restart_edit;
7147 int save_State = State;
7148 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007149#ifdef FEAT_RIGHTLEFT
7150 int save_cmdmsg_rl = cmdmsg_rl;
7151#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007152#ifdef FEAT_FOLDING
7153 int save_KeyTyped;
7154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 /* Can't do this recursively. Can't do it when typing a password. */
7157 if (cmdwin_type != 0
7158# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7159 || cmdline_star > 0
7160# endif
7161 )
7162 {
7163 beep_flush();
7164 return K_IGNORE;
7165 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007166 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168 /* Save current window sizes. */
7169 win_size_save(&winsizes);
7170
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007172 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007173
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007174 /* don't use a new tab page */
7175 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007176 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 /* Create a window for the command-line buffer. */
7179 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7180 {
7181 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007182 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 return K_IGNORE;
7184 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007185 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
7187 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007188 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007189 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007192#ifdef FEAT_FOLDING
7193 curwin->w_p_fen = FALSE;
7194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007196 curwin->w_p_rl = cmdmsg_rl;
7197 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007199 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007202 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007203 /* But don't allow switching to another buffer. */
7204 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
Bram Moolenaar46152342005-09-07 21:18:43 +00007206 /* Showing the prompt may have set need_wait_return, reset it. */
7207 need_wait_return = FALSE;
7208
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007209 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7211 {
7212 if (p_wc == TAB)
7213 {
7214 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7215 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7216 }
7217 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7218 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007219 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007221 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7222 * sets 'textwidth' to 78). */
7223 curbuf->b_p_tw = 0;
7224
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 /* Fill the buffer with the history. */
7226 init_history();
7227 if (hislen > 0)
7228 {
7229 i = hisidx[histtype];
7230 if (i >= 0)
7231 {
7232 lnum = 0;
7233 do
7234 {
7235 if (++i == hislen)
7236 i = 0;
7237 if (history[histtype][i].hisstr != NULL)
7238 ml_append(lnum++, history[histtype][i].hisstr,
7239 (colnr_T)0, FALSE);
7240 }
7241 while (i != hisidx[histtype]);
7242 }
7243 }
7244
7245 /* Replace the empty last line with the current command-line and put the
7246 * cursor there. */
7247 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7248 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7249 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007250 changed_line_abv_curs();
7251 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007252 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
7254 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007255 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
7257 /* No Ex mode here! */
7258 exmode_active = 0;
7259
7260 State = NORMAL;
7261# ifdef FEAT_MOUSE
7262 setmouse();
7263# endif
7264
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007266 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007267 if (restart_edit != 0) /* autocmd with ":startinsert" */
7268 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 i = RedrawingDisabled;
7271 RedrawingDisabled = 0;
7272
7273 /*
7274 * Call the main loop until <CR> or CTRL-C is typed.
7275 */
7276 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007277 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
7279 RedrawingDisabled = i;
7280
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007281# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007282 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007283# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007286 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007287
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007288# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007289 /* Restore KeyTyped in case it is modified by autocommands */
7290 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291# endif
7292
Bram Moolenaarccc18222007-05-10 18:25:20 +00007293 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007294 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 cmdwin_type = 0;
7296
7297 exmode_active = save_exmode;
7298
Bram Moolenaarf9821062008-06-20 16:31:07 +00007299 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007301 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 cmdwin_result = Ctrl_C;
7304 EMSG(_("E199: Active window or buffer deleted"));
7305 }
7306 else
7307 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007308# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 /* autocmds may abort script processing */
7310 if (aborting() && cmdwin_result != K_IGNORE)
7311 cmdwin_result = Ctrl_C;
7312# endif
7313 /* Set the new command line from the cmdline buffer. */
7314 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007315 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007317 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7318
7319 if (histtype == HIST_CMD)
7320 {
7321 /* Execute the command directly. */
7322 ccline.cmdbuff = vim_strsave((char_u *)p);
7323 cmdwin_result = CAR;
7324 }
7325 else
7326 {
7327 /* First need to cancel what we were doing. */
7328 ccline.cmdbuff = NULL;
7329 stuffcharReadbuff(':');
7330 stuffReadbuff((char_u *)p);
7331 stuffcharReadbuff(CAR);
7332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 }
7334 else if (cmdwin_result == K_XF2) /* :qa typed */
7335 {
7336 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7337 cmdwin_result = CAR;
7338 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007339 else if (cmdwin_result == Ctrl_C)
7340 {
7341 /* :q or :close, don't execute any command
7342 * and don't modify the cmd window. */
7343 ccline.cmdbuff = NULL;
7344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 else
7346 ccline.cmdbuff = vim_strsave(ml_get_curline());
7347 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007348 {
7349 ccline.cmdbuff = vim_strsave((char_u *)"");
7350 ccline.cmdlen = 0;
7351 ccline.cmdbufflen = 1;
7352 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 else
7356 {
7357 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7358 ccline.cmdbufflen = ccline.cmdlen + 1;
7359 ccline.cmdpos = curwin->w_cursor.col;
7360 if (ccline.cmdpos > ccline.cmdlen)
7361 ccline.cmdpos = ccline.cmdlen;
7362 if (cmdwin_result == K_IGNORE)
7363 {
7364 set_cmdspos_cursor();
7365 redrawcmd();
7366 }
7367 }
7368
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007370 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007371# ifdef FEAT_CONCEAL
7372 /* Avoid command-line window first character being concealed. */
7373 curwin->w_p_cole = 0;
7374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007376 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 win_goto(old_curwin);
7378 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007379
7380 /* win_close() may have already wiped the buffer when 'bh' is
7381 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007382 if (bufref_valid(&bufref))
7383 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 /* Restore window sizes. */
7386 win_size_restore(&winsizes);
7387
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007388 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
7390
7391 ga_clear(&winsizes);
7392 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007393# ifdef FEAT_RIGHTLEFT
7394 cmdmsg_rl = save_cmdmsg_rl;
7395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
7397 State = save_State;
7398# ifdef FEAT_MOUSE
7399 setmouse();
7400# endif
7401
7402 return cmdwin_result;
7403}
7404#endif /* FEAT_CMDWIN */
7405
7406/*
7407 * Used for commands that either take a simple command string argument, or:
7408 * cmd << endmarker
7409 * {script}
7410 * endmarker
7411 * Returns a pointer to allocated memory with {script} or NULL.
7412 */
7413 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007414script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 char_u *theline;
7417 char *end_pattern = NULL;
7418 char dot[] = ".";
7419 garray_T ga;
7420
7421 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7422 return NULL;
7423
7424 ga_init2(&ga, 1, 0x400);
7425
7426 if (cmd[2] != NUL)
7427 end_pattern = (char *)skipwhite(cmd + 2);
7428 else
7429 end_pattern = dot;
7430
7431 for (;;)
7432 {
7433 theline = eap->getline(
7434#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007435 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436#endif
7437 NUL, eap->cookie, 0);
7438
7439 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007440 {
7441 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
7445 ga_concat(&ga, theline);
7446 ga_append(&ga, '\n');
7447 vim_free(theline);
7448 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007449 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451 return (char_u *)ga.ga_data;
7452}