blob: fb16743f0c01fe18163e795dcc651680986740c7 [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 Moolenaardef7b1d2018-08-13 22:54:35 +0200301 // Check for "global!/".
302 if (*cmd == 'g' && *p == '!')
303 {
304 p++;
305 if (*skipwhite(p) == NUL)
306 return FALSE;
307 }
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200308 p = skipwhite(p);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200309 delim = *p++;
310 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200311 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200312 {
313 char_u *dummy;
314 exarg_T ea;
315 pos_T save_cursor = curwin->w_cursor;
316
317 // found a non-empty pattern
318 *skiplen = (int)(p - ccline.cmdbuff);
319 *patlen = (int)(end - p);
320
321 // parse the address range
322 vim_memset(&ea, 0, sizeof(ea));
323 ea.line1 = 1;
324 ea.line2 = 1;
325 ea.cmd = ccline.cmdbuff;
326 ea.addr_type = ADDR_LINES;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200327 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200328 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200329 if (ea.addr_count > 0)
330 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200331 // Allow for reverse match.
332 if (ea.line2 < ea.line1)
333 {
334 search_first_line = ea.line2;
335 search_last_line = ea.line1;
336 }
337 else
338 {
339 search_first_line = ea.line1;
340 search_last_line = ea.line2;
341 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200342 }
343 else if (*cmd == 's')
344 {
345 // :s defaults to the current line
346 search_first_line = curwin->w_cursor.lnum;
347 search_last_line = curwin->w_cursor.lnum;
348 }
349
350 curwin->w_cursor = save_cursor;
351 return TRUE;
352 }
353 }
354 }
355 }
356 }
357
358 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200359}
360
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200361 static void
362finish_incsearch_highlighting(
363 int gotesc,
364 incsearch_state_T *is_state,
365 int call_update_screen)
366{
367 if (is_state->did_incsearch)
368 {
369 is_state->did_incsearch = FALSE;
370 if (gotesc)
371 curwin->w_cursor = is_state->save_cursor;
372 else
373 {
374 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
375 {
376 // put the '" mark at the original position
377 curwin->w_cursor = is_state->save_cursor;
378 setpcmark();
379 }
380 curwin->w_cursor = is_state->search_start;
381 }
382 restore_viewstate(&is_state->old_viewstate);
383 highlight_match = FALSE;
384 validate_cursor(); /* needed for TAB */
385 if (call_update_screen)
386 update_screen(SOME_VALID);
387 else
388 redraw_all_later(SOME_VALID);
389 }
390}
391
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200392/*
393 * Do 'incsearch' highlighting if desired.
394 */
395 static void
396may_do_incsearch_highlighting(
397 int firstc,
398 long count,
399 incsearch_state_T *is_state)
400{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200401 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402 int i;
403 pos_T end_pos;
404 struct cmdline_info save_ccline;
405#ifdef FEAT_RELTIME
406 proftime_T tm;
407#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200408 int next_char;
409 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200410
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200411 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200412 {
413 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200414 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200415 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200416
417 // If there is a character waiting, search and redraw later.
418 if (char_avail())
419 {
420 is_state->incsearch_postponed = TRUE;
421 return;
422 }
423 is_state->incsearch_postponed = FALSE;
424
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200425 if (search_first_line == 0)
426 // start at the original cursor position
427 curwin->w_cursor = is_state->search_start;
428 else
429 {
430 // start at the first line in the range
431 curwin->w_cursor.lnum = search_first_line;
432 curwin->w_cursor.col = 0;
433 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200434 save_last_search_pattern();
435
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200436 // Use the previous pattern for ":s//".
437 next_char = ccline.cmdbuff[skiplen + patlen];
438 use_last_pat = patlen == 0 && skiplen > 0
439 && ccline.cmdbuff[skiplen - 1] == next_char;
440
441 // If there is no pattern, don't do anything.
442 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200443 {
444 i = 0;
445 set_no_hlsearch(TRUE); // turn off previous highlight
446 redraw_all_later(SOME_VALID);
447 }
448 else
449 {
450 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
451
452 cursor_off(); // so the user knows we're busy
453 out_flush();
454 ++emsg_off; // so it doesn't beep if bad expr
455#ifdef FEAT_RELTIME
456 // Set the time limit to half a second.
457 profile_setlimit(500L, &tm);
458#endif
459 if (!p_hls)
460 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200461 if (search_first_line != 0)
462 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200463 ccline.cmdbuff[skiplen + patlen] = NUL;
464 i = do_search(NULL, firstc == ':' ? '/' : firstc,
465 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466#ifdef FEAT_RELTIME
467 &tm, NULL
468#else
469 NULL, NULL
470#endif
471 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200472 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473 --emsg_off;
474
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200475 if (curwin->w_cursor.lnum < search_first_line
476 || curwin->w_cursor.lnum > search_last_line)
477 // match outside of address range
478 i = 0;
479
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200480 // if interrupted while searching, behave like it failed
481 if (got_int)
482 {
483 (void)vpeekc(); // remove <C-C> from input stream
484 got_int = FALSE; // don't abandon the command line
485 i = 0;
486 }
487 else if (char_avail())
488 // cancelled searching because a char was typed
489 is_state->incsearch_postponed = TRUE;
490 }
491 if (i != 0)
492 highlight_match = TRUE; // highlight position
493 else
494 highlight_match = FALSE; // remove highlight
495
496 // First restore the old curwin values, so the screen is positioned in the
497 // same way as the actual search command.
498 restore_viewstate(&is_state->old_viewstate);
499 changed_cline_bef_curs();
500 update_topline();
501
502 if (i != 0)
503 {
504 pos_T save_pos = curwin->w_cursor;
505
506 is_state->match_start = curwin->w_cursor;
507 set_search_match(&curwin->w_cursor);
508 validate_cursor();
509 end_pos = curwin->w_cursor;
510 is_state->match_end = end_pos;
511 curwin->w_cursor = save_pos;
512 }
513 else
514 end_pos = curwin->w_cursor; // shutup gcc 4
515
516 // Disable 'hlsearch' highlighting if the pattern matches everything.
517 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200518 if (!use_last_pat)
519 {
520 next_char = ccline.cmdbuff[skiplen + patlen];
521 ccline.cmdbuff[skiplen + patlen] = NUL;
522 if (empty_pattern(ccline.cmdbuff))
523 set_no_hlsearch(TRUE);
524 ccline.cmdbuff[skiplen + patlen] = next_char;
525 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200526
527 validate_cursor();
528 // May redraw the status line to show the cursor position.
529 if (p_ru && curwin->w_status_height > 0)
530 curwin->w_redr_status = TRUE;
531
532 save_cmdline(&save_ccline);
533 update_screen(SOME_VALID);
534 restore_cmdline(&save_ccline);
535 restore_last_search_pattern();
536
537 // Leave it at the end to make CTRL-R CTRL-W work.
538 if (i != 0)
539 curwin->w_cursor = end_pos;
540
541 msg_starthere();
542 redrawcmdline();
543 is_state->did_incsearch = TRUE;
544}
545
546/*
547 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
548 * or previous match.
549 * Returns FAIL when jumping to cmdline_not_changed;
550 */
551 static int
552may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200553 int firstc,
554 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200555 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200556 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200557{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200558 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200559 pos_T t;
560 char_u *pat;
561 int search_flags = SEARCH_NOOF;
562 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200563 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200565 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200566 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200567 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568 return FAIL;
569
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200570 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200571 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200572 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200573 skiplen = 0;
574 patlen = STRLEN(pat);
575 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200577 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578
579 save_last_search_pattern();
580 cursor_off();
581 out_flush();
582 if (c == Ctrl_G)
583 {
584 t = is_state->match_end;
585 if (LT_POS(is_state->match_start, is_state->match_end))
586 // Start searching at the end of the match not at the beginning of
587 // the next column.
588 (void)decl(&t);
589 search_flags += SEARCH_COL;
590 }
591 else
592 t = is_state->match_start;
593 if (!p_hls)
594 search_flags += SEARCH_KEEP;
595 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200596 save = pat[patlen];
597 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200598 i = searchit(curwin, curbuf, &t,
599 c == Ctrl_G ? FORWARD : BACKWARD,
600 pat, count, search_flags,
601 RE_SEARCH, 0, NULL, NULL);
602 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200603 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 if (i)
605 {
606 is_state->search_start = is_state->match_start;
607 is_state->match_end = t;
608 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200609 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200610 {
611 // Move just before 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)decl(&is_state->search_start);
615 }
616 else if (c == Ctrl_G && firstc == '?')
617 {
618 // Move just after the current match, so that when nv_search
619 // finishes the cursor will be put back on the match.
620 is_state->search_start = t;
621 (void)incl(&is_state->search_start);
622 }
623 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
624 {
625 // wrap around
626 is_state->search_start = t;
627 if (firstc == '?')
628 (void)incl(&is_state->search_start);
629 else
630 (void)decl(&is_state->search_start);
631 }
632
633 set_search_match(&is_state->match_end);
634 curwin->w_cursor = is_state->match_start;
635 changed_cline_bef_curs();
636 update_topline();
637 validate_cursor();
638 highlight_match = TRUE;
639 save_viewstate(&is_state->old_viewstate);
640 update_screen(NOT_VALID);
641 redrawcmdline();
642 }
643 else
644 vim_beep(BO_ERROR);
645 restore_last_search_pattern();
646 return FAIL;
647}
648
649/*
650 * When CTRL-L typed: add character from the match to the pattern.
651 * May set "*c" to the added character.
652 * Return OK when jumping to cmdline_not_changed.
653 */
654 static int
655may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
656{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200657 int skiplen, patlen;
658
659 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 return FAIL;
661
662 // Add a character from under the cursor for 'incsearch'.
663 if (is_state->did_incsearch)
664 {
665 curwin->w_cursor = is_state->match_end;
666 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
667 {
668 *c = gchar_cursor();
669
670 // If 'ignorecase' and 'smartcase' are set and the
671 // command line has no uppercase characters, convert
672 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200673 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200674 *c = MB_TOLOWER(*c);
675 if (*c != NUL)
676 {
677 if (*c == firstc || vim_strchr((char_u *)(
678 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
679 {
680 // put a backslash before special characters
681 stuffcharReadbuff(*c);
682 *c = '\\';
683 }
684 return FAIL;
685 }
686 }
687 }
688 return OK;
689}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200690#endif
691
Bram Moolenaar66216052017-12-16 16:33:44 +0100692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 * getcmdline() - accept a command line starting with firstc.
694 *
695 * firstc == ':' get ":" command line.
696 * firstc == '/' or '?' get search pattern
697 * firstc == '=' get expression
698 * firstc == '@' get text for input() function
699 * firstc == '>' get text for debug mode
700 * firstc == NUL get text for :insert command
701 * firstc == -1 like NUL, and break on CTRL-C
702 *
703 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
704 * command line.
705 *
706 * Careful: getcmdline() can be called recursively!
707 *
708 * Return pointer to allocated string if there is a commandline, NULL
709 * otherwise.
710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100712getcmdline(
713 int firstc,
714 long count UNUSED, /* only used for incremental search */
715 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
717 int c;
718 int i;
719 int j;
720 int gotesc = FALSE; /* TRUE when <ESC> just typed */
721 int do_abbr; /* when TRUE check for abbr. */
722#ifdef FEAT_CMDHIST
723 char_u *lookfor = NULL; /* string to match */
724 int hiscnt; /* current history line in use */
725 int histype; /* history type to be used */
726#endif
727#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200728 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#endif
730 int did_wild_list = FALSE; /* did wild_list() recently */
731 int wim_index = 0; /* index in wim_flags[] */
732 int res;
733 int save_msg_scroll = msg_scroll;
734 int save_State = State; /* remember State when called */
735 int some_key_typed = FALSE; /* one of the keys was typed */
736#ifdef FEAT_MOUSE
737 /* mouse drag and release events are ignored, unless they are
738 * preceded with a mouse down event */
739 int ignore_drag_release = TRUE;
740#endif
741#ifdef FEAT_EVAL
742 int break_ctrl_c = FALSE;
743#endif
744 expand_T xpc;
745 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200746#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000747 /* Everything that may work recursively should save and restore the
748 * current command line in save_ccline. That includes update_screen(), a
749 * custom status line may invoke ":normal". */
750 struct cmdline_info save_ccline;
751#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200752 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754#ifdef FEAT_EVAL
755 if (firstc == -1)
756 {
757 firstc = NUL;
758 break_ctrl_c = TRUE;
759 }
760#endif
761#ifdef FEAT_RIGHTLEFT
762 /* start without Hebrew mapping for a command line */
763 if (firstc == ':' || firstc == '=' || firstc == '>')
764 cmd_hkmap = 0;
765#endif
766
767 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200770 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772
773 /*
774 * set some variables for redrawcmd()
775 */
776 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000777 ccline.cmdindent = (firstc > 0 ? indent : 0);
778
779 /* alloc initial ccline.cmdbuff */
780 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (ccline.cmdbuff == NULL)
782 return NULL; /* out of memory */
783 ccline.cmdlen = ccline.cmdpos = 0;
784 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100785 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000787 /* autoindent for :insert and :append */
788 if (firstc <= 0)
789 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200790 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000791 ccline.cmdbuff[indent] = NUL;
792 ccline.cmdpos = indent;
793 ccline.cmdspos = indent;
794 ccline.cmdlen = indent;
795 }
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000798 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800#ifdef FEAT_RIGHTLEFT
801 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
802 && (firstc == '/' || firstc == '?'))
803 cmdmsg_rl = TRUE;
804 else
805 cmdmsg_rl = FALSE;
806#endif
807
808 redir_off = TRUE; /* don't redirect the typed command */
809 if (!cmd_silent)
810 {
811 i = msg_scrolled;
812 msg_scrolled = 0; /* avoid wait_return message */
813 gotocmdline(TRUE);
814 msg_scrolled += i;
815 redrawcmdprompt(); /* draw prompt or indent */
816 set_cmdspos();
817 }
818 xpc.xp_context = EXPAND_NOTHING;
819 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000820#ifndef BACKSLASH_IN_FILENAME
821 xpc.xp_shell = FALSE;
822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000824#if defined(FEAT_EVAL)
825 if (ccline.input_fn)
826 {
827 xpc.xp_context = ccline.xp_context;
828 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000829# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000830 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000831# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000832 }
833#endif
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 /*
836 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
837 * doing ":@0" when register 0 doesn't contain a CR.
838 */
839 msg_scroll = FALSE;
840
841 State = CMDLINE;
842
843 if (firstc == '/' || firstc == '?' || firstc == '@')
844 {
845 /* Use ":lmap" mappings for search pattern and input(). */
846 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
847 b_im_ptr = &curbuf->b_p_iminsert;
848 else
849 b_im_ptr = &curbuf->b_p_imsearch;
850 if (*b_im_ptr == B_IMODE_LMAP)
851 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100852#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 im_set_active(*b_im_ptr == B_IMODE_IM);
854#endif
855 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100856#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 else if (p_imcmdline)
858 im_set_active(TRUE);
859#endif
860
861#ifdef FEAT_MOUSE
862 setmouse();
863#endif
864#ifdef CURSOR_SHAPE
865 ui_cursor_shape(); /* may show different cursor shape */
866#endif
867
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000868 /* When inside an autocommand for writing "exiting" may be set and
869 * terminal mode set to cooked. Need to set raw mode here then. */
870 settmode(TMODE_RAW);
871
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200872 /* Trigger CmdlineEnter autocommands. */
873 cmdline_type = firstc == NUL ? '-' : firstc;
874 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef FEAT_CMDHIST
877 init_history();
878 hiscnt = hislen; /* set hiscnt to impossible history value */
879 histype = hist_char2type(firstc);
880#endif
881
882#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000883 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#endif
885
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200886 /* If something above caused an error, reset the flags, we do want to type
887 * and execute commands. Display may be messed up a bit. */
888 if (did_emsg)
889 redrawcmd();
890 did_emsg = FALSE;
891 got_int = FALSE;
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 /*
894 * Collect the command string, handling editing keys.
895 */
896 for (;;)
897 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000898 redir_off = TRUE; /* Don't redirect the typed command.
899 Repeated, because a ":redir" inside
900 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef USE_ON_FLY_SCROLL
902 dont_scroll = FALSE; /* allow scrolling here */
903#endif
904 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
905
Bram Moolenaar72532d32018-04-07 19:09:09 +0200906 did_emsg = FALSE; /* There can't really be a reason why an error
907 that occurs while typing a command should
908 cause the command not to be executed. */
909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000911
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100912 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
913 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000914 do
915 {
916 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100917 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (KeyTyped)
920 {
921 some_key_typed = TRUE;
922#ifdef FEAT_RIGHTLEFT
923 if (cmd_hkmap)
924 c = hkmap(c);
925# ifdef FEAT_FKMAP
926 if (cmd_fkmap)
927 c = cmdl_fkmap(c);
928# endif
929 if (cmdmsg_rl && !KeyStuffed)
930 {
931 /* Invert horizontal movements and operations. Only when
932 * typed by the user directly, not when the result of a
933 * mapping. */
934 switch (c)
935 {
936 case K_RIGHT: c = K_LEFT; break;
937 case K_S_RIGHT: c = K_S_LEFT; break;
938 case K_C_RIGHT: c = K_C_LEFT; break;
939 case K_LEFT: c = K_RIGHT; break;
940 case K_S_LEFT: c = K_S_RIGHT; break;
941 case K_C_LEFT: c = K_C_RIGHT; break;
942 }
943 }
944#endif
945 }
946
947 /*
948 * Ignore got_int when CTRL-C was typed here.
949 * Don't ignore it in :global, we really need to break then, e.g., for
950 * ":g/pat/normal /pat" (without the <CR>).
951 * Don't ignore it for the input() function.
952 */
953 if ((c == Ctrl_C
954#ifdef UNIX
955 || c == intr_char
956#endif
957 )
958#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
959 && firstc != '@'
960#endif
961#ifdef FEAT_EVAL
962 && !break_ctrl_c
963#endif
964 && !global_busy)
965 got_int = FALSE;
966
967#ifdef FEAT_CMDHIST
968 /* free old command line when finished moving around in the history
969 * list */
970 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000971 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000972 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 && c != K_PAGEDOWN && c != K_PAGEUP
974 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000975 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100977 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#endif
979
980 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000981 * When there are matching completions to select <S-Tab> works like
982 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000984 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 c = Ctrl_P;
986
987#ifdef FEAT_WILDMENU
988 /* Special translations for 'wildmenu' */
989 if (did_wild_list && p_wmnu)
990 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000991 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000993 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 c = Ctrl_N;
995 }
996 /* Hitting CR after "emenu Name.": complete submenu */
997 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
998 && ccline.cmdpos > 1
999 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1000 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1001 && (c == '\n' || c == '\r' || c == K_KENTER))
1002 c = K_DOWN;
1003#endif
1004
1005 /* free expanded names when finished walking through matches */
1006 if (xpc.xp_numfiles != -1
1007 && !(c == p_wc && KeyTyped) && c != p_wcm
1008 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1009 && c != Ctrl_L)
1010 {
1011 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1012 did_wild_list = FALSE;
1013#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001014 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016 xpc.xp_context = EXPAND_NOTHING;
1017 wim_index = 0;
1018#ifdef FEAT_WILDMENU
1019 if (p_wmnu && wild_menu_showing != 0)
1020 {
1021 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001022 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001023
1024 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001025 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 if (wild_menu_showing == WM_SCROLLED)
1028 {
1029 /* Entered command line, move it up */
1030 cmdline_row--;
1031 redrawcmd();
1032 }
1033 else if (save_p_ls != -1)
1034 {
1035 /* restore 'laststatus' and 'winminheight' */
1036 p_ls = save_p_ls;
1037 p_wmh = save_p_wmh;
1038 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001039 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001041 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 redrawcmd();
1043 save_p_ls = -1;
1044 }
1045 else
1046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 redraw_statuslines();
1049 }
1050 KeyTyped = skt;
1051 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001052 if (ccline.input_fn)
1053 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 }
1055#endif
1056 }
1057
1058#ifdef FEAT_WILDMENU
1059 /* Special translations for 'wildmenu' */
1060 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1061 {
1062 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001063 if (c == K_DOWN && ccline.cmdpos > 0
1064 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001066 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 /* Hitting <Up>: Remove one submenu name in front of the
1069 * cursor */
1070 int found = FALSE;
1071
1072 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1073 i = 0;
1074 while (--j > 0)
1075 {
1076 /* check for start of menu name */
1077 if (ccline.cmdbuff[j] == ' '
1078 && ccline.cmdbuff[j - 1] != '\\')
1079 {
1080 i = j + 1;
1081 break;
1082 }
1083 /* check for start of submenu name */
1084 if (ccline.cmdbuff[j] == '.'
1085 && ccline.cmdbuff[j - 1] != '\\')
1086 {
1087 if (found)
1088 {
1089 i = j + 1;
1090 break;
1091 }
1092 else
1093 found = TRUE;
1094 }
1095 }
1096 if (i > 0)
1097 cmdline_del(i);
1098 c = p_wc;
1099 xpc.xp_context = EXPAND_NOTHING;
1100 }
1101 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001102 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001103 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001104 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106 char_u upseg[5];
1107
1108 upseg[0] = PATHSEP;
1109 upseg[1] = '.';
1110 upseg[2] = '.';
1111 upseg[3] = PATHSEP;
1112 upseg[4] = NUL;
1113
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001114 if (c == K_DOWN
1115 && ccline.cmdpos > 0
1116 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1117 && (ccline.cmdpos < 3
1118 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1120 {
1121 /* go down a directory */
1122 c = p_wc;
1123 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001124 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
1126 /* If in a direct ancestor, strip off one ../ to go down */
1127 int found = FALSE;
1128
1129 j = ccline.cmdpos;
1130 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1131 while (--j > i)
1132 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001133#ifdef FEAT_MBYTE
1134 if (has_mbyte)
1135 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (vim_ispathsep(ccline.cmdbuff[j]))
1138 {
1139 found = TRUE;
1140 break;
1141 }
1142 }
1143 if (found
1144 && ccline.cmdbuff[j - 1] == '.'
1145 && ccline.cmdbuff[j - 2] == '.'
1146 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1147 {
1148 cmdline_del(j - 2);
1149 c = p_wc;
1150 }
1151 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001152 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 /* go up a directory */
1155 int found = FALSE;
1156
1157 j = ccline.cmdpos - 1;
1158 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1159 while (--j > i)
1160 {
1161#ifdef FEAT_MBYTE
1162 if (has_mbyte)
1163 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1164#endif
1165 if (vim_ispathsep(ccline.cmdbuff[j])
1166#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001167 && vim_strchr((char_u *)" *?[{`$%#",
1168 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif
1170 )
1171 {
1172 if (found)
1173 {
1174 i = j + 1;
1175 break;
1176 }
1177 else
1178 found = TRUE;
1179 }
1180 }
1181
1182 if (!found)
1183 j = i;
1184 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1185 j += 4;
1186 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1187 && j == i)
1188 j += 3;
1189 else
1190 j = 0;
1191 if (j > 0)
1192 {
1193 /* TODO this is only for DOS/UNIX systems - need to put in
1194 * machine-specific stuff here and in upseg init */
1195 cmdline_del(j);
1196 put_on_cmdline(upseg + 1, 3, FALSE);
1197 }
1198 else if (ccline.cmdpos > i)
1199 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001200
1201 /* Now complete in the new directory. Set KeyTyped in case the
1202 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001204 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
1208#endif /* FEAT_WILDMENU */
1209
1210 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1211 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1212 if (c == Ctrl_BSL)
1213 {
1214 ++no_mapping;
1215 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001216 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 --no_mapping;
1218 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001219 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1220 * is in a mapping. */
1221 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1222 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
1224 vungetc(c);
1225 c = Ctrl_BSL;
1226 }
1227#ifdef FEAT_EVAL
1228 else if (c == 'e')
1229 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001230 char_u *p = NULL;
1231 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
1233 /*
1234 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001235 * Need to save and restore the current command line, to be
1236 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
1238 if (ccline.cmdpos == ccline.cmdlen)
1239 new_cmdpos = 99999; /* keep it at the end */
1240 else
1241 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001242
1243 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001245 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (c == '=')
1247 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001248 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001249 * to avoid nasty things like going to another buffer when
1250 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001251 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001252 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001254 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001255 restore_cmdline(&save_ccline);
1256
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001257 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001259 len = (int)STRLEN(p);
1260 if (realloc_cmdbuff(len + 1) == OK)
1261 {
1262 ccline.cmdlen = len;
1263 STRCPY(ccline.cmdbuff, p);
1264 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001266 /* Restore the cursor or use the position set with
1267 * set_cmdline_pos(). */
1268 if (new_cmdpos > ccline.cmdlen)
1269 ccline.cmdpos = ccline.cmdlen;
1270 else
1271 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001273 KeyTyped = FALSE; /* Don't do p_wc completion. */
1274 redrawcmd();
1275 goto cmdline_changed;
1276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278 }
1279 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001280 got_int = FALSE; /* don't abandon the command line */
1281 did_emsg = FALSE;
1282 emsg_on_display = FALSE;
1283 redrawcmd();
1284 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286#endif
1287 else
1288 {
1289 if (c == Ctrl_G && p_im && restart_edit == 0)
1290 restart_edit = 'a';
1291 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1292 in history */
1293 goto returncmd; /* back to Normal mode */
1294 }
1295 }
1296
1297#ifdef FEAT_CMDWIN
1298 if (c == cedit_key || c == K_CMDWIN)
1299 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001300 if (ex_normal_busy == 0 && got_int == FALSE)
1301 {
1302 /*
1303 * Open a window to edit the command line (and history).
1304 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001305 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001306 some_key_typed = TRUE;
1307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309# ifdef FEAT_DIGRAPHS
1310 else
1311# endif
1312#endif
1313#ifdef FEAT_DIGRAPHS
1314 c = do_digraph(c);
1315#endif
1316
1317 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1318 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1319 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001320 /* In Ex mode a backslash escapes a newline. */
1321 if (exmode_active
1322 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001323 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001324 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001325 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001327 if (c == K_KENTER)
1328 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001330 else
1331 {
1332 gotesc = FALSE; /* Might have typed ESC previously, don't
1333 truncate the cmdline now. */
1334 if (ccheck_abbr(c + ABBR_OFF))
1335 goto cmdline_changed;
1336 if (!cmd_silent)
1337 {
1338 windgoto(msg_row, 0);
1339 out_flush();
1340 }
1341 break;
1342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344
1345 /*
1346 * Completion for 'wildchar' or 'wildcharm' key.
1347 * - hitting <ESC> twice means: abandon command line.
1348 * - wildcard expansion is only done when the 'wildchar' key is really
1349 * typed, not when it comes from a macro
1350 */
1351 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1352 {
1353 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1354 {
1355 /* if 'wildmode' contains "list" may still need to list */
1356 if (xpc.xp_numfiles > 1
1357 && !did_wild_list
1358 && (wim_flags[wim_index] & WIM_LIST))
1359 {
1360 (void)showmatches(&xpc, FALSE);
1361 redrawcmd();
1362 did_wild_list = TRUE;
1363 }
1364 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001365 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1366 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001368 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1369 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 else
1371 res = OK; /* don't insert 'wildchar' now */
1372 }
1373 else /* typed p_wc first time */
1374 {
1375 wim_index = 0;
1376 j = ccline.cmdpos;
1377 /* if 'wildmode' first contains "longest", get longest
1378 * common part */
1379 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001380 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1381 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001383 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1384 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386 /* if interrupted while completing, behave like it failed */
1387 if (got_int)
1388 {
1389 (void)vpeekc(); /* remove <C-C> from input stream */
1390 got_int = FALSE; /* don't abandon the command line */
1391 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1392#ifdef FEAT_WILDMENU
1393 xpc.xp_context = EXPAND_NOTHING;
1394#endif
1395 goto cmdline_changed;
1396 }
1397
1398 /* when more than one match, and 'wildmode' first contains
1399 * "list", or no change and 'wildmode' contains "longest,list",
1400 * list all matches */
1401 if (res == OK && xpc.xp_numfiles > 1)
1402 {
1403 /* a "longest" that didn't do anything is skipped (but not
1404 * "list:longest") */
1405 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1406 wim_index = 1;
1407 if ((wim_flags[wim_index] & WIM_LIST)
1408#ifdef FEAT_WILDMENU
1409 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1410#endif
1411 )
1412 {
1413 if (!(wim_flags[0] & WIM_LONGEST))
1414 {
1415#ifdef FEAT_WILDMENU
1416 int p_wmnu_save = p_wmnu;
1417 p_wmnu = 0;
1418#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001419 /* remove match */
1420 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421#ifdef FEAT_WILDMENU
1422 p_wmnu = p_wmnu_save;
1423#endif
1424 }
1425#ifdef FEAT_WILDMENU
1426 (void)showmatches(&xpc, p_wmnu
1427 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1428#else
1429 (void)showmatches(&xpc, FALSE);
1430#endif
1431 redrawcmd();
1432 did_wild_list = TRUE;
1433 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001434 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1435 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001437 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1438 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 }
1440 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001441 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443#ifdef FEAT_WILDMENU
1444 else if (xpc.xp_numfiles == -1)
1445 xpc.xp_context = EXPAND_NOTHING;
1446#endif
1447 }
1448 if (wim_index < 3)
1449 ++wim_index;
1450 if (c == ESC)
1451 gotesc = TRUE;
1452 if (res == OK)
1453 goto cmdline_changed;
1454 }
1455
1456 gotesc = FALSE;
1457
1458 /* <S-Tab> goes to last match, in a clumsy way */
1459 if (c == K_S_TAB && KeyTyped)
1460 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001461 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1462 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1463 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 goto cmdline_changed;
1465 }
1466
1467 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1468 c = NL;
1469
1470 do_abbr = TRUE; /* default: check for abbreviation */
1471
1472 /*
1473 * Big switch for a typed command line character.
1474 */
1475 switch (c)
1476 {
1477 case K_BS:
1478 case Ctrl_H:
1479 case K_DEL:
1480 case K_KDEL:
1481 case Ctrl_W:
1482#ifdef FEAT_FKMAP
1483 if (cmd_fkmap && c == K_BS)
1484 c = K_DEL;
1485#endif
1486 if (c == K_KDEL)
1487 c = K_DEL;
1488
1489 /*
1490 * delete current character is the same as backspace on next
1491 * character, except at end of line
1492 */
1493 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1494 ++ccline.cmdpos;
1495#ifdef FEAT_MBYTE
1496 if (has_mbyte && c == K_DEL)
1497 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1498 ccline.cmdbuff + ccline.cmdpos);
1499#endif
1500 if (ccline.cmdpos > 0)
1501 {
1502 char_u *p;
1503
1504 j = ccline.cmdpos;
1505 p = ccline.cmdbuff + j;
1506#ifdef FEAT_MBYTE
1507 if (has_mbyte)
1508 {
1509 p = mb_prevptr(ccline.cmdbuff, p);
1510 if (c == Ctrl_W)
1511 {
1512 while (p > ccline.cmdbuff && vim_isspace(*p))
1513 p = mb_prevptr(ccline.cmdbuff, p);
1514 i = mb_get_class(p);
1515 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1516 p = mb_prevptr(ccline.cmdbuff, p);
1517 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001518 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520 }
1521 else
1522#endif
1523 if (c == Ctrl_W)
1524 {
1525 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1526 --p;
1527 i = vim_iswordc(p[-1]);
1528 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1529 && vim_iswordc(p[-1]) == i)
1530 --p;
1531 }
1532 else
1533 --p;
1534 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1535 ccline.cmdlen -= j - ccline.cmdpos;
1536 i = ccline.cmdpos;
1537 while (i < ccline.cmdlen)
1538 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1539
1540 /* Truncate at the end, required for multi-byte chars. */
1541 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001542#ifdef FEAT_SEARCH_EXTRA
1543 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001544 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001545 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001546 /* save view settings, so that the screen
1547 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001548 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001549 }
1550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 redrawcmd();
1552 }
1553 else if (ccline.cmdlen == 0 && c != Ctrl_W
1554 && ccline.cmdprompt == NULL && indent == 0)
1555 {
1556 /* In ex and debug mode it doesn't make sense to return. */
1557 if (exmode_active
1558#ifdef FEAT_EVAL
1559 || ccline.cmdfirstc == '>'
1560#endif
1561 )
1562 goto cmdline_not_changed;
1563
Bram Moolenaard23a8232018-02-10 18:45:26 +01001564 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (!cmd_silent)
1566 {
1567#ifdef FEAT_RIGHTLEFT
1568 if (cmdmsg_rl)
1569 msg_col = Columns;
1570 else
1571#endif
1572 msg_col = 0;
1573 msg_putchar(' '); /* delete ':' */
1574 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001575#ifdef FEAT_SEARCH_EXTRA
1576 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001577 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 redraw_cmdline = TRUE;
1580 goto returncmd; /* back to cmd mode */
1581 }
1582 goto cmdline_changed;
1583
1584 case K_INS:
1585 case K_KINS:
1586#ifdef FEAT_FKMAP
1587 /* if Farsi mode set, we are in reverse insert mode -
1588 Do not change the mode */
1589 if (cmd_fkmap)
1590 beep_flush();
1591 else
1592#endif
1593 ccline.overstrike = !ccline.overstrike;
1594#ifdef CURSOR_SHAPE
1595 ui_cursor_shape(); /* may show different cursor shape */
1596#endif
1597 goto cmdline_not_changed;
1598
1599 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001600 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 /* ":lmap" mappings exists, toggle use of mappings. */
1603 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001604#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 im_set_active(FALSE); /* Disable input method */
1606#endif
1607 if (b_im_ptr != NULL)
1608 {
1609 if (State & LANGMAP)
1610 *b_im_ptr = B_IMODE_LMAP;
1611 else
1612 *b_im_ptr = B_IMODE_NONE;
1613 }
1614 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001615#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 else
1617 {
1618 /* There are no ":lmap" mappings, toggle IM. When
1619 * 'imdisable' is set don't try getting the status, it's
1620 * always off. */
1621 if ((p_imdisable && b_im_ptr != NULL)
1622 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1623 {
1624 im_set_active(FALSE); /* Disable input method */
1625 if (b_im_ptr != NULL)
1626 *b_im_ptr = B_IMODE_NONE;
1627 }
1628 else
1629 {
1630 im_set_active(TRUE); /* Enable input method */
1631 if (b_im_ptr != NULL)
1632 *b_im_ptr = B_IMODE_IM;
1633 }
1634 }
1635#endif
1636 if (b_im_ptr != NULL)
1637 {
1638 if (b_im_ptr == &curbuf->b_p_iminsert)
1639 set_iminsert_global();
1640 else
1641 set_imsearch_global();
1642 }
1643#ifdef CURSOR_SHAPE
1644 ui_cursor_shape(); /* may show different cursor shape */
1645#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001646#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 /* Show/unshow value of 'keymap' in status lines later. */
1648 status_redraw_curbuf();
1649#endif
1650 goto cmdline_not_changed;
1651
1652/* case '@': only in very old vi */
1653 case Ctrl_U:
1654 /* delete all characters left of the cursor */
1655 j = ccline.cmdpos;
1656 ccline.cmdlen -= j;
1657 i = ccline.cmdpos = 0;
1658 while (i < ccline.cmdlen)
1659 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1660 /* Truncate at the end, required for multi-byte chars. */
1661 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001662#ifdef FEAT_SEARCH_EXTRA
1663 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001664 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 redrawcmd();
1667 goto cmdline_changed;
1668
1669#ifdef FEAT_CLIPBOARD
1670 case Ctrl_Y:
1671 /* Copy the modeless selection, if there is one. */
1672 if (clip_star.state != SELECT_CLEARED)
1673 {
1674 if (clip_star.state == SELECT_DONE)
1675 clip_copy_modeless_selection(TRUE);
1676 goto cmdline_not_changed;
1677 }
1678 break;
1679#endif
1680
1681 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1682 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001683 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001684 * ":normal" runs out of characters. */
1685 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001686 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 goto cmdline_not_changed;
1688
1689 gotesc = TRUE; /* will free ccline.cmdbuff after
1690 putting it in history */
1691 goto returncmd; /* back to cmd mode */
1692
1693 case Ctrl_R: /* insert register */
1694#ifdef USE_ON_FLY_SCROLL
1695 dont_scroll = TRUE; /* disallow scrolling here */
1696#endif
1697 putcmdline('"', TRUE);
1698 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001699 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if (i == Ctrl_O)
1701 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1702 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001703 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001704 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 --no_mapping;
1706#ifdef FEAT_EVAL
1707 /*
1708 * Insert the result of an expression.
1709 * Need to save the current command line, to be able to enter
1710 * a new one...
1711 */
1712 new_cmdpos = -1;
1713 if (c == '=')
1714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1716 {
1717 beep_flush();
1718 c = ESC;
1719 }
1720 else
1721 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001722 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001724 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
1726 }
1727#endif
1728 if (c != ESC) /* use ESC to cancel inserting register */
1729 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001730 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001731
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001732#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001733 /* When there was a serious error abort getting the
1734 * command line. */
1735 if (aborting())
1736 {
1737 gotesc = TRUE; /* will free ccline.cmdbuff after
1738 putting it in history */
1739 goto returncmd; /* back to cmd mode */
1740 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 KeyTyped = FALSE; /* Don't do p_wc completion. */
1743#ifdef FEAT_EVAL
1744 if (new_cmdpos >= 0)
1745 {
1746 /* set_cmdline_pos() was used */
1747 if (new_cmdpos > ccline.cmdlen)
1748 ccline.cmdpos = ccline.cmdlen;
1749 else
1750 ccline.cmdpos = new_cmdpos;
1751 }
1752#endif
1753 }
1754 redrawcmd();
1755 goto cmdline_changed;
1756
1757 case Ctrl_D:
1758 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1759 break; /* Use ^D as normal char instead */
1760
1761 redrawcmd();
1762 continue; /* don't do incremental search now */
1763
1764 case K_RIGHT:
1765 case K_S_RIGHT:
1766 case K_C_RIGHT:
1767 do
1768 {
1769 if (ccline.cmdpos >= ccline.cmdlen)
1770 break;
1771 i = cmdline_charsize(ccline.cmdpos);
1772 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1773 break;
1774 ccline.cmdspos += i;
1775#ifdef FEAT_MBYTE
1776 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001777 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 + ccline.cmdpos);
1779 else
1780#endif
1781 ++ccline.cmdpos;
1782 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001783 while ((c == K_S_RIGHT || c == K_C_RIGHT
1784 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1786#ifdef FEAT_MBYTE
1787 if (has_mbyte)
1788 set_cmdspos_cursor();
1789#endif
1790 goto cmdline_not_changed;
1791
1792 case K_LEFT:
1793 case K_S_LEFT:
1794 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001795 if (ccline.cmdpos == 0)
1796 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 do
1798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 --ccline.cmdpos;
1800#ifdef FEAT_MBYTE
1801 if (has_mbyte) /* move to first byte of char */
1802 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1803 ccline.cmdbuff + ccline.cmdpos);
1804#endif
1805 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1806 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001807 while (ccline.cmdpos > 0
1808 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001809 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1811#ifdef FEAT_MBYTE
1812 if (has_mbyte)
1813 set_cmdspos_cursor();
1814#endif
1815 goto cmdline_not_changed;
1816
1817 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001818 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001819 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaar4770d092006-01-12 23:22:24 +00001821#ifdef FEAT_GUI_W32
1822 /* On Win32 ignore <M-F4>, we get it when closing the window was
1823 * cancelled. */
1824 case K_F4:
1825 if (mod_mask == MOD_MASK_ALT)
1826 {
1827 redrawcmd(); /* somehow the cmdline is cleared */
1828 goto cmdline_not_changed;
1829 }
1830 break;
1831#endif
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#ifdef FEAT_MOUSE
1834 case K_MIDDLEDRAG:
1835 case K_MIDDLERELEASE:
1836 goto cmdline_not_changed; /* Ignore mouse */
1837
1838 case K_MIDDLEMOUSE:
1839# ifdef FEAT_GUI
1840 /* When GUI is active, also paste when 'mouse' is empty */
1841 if (!gui.in_use)
1842# endif
1843 if (!mouse_has(MOUSE_COMMAND))
1844 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001845# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001847 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001849# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001850 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 redrawcmd();
1852 goto cmdline_changed;
1853
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001854# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001856 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 redrawcmd();
1858 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
1861 case K_LEFTDRAG:
1862 case K_LEFTRELEASE:
1863 case K_RIGHTDRAG:
1864 case K_RIGHTRELEASE:
1865 /* Ignore drag and release events when the button-down wasn't
1866 * seen before. */
1867 if (ignore_drag_release)
1868 goto cmdline_not_changed;
1869 /* FALLTHROUGH */
1870 case K_LEFTMOUSE:
1871 case K_RIGHTMOUSE:
1872 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1873 ignore_drag_release = TRUE;
1874 else
1875 ignore_drag_release = FALSE;
1876# ifdef FEAT_GUI
1877 /* When GUI is active, also move when 'mouse' is empty */
1878 if (!gui.in_use)
1879# endif
1880 if (!mouse_has(MOUSE_COMMAND))
1881 goto cmdline_not_changed; /* Ignore mouse */
1882# ifdef FEAT_CLIPBOARD
1883 if (mouse_row < cmdline_row && clip_star.available)
1884 {
1885 int button, is_click, is_drag;
1886
1887 /*
1888 * Handle modeless selection.
1889 */
1890 button = get_mouse_button(KEY2TERMCAP1(c),
1891 &is_click, &is_drag);
1892 if (mouse_model_popup() && button == MOUSE_LEFT
1893 && (mod_mask & MOD_MASK_SHIFT))
1894 {
1895 /* Translate shift-left to right button. */
1896 button = MOUSE_RIGHT;
1897 mod_mask &= ~MOD_MASK_SHIFT;
1898 }
1899 clip_modeless(button, is_click, is_drag);
1900 goto cmdline_not_changed;
1901 }
1902# endif
1903
1904 set_cmdspos();
1905 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1906 ++ccline.cmdpos)
1907 {
1908 i = cmdline_charsize(ccline.cmdpos);
1909 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1910 && mouse_col < ccline.cmdspos % Columns + i)
1911 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001912# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (has_mbyte)
1914 {
1915 /* Count ">" for double-wide char that doesn't fit. */
1916 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001917 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 + ccline.cmdpos) - 1;
1919 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 ccline.cmdspos += i;
1922 }
1923 goto cmdline_not_changed;
1924
1925 /* Mouse scroll wheel: ignored here */
1926 case K_MOUSEDOWN:
1927 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001928 case K_MOUSELEFT:
1929 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 /* Alternate buttons ignored here */
1931 case K_X1MOUSE:
1932 case K_X1DRAG:
1933 case K_X1RELEASE:
1934 case K_X2MOUSE:
1935 case K_X2DRAG:
1936 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001937 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 goto cmdline_not_changed;
1939
1940#endif /* FEAT_MOUSE */
1941
1942#ifdef FEAT_GUI
1943 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1944 case K_LEFTRELEASE_NM:
1945 goto cmdline_not_changed;
1946
1947 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001948 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 gui_do_scroll();
1951 redrawcmd();
1952 }
1953 goto cmdline_not_changed;
1954
1955 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001956 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001958 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 redrawcmd();
1960 }
1961 goto cmdline_not_changed;
1962#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001963#ifdef FEAT_GUI_TABLINE
1964 case K_TABLINE:
1965 case K_TABMENU:
1966 /* Don't want to change any tabs here. Make sure the same tab
1967 * is still selected. */
1968 if (gui_use_tabline())
1969 gui_mch_set_curtab(tabpage_index(curtab));
1970 goto cmdline_not_changed;
1971#endif
1972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 case K_SELECT: /* end of Select mode mapping - ignore */
1974 goto cmdline_not_changed;
1975
1976 case Ctrl_B: /* begin of command line */
1977 case K_HOME:
1978 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 case K_S_HOME:
1980 case K_C_HOME:
1981 ccline.cmdpos = 0;
1982 set_cmdspos();
1983 goto cmdline_not_changed;
1984
1985 case Ctrl_E: /* end of command line */
1986 case K_END:
1987 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 case K_S_END:
1989 case K_C_END:
1990 ccline.cmdpos = ccline.cmdlen;
1991 set_cmdspos_cursor();
1992 goto cmdline_not_changed;
1993
1994 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001995 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 break;
1997 goto cmdline_changed;
1998
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001999 case Ctrl_L:
2000#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002001 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002002 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002003#endif
2004
2005 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002006 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 break;
2008 goto cmdline_changed;
2009
2010 case Ctrl_N: /* next match */
2011 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002012 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002014 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2015 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002017 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002020 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 case K_UP:
2022 case K_DOWN:
2023 case K_S_UP:
2024 case K_S_DOWN:
2025 case K_PAGEUP:
2026 case K_KPAGEUP:
2027 case K_PAGEDOWN:
2028 case K_KPAGEDOWN:
2029 if (hislen == 0 || firstc == NUL) /* no history */
2030 goto cmdline_not_changed;
2031
2032 i = hiscnt;
2033
2034 /* save current command string so it can be restored later */
2035 if (lookfor == NULL)
2036 {
2037 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2038 goto cmdline_not_changed;
2039 lookfor[ccline.cmdpos] = NUL;
2040 }
2041
2042 j = (int)STRLEN(lookfor);
2043 for (;;)
2044 {
2045 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002046 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002047 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
2049 if (hiscnt == hislen) /* first time */
2050 hiscnt = hisidx[histype];
2051 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2052 hiscnt = hislen - 1;
2053 else if (hiscnt != hisidx[histype] + 1)
2054 --hiscnt;
2055 else /* at top of list */
2056 {
2057 hiscnt = i;
2058 break;
2059 }
2060 }
2061 else /* one step forwards */
2062 {
2063 /* on last entry, clear the line */
2064 if (hiscnt == hisidx[histype])
2065 {
2066 hiscnt = hislen;
2067 break;
2068 }
2069
2070 /* not on a history line, nothing to do */
2071 if (hiscnt == hislen)
2072 break;
2073 if (hiscnt == hislen - 1) /* wrap around */
2074 hiscnt = 0;
2075 else
2076 ++hiscnt;
2077 }
2078 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2079 {
2080 hiscnt = i;
2081 break;
2082 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002083 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002084 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 || STRNCMP(history[histype][hiscnt].hisstr,
2086 lookfor, (size_t)j) == 0)
2087 break;
2088 }
2089
2090 if (hiscnt != i) /* jumped to other entry */
2091 {
2092 char_u *p;
2093 int len;
2094 int old_firstc;
2095
2096 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002097 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 if (hiscnt == hislen)
2099 p = lookfor; /* back to the old one */
2100 else
2101 p = history[histype][hiscnt].hisstr;
2102
2103 if (histype == HIST_SEARCH
2104 && p != lookfor
2105 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2106 {
2107 /* Correct for the separator character used when
2108 * adding the history entry vs the one used now.
2109 * First loop: count length.
2110 * Second loop: copy the characters. */
2111 for (i = 0; i <= 1; ++i)
2112 {
2113 len = 0;
2114 for (j = 0; p[j] != NUL; ++j)
2115 {
2116 /* Replace old sep with new sep, unless it is
2117 * escaped. */
2118 if (p[j] == old_firstc
2119 && (j == 0 || p[j - 1] != '\\'))
2120 {
2121 if (i > 0)
2122 ccline.cmdbuff[len] = firstc;
2123 }
2124 else
2125 {
2126 /* Escape new sep, unless it is already
2127 * escaped. */
2128 if (p[j] == firstc
2129 && (j == 0 || p[j - 1] != '\\'))
2130 {
2131 if (i > 0)
2132 ccline.cmdbuff[len] = '\\';
2133 ++len;
2134 }
2135 if (i > 0)
2136 ccline.cmdbuff[len] = p[j];
2137 }
2138 ++len;
2139 }
2140 if (i == 0)
2141 {
2142 alloc_cmdbuff(len);
2143 if (ccline.cmdbuff == NULL)
2144 goto returncmd;
2145 }
2146 }
2147 ccline.cmdbuff[len] = NUL;
2148 }
2149 else
2150 {
2151 alloc_cmdbuff((int)STRLEN(p));
2152 if (ccline.cmdbuff == NULL)
2153 goto returncmd;
2154 STRCPY(ccline.cmdbuff, p);
2155 }
2156
2157 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2158 redrawcmd();
2159 goto cmdline_changed;
2160 }
2161 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002162#endif
2163 goto cmdline_not_changed;
2164
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002165#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002166 case Ctrl_G: /* next match */
2167 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002168 if (may_adjust_incsearch_highlighting(
2169 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002170 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#endif
2173
2174 case Ctrl_V:
2175 case Ctrl_Q:
2176#ifdef FEAT_MOUSE
2177 ignore_drag_release = TRUE;
2178#endif
2179 putcmdline('^', TRUE);
2180 c = get_literal(); /* get next (two) character(s) */
2181 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002182 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#ifdef FEAT_MBYTE
2184 /* may need to remove ^ when composing char was typed */
2185 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2186 {
2187 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2188 msg_putchar(' ');
2189 cursorcmd();
2190 }
2191#endif
2192 break;
2193
2194#ifdef FEAT_DIGRAPHS
2195 case Ctrl_K:
2196#ifdef FEAT_MOUSE
2197 ignore_drag_release = TRUE;
2198#endif
2199 putcmdline('?', TRUE);
2200#ifdef USE_ON_FLY_SCROLL
2201 dont_scroll = TRUE; /* disallow scrolling here */
2202#endif
2203 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002204 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (c != NUL)
2206 break;
2207
2208 redrawcmd();
2209 goto cmdline_not_changed;
2210#endif /* FEAT_DIGRAPHS */
2211
2212#ifdef FEAT_RIGHTLEFT
2213 case Ctrl__: /* CTRL-_: switch language mode */
2214 if (!p_ari)
2215 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002216# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (p_altkeymap)
2218 {
2219 cmd_fkmap = !cmd_fkmap;
2220 if (cmd_fkmap) /* in Farsi always in Insert mode */
2221 ccline.overstrike = FALSE;
2222 }
2223 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 cmd_hkmap = !cmd_hkmap;
2226 goto cmdline_not_changed;
2227#endif
2228
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002229 case K_PS:
2230 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2231 goto cmdline_changed;
2232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 default:
2234#ifdef UNIX
2235 if (c == intr_char)
2236 {
2237 gotesc = TRUE; /* will free ccline.cmdbuff after
2238 putting it in history */
2239 goto returncmd; /* back to Normal mode */
2240 }
2241#endif
2242 /*
2243 * Normal character with no special meaning. Just set mod_mask
2244 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2245 * the string <S-Space>. This should only happen after ^V.
2246 */
2247 if (!IS_SPECIAL(c))
2248 mod_mask = 0x0;
2249 break;
2250 }
2251 /*
2252 * End of switch on command line character.
2253 * We come here if we have a normal character.
2254 */
2255
Bram Moolenaarede3e632013-06-23 16:16:19 +02002256 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257#ifdef FEAT_MBYTE
2258 /* Add ABBR_OFF for characters above 0x100, this is
2259 * what check_abbr() expects. */
2260 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2261#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002262 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 goto cmdline_changed;
2264
2265 /*
2266 * put the character in the command line
2267 */
2268 if (IS_SPECIAL(c) || mod_mask != 0)
2269 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2270 else
2271 {
2272#ifdef FEAT_MBYTE
2273 if (has_mbyte)
2274 {
2275 j = (*mb_char2bytes)(c, IObuff);
2276 IObuff[j] = NUL; /* exclude composing chars */
2277 put_on_cmdline(IObuff, j, TRUE);
2278 }
2279 else
2280#endif
2281 {
2282 IObuff[0] = c;
2283 put_on_cmdline(IObuff, 1, TRUE);
2284 }
2285 }
2286 goto cmdline_changed;
2287
2288/*
2289 * This part implements incremental searches for "/" and "?"
2290 * Jump to cmdline_not_changed when a character has been read but the command
2291 * line did not change. Then we only search and redraw if something changed in
2292 * the past.
2293 * Jump to cmdline_changed when the command line did change.
2294 * (Sorry for the goto's, I know it is ugly).
2295 */
2296cmdline_not_changed:
2297#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002298 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 continue;
2300#endif
2301
2302cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002303 /* Trigger CmdlineChanged autocommands. */
2304 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002307 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#endif
2309
2310#ifdef FEAT_RIGHTLEFT
2311 if (cmdmsg_rl
2312# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002313 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314# endif
2315 )
2316 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002317 * right-left typing. Not efficient, but it works.
2318 * Do it only when there are no characters left to read
2319 * to avoid useless intermediate redraws. */
2320 if (vpeekc() == NUL)
2321 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#endif
2323 }
2324
2325returncmd:
2326
2327#ifdef FEAT_RIGHTLEFT
2328 cmdmsg_rl = FALSE;
2329#endif
2330
2331#ifdef FEAT_FKMAP
2332 cmd_fkmap = 0;
2333#endif
2334
2335 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002336 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002339 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#endif
2341
2342 if (ccline.cmdbuff != NULL)
2343 {
2344 /*
2345 * Put line in history buffer (":" and "=" only when it was typed).
2346 */
2347#ifdef FEAT_CMDHIST
2348 if (ccline.cmdlen && firstc != NUL
2349 && (some_key_typed || histype == HIST_SEARCH))
2350 {
2351 add_to_history(histype, ccline.cmdbuff, TRUE,
2352 histype == HIST_SEARCH ? firstc : NUL);
2353 if (firstc == ':')
2354 {
2355 vim_free(new_last_cmdline);
2356 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2357 }
2358 }
2359#endif
2360
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002361 if (gotesc)
2362 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364
2365 /*
2366 * If the screen was shifted up, redraw the whole screen (later).
2367 * If the line is too long, clear it, so ruler and shown command do
2368 * not get printed in the middle of it.
2369 */
2370 msg_check();
2371 msg_scroll = save_msg_scroll;
2372 redir_off = FALSE;
2373
2374 /* When the command line was typed, no need for a wait-return prompt. */
2375 if (some_key_typed)
2376 need_wait_return = FALSE;
2377
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002378 /* Trigger CmdlineLeave autocommands. */
2379 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002382#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2384 im_save_status(b_im_ptr);
2385 im_set_active(FALSE);
2386#endif
2387#ifdef FEAT_MOUSE
2388 setmouse();
2389#endif
2390#ifdef CURSOR_SHAPE
2391 ui_cursor_shape(); /* may show different cursor shape */
2392#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002393 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002395 {
2396 char_u *p = ccline.cmdbuff;
2397
2398 /* Make ccline empty, getcmdline() may try to use it. */
2399 ccline.cmdbuff = NULL;
2400 return p;
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402}
2403
2404#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2405/*
2406 * Get a command line with a prompt.
2407 * This is prepared to be called recursively from getcmdline() (e.g. by
2408 * f_input() when evaluating an expression from CTRL-R =).
2409 * Returns the command line in allocated memory, or NULL.
2410 */
2411 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002412getcmdline_prompt(
2413 int firstc,
2414 char_u *prompt, /* command line prompt */
2415 int attr, /* attributes for prompt */
2416 int xp_context, /* type of expansion */
2417 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 char_u *s;
2420 struct cmdline_info save_ccline;
2421 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002422 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002424 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 ccline.cmdprompt = prompt;
2426 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002427# ifdef FEAT_EVAL
2428 ccline.xp_context = xp_context;
2429 ccline.xp_arg = xp_arg;
2430 ccline.input_fn = (firstc == '@');
2431# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002432 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002434 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002435 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002436 /* Restore msg_col, the prompt from input() may have changed it.
2437 * But only if called recursively and the commandline is therefore being
2438 * restored to an old one; if not, the input() prompt stays on the screen,
2439 * so we need its modified msg_col left intact. */
2440 if (ccline.cmdbuff != NULL)
2441 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443 return s;
2444}
2445#endif
2446
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002447/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002448 * Return TRUE when the text must not be changed and we can't switch to
2449 * another window or buffer. Used when editing the command line, evaluating
2450 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002451 */
2452 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002453text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002454{
2455#ifdef FEAT_CMDWIN
2456 if (cmdwin_type != 0)
2457 return TRUE;
2458#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002459 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002460}
2461
2462/*
2463 * Give an error message for a command that isn't allowed while the cmdline
2464 * window is open or editing the cmdline in another way.
2465 */
2466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002467text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002468{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002469 EMSG(_(get_text_locked_msg()));
2470}
2471
2472 char_u *
2473get_text_locked_msg(void)
2474{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002475#ifdef FEAT_CMDWIN
2476 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002477 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002478#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002479 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002480}
2481
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002482/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002483 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2484 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002485 */
2486 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002487curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002488{
2489 if (curbuf_lock > 0)
2490 {
2491 EMSG(_("E788: Not allowed to edit another buffer now"));
2492 return TRUE;
2493 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002494 return allbuf_locked();
2495}
2496
2497/*
2498 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2499 * message.
2500 */
2501 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002502allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002503{
2504 if (allbuf_lock > 0)
2505 {
2506 EMSG(_("E811: Not allowed to change buffer information now"));
2507 return TRUE;
2508 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002509 return FALSE;
2510}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002511
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002513cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2516 if (cmdline_star > 0) /* showing '*', always 1 position */
2517 return 1;
2518#endif
2519 return ptr2cells(ccline.cmdbuff + idx);
2520}
2521
2522/*
2523 * Compute the offset of the cursor on the command line for the prompt and
2524 * indent.
2525 */
2526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002527set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002529 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 ccline.cmdspos = 1 + ccline.cmdindent;
2531 else
2532 ccline.cmdspos = 0 + ccline.cmdindent;
2533}
2534
2535/*
2536 * Compute the screen position for the cursor on the command line.
2537 */
2538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002539set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 int i, m, c;
2542
2543 set_cmdspos();
2544 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002547 if (m < 0) /* overflow, Columns or Rows at weird value */
2548 m = MAXCOL;
2549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 else
2551 m = MAXCOL;
2552 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2553 {
2554 c = cmdline_charsize(i);
2555#ifdef FEAT_MBYTE
2556 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2557 if (has_mbyte)
2558 correct_cmdspos(i, c);
2559#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002560 /* If the cmdline doesn't fit, show cursor on last visible char.
2561 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if ((ccline.cmdspos += c) >= m)
2563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 ccline.cmdspos -= c;
2565 break;
2566 }
2567#ifdef FEAT_MBYTE
2568 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002569 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570#endif
2571 }
2572}
2573
2574#ifdef FEAT_MBYTE
2575/*
2576 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2577 * character that doesn't fit, so that a ">" must be displayed.
2578 */
2579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002580correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002582 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2584 && ccline.cmdspos % Columns + cells > Columns)
2585 ccline.cmdspos++;
2586}
2587#endif
2588
2589/*
2590 * Get an Ex command line for the ":" command.
2591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002593getexline(
2594 int c, /* normally ':', NUL for ":append" */
2595 void *cookie UNUSED,
2596 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 /* When executing a register, remove ':' that's in front of each line. */
2599 if (exec_from_reg && vpeekc() == ':')
2600 (void)vgetc();
2601 return getcmdline(c, 1L, indent);
2602}
2603
2604/*
2605 * Get an Ex command line for Ex mode.
2606 * In Ex mode we only use the OS supplied line editing features and no
2607 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002608 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002611getexmodeline(
2612 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002613 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002614 void *cookie UNUSED,
2615 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002617 garray_T line_ga;
2618 char_u *pend;
2619 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002620 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002621 int escaped = FALSE; /* CTRL-V typed */
2622 int vcol = 0;
2623 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002624 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002625 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 /* Switch cursor on now. This avoids that it happens after the "\n", which
2628 * confuses the system function that computes tabstops. */
2629 cursor_on();
2630
2631 /* always start in column 0; write a newline if necessary */
2632 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002633 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002635 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002637 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638 if (p_prompt)
2639 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 while (indent-- > 0)
2641 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644
2645 ga_init2(&line_ga, 1, 30);
2646
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002647 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002648 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002649 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002650 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002651 while (indent >= 8)
2652 {
2653 ga_append(&line_ga, TAB);
2654 msg_puts((char_u *)" ");
2655 indent -= 8;
2656 }
2657 while (indent-- > 0)
2658 {
2659 ga_append(&line_ga, ' ');
2660 msg_putchar(' ');
2661 }
2662 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002663 ++no_mapping;
2664 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002665
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 /*
2667 * Get the line, one character at a time.
2668 */
2669 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002670 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002672 long sw;
2673 char_u *s;
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (ga_grow(&line_ga, 40) == FAIL)
2676 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
Bram Moolenaarba748c82017-02-26 14:00:07 +01002678 /*
2679 * Get one character at a time.
2680 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002681 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002682
2683 /* Check for a ":normal" command and no more characters left. */
2684 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2685 c1 = '\n';
2686 else
2687 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
2689 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002690 * Handle line editing.
2691 * Previously this was left to the system, putting the terminal in
2692 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002694 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 msg_putchar('\n');
2697 break;
2698 }
2699
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002700 if (c1 == K_PS)
2701 {
2702 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2703 goto redraw;
2704 }
2705
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 if (!escaped)
2707 {
2708 /* CR typed means "enter", which is NL */
2709 if (c1 == '\r')
2710 c1 = '\n';
2711
2712 if (c1 == BS || c1 == K_BS
2713 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 if (line_ga.ga_len > 0)
2716 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002717#ifdef FEAT_MBYTE
2718 if (has_mbyte)
2719 {
2720 p = (char_u *)line_ga.ga_data;
2721 p[line_ga.ga_len] = NUL;
2722 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2723 line_ga.ga_len -= len;
2724 }
2725 else
2726#endif
2727 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 goto redraw;
2729 }
2730 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 msg_col = startcol;
2736 msg_clr_eos();
2737 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002738 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002739 }
2740
2741 if (c1 == Ctrl_T)
2742 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002743 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 p = (char_u *)line_ga.ga_data;
2745 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002746 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002747 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002749 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002751 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002752 p = (char_u *)line_ga.ga_data;
2753 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2755 *s = ' ';
2756 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758redraw:
2759 /* redraw the line */
2760 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002761 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002762 p = (char_u *)line_ga.ga_data;
2763 p[line_ga.ga_len] = NUL;
2764 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002766 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770 msg_putchar(' ');
2771 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002772 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002776 len = MB_PTR2LEN(p);
2777 msg_outtrans_len(p, len);
2778 vcol += ptr2cells(p);
2779 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002782 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002783 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002784 continue;
2785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 if (c1 == Ctrl_D)
2788 {
2789 /* Delete one shiftwidth. */
2790 p = (char_u *)line_ga.ga_data;
2791 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 if (prev_char == '^')
2794 ex_keep_indent = TRUE;
2795 indent = 0;
2796 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 else
2799 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002801 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002802 if (indent > 0)
2803 {
2804 --indent;
2805 indent -= indent % get_sw_value(curbuf);
2806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002808 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002810 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2812 --line_ga.ga_len;
2813 }
2814 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816
2817 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2818 {
2819 escaped = TRUE;
2820 continue;
2821 }
2822
2823 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2824 if (IS_SPECIAL(c1))
2825 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827
2828 if (IS_SPECIAL(c1))
2829 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002830#ifdef FEAT_MBYTE
2831 if (has_mbyte)
2832 len = (*mb_char2bytes)(c1,
2833 (char_u *)line_ga.ga_data + line_ga.ga_len);
2834 else
2835#endif
2836 {
2837 len = 1;
2838 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2839 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002840 if (c1 == '\n')
2841 msg_putchar('\n');
2842 else if (c1 == TAB)
2843 {
2844 /* Don't use chartabsize(), 'ts' can be different */
2845 do
2846 {
2847 msg_putchar(' ');
2848 } while (++vcol % 8);
2849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002853 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002856 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 escaped = FALSE;
2858
2859 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002860 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002862 /* We are done when a NL is entered, but not when it comes after an
2863 * odd number of backslashes, that results in a NUL. */
2864 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002866 int bcount = 0;
2867
2868 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2869 ++bcount;
2870
2871 if (bcount > 0)
2872 {
2873 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2874 * "\NL", etc. */
2875 line_ga.ga_len -= (bcount + 1) / 2;
2876 pend -= (bcount + 1) / 2;
2877 pend[-1] = '\n';
2878 }
2879
2880 if ((bcount & 1) == 0)
2881 {
2882 --line_ga.ga_len;
2883 --pend;
2884 *pend = NUL;
2885 break;
2886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888 }
2889
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002890 --no_mapping;
2891 --allow_keys;
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 /* make following messages go to the next line */
2894 msg_didout = FALSE;
2895 msg_col = 0;
2896 if (msg_row < Rows - 1)
2897 ++msg_row;
2898 emsg_on_display = FALSE; /* don't want ui_delay() */
2899
2900 if (got_int)
2901 ga_clear(&line_ga);
2902
2903 return (char_u *)line_ga.ga_data;
2904}
2905
Bram Moolenaare344bea2005-09-01 20:46:49 +00002906# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2907 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908/*
2909 * Return TRUE if ccline.overstrike is on.
2910 */
2911 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002912cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 return ccline.overstrike;
2915}
2916
2917/*
2918 * Return TRUE if the cursor is at the end of the cmdline.
2919 */
2920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002921cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 return (ccline.cmdpos >= ccline.cmdlen);
2924}
2925#endif
2926
Bram Moolenaar9372a112005-12-06 19:59:18 +00002927#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928/*
2929 * Return the virtual column number at the current cursor position.
2930 * This is used by the IM code to obtain the start of the preedit string.
2931 */
2932 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002933cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2936 return MAXCOL;
2937
2938# ifdef FEAT_MBYTE
2939 if (has_mbyte)
2940 {
2941 colnr_T col;
2942 int i = 0;
2943
2944 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002945 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 return col;
2948 }
2949 else
2950# endif
2951 return ccline.cmdpos;
2952}
2953#endif
2954
2955#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2956/*
2957 * If part of the command line is an IM preedit string, redraw it with
2958 * IM feedback attributes. The cursor position is restored after drawing.
2959 */
2960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002961redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 if ((State & CMDLINE)
2964 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002965 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 && !p_imdisable
2967 && im_is_preediting())
2968 {
2969 int cmdpos = 0;
2970 int cmdspos;
2971 int old_row;
2972 int old_col;
2973 colnr_T col;
2974
2975 old_row = msg_row;
2976 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002977 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979# ifdef FEAT_MBYTE
2980 if (has_mbyte)
2981 {
2982 for (col = 0; col < preedit_start_col
2983 && cmdpos < ccline.cmdlen; ++col)
2984 {
2985 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002986 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
2988 }
2989 else
2990# endif
2991 {
2992 cmdspos += preedit_start_col;
2993 cmdpos += preedit_start_col;
2994 }
2995
2996 msg_row = cmdline_row + (cmdspos / (int)Columns);
2997 msg_col = cmdspos % (int)Columns;
2998 if (msg_row >= Rows)
2999 msg_row = Rows - 1;
3000
3001 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3002 {
3003 int char_len;
3004 int char_attr;
3005
3006 char_attr = im_get_feedback_attr(col);
3007 if (char_attr < 0)
3008 break; /* end of preedit string */
3009
3010# ifdef FEAT_MBYTE
3011 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003012 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 else
3014# endif
3015 char_len = 1;
3016
3017 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3018 cmdpos += char_len;
3019 }
3020
3021 msg_row = old_row;
3022 msg_col = old_col;
3023 }
3024}
3025#endif /* FEAT_XIM && FEAT_GUI_GTK */
3026
3027/*
3028 * Allocate a new command line buffer.
3029 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3030 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3031 */
3032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003033alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 /*
3036 * give some extra space to avoid having to allocate all the time
3037 */
3038 if (len < 80)
3039 len = 100;
3040 else
3041 len += 20;
3042
3043 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3044 ccline.cmdbufflen = len;
3045}
3046
3047/*
3048 * Re-allocate the command line to length len + something extra.
3049 * return FAIL for failure, OK otherwise
3050 */
3051 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003052realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 char_u *p;
3055
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003056 if (len < ccline.cmdbufflen)
3057 return OK; /* no need to resize */
3058
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 p = ccline.cmdbuff;
3060 alloc_cmdbuff(len); /* will get some more */
3061 if (ccline.cmdbuff == NULL) /* out of memory */
3062 {
3063 ccline.cmdbuff = p; /* keep the old one */
3064 return FAIL;
3065 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003066 /* There isn't always a NUL after the command, but it may need to be
3067 * there, thus copy up to the NUL and add a NUL. */
3068 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3069 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003071
3072 if (ccline.xpc != NULL
3073 && ccline.xpc->xp_pattern != NULL
3074 && ccline.xpc->xp_context != EXPAND_NOTHING
3075 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3076 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003077 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003078
3079 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3080 * to point into the newly allocated memory. */
3081 if (i >= 0 && i <= ccline.cmdlen)
3082 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3083 }
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return OK;
3086}
3087
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003088#if defined(FEAT_ARABIC) || defined(PROTO)
3089static char_u *arshape_buf = NULL;
3090
3091# if defined(EXITFREE) || defined(PROTO)
3092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003093free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003094{
3095 vim_free(arshape_buf);
3096}
3097# endif
3098#endif
3099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100/*
3101 * Draw part of the cmdline at the current cursor position. But draw stars
3102 * when cmdline_star is TRUE.
3103 */
3104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003105draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3108 int i;
3109
3110 if (cmdline_star > 0)
3111 for (i = 0; i < len; ++i)
3112 {
3113 msg_putchar('*');
3114# ifdef FEAT_MBYTE
3115 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003116 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117# endif
3118 }
3119 else
3120#endif
3121#ifdef FEAT_ARABIC
3122 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 static int buflen = 0;
3125 char_u *p;
3126 int j;
3127 int newlen = 0;
3128 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003129 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 int prev_c = 0;
3131 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003132 int u8c;
3133 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * Do arabic shaping into a temporary buffer. This is very
3138 * inefficient!
3139 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003140 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
3142 /* Re-allocate the buffer. We keep it around to avoid a lot of
3143 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003144 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003145 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146 arshape_buf = alloc(buflen);
3147 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 return; /* out of memory */
3149 }
3150
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003151 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3152 {
3153 /* Prepend a space to draw the leading composing char on. */
3154 arshape_buf[0] = ' ';
3155 newlen = 1;
3156 }
3157
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 for (j = start; j < start + len; j += mb_l)
3159 {
3160 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003161 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003162 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (ARABIC_CHAR(u8c))
3164 {
3165 /* Do Arabic shaping. */
3166 if (cmdmsg_rl)
3167 {
3168 /* displaying from right to left */
3169 pc = prev_c;
3170 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003171 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (j + mb_l >= start + len)
3173 nc = NUL;
3174 else
3175 nc = utf_ptr2char(p + mb_l);
3176 }
3177 else
3178 {
3179 /* displaying from left to right */
3180 if (j + mb_l >= start + len)
3181 pc = NUL;
3182 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003183 {
3184 int pcc[MAX_MCO];
3185
3186 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003188 pc1 = pcc[0];
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 nc = prev_c;
3191 }
3192 prev_c = u8c;
3193
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003194 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003196 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003197 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003199 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3200 if (u8cc[1] != 0)
3201 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 }
3205 else
3206 {
3207 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003208 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 newlen += mb_l;
3210 }
3211 }
3212
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003213 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
3215 else
3216#endif
3217 msg_outtrans_len(ccline.cmdbuff + start, len);
3218}
3219
3220/*
3221 * Put a character on the command line. Shifts the following text to the
3222 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3223 * "c" must be printable (fit in one display cell)!
3224 */
3225 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003226putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 if (cmd_silent)
3229 return;
3230 msg_no_more = TRUE;
3231 msg_putchar(c);
3232 if (shift)
3233 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3234 msg_no_more = FALSE;
3235 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003236 extra_char = c;
3237 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238}
3239
3240/*
3241 * Undo a putcmdline(c, FALSE).
3242 */
3243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003244unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 if (cmd_silent)
3247 return;
3248 msg_no_more = TRUE;
3249 if (ccline.cmdlen == ccline.cmdpos)
3250 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003251#ifdef FEAT_MBYTE
3252 else if (has_mbyte)
3253 draw_cmdline(ccline.cmdpos,
3254 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 else
3257 draw_cmdline(ccline.cmdpos, 1);
3258 msg_no_more = FALSE;
3259 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003260 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
3263/*
3264 * Put the given string, of the given length, onto the command line.
3265 * If len is -1, then STRLEN() is used to calculate the length.
3266 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3267 * part will be redrawn, otherwise it will not. If this function is called
3268 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3269 * called afterwards.
3270 */
3271 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003272put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 int retval;
3275 int i;
3276 int m;
3277 int c;
3278
3279 if (len < 0)
3280 len = (int)STRLEN(str);
3281
3282 /* Check if ccline.cmdbuff needs to be longer */
3283 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003284 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 else
3286 retval = OK;
3287 if (retval == OK)
3288 {
3289 if (!ccline.overstrike)
3290 {
3291 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3292 ccline.cmdbuff + ccline.cmdpos,
3293 (size_t)(ccline.cmdlen - ccline.cmdpos));
3294 ccline.cmdlen += len;
3295 }
3296 else
3297 {
3298#ifdef FEAT_MBYTE
3299 if (has_mbyte)
3300 {
3301 /* Count nr of characters in the new string. */
3302 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003303 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 ++m;
3305 /* Count nr of bytes in cmdline that are overwritten by these
3306 * characters. */
3307 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003308 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 --m;
3310 if (i < ccline.cmdlen)
3311 {
3312 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3313 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3314 ccline.cmdlen += ccline.cmdpos + len - i;
3315 }
3316 else
3317 ccline.cmdlen = ccline.cmdpos + len;
3318 }
3319 else
3320#endif
3321 if (ccline.cmdpos + len > ccline.cmdlen)
3322 ccline.cmdlen = ccline.cmdpos + len;
3323 }
3324 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3325 ccline.cmdbuff[ccline.cmdlen] = NUL;
3326
3327#ifdef FEAT_MBYTE
3328 if (enc_utf8)
3329 {
3330 /* When the inserted text starts with a composing character,
3331 * backup to the character before it. There could be two of them.
3332 */
3333 i = 0;
3334 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3335 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3336 {
3337 i = (*mb_head_off)(ccline.cmdbuff,
3338 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3339 ccline.cmdpos -= i;
3340 len += i;
3341 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3342 }
3343# ifdef FEAT_ARABIC
3344 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3345 {
3346 /* Check the previous character for Arabic combining pair. */
3347 i = (*mb_head_off)(ccline.cmdbuff,
3348 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3349 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3350 + ccline.cmdpos - i), c))
3351 {
3352 ccline.cmdpos -= i;
3353 len += i;
3354 }
3355 else
3356 i = 0;
3357 }
3358# endif
3359 if (i != 0)
3360 {
3361 /* Also backup the cursor position. */
3362 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3363 ccline.cmdspos -= i;
3364 msg_col -= i;
3365 if (msg_col < 0)
3366 {
3367 msg_col += Columns;
3368 --msg_row;
3369 }
3370 }
3371 }
3372#endif
3373
3374 if (redraw && !cmd_silent)
3375 {
3376 msg_no_more = TRUE;
3377 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003378 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3380 /* Avoid clearing the rest of the line too often. */
3381 if (cmdline_row != i || ccline.overstrike)
3382 msg_clr_eos();
3383 msg_no_more = FALSE;
3384 }
3385#ifdef FEAT_FKMAP
3386 /*
3387 * If we are in Farsi command mode, the character input must be in
3388 * Insert mode. So do not advance the cmdpos.
3389 */
3390 if (!cmd_fkmap)
3391#endif
3392 {
3393 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003396 if (m < 0) /* overflow, Columns or Rows at weird value */
3397 m = MAXCOL;
3398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
3400 m = MAXCOL;
3401 for (i = 0; i < len; ++i)
3402 {
3403 c = cmdline_charsize(ccline.cmdpos);
3404#ifdef FEAT_MBYTE
3405 /* count ">" for a double-wide char that doesn't fit. */
3406 if (has_mbyte)
3407 correct_cmdspos(ccline.cmdpos, c);
3408#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003409 /* Stop cursor at the end of the screen, but do increment the
3410 * insert position, so that entering a very long command
3411 * works, even though you can't see it. */
3412 if (ccline.cmdspos + c < m)
3413 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#ifdef FEAT_MBYTE
3415 if (has_mbyte)
3416 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003417 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (c > len - i - 1)
3419 c = len - i - 1;
3420 ccline.cmdpos += c;
3421 i += c;
3422 }
3423#endif
3424 ++ccline.cmdpos;
3425 }
3426 }
3427 }
3428 if (redraw)
3429 msg_check();
3430 return retval;
3431}
3432
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003433static struct cmdline_info prev_ccline;
3434static int prev_ccline_used = FALSE;
3435
3436/*
3437 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3438 * and overwrite it. But get_cmdline_str() may need it, thus make it
3439 * available globally in prev_ccline.
3440 */
3441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003442save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003443{
3444 if (!prev_ccline_used)
3445 {
3446 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3447 prev_ccline_used = TRUE;
3448 }
3449 *ccp = prev_ccline;
3450 prev_ccline = ccline;
3451 ccline.cmdbuff = NULL;
3452 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003453 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003454}
3455
3456/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003457 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003458 */
3459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003460restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003461{
3462 ccline = prev_ccline;
3463 prev_ccline = *ccp;
3464}
3465
Bram Moolenaar5a305422006-04-28 22:38:25 +00003466#if defined(FEAT_EVAL) || defined(PROTO)
3467/*
3468 * Save the command line into allocated memory. Returns a pointer to be
3469 * passed to restore_cmdline_alloc() later.
3470 * Returns NULL when failed.
3471 */
3472 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003473save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003474{
3475 struct cmdline_info *p;
3476
3477 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3478 if (p != NULL)
3479 save_cmdline(p);
3480 return (char_u *)p;
3481}
3482
3483/*
3484 * Restore the command line from the return value of save_cmdline_alloc().
3485 */
3486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003487restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003488{
3489 if (p != NULL)
3490 {
3491 restore_cmdline((struct cmdline_info *)p);
3492 vim_free(p);
3493 }
3494}
3495#endif
3496
Bram Moolenaar8299df92004-07-10 09:47:34 +00003497/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003498 * Paste a yank register into the command line.
3499 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003500 * insert_reg() can't be used here, because special characters from the
3501 * register contents will be interpreted as commands.
3502 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003503 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003504 */
3505 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003506cmdline_paste(
3507 int regname,
3508 int literally, /* Insert text literally instead of "as typed" */
3509 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003510{
3511 long i;
3512 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003513 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003514 int allocated;
3515 struct cmdline_info save_ccline;
3516
3517 /* check for valid regname; also accept special characters for CTRL-R in
3518 * the command line */
3519 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003520 && regname != Ctrl_A && regname != Ctrl_L
3521 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003522 return FAIL;
3523
3524 /* A register containing CTRL-R can cause an endless loop. Allow using
3525 * CTRL-C to break the loop. */
3526 line_breakcheck();
3527 if (got_int)
3528 return FAIL;
3529
3530#ifdef FEAT_CLIPBOARD
3531 regname = may_get_selection(regname);
3532#endif
3533
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003534 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003535 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003536 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003537 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003538 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003539 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003540 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541
3542 if (i)
3543 {
3544 /* Got the value of a special register in "arg". */
3545 if (arg == NULL)
3546 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003547
3548 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3549 * part of the word. */
3550 p = arg;
3551 if (p_is && regname == Ctrl_W)
3552 {
3553 char_u *w;
3554 int len;
3555
3556 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003557 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003558 {
3559#ifdef FEAT_MBYTE
3560 if (has_mbyte)
3561 {
3562 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3563 if (!vim_iswordc(mb_ptr2char(w - len)))
3564 break;
3565 w -= len;
3566 }
3567 else
3568#endif
3569 {
3570 if (!vim_iswordc(w[-1]))
3571 break;
3572 --w;
3573 }
3574 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003575 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003576 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3577 p += len;
3578 }
3579
3580 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581 if (allocated)
3582 vim_free(arg);
3583 return OK;
3584 }
3585
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003586 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003587}
3588
3589/*
3590 * Put a string on the command line.
3591 * When "literally" is TRUE, insert literally.
3592 * When "literally" is FALSE, insert as typed, but don't leave the command
3593 * line.
3594 */
3595 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003596cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597{
3598 int c, cv;
3599
3600 if (literally)
3601 put_on_cmdline(s, -1, TRUE);
3602 else
3603 while (*s != NUL)
3604 {
3605 cv = *s;
3606 if (cv == Ctrl_V && s[1])
3607 ++s;
3608#ifdef FEAT_MBYTE
3609 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003610 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003611 else
3612#endif
3613 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003614 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3615 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003616#ifdef UNIX
3617 || c == intr_char
3618#endif
3619 || (c == Ctrl_BSL && *s == Ctrl_N))
3620 stuffcharReadbuff(Ctrl_V);
3621 stuffcharReadbuff(c);
3622 }
3623}
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625#ifdef FEAT_WILDMENU
3626/*
3627 * Delete characters on the command line, from "from" to the current
3628 * position.
3629 */
3630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003631cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3634 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3635 ccline.cmdlen -= ccline.cmdpos - from;
3636 ccline.cmdpos = from;
3637}
3638#endif
3639
3640/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003641 * This function is called when the screen size changes and with incremental
3642 * search and in other situations where the command line may have been
3643 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
3645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003646redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003648 redrawcmdline_ex(TRUE);
3649}
3650
3651 void
3652redrawcmdline_ex(int do_compute_cmdrow)
3653{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (cmd_silent)
3655 return;
3656 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003657 if (do_compute_cmdrow)
3658 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 redrawcmd();
3660 cursorcmd();
3661}
3662
3663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003664redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 int i;
3667
3668 if (cmd_silent)
3669 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003670 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 msg_putchar(ccline.cmdfirstc);
3672 if (ccline.cmdprompt != NULL)
3673 {
3674 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3675 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3676 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003677 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 --ccline.cmdindent;
3679 }
3680 else
3681 for (i = ccline.cmdindent; i > 0; --i)
3682 msg_putchar(' ');
3683}
3684
3685/*
3686 * Redraw what is currently on the command line.
3687 */
3688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003689redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 if (cmd_silent)
3692 return;
3693
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003694 /* when 'incsearch' is set there may be no command line while redrawing */
3695 if (ccline.cmdbuff == NULL)
3696 {
3697 windgoto(cmdline_row, 0);
3698 msg_clr_eos();
3699 return;
3700 }
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 msg_start();
3703 redrawcmdprompt();
3704
3705 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3706 msg_no_more = TRUE;
3707 draw_cmdline(0, ccline.cmdlen);
3708 msg_clr_eos();
3709 msg_no_more = FALSE;
3710
3711 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003712 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003713 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715 /*
3716 * An emsg() before may have set msg_scroll. This is used in normal mode,
3717 * in cmdline mode we can reset them now.
3718 */
3719 msg_scroll = FALSE; /* next message overwrites cmdline */
3720
3721 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3722 * in cmdline mode */
3723 skip_redraw = FALSE;
3724}
3725
3726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003727compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003729 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 cmdline_row = Rows - 1;
3731 else
3732 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003733 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734}
3735
3736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003737cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 if (cmd_silent)
3740 return;
3741
3742#ifdef FEAT_RIGHTLEFT
3743 if (cmdmsg_rl)
3744 {
3745 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3746 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3747 if (msg_row <= 0)
3748 msg_row = Rows - 1;
3749 }
3750 else
3751#endif
3752 {
3753 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3754 msg_col = ccline.cmdspos % (int)Columns;
3755 if (msg_row >= Rows)
3756 msg_row = Rows - 1;
3757 }
3758
3759 windgoto(msg_row, msg_col);
3760#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003761 if (p_imst == IM_ON_THE_SPOT)
3762 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#endif
3764#ifdef MCH_CURSOR_SHAPE
3765 mch_update_cursor();
3766#endif
3767}
3768
3769 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003770gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
3772 msg_start();
3773#ifdef FEAT_RIGHTLEFT
3774 if (cmdmsg_rl)
3775 msg_col = Columns - 1;
3776 else
3777#endif
3778 msg_col = 0; /* always start in column 0 */
3779 if (clr) /* clear the bottom line(s) */
3780 msg_clr_eos(); /* will reset clear_cmdline */
3781 windgoto(cmdline_row, 0);
3782}
3783
3784/*
3785 * Check the word in front of the cursor for an abbreviation.
3786 * Called when the non-id character "c" has been entered.
3787 * When an abbreviation is recognized it is removed from the text with
3788 * backspaces and the replacement string is inserted, followed by "c".
3789 */
3790 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003791ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003793 int spos = 0;
3794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3796 return FALSE;
3797
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003798 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3799 * Actually accepts any mark. */
3800 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3801 spos++;
3802 if (ccline.cmdlen - spos > 5
3803 && ccline.cmdbuff[spos] == '\''
3804 && ccline.cmdbuff[spos + 2] == ','
3805 && ccline.cmdbuff[spos + 3] == '\'')
3806 spos += 5;
3807 else
3808 /* check abbreviation from the beginning of the commandline */
3809 spos = 0;
3810
3811 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812}
3813
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003814#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3815 static int
3816#ifdef __BORLANDC__
3817_RTLENTRYF
3818#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003819sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003820{
3821 char_u *p1 = *(char_u **)s1;
3822 char_u *p2 = *(char_u **)s2;
3823
3824 if (*p1 != '<' && *p2 == '<') return -1;
3825 if (*p1 == '<' && *p2 != '<') return 1;
3826 return STRCMP(p1, p2);
3827}
3828#endif
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830/*
3831 * Return FAIL if this is not an appropriate context in which to do
3832 * completion of anything, return OK if it is (even if there are no matches).
3833 * For the caller, this means that the character is just passed through like a
3834 * normal character (instead of being expanded). This allows :s/^I^D etc.
3835 */
3836 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003837nextwild(
3838 expand_T *xp,
3839 int type,
3840 int options, /* extra options for ExpandOne() */
3841 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
3843 int i, j;
3844 char_u *p1;
3845 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 int difflen;
3847 int v;
3848
3849 if (xp->xp_numfiles == -1)
3850 {
3851 set_expand_context(xp);
3852 cmd_showtail = expand_showtail(xp);
3853 }
3854
3855 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3856 {
3857 beep_flush();
3858 return OK; /* Something illegal on command line */
3859 }
3860 if (xp->xp_context == EXPAND_NOTHING)
3861 {
3862 /* Caller can use the character as a normal char instead */
3863 return FAIL;
3864 }
3865
3866 MSG_PUTS("..."); /* show that we are busy */
3867 out_flush();
3868
3869 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003870 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872 if (type == WILD_NEXT || type == WILD_PREV)
3873 {
3874 /*
3875 * Get next/previous match for a previous expanded pattern.
3876 */
3877 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3878 }
3879 else
3880 {
3881 /*
3882 * Translate string into pattern and expand it.
3883 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003884 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3885 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 p2 = NULL;
3887 else
3888 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003889 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003890 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3891 if (escape)
3892 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003893
3894 if (p_wic)
3895 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003896 p2 = ExpandOne(xp, p1,
3897 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003898 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003900 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (p2 != NULL && type == WILD_LONGEST)
3902 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003903 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (ccline.cmdbuff[i + j] == '*'
3905 || ccline.cmdbuff[i + j] == '?')
3906 break;
3907 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003908 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 }
3911 }
3912
3913 if (p2 != NULL && !got_int)
3914 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003915 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003916 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003918 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 xp->xp_pattern = ccline.cmdbuff + i;
3920 }
3921 else
3922 v = OK;
3923 if (v == OK)
3924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3926 &ccline.cmdbuff[ccline.cmdpos],
3927 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3928 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 ccline.cmdlen += difflen;
3930 ccline.cmdpos += difflen;
3931 }
3932 }
3933 vim_free(p2);
3934
3935 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003936 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 /* When expanding a ":map" command and no matches are found, assume that
3939 * the key is supposed to be inserted literally */
3940 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3941 return FAIL;
3942
3943 if (xp->xp_numfiles <= 0 && p2 == NULL)
3944 beep_flush();
3945 else if (xp->xp_numfiles == 1)
3946 /* free expanded pattern */
3947 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3948
3949 return OK;
3950}
3951
3952/*
3953 * Do wildcard expansion on the string 'str'.
3954 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003955 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * Return NULL for failure.
3957 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003958 * "orig" is the originally expanded string, copied to allocated memory. It
3959 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3960 * WILD_PREV "orig" should be NULL.
3961 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003962 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3963 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 *
3965 * mode = WILD_FREE: just free previously expanded matches
3966 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3967 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3968 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3969 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3970 * mode = WILD_ALL: return all matches concatenated
3971 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003972 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *
3974 * options = WILD_LIST_NOTFOUND: list entries without a match
3975 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3976 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3977 * options = WILD_NO_BEEP: Don't beep for multiple matches
3978 * options = WILD_ADD_SLASH: add a slash after directory names
3979 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3980 * options = WILD_SILENT: don't print warning messages
3981 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003982 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 *
3984 * The variables xp->xp_context and xp->xp_backslash must have been set!
3985 */
3986 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003987ExpandOne(
3988 expand_T *xp,
3989 char_u *str,
3990 char_u *orig, /* allocated copy of original of expanded string */
3991 int options,
3992 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
3994 char_u *ss = NULL;
3995 static int findex;
3996 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003997 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 int i;
3999 long_u len;
4000 int non_suf_match; /* number without matching suffix */
4001
4002 /*
4003 * first handle the case of using an old match
4004 */
4005 if (mode == WILD_NEXT || mode == WILD_PREV)
4006 {
4007 if (xp->xp_numfiles > 0)
4008 {
4009 if (mode == WILD_PREV)
4010 {
4011 if (findex == -1)
4012 findex = xp->xp_numfiles;
4013 --findex;
4014 }
4015 else /* mode == WILD_NEXT */
4016 ++findex;
4017
4018 /*
4019 * When wrapping around, return the original string, set findex to
4020 * -1.
4021 */
4022 if (findex < 0)
4023 {
4024 if (orig_save == NULL)
4025 findex = xp->xp_numfiles - 1;
4026 else
4027 findex = -1;
4028 }
4029 if (findex >= xp->xp_numfiles)
4030 {
4031 if (orig_save == NULL)
4032 findex = 0;
4033 else
4034 findex = -1;
4035 }
4036#ifdef FEAT_WILDMENU
4037 if (p_wmnu)
4038 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4039 findex, cmd_showtail);
4040#endif
4041 if (findex == -1)
4042 return vim_strsave(orig_save);
4043 return vim_strsave(xp->xp_files[findex]);
4044 }
4045 else
4046 return NULL;
4047 }
4048
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004049 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4051 {
4052 FreeWild(xp->xp_numfiles, xp->xp_files);
4053 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004054 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
4056 findex = 0;
4057
4058 if (mode == WILD_FREE) /* only release file name */
4059 return NULL;
4060
4061 if (xp->xp_numfiles == -1)
4062 {
4063 vim_free(orig_save);
4064 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004065 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 /*
4068 * Do the expansion.
4069 */
4070 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4071 options) == FAIL)
4072 {
4073#ifdef FNAME_ILLEGAL
4074 /* Illegal file name has been silently skipped. But when there
4075 * are wildcards, the real problem is that there was no match,
4076 * causing the pattern to be added, which has illegal characters.
4077 */
4078 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4079 EMSG2(_(e_nomatch2), str);
4080#endif
4081 }
4082 else if (xp->xp_numfiles == 0)
4083 {
4084 if (!(options & WILD_SILENT))
4085 EMSG2(_(e_nomatch2), str);
4086 }
4087 else
4088 {
4089 /* Escape the matches for use on the command line. */
4090 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4091
4092 /*
4093 * Check for matching suffixes in file names.
4094 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004095 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4096 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
4098 if (xp->xp_numfiles)
4099 non_suf_match = xp->xp_numfiles;
4100 else
4101 non_suf_match = 1;
4102 if ((xp->xp_context == EXPAND_FILES
4103 || xp->xp_context == EXPAND_DIRECTORIES)
4104 && xp->xp_numfiles > 1)
4105 {
4106 /*
4107 * More than one match; check suffix.
4108 * The files will have been sorted on matching suffix in
4109 * expand_wildcards, only need to check the first two.
4110 */
4111 non_suf_match = 0;
4112 for (i = 0; i < 2; ++i)
4113 if (match_suffix(xp->xp_files[i]))
4114 ++non_suf_match;
4115 }
4116 if (non_suf_match != 1)
4117 {
4118 /* Can we ever get here unless it's while expanding
4119 * interactively? If not, we can get rid of this all
4120 * together. Don't really want to wait for this message
4121 * (and possibly have to hit return to continue!).
4122 */
4123 if (!(options & WILD_SILENT))
4124 EMSG(_(e_toomany));
4125 else if (!(options & WILD_NO_BEEP))
4126 beep_flush();
4127 }
4128 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4129 ss = vim_strsave(xp->xp_files[0]);
4130 }
4131 }
4132 }
4133
4134 /* Find longest common part */
4135 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4136 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004137 int mb_len = 1;
4138 int c0, ci;
4139
4140 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004142#ifdef FEAT_MBYTE
4143 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004145 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4146 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4147 }
4148 else
4149#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004150 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004151 for (i = 1; i < xp->xp_numfiles; ++i)
4152 {
4153#ifdef FEAT_MBYTE
4154 if (has_mbyte)
4155 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4156 else
4157#endif
4158 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004159 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004161 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004162 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004164 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 break;
4166 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004167 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 break;
4169 }
4170 if (i < xp->xp_numfiles)
4171 {
4172 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004173 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 break;
4175 }
4176 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 ss = alloc((unsigned)len + 1);
4179 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004180 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 findex = -1; /* next p_wc gets first one */
4182 }
4183
4184 /* Concatenate all matching names */
4185 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4186 {
4187 len = 0;
4188 for (i = 0; i < xp->xp_numfiles; ++i)
4189 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4190 ss = lalloc(len, TRUE);
4191 if (ss != NULL)
4192 {
4193 *ss = NUL;
4194 for (i = 0; i < xp->xp_numfiles; ++i)
4195 {
4196 STRCAT(ss, xp->xp_files[i]);
4197 if (i != xp->xp_numfiles - 1)
4198 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4199 }
4200 }
4201 }
4202
4203 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4204 ExpandCleanup(xp);
4205
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004206 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004207 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004208 vim_free(orig);
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return ss;
4211}
4212
4213/*
4214 * Prepare an expand structure for use.
4215 */
4216 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004217ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004219 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004220 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004222#ifndef BACKSLASH_IN_FILENAME
4223 xp->xp_shell = FALSE;
4224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 xp->xp_numfiles = -1;
4226 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004227#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4228 xp->xp_arg = NULL;
4229#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004230 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
4233/*
4234 * Cleanup an expand structure after use.
4235 */
4236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004237ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239 if (xp->xp_numfiles >= 0)
4240 {
4241 FreeWild(xp->xp_numfiles, xp->xp_files);
4242 xp->xp_numfiles = -1;
4243 }
4244}
4245
4246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004247ExpandEscape(
4248 expand_T *xp,
4249 char_u *str,
4250 int numfiles,
4251 char_u **files,
4252 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 int i;
4255 char_u *p;
4256
4257 /*
4258 * May change home directory back to "~"
4259 */
4260 if (options & WILD_HOME_REPLACE)
4261 tilde_replace(str, numfiles, files);
4262
4263 if (options & WILD_ESCAPE)
4264 {
4265 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004266 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004267 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 || xp->xp_context == EXPAND_BUFFERS
4269 || xp->xp_context == EXPAND_DIRECTORIES)
4270 {
4271 /*
4272 * Insert a backslash into a file name before a space, \, %, #
4273 * and wildmatch characters, except '~'.
4274 */
4275 for (i = 0; i < numfiles; ++i)
4276 {
4277 /* for ":set path=" we need to escape spaces twice */
4278 if (xp->xp_backslash == XP_BS_THREE)
4279 {
4280 p = vim_strsave_escaped(files[i], (char_u *)" ");
4281 if (p != NULL)
4282 {
4283 vim_free(files[i]);
4284 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004285#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 p = vim_strsave_escaped(files[i], (char_u *)" ");
4287 if (p != NULL)
4288 {
4289 vim_free(files[i]);
4290 files[i] = p;
4291 }
4292#endif
4293 }
4294 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004295#ifdef BACKSLASH_IN_FILENAME
4296 p = vim_strsave_fnameescape(files[i], FALSE);
4297#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004298 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (p != NULL)
4301 {
4302 vim_free(files[i]);
4303 files[i] = p;
4304 }
4305
4306 /* If 'str' starts with "\~", replace "~" at start of
4307 * files[i] with "\~". */
4308 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004309 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004312
4313 /* If the first file starts with a '+' escape it. Otherwise it
4314 * could be seen as "+cmd". */
4315 if (*files[0] == '+')
4316 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318 else if (xp->xp_context == EXPAND_TAGS)
4319 {
4320 /*
4321 * Insert a backslash before characters in a tag name that
4322 * would terminate the ":tag" command.
4323 */
4324 for (i = 0; i < numfiles; ++i)
4325 {
4326 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4327 if (p != NULL)
4328 {
4329 vim_free(files[i]);
4330 files[i] = p;
4331 }
4332 }
4333 }
4334 }
4335}
4336
4337/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004338 * Escape special characters in "fname" for when used as a file name argument
4339 * after a Vim command, or, when "shell" is non-zero, a shell command.
4340 * Returns the result in allocated memory.
4341 */
4342 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004343vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004344{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004345 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004346#ifdef BACKSLASH_IN_FILENAME
4347 char_u buf[20];
4348 int j = 0;
4349
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004350 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004351 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004352 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004353 buf[j++] = *p;
4354 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004355 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004356#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004357 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4358 if (shell && csh_like_shell() && p != NULL)
4359 {
4360 char_u *s;
4361
4362 /* For csh and similar shells need to put two backslashes before '!'.
4363 * One is taken by Vim, one by the shell. */
4364 s = vim_strsave_escaped(p, (char_u *)"!");
4365 vim_free(p);
4366 p = s;
4367 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004368#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004369
4370 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4371 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004372 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004373 escape_fname(&p);
4374
4375 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004376}
4377
4378/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004379 * Put a backslash before the file name in "pp", which is in allocated memory.
4380 */
4381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004382escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004383{
4384 char_u *p;
4385
4386 p = alloc((unsigned)(STRLEN(*pp) + 2));
4387 if (p != NULL)
4388 {
4389 p[0] = '\\';
4390 STRCPY(p + 1, *pp);
4391 vim_free(*pp);
4392 *pp = p;
4393 }
4394}
4395
4396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 * For each file name in files[num_files]:
4398 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4399 */
4400 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004401tilde_replace(
4402 char_u *orig_pat,
4403 int num_files,
4404 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
4406 int i;
4407 char_u *p;
4408
4409 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4410 {
4411 for (i = 0; i < num_files; ++i)
4412 {
4413 p = home_replace_save(NULL, files[i]);
4414 if (p != NULL)
4415 {
4416 vim_free(files[i]);
4417 files[i] = p;
4418 }
4419 }
4420 }
4421}
4422
4423/*
4424 * Show all matches for completion on the command line.
4425 * Returns EXPAND_NOTHING when the character that triggered expansion should
4426 * be inserted like a normal character.
4427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004429showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
4431#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4432 int num_files;
4433 char_u **files_found;
4434 int i, j, k;
4435 int maxlen;
4436 int lines;
4437 int columns;
4438 char_u *p;
4439 int lastlen;
4440 int attr;
4441 int showtail;
4442
4443 if (xp->xp_numfiles == -1)
4444 {
4445 set_expand_context(xp);
4446 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4447 &num_files, &files_found);
4448 showtail = expand_showtail(xp);
4449 if (i != EXPAND_OK)
4450 return i;
4451
4452 }
4453 else
4454 {
4455 num_files = xp->xp_numfiles;
4456 files_found = xp->xp_files;
4457 showtail = cmd_showtail;
4458 }
4459
4460#ifdef FEAT_WILDMENU
4461 if (!wildmenu)
4462 {
4463#endif
4464 msg_didany = FALSE; /* lines_left will be set */
4465 msg_start(); /* prepare for paging */
4466 msg_putchar('\n');
4467 out_flush();
4468 cmdline_row = msg_row;
4469 msg_didany = FALSE; /* lines_left will be set again */
4470 msg_start(); /* prepare for paging */
4471#ifdef FEAT_WILDMENU
4472 }
4473#endif
4474
4475 if (got_int)
4476 got_int = FALSE; /* only int. the completion, not the cmd line */
4477#ifdef FEAT_WILDMENU
4478 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004479 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480#endif
4481 else
4482 {
4483 /* find the length of the longest file name */
4484 maxlen = 0;
4485 for (i = 0; i < num_files; ++i)
4486 {
4487 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004488 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 || xp->xp_context == EXPAND_BUFFERS))
4490 {
4491 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4492 j = vim_strsize(NameBuff);
4493 }
4494 else
4495 j = vim_strsize(L_SHOWFILE(i));
4496 if (j > maxlen)
4497 maxlen = j;
4498 }
4499
4500 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4501 lines = num_files;
4502 else
4503 {
4504 /* compute the number of columns and lines for the listing */
4505 maxlen += 2; /* two spaces between file names */
4506 columns = ((int)Columns + 2) / maxlen;
4507 if (columns < 1)
4508 columns = 1;
4509 lines = (num_files + columns - 1) / columns;
4510 }
4511
Bram Moolenaar8820b482017-03-16 17:23:31 +01004512 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4515 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004516 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 msg_clr_eos();
4518 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004519 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521
4522 /* list the files line by line */
4523 for (i = 0; i < lines; ++i)
4524 {
4525 lastlen = 999;
4526 for (k = i; k < num_files; k += lines)
4527 {
4528 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4529 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004530 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 p = files_found[k] + STRLEN(files_found[k]) + 1;
4532 msg_advance(maxlen + 1);
4533 msg_puts(p);
4534 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004535 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 break;
4537 }
4538 for (j = maxlen - lastlen; --j >= 0; )
4539 msg_putchar(' ');
4540 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004541 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 || xp->xp_context == EXPAND_BUFFERS)
4543 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004544 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004545 if (xp->xp_numfiles != -1)
4546 {
4547 char_u *halved_slash;
4548 char_u *exp_path;
4549
4550 /* Expansion was done before and special characters
4551 * were escaped, need to halve backslashes. Also
4552 * $HOME has been replaced with ~/. */
4553 exp_path = expand_env_save_opt(files_found[k], TRUE);
4554 halved_slash = backslash_halve_save(
4555 exp_path != NULL ? exp_path : files_found[k]);
4556 j = mch_isdir(halved_slash != NULL ? halved_slash
4557 : files_found[k]);
4558 vim_free(exp_path);
4559 vim_free(halved_slash);
4560 }
4561 else
4562 /* Expansion was done here, file names are literal. */
4563 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (showtail)
4565 p = L_SHOWFILE(k);
4566 else
4567 {
4568 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4569 TRUE);
4570 p = NameBuff;
4571 }
4572 }
4573 else
4574 {
4575 j = FALSE;
4576 p = L_SHOWFILE(k);
4577 }
4578 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4579 }
4580 if (msg_col > 0) /* when not wrapped around */
4581 {
4582 msg_clr_eos();
4583 msg_putchar('\n');
4584 }
4585 out_flush(); /* show one line at a time */
4586 if (got_int)
4587 {
4588 got_int = FALSE;
4589 break;
4590 }
4591 }
4592
4593 /*
4594 * we redraw the command below the lines that we have just listed
4595 * This is a bit tricky, but it saves a lot of screen updating.
4596 */
4597 cmdline_row = msg_row; /* will put it back later */
4598 }
4599
4600 if (xp->xp_numfiles == -1)
4601 FreeWild(num_files, files_found);
4602
4603 return EXPAND_OK;
4604}
4605
4606/*
4607 * Private gettail for showmatches() (and win_redr_status_matches()):
4608 * Find tail of file name path, but ignore trailing "/".
4609 */
4610 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004611sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
4613 char_u *p;
4614 char_u *t = s;
4615 int had_sep = FALSE;
4616
4617 for (p = s; *p != NUL; )
4618 {
4619 if (vim_ispathsep(*p)
4620#ifdef BACKSLASH_IN_FILENAME
4621 && !rem_backslash(p)
4622#endif
4623 )
4624 had_sep = TRUE;
4625 else if (had_sep)
4626 {
4627 t = p;
4628 had_sep = FALSE;
4629 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004630 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 return t;
4633}
4634
4635/*
4636 * Return TRUE if we only need to show the tail of completion matches.
4637 * When not completing file names or there is a wildcard in the path FALSE is
4638 * returned.
4639 */
4640 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004641expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 char_u *s;
4644 char_u *end;
4645
4646 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004647 if (xp->xp_context != EXPAND_FILES
4648 && xp->xp_context != EXPAND_SHELLCMD
4649 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return FALSE;
4651
4652 end = gettail(xp->xp_pattern);
4653 if (end == xp->xp_pattern) /* there is no path separator */
4654 return FALSE;
4655
4656 for (s = xp->xp_pattern; s < end; s++)
4657 {
4658 /* Skip escaped wildcards. Only when the backslash is not a path
4659 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4660 if (rem_backslash(s))
4661 ++s;
4662 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4663 return FALSE;
4664 }
4665 return TRUE;
4666}
4667
4668/*
4669 * Prepare a string for expansion.
4670 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004671 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 * When expanding other names: The string will be used with regcomp(). Copy
4673 * the name into allocated memory and prepend "^".
4674 */
4675 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004676addstar(
4677 char_u *fname,
4678 int len,
4679 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680{
4681 char_u *retval;
4682 int i, j;
4683 int new_len;
4684 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004685 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004687 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004688 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004689 && context != EXPAND_SHELLCMD
4690 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 /*
4693 * Matching will be done internally (on something other than files).
4694 * So we convert the file-matching-type wildcards into our kind for
4695 * use with vim_regcomp(). First work out how long it will be:
4696 */
4697
4698 /* For help tags the translation is done in find_help_tags().
4699 * For a tag pattern starting with "/" no translation is needed. */
4700 if (context == EXPAND_HELP
4701 || context == EXPAND_COLORS
4702 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004703 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004704 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004705 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004706 || ((context == EXPAND_TAGS_LISTFILES
4707 || context == EXPAND_TAGS)
4708 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 retval = vim_strnsave(fname, len);
4710 else
4711 {
4712 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4713 for (i = 0; i < len; i++)
4714 {
4715 if (fname[i] == '*' || fname[i] == '~')
4716 new_len++; /* '*' needs to be replaced by ".*"
4717 '~' needs to be replaced by "\~" */
4718
4719 /* Buffer names are like file names. "." should be literal */
4720 if (context == EXPAND_BUFFERS && fname[i] == '.')
4721 new_len++; /* "." becomes "\." */
4722
4723 /* Custom expansion takes care of special things, match
4724 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004725 if ((context == EXPAND_USER_DEFINED
4726 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 new_len++; /* '\' becomes "\\" */
4728 }
4729 retval = alloc(new_len);
4730 if (retval != NULL)
4731 {
4732 retval[0] = '^';
4733 j = 1;
4734 for (i = 0; i < len; i++, j++)
4735 {
4736 /* Skip backslash. But why? At least keep it for custom
4737 * expansion. */
4738 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004739 && context != EXPAND_USER_LIST
4740 && fname[i] == '\\'
4741 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 break;
4743
4744 switch (fname[i])
4745 {
4746 case '*': retval[j++] = '.';
4747 break;
4748 case '~': retval[j++] = '\\';
4749 break;
4750 case '?': retval[j] = '.';
4751 continue;
4752 case '.': if (context == EXPAND_BUFFERS)
4753 retval[j++] = '\\';
4754 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004755 case '\\': if (context == EXPAND_USER_DEFINED
4756 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 retval[j++] = '\\';
4758 break;
4759 }
4760 retval[j] = fname[i];
4761 }
4762 retval[j] = NUL;
4763 }
4764 }
4765 }
4766 else
4767 {
4768 retval = alloc(len + 4);
4769 if (retval != NULL)
4770 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004771 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004774 * Don't add a star to *, ~, ~user, $var or `cmd`.
4775 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 * ~ would be at the start of the file name, but not the tail.
4777 * $ could be anywhere in the tail.
4778 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004779 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 */
4781 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004782 ends_in_star = (len > 0 && retval[len - 1] == '*');
4783#ifndef BACKSLASH_IN_FILENAME
4784 for (i = len - 2; i >= 0; --i)
4785 {
4786 if (retval[i] != '\\')
4787 break;
4788 ends_in_star = !ends_in_star;
4789 }
4790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004792 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 && vim_strchr(tail, '$') == NULL
4794 && vim_strchr(retval, '`') == NULL)
4795 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004796 else if (len > 0 && retval[len - 1] == '$')
4797 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 retval[len] = NUL;
4799 }
4800 }
4801 return retval;
4802}
4803
4804/*
4805 * Must parse the command line so far to work out what context we are in.
4806 * Completion can then be done based on that context.
4807 * This routine sets the variables:
4808 * xp->xp_pattern The start of the pattern to be expanded within
4809 * the command line (ends at the cursor).
4810 * xp->xp_context The type of thing to expand. Will be one of:
4811 *
4812 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4813 * the command line, like an unknown command. Caller
4814 * should beep.
4815 * EXPAND_NOTHING Unrecognised context for completion, use char like
4816 * a normal char, rather than for completion. eg
4817 * :s/^I/
4818 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4819 * it.
4820 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4821 * EXPAND_FILES After command with XFILE set, or after setting
4822 * with P_EXPAND set. eg :e ^I, :w>>^I
4823 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4824 * when we know only directories are of interest. eg
4825 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004826 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4828 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4829 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4830 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4831 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4832 * EXPAND_EVENTS Complete event names
4833 * EXPAND_SYNTAX Complete :syntax command arguments
4834 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4835 * EXPAND_AUGROUP Complete autocommand group names
4836 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4837 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4838 * eg :unmap a^I , :cunab x^I
4839 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4840 * eg :call sub^I
4841 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4842 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4843 * names in expressions, eg :while s^I
4844 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004845 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
4847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004848set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004850 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 if (ccline.cmdfirstc != ':'
4852#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004853 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004854 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#endif
4856 )
4857 {
4858 xp->xp_context = EXPAND_NOTHING;
4859 return;
4860 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004861 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862}
4863
4864 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004865set_cmd_context(
4866 expand_T *xp,
4867 char_u *str, /* start of command line */
4868 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004869 int col, /* position of cursor */
4870 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
4872 int old_char = NUL;
4873 char_u *nextcomm;
4874
4875 /*
4876 * Avoid a UMR warning from Purify, only save the character if it has been
4877 * written before.
4878 */
4879 if (col < len)
4880 old_char = str[col];
4881 str[col] = NUL;
4882 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004883
4884#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004885 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004886 {
4887# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004888 /* pass CMD_SIZE because there is no real command */
4889 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004890# endif
4891 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004892 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004893 {
4894 xp->xp_context = ccline.xp_context;
4895 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004896# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004897 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004898# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004899 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004900 else
4901#endif
4902 while (nextcomm != NULL)
4903 nextcomm = set_one_cmd_context(xp, nextcomm);
4904
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004905 /* Store the string here so that call_user_expand_func() can get to them
4906 * easily. */
4907 xp->xp_line = str;
4908 xp->xp_col = col;
4909
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 str[col] = old_char;
4911}
4912
4913/*
4914 * Expand the command line "str" from context "xp".
4915 * "xp" must have been set by set_cmd_context().
4916 * xp->xp_pattern points into "str", to where the text that is to be expanded
4917 * starts.
4918 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4919 * cursor.
4920 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4921 * key that triggered expansion literally.
4922 * Returns EXPAND_OK otherwise.
4923 */
4924 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004925expand_cmdline(
4926 expand_T *xp,
4927 char_u *str, /* start of command line */
4928 int col, /* position of cursor */
4929 int *matchcount, /* return: nr of matches */
4930 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
4932 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004933 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
4935 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4936 {
4937 beep_flush();
4938 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4939 }
4940 if (xp->xp_context == EXPAND_NOTHING)
4941 {
4942 /* Caller can use the character as a normal char instead */
4943 return EXPAND_NOTHING;
4944 }
4945
4946 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004947 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4948 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (file_str == NULL)
4950 return EXPAND_UNSUCCESSFUL;
4951
Bram Moolenaar94950a92010-12-02 16:01:29 +01004952 if (p_wic)
4953 options += WILD_ICASE;
4954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004956 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 *matchcount = 0;
4959 *matches = NULL;
4960 }
4961 vim_free(file_str);
4962
4963 return EXPAND_OK;
4964}
4965
4966#ifdef FEAT_MULTI_LANG
4967/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004968 * Cleanup matches for help tags:
4969 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4970 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004972static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004975cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
4977 int i, j;
4978 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004979 char_u buf[4];
4980 char_u *p = buf;
4981
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004982 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004983 {
4984 *p++ = '@';
4985 *p++ = p_hlg[0];
4986 *p++ = p_hlg[1];
4987 }
4988 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
4990 for (i = 0; i < num_file; ++i)
4991 {
4992 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004993 if (len <= 0)
4994 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004995 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 /* Sorting on priority means the same item in another language may
4998 * be anywhere. Search all items for a match up to the "@en". */
4999 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005000 if (j != i && (int)STRLEN(file[j]) == len + 3
5001 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 break;
5003 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005004 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 file[i][len] = NUL;
5006 }
5007 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005008
5009 if (*buf != NUL)
5010 for (i = 0; i < num_file; ++i)
5011 {
5012 len = (int)STRLEN(file[i]) - 3;
5013 if (len <= 0)
5014 continue;
5015 if (STRCMP(file[i] + len, buf) == 0)
5016 {
5017 /* remove the default language */
5018 file[i][len] = NUL;
5019 }
5020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021}
5022#endif
5023
5024/*
5025 * Do the expansion based on xp->xp_context and "pat".
5026 */
5027 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028ExpandFromContext(
5029 expand_T *xp,
5030 char_u *pat,
5031 int *num_file,
5032 char_u ***file,
5033 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035#ifdef FEAT_CMDL_COMPL
5036 regmatch_T regmatch;
5037#endif
5038 int ret;
5039 int flags;
5040
5041 flags = EW_DIR; /* include directories */
5042 if (options & WILD_LIST_NOTFOUND)
5043 flags |= EW_NOTFOUND;
5044 if (options & WILD_ADD_SLASH)
5045 flags |= EW_ADDSLASH;
5046 if (options & WILD_KEEP_ALL)
5047 flags |= EW_KEEPALL;
5048 if (options & WILD_SILENT)
5049 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005050 if (options & WILD_ALLLINKS)
5051 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005053 if (xp->xp_context == EXPAND_FILES
5054 || xp->xp_context == EXPAND_DIRECTORIES
5055 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 /*
5058 * Expand file or directory names.
5059 */
5060 int free_pat = FALSE;
5061 int i;
5062
5063 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5064 * space */
5065 if (xp->xp_backslash != XP_BS_NONE)
5066 {
5067 free_pat = TRUE;
5068 pat = vim_strsave(pat);
5069 for (i = 0; pat[i]; ++i)
5070 if (pat[i] == '\\')
5071 {
5072 if (xp->xp_backslash == XP_BS_THREE
5073 && pat[i + 1] == '\\'
5074 && pat[i + 2] == '\\'
5075 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005076 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (xp->xp_backslash == XP_BS_ONE
5078 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005079 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 }
5082
5083 if (xp->xp_context == EXPAND_FILES)
5084 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005085 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5086 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 else
5088 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005089 if (options & WILD_ICASE)
5090 flags |= EW_ICASE;
5091
Bram Moolenaard7834d32009-12-02 16:14:36 +00005092 /* Expand wildcards, supporting %:h and the like. */
5093 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (free_pat)
5095 vim_free(pat);
5096 return ret;
5097 }
5098
5099 *file = (char_u **)"";
5100 *num_file = 0;
5101 if (xp->xp_context == EXPAND_HELP)
5102 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005103 /* With an empty argument we would get all the help tags, which is
5104 * very slow. Get matches for "help" instead. */
5105 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5106 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108#ifdef FEAT_MULTI_LANG
5109 cleanup_help_tags(*num_file, *file);
5110#endif
5111 return OK;
5112 }
5113 return FAIL;
5114 }
5115
5116#ifndef FEAT_CMDL_COMPL
5117 return FAIL;
5118#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005119 if (xp->xp_context == EXPAND_SHELLCMD)
5120 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (xp->xp_context == EXPAND_OLD_SETTING)
5122 return ExpandOldSetting(num_file, file);
5123 if (xp->xp_context == EXPAND_BUFFERS)
5124 return ExpandBufnames(pat, num_file, file, options);
5125 if (xp->xp_context == EXPAND_TAGS
5126 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5127 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5128 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005129 {
5130 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005131 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5132 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005135 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005136 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005137 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005138 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005139 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005140 {
5141 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005142 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005143 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005144 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005145 {
5146 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005147 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005148 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005149# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5150 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005151 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005152# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005153 if (xp->xp_context == EXPAND_PACKADD)
5154 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5157 if (regmatch.regprog == NULL)
5158 return FAIL;
5159
5160 /* set ignore-case according to p_ic, p_scs and pat */
5161 regmatch.rm_ic = ignorecase(pat);
5162
5163 if (xp->xp_context == EXPAND_SETTINGS
5164 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5165 ret = ExpandSettings(xp, &regmatch, num_file, file);
5166 else if (xp->xp_context == EXPAND_MAPPINGS)
5167 ret = ExpandMappings(&regmatch, num_file, file);
5168# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5169 else if (xp->xp_context == EXPAND_USER_DEFINED)
5170 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5171# endif
5172 else
5173 {
5174 static struct expgen
5175 {
5176 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005177 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005179 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 } tab[] =
5181 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005182 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5183 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005184 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005185 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005186#ifdef FEAT_CMDHIST
5187 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005190 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005191 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005192 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5193 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5194 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#endif
5196#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005197 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5198 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5199 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5200 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
5202#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005203 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5204 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#endif
5206#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005207 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005209#ifdef FEAT_PROFILE
5210 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5211#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005212 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005213 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5214 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005215#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005217#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005218#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005219 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005220#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005221#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5225 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005226 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5227 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005229 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005230 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005231 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 };
5233 int i;
5234
5235 /*
5236 * Find a context in the table and call the ExpandGeneric() with the
5237 * right function to do the expansion.
5238 */
5239 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005240 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (xp->xp_context == tab[i].context)
5242 {
5243 if (tab[i].ic)
5244 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005245 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005246 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 break;
5248 }
5249 }
5250
Bram Moolenaar473de612013-06-08 18:19:48 +02005251 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 return ret;
5254#endif /* FEAT_CMDL_COMPL */
5255}
5256
5257#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5258/*
5259 * Expand a list of names.
5260 *
5261 * Generic function for command line completion. It calls a function to
5262 * obtain strings, one by one. The strings are matched against a regexp
5263 * program. Matching strings are copied into an array, which is returned.
5264 *
5265 * Returns OK when no problems encountered, FAIL for error (out of memory).
5266 */
5267 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005268ExpandGeneric(
5269 expand_T *xp,
5270 regmatch_T *regmatch,
5271 int *num_file,
5272 char_u ***file,
5273 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005275 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
5277 int i;
5278 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005279 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 char_u *str;
5281
5282 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005283 * round == 0: count the number of matching names
5284 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005286 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
5288 for (i = 0; ; ++i)
5289 {
5290 str = (*func)(xp, i);
5291 if (str == NULL) /* end of list */
5292 break;
5293 if (*str == NUL) /* skip empty strings */
5294 continue;
5295
5296 if (vim_regexec(regmatch, str, (colnr_T)0))
5297 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005298 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005300 if (escaped)
5301 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5302 else
5303 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 (*file)[count] = str;
5305#ifdef FEAT_MENU
5306 if (func == get_menu_names && str != NULL)
5307 {
5308 /* test for separator added by get_menu_names() */
5309 str += STRLEN(str) - 1;
5310 if (*str == '\001')
5311 *str = '.';
5312 }
5313#endif
5314 }
5315 ++count;
5316 }
5317 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005318 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 if (count == 0)
5321 return OK;
5322 *num_file = count;
5323 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5324 if (*file == NULL)
5325 {
5326 *file = (char_u **)"";
5327 return FAIL;
5328 }
5329 count = 0;
5330 }
5331 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005332
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005333 /* Sort the results. Keep menu's in the specified order. */
5334 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005335 {
5336 if (xp->xp_context == EXPAND_EXPRESSION
5337 || xp->xp_context == EXPAND_FUNCTIONS
5338 || xp->xp_context == EXPAND_USER_FUNC)
5339 /* <SNR> functions should be sorted to the end. */
5340 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5341 sort_func_compare);
5342 else
5343 sort_strings(*file, *num_file);
5344 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005345
Bram Moolenaar4f688582007-07-24 12:34:30 +00005346#ifdef FEAT_CMDL_COMPL
5347 /* Reset the variables used for special highlight names expansion, so that
5348 * they don't show up when getting normal highlight names by ID. */
5349 reset_expand_highlight();
5350#endif
5351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 return OK;
5353}
5354
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005355/*
5356 * Complete a shell command.
5357 * Returns FAIL or OK;
5358 */
5359 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005360expand_shellcmd(
5361 char_u *filepat, /* pattern to match with command names */
5362 int *num_file, /* return: number of matches */
5363 char_u ***file, /* return: array with matches */
5364 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005365{
5366 char_u *pat;
5367 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005368 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005369 int mustfree = FALSE;
5370 garray_T ga;
5371 char_u *buf = alloc(MAXPATHL);
5372 size_t l;
5373 char_u *s, *e;
5374 int flags = flagsarg;
5375 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005376 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005377 hashtab_T found_ht;
5378 hashitem_T *hi;
5379 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005380
5381 if (buf == NULL)
5382 return FAIL;
5383
5384 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5385 * space */
5386 pat = vim_strsave(filepat);
5387 for (i = 0; pat[i]; ++i)
5388 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005389 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005390
Bram Moolenaarb5971142015-03-21 17:32:19 +01005391 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005392
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005393 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5394 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005395 path = (char_u *)".";
5396 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005397 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005398 /* For an absolute name we don't use $PATH. */
5399 if (!mch_isFullName(pat))
5400 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005401 if (path == NULL)
5402 path = (char_u *)"";
5403 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005404
5405 /*
5406 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005407 * collect them in "ga". When "." is not in $PATH also expand for the
5408 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005409 */
5410 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005411 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005412 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005413 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005414#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005415 e = vim_strchr(s, ';');
5416#else
5417 e = vim_strchr(s, ':');
5418#endif
5419 if (e == NULL)
5420 e = s + STRLEN(s);
5421
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005422 if (*s == NUL)
5423 {
5424 if (did_curdir)
5425 break;
5426 // Find directories in the current directory, path is empty.
5427 did_curdir = TRUE;
5428 flags |= EW_DIR;
5429 }
5430 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5431 {
5432 did_curdir = TRUE;
5433 flags |= EW_DIR;
5434 }
5435 else
5436 // Do not match directories inside a $PATH item.
5437 flags &= ~EW_DIR;
5438
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005439 l = e - s;
5440 if (l > MAXPATHL - 5)
5441 break;
5442 vim_strncpy(buf, s, l);
5443 add_pathsep(buf);
5444 l = STRLEN(buf);
5445 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5446
5447 /* Expand matches in one directory of $PATH. */
5448 ret = expand_wildcards(1, &buf, num_file, file, flags);
5449 if (ret == OK)
5450 {
5451 if (ga_grow(&ga, *num_file) == FAIL)
5452 FreeWild(*num_file, *file);
5453 else
5454 {
5455 for (i = 0; i < *num_file; ++i)
5456 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005457 char_u *name = (*file)[i];
5458
5459 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005460 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005461 // Check if this name was already found.
5462 hash = hash_hash(name + l);
5463 hi = hash_lookup(&found_ht, name + l, hash);
5464 if (HASHITEM_EMPTY(hi))
5465 {
5466 // Remove the path that was prepended.
5467 STRMOVE(name, name + l);
5468 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5469 hash_add_item(&found_ht, hi, name, hash);
5470 name = NULL;
5471 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005472 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005473 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005474 }
5475 vim_free(*file);
5476 }
5477 }
5478 if (*e != NUL)
5479 ++e;
5480 }
5481 *file = ga.ga_data;
5482 *num_file = ga.ga_len;
5483
5484 vim_free(buf);
5485 vim_free(pat);
5486 if (mustfree)
5487 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005488 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005489 return OK;
5490}
5491
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5494/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005495 * Call "user_expand_func()" to invoke a user defined Vim script function and
5496 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005498 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005500 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005501 expand_T *xp,
5502 int *num_file,
5503 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005505 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005506 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005508 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005509 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005510 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
Bram Moolenaarb7515462013-06-29 12:58:33 +02005512 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005513 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 *num_file = 0;
5515 *file = NULL;
5516
Bram Moolenaarb7515462013-06-29 12:58:33 +02005517 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005518 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005519 keep = ccline.cmdbuff[ccline.cmdlen];
5520 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005521 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005522
Bram Moolenaarffa96842018-06-12 22:05:14 +02005523 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5524
5525 args[0].v_type = VAR_STRING;
5526 args[0].vval.v_string = pat;
5527 args[1].v_type = VAR_STRING;
5528 args[1].vval.v_string = xp->xp_line;
5529 args[2].v_type = VAR_NUMBER;
5530 args[2].vval.v_number = xp->xp_col;
5531 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005533 /* Save the cmdline, we don't know what the function may do. */
5534 save_ccline = ccline;
5535 ccline.cmdbuff = NULL;
5536 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005538
Bram Moolenaarded27a12018-08-01 19:06:03 +02005539 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005540
5541 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005543 if (ccline.cmdbuff != NULL)
5544 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005545
Bram Moolenaarffa96842018-06-12 22:05:14 +02005546 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005547 return ret;
5548}
5549
5550/*
5551 * Expand names with a function defined by the user.
5552 */
5553 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005554ExpandUserDefined(
5555 expand_T *xp,
5556 regmatch_T *regmatch,
5557 int *num_file,
5558 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005559{
5560 char_u *retstr;
5561 char_u *s;
5562 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005563 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005564 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005565 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005566
5567 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5568 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 return FAIL;
5570
5571 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005572 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
5574 e = vim_strchr(s, '\n');
5575 if (e == NULL)
5576 e = s + STRLEN(s);
5577 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005578 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005580 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5581 *e = keep;
5582
5583 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005585 if (ga_grow(&ga, 1) == FAIL)
5586 break;
5587 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5588 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (*e != NUL)
5592 ++e;
5593 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005594 vim_free(retstr);
5595 *file = ga.ga_data;
5596 *num_file = ga.ga_len;
5597 return OK;
5598}
5599
5600/*
5601 * Expand names with a list returned by a function defined by the user.
5602 */
5603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005604ExpandUserList(
5605 expand_T *xp,
5606 int *num_file,
5607 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005608{
5609 list_T *retlist;
5610 listitem_T *li;
5611 garray_T ga;
5612
5613 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5614 if (retlist == NULL)
5615 return FAIL;
5616
5617 ga_init2(&ga, (int)sizeof(char *), 3);
5618 /* Loop over the items in the list. */
5619 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5620 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005621 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5622 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005623
5624 if (ga_grow(&ga, 1) == FAIL)
5625 break;
5626
5627 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005628 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005629 ++ga.ga_len;
5630 }
5631 list_unref(retlist);
5632
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 *file = ga.ga_data;
5634 *num_file = ga.ga_len;
5635 return OK;
5636}
5637#endif
5638
5639/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005640 * Expand color scheme, compiler or filetype names.
5641 * Search from 'runtimepath':
5642 * 'runtimepath'/{dirnames}/{pat}.vim
5643 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5644 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5645 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5646 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005647 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 */
5649 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005650ExpandRTDir(
5651 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005652 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005653 int *num_file,
5654 char_u ***file,
5655 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 char_u *s;
5658 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005659 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005661 int i;
5662 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 *num_file = 0;
5665 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005666 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005667 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005669 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005671 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5672 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005674 ga_clear_strings(&ga);
5675 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005677 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005678 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005679 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005681
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005682 if (flags & DIP_START) {
5683 for (i = 0; dirnames[i] != NULL; ++i)
5684 {
5685 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5686 if (s == NULL)
5687 {
5688 ga_clear_strings(&ga);
5689 return FAIL;
5690 }
5691 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5692 globpath(p_pp, s, &ga, 0);
5693 vim_free(s);
5694 }
5695 }
5696
5697 if (flags & DIP_OPT) {
5698 for (i = 0; dirnames[i] != NULL; ++i)
5699 {
5700 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5701 if (s == NULL)
5702 {
5703 ga_clear_strings(&ga);
5704 return FAIL;
5705 }
5706 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5707 globpath(p_pp, s, &ga, 0);
5708 vim_free(s);
5709 }
5710 }
5711
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005712 for (i = 0; i < ga.ga_len; ++i)
5713 {
5714 match = ((char_u **)ga.ga_data)[i];
5715 s = match;
5716 e = s + STRLEN(s);
5717 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5718 {
5719 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005720 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005721 if (s < match || vim_ispathsep(*s))
5722 break;
5723 ++s;
5724 *e = NUL;
5725 mch_memmove(match, s, e - s + 1);
5726 }
5727 }
5728
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005729 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005730 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005731
5732 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005733 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005734 remove_duplicates(&ga);
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 *file = ga.ga_data;
5737 *num_file = ga.ga_len;
5738 return OK;
5739}
5740
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005741/*
5742 * Expand loadplugin names:
5743 * 'packpath'/pack/ * /opt/{pat}
5744 */
5745 static int
5746ExpandPackAddDir(
5747 char_u *pat,
5748 int *num_file,
5749 char_u ***file)
5750{
5751 char_u *s;
5752 char_u *e;
5753 char_u *match;
5754 garray_T ga;
5755 int i;
5756 int pat_len;
5757
5758 *num_file = 0;
5759 *file = NULL;
5760 pat_len = (int)STRLEN(pat);
5761 ga_init2(&ga, (int)sizeof(char *), 10);
5762
5763 s = alloc((unsigned)(pat_len + 26));
5764 if (s == NULL)
5765 {
5766 ga_clear_strings(&ga);
5767 return FAIL;
5768 }
5769 sprintf((char *)s, "pack/*/opt/%s*", pat);
5770 globpath(p_pp, s, &ga, 0);
5771 vim_free(s);
5772
5773 for (i = 0; i < ga.ga_len; ++i)
5774 {
5775 match = ((char_u **)ga.ga_data)[i];
5776 s = gettail(match);
5777 e = s + STRLEN(s);
5778 mch_memmove(match, s, e - s + 1);
5779 }
5780
5781 if (ga.ga_len == 0)
5782 return FAIL;
5783
5784 /* Sort and remove duplicates which can happen when specifying multiple
5785 * directories in dirnames. */
5786 remove_duplicates(&ga);
5787
5788 *file = ga.ga_data;
5789 *num_file = ga.ga_len;
5790 return OK;
5791}
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793#endif
5794
5795#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5796/*
5797 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005798 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005800 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005801globpath(
5802 char_u *path,
5803 char_u *file,
5804 garray_T *ga,
5805 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
5807 expand_T xpc;
5808 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 int num_p;
5811 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
5813 buf = alloc(MAXPATHL);
5814 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005815 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005817 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 /* Loop over all entries in {path}. */
5821 while (*path != NUL)
5822 {
5823 /* Copy one item of the path to buf[] and concatenate the file name. */
5824 copy_option_part(&path, buf, MAXPATHL, ",");
5825 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5826 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005827# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005828 /* Using the platform's path separator (\) makes vim incorrectly
5829 * treat it as an escape character, use '/' instead. */
5830 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5831 STRCAT(buf, "/");
5832# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005836 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5837 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005839 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005841 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 for (i = 0; i < num_p; ++i)
5844 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005845 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005846 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005847 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 FreeWild(num_p, p);
5852 }
5853 }
5854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
5856 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857}
5858
5859#endif
5860
5861#if defined(FEAT_CMDHIST) || defined(PROTO)
5862
5863/*********************************
5864 * Command line history stuff *
5865 *********************************/
5866
5867/*
5868 * Translate a history character to the associated type number.
5869 */
5870 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005871hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 if (c == ':')
5874 return HIST_CMD;
5875 if (c == '=')
5876 return HIST_EXPR;
5877 if (c == '@')
5878 return HIST_INPUT;
5879 if (c == '>')
5880 return HIST_DEBUG;
5881 return HIST_SEARCH; /* must be '?' or '/' */
5882}
5883
5884/*
5885 * Table of history names.
5886 * These names are used in :history and various hist...() functions.
5887 * It is sufficient to give the significant prefix of a history name.
5888 */
5889
5890static char *(history_names[]) =
5891{
5892 "cmd",
5893 "search",
5894 "expr",
5895 "input",
5896 "debug",
5897 NULL
5898};
5899
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005900#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5901/*
5902 * Function given to ExpandGeneric() to obtain the possible first
5903 * arguments of the ":history command.
5904 */
5905 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005906get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005907{
5908 static char_u compl[2] = { NUL, NUL };
5909 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005910 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005911 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5912
5913 if (idx < short_names_count)
5914 {
5915 compl[0] = (char_u)short_names[idx];
5916 return compl;
5917 }
5918 if (idx < short_names_count + history_name_count)
5919 return (char_u *)history_names[idx - short_names_count];
5920 if (idx == short_names_count + history_name_count)
5921 return (char_u *)"all";
5922 return NULL;
5923}
5924#endif
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926/*
5927 * init_history() - Initialize the command line history.
5928 * Also used to re-allocate the history when the size changes.
5929 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005930 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005931init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932{
5933 int newlen; /* new length of history table */
5934 histentry_T *temp;
5935 int i;
5936 int j;
5937 int type;
5938
5939 /*
5940 * If size of history table changed, reallocate it
5941 */
5942 newlen = (int)p_hi;
5943 if (newlen != hislen) /* history length changed */
5944 {
5945 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5946 {
5947 if (newlen)
5948 {
5949 temp = (histentry_T *)lalloc(
5950 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5951 if (temp == NULL) /* out of memory! */
5952 {
5953 if (type == 0) /* first one: just keep the old length */
5954 {
5955 newlen = hislen;
5956 break;
5957 }
5958 /* Already changed one table, now we can only have zero
5959 * length for all tables. */
5960 newlen = 0;
5961 type = -1;
5962 continue;
5963 }
5964 }
5965 else
5966 temp = NULL;
5967 if (newlen == 0 || temp != NULL)
5968 {
5969 if (hisidx[type] < 0) /* there are no entries yet */
5970 {
5971 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005972 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 }
5974 else if (newlen > hislen) /* array becomes bigger */
5975 {
5976 for (i = 0; i <= hisidx[type]; ++i)
5977 temp[i] = history[type][i];
5978 j = i;
5979 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005980 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 for ( ; j < hislen; ++i, ++j)
5982 temp[i] = history[type][j];
5983 }
5984 else /* array becomes smaller or 0 */
5985 {
5986 j = hisidx[type];
5987 for (i = newlen - 1; ; --i)
5988 {
5989 if (i >= 0) /* copy newest entries */
5990 temp[i] = history[type][j];
5991 else /* remove older entries */
5992 vim_free(history[type][j].hisstr);
5993 if (--j < 0)
5994 j = hislen - 1;
5995 if (j == hisidx[type])
5996 break;
5997 }
5998 hisidx[type] = newlen - 1;
5999 }
6000 vim_free(history[type]);
6001 history[type] = temp;
6002 }
6003 }
6004 hislen = newlen;
6005 }
6006}
6007
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006009clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006010{
6011 hisptr->hisnum = 0;
6012 hisptr->viminfo = FALSE;
6013 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006014 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006015}
6016
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017/*
6018 * Check if command line 'str' is already in history.
6019 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6020 */
6021 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006022in_history(
6023 int type,
6024 char_u *str,
6025 int move_to_front, /* Move the entry to the front if it exists */
6026 int sep,
6027 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 int i;
6030 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006031 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033 if (hisidx[type] < 0)
6034 return FALSE;
6035 i = hisidx[type];
6036 do
6037 {
6038 if (history[type][i].hisstr == NULL)
6039 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006040
6041 /* For search history, check that the separator character matches as
6042 * well. */
6043 p = history[type][i].hisstr;
6044 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006045 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006046 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 {
6048 if (!move_to_front)
6049 return TRUE;
6050 last_i = i;
6051 break;
6052 }
6053 if (--i < 0)
6054 i = hislen - 1;
6055 } while (i != hisidx[type]);
6056
6057 if (last_i >= 0)
6058 {
6059 str = history[type][i].hisstr;
6060 while (i != hisidx[type])
6061 {
6062 if (++i >= hislen)
6063 i = 0;
6064 history[type][last_i] = history[type][i];
6065 last_i = i;
6066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006068 history[type][i].viminfo = FALSE;
6069 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006070 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 return TRUE;
6072 }
6073 return FALSE;
6074}
6075
6076/*
6077 * Convert history name (from table above) to its HIST_ equivalent.
6078 * When "name" is empty, return "cmd" history.
6079 * Returns -1 for unknown history name.
6080 */
6081 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006082get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 int i;
6085 int len = (int)STRLEN(name);
6086
6087 /* No argument: use current history. */
6088 if (len == 0)
6089 return hist_char2type(ccline.cmdfirstc);
6090
6091 for (i = 0; history_names[i] != NULL; ++i)
6092 if (STRNICMP(name, history_names[i], len) == 0)
6093 return i;
6094
6095 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6096 return hist_char2type(name[0]);
6097
6098 return -1;
6099}
6100
6101static int last_maptick = -1; /* last seen maptick */
6102
6103/*
6104 * Add the given string to the given history. If the string is already in the
6105 * history then it is moved to the front. "histype" may be one of he HIST_
6106 * values.
6107 */
6108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006109add_to_history(
6110 int histype,
6111 char_u *new_entry,
6112 int in_map, /* consider maptick when inside a mapping */
6113 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
6115 histentry_T *hisptr;
6116 int len;
6117
6118 if (hislen == 0) /* no history */
6119 return;
6120
Bram Moolenaara939e432013-11-09 05:30:26 +01006121 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6122 return;
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 /*
6125 * Searches inside the same mapping overwrite each other, so that only
6126 * the last line is kept. Be careful not to remove a line that was moved
6127 * down, only lines that were added.
6128 */
6129 if (histype == HIST_SEARCH && in_map)
6130 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006131 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
6133 /* Current line is from the same mapping, remove it */
6134 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6135 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006136 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 --hisnum[histype];
6138 if (--hisidx[HIST_SEARCH] < 0)
6139 hisidx[HIST_SEARCH] = hislen - 1;
6140 }
6141 last_maptick = -1;
6142 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006143 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {
6145 if (++hisidx[histype] == hislen)
6146 hisidx[histype] = 0;
6147 hisptr = &history[histype][hisidx[histype]];
6148 vim_free(hisptr->hisstr);
6149
6150 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006151 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6153 if (hisptr->hisstr != NULL)
6154 hisptr->hisstr[len + 1] = sep;
6155
6156 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006157 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006158 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (histype == HIST_SEARCH && in_map)
6160 last_maptick = maptick;
6161 }
6162}
6163
6164#if defined(FEAT_EVAL) || defined(PROTO)
6165
6166/*
6167 * Get identifier of newest history entry.
6168 * "histype" may be one of the HIST_ values.
6169 */
6170 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006171get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6174 || hisidx[histype] < 0)
6175 return -1;
6176
6177 return history[histype][hisidx[histype]].hisnum;
6178}
6179
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 * Calculate history index from a number:
6182 * num > 0: seen as identifying number of a history entry
6183 * num < 0: relative position in history wrt newest entry
6184 * "histype" may be one of the HIST_ values.
6185 */
6186 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189 int i;
6190 histentry_T *hist;
6191 int wrapped = FALSE;
6192
6193 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6194 || (i = hisidx[histype]) < 0 || num == 0)
6195 return -1;
6196
6197 hist = history[histype];
6198 if (num > 0)
6199 {
6200 while (hist[i].hisnum > num)
6201 if (--i < 0)
6202 {
6203 if (wrapped)
6204 break;
6205 i += hislen;
6206 wrapped = TRUE;
6207 }
6208 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6209 return i;
6210 }
6211 else if (-num <= hislen)
6212 {
6213 i += num + 1;
6214 if (i < 0)
6215 i += hislen;
6216 if (hist[i].hisstr != NULL)
6217 return i;
6218 }
6219 return -1;
6220}
6221
6222/*
6223 * Get a history entry by its index.
6224 * "histype" may be one of the HIST_ values.
6225 */
6226 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006227get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 idx = calc_hist_idx(histype, idx);
6230 if (idx >= 0)
6231 return history[histype][idx].hisstr;
6232 else
6233 return (char_u *)"";
6234}
6235
6236/*
6237 * Clear all entries of a history.
6238 * "histype" may be one of the HIST_ values.
6239 */
6240 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006241clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 int i;
6244 histentry_T *hisptr;
6245
6246 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6247 {
6248 hisptr = history[histype];
6249 for (i = hislen; i--;)
6250 {
6251 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006252 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006253 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255 hisidx[histype] = -1; /* mark history as cleared */
6256 hisnum[histype] = 0; /* reset identifier counter */
6257 return OK;
6258 }
6259 return FAIL;
6260}
6261
6262/*
6263 * Remove all entries matching {str} from a history.
6264 * "histype" may be one of the HIST_ values.
6265 */
6266 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006267del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268{
6269 regmatch_T regmatch;
6270 histentry_T *hisptr;
6271 int idx;
6272 int i;
6273 int last;
6274 int found = FALSE;
6275
6276 regmatch.regprog = NULL;
6277 regmatch.rm_ic = FALSE; /* always match case */
6278 if (hislen != 0
6279 && histype >= 0
6280 && histype < HIST_COUNT
6281 && *str != NUL
6282 && (idx = hisidx[histype]) >= 0
6283 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6284 != NULL)
6285 {
6286 i = last = idx;
6287 do
6288 {
6289 hisptr = &history[histype][i];
6290 if (hisptr->hisstr == NULL)
6291 break;
6292 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6293 {
6294 found = TRUE;
6295 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006296 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298 else
6299 {
6300 if (i != last)
6301 {
6302 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006303 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305 if (--last < 0)
6306 last += hislen;
6307 }
6308 if (--i < 0)
6309 i += hislen;
6310 } while (i != idx);
6311 if (history[histype][idx].hisstr == NULL)
6312 hisidx[histype] = -1;
6313 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006314 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 return found;
6316}
6317
6318/*
6319 * Remove an indexed entry from a history.
6320 * "histype" may be one of the HIST_ values.
6321 */
6322 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 int i, j;
6326
6327 i = calc_hist_idx(histype, idx);
6328 if (i < 0)
6329 return FALSE;
6330 idx = hisidx[histype];
6331 vim_free(history[histype][i].hisstr);
6332
6333 /* When deleting the last added search string in a mapping, reset
6334 * last_maptick, so that the last added search string isn't deleted again.
6335 */
6336 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6337 last_maptick = -1;
6338
6339 while (i != idx)
6340 {
6341 j = (i + 1) % hislen;
6342 history[histype][i] = history[histype][j];
6343 i = j;
6344 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006345 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (--i < 0)
6347 i += hislen;
6348 hisidx[histype] = i;
6349 return TRUE;
6350}
6351
6352#endif /* FEAT_EVAL */
6353
6354#if defined(FEAT_CRYPT) || defined(PROTO)
6355/*
6356 * Very specific function to remove the value in ":set key=val" from the
6357 * history.
6358 */
6359 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006360remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 char_u *p;
6363 int i;
6364
6365 i = hisidx[HIST_CMD];
6366 if (i < 0)
6367 return;
6368 p = history[HIST_CMD][i].hisstr;
6369 if (p != NULL)
6370 for ( ; *p; ++p)
6371 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6372 {
6373 p = vim_strchr(p + 3, '=');
6374 if (p == NULL)
6375 break;
6376 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006377 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 if (p[i] == '\\' && p[i + 1])
6379 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006380 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 --p;
6382 }
6383}
6384#endif
6385
6386#endif /* FEAT_CMDHIST */
6387
Bram Moolenaar064154c2016-03-19 22:50:43 +01006388#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006389/*
6390 * Get pointer to the command line info to use. cmdline_paste() may clear
6391 * ccline and put the previous value in prev_ccline.
6392 */
6393 static struct cmdline_info *
6394get_ccline_ptr(void)
6395{
6396 if ((State & CMDLINE) == 0)
6397 return NULL;
6398 if (ccline.cmdbuff != NULL)
6399 return &ccline;
6400 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6401 return &prev_ccline;
6402 return NULL;
6403}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006404#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006405
Bram Moolenaar064154c2016-03-19 22:50:43 +01006406#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006407/*
6408 * Get the current command line in allocated memory.
6409 * Only works when the command line is being edited.
6410 * Returns NULL when something is wrong.
6411 */
6412 char_u *
6413get_cmdline_str(void)
6414{
6415 struct cmdline_info *p = get_ccline_ptr();
6416
6417 if (p == NULL)
6418 return NULL;
6419 return vim_strnsave(p->cmdbuff, p->cmdlen);
6420}
6421
6422/*
6423 * Get the current command line position, counted in bytes.
6424 * Zero is the first position.
6425 * Only works when the command line is being edited.
6426 * Returns -1 when something is wrong.
6427 */
6428 int
6429get_cmdline_pos(void)
6430{
6431 struct cmdline_info *p = get_ccline_ptr();
6432
6433 if (p == NULL)
6434 return -1;
6435 return p->cmdpos;
6436}
6437
6438/*
6439 * Set the command line byte position to "pos". Zero is the first position.
6440 * Only works when the command line is being edited.
6441 * Returns 1 when failed, 0 when OK.
6442 */
6443 int
6444set_cmdline_pos(
6445 int pos)
6446{
6447 struct cmdline_info *p = get_ccline_ptr();
6448
6449 if (p == NULL)
6450 return 1;
6451
6452 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6453 * changed the command line. */
6454 if (pos < 0)
6455 new_cmdpos = 0;
6456 else
6457 new_cmdpos = pos;
6458 return 0;
6459}
6460#endif
6461
6462#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6463/*
6464 * Get the current command-line type.
6465 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6466 * Only works when the command line is being edited.
6467 * Returns NUL when something is wrong.
6468 */
6469 int
6470get_cmdline_type(void)
6471{
6472 struct cmdline_info *p = get_ccline_ptr();
6473
6474 if (p == NULL)
6475 return NUL;
6476 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006477 return
6478# ifdef FEAT_EVAL
6479 (p->input_fn) ? '@' :
6480# endif
6481 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006482 return p->cmdfirstc;
6483}
6484#endif
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6487/*
6488 * Get indices "num1,num2" that specify a range within a list (not a range of
6489 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6490 * Returns OK if parsed successfully, otherwise FAIL.
6491 */
6492 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006493get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 int len;
6496 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006497 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
6499 *str = skipwhite(*str);
6500 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6501 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006502 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 *str += len;
6504 *num1 = (int)num;
6505 first = TRUE;
6506 }
6507 *str = skipwhite(*str);
6508 if (**str == ',') /* parse "to" part of range */
6509 {
6510 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006511 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (len > 0)
6513 {
6514 *num2 = (int)num;
6515 *str = skipwhite(*str + len);
6516 }
6517 else if (!first) /* no number given at all */
6518 return FAIL;
6519 }
6520 else if (first) /* only one number given */
6521 *num2 = *num1;
6522 return OK;
6523}
6524#endif
6525
6526#if defined(FEAT_CMDHIST) || defined(PROTO)
6527/*
6528 * :history command - print a history
6529 */
6530 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006531ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 histentry_T *hist;
6534 int histype1 = HIST_CMD;
6535 int histype2 = HIST_CMD;
6536 int hisidx1 = 1;
6537 int hisidx2 = -1;
6538 int idx;
6539 int i, j, k;
6540 char_u *end;
6541 char_u *arg = eap->arg;
6542
6543 if (hislen == 0)
6544 {
6545 MSG(_("'history' option is zero"));
6546 return;
6547 }
6548
6549 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6550 {
6551 end = arg;
6552 while (ASCII_ISALPHA(*end)
6553 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6554 end++;
6555 i = *end;
6556 *end = NUL;
6557 histype1 = get_histtype(arg);
6558 if (histype1 == -1)
6559 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006560 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
6562 histype1 = 0;
6563 histype2 = HIST_COUNT-1;
6564 }
6565 else
6566 {
6567 *end = i;
6568 EMSG(_(e_trailing));
6569 return;
6570 }
6571 }
6572 else
6573 histype2 = histype1;
6574 *end = i;
6575 }
6576 else
6577 end = arg;
6578 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6579 {
6580 EMSG(_(e_trailing));
6581 return;
6582 }
6583
6584 for (; !got_int && histype1 <= histype2; ++histype1)
6585 {
6586 STRCPY(IObuff, "\n # ");
6587 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6588 MSG_PUTS_TITLE(IObuff);
6589 idx = hisidx[histype1];
6590 hist = history[histype1];
6591 j = hisidx1;
6592 k = hisidx2;
6593 if (j < 0)
6594 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6595 if (k < 0)
6596 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6597 if (idx >= 0 && j <= k)
6598 for (i = idx + 1; !got_int; ++i)
6599 {
6600 if (i == hislen)
6601 i = 0;
6602 if (hist[i].hisstr != NULL
6603 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6604 {
6605 msg_putchar('\n');
6606 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6607 hist[i].hisnum);
6608 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6609 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006610 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 else
6612 STRCAT(IObuff, hist[i].hisstr);
6613 msg_outtrans(IObuff);
6614 out_flush();
6615 }
6616 if (i == idx)
6617 break;
6618 }
6619 }
6620}
6621#endif
6622
6623#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006624/*
6625 * Buffers for history read from a viminfo file. Only valid while reading.
6626 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006627static histentry_T *viminfo_history[HIST_COUNT] =
6628 {NULL, NULL, NULL, NULL, NULL};
6629static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6630static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631static int viminfo_add_at_front = FALSE;
6632
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006633static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635/*
6636 * Translate a history type number to the associated character.
6637 */
6638 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006639hist_type2char(
6640 int type,
6641 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 if (type == HIST_CMD)
6644 return ':';
6645 if (type == HIST_SEARCH)
6646 {
6647 if (use_question)
6648 return '?';
6649 else
6650 return '/';
6651 }
6652 if (type == HIST_EXPR)
6653 return '=';
6654 return '@';
6655}
6656
6657/*
6658 * Prepare for reading the history from the viminfo file.
6659 * This allocates history arrays to store the read history lines.
6660 */
6661 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006662prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
6664 int i;
6665 int num;
6666 int type;
6667 int len;
6668
6669 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006670 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (asklen > hislen)
6672 asklen = hislen;
6673
6674 for (type = 0; type < HIST_COUNT; ++type)
6675 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006676 /* Count the number of empty spaces in the history list. Entries read
6677 * from viminfo previously are also considered empty. If there are
6678 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006680 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 num++;
6682 len = asklen;
6683 if (num > len)
6684 len = num;
6685 if (len <= 0)
6686 viminfo_history[type] = NULL;
6687 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006688 viminfo_history[type] = (histentry_T *)lalloc(
6689 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (viminfo_history[type] == NULL)
6691 len = 0;
6692 viminfo_hislen[type] = len;
6693 viminfo_hisidx[type] = 0;
6694 }
6695}
6696
6697/*
6698 * Accept a line from the viminfo, store it in the history array when it's
6699 * new.
6700 */
6701 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006702read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 int type;
6705 long_u len;
6706 char_u *val;
6707 char_u *p;
6708
6709 type = hist_char2type(virp->vir_line[0]);
6710 if (viminfo_hisidx[type] < viminfo_hislen[type])
6711 {
6712 val = viminfo_readstring(virp, 1, TRUE);
6713 if (val != NULL && *val != NUL)
6714 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006715 int sep = (*val == ' ' ? NUL : *val);
6716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006718 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
6720 /* Need to re-allocate to append the separator byte. */
6721 len = STRLEN(val);
6722 p = lalloc(len + 2, TRUE);
6723 if (p != NULL)
6724 {
6725 if (type == HIST_SEARCH)
6726 {
6727 /* Search entry: Move the separator from the first
6728 * column to after the NUL. */
6729 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006730 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
6732 else
6733 {
6734 /* Not a search entry: No separator in the viminfo
6735 * file, add a NUL separator. */
6736 mch_memmove(p, val, (size_t)len + 1);
6737 p[len + 1] = NUL;
6738 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006739 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6740 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006741 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6742 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006743 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 }
6745 }
6746 }
6747 vim_free(val);
6748 }
6749 return viminfo_readline(virp);
6750}
6751
Bram Moolenaar07219f92013-04-14 23:19:36 +02006752/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006753 * Accept a new style history line from the viminfo, store it in the history
6754 * array when it's new.
6755 */
6756 void
6757handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006758 garray_T *values,
6759 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006760{
6761 int type;
6762 long_u len;
6763 char_u *val;
6764 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006765 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006766
6767 /* Check the format:
6768 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006769 if (values->ga_len < 4
6770 || vp[0].bv_type != BVAL_NR
6771 || vp[1].bv_type != BVAL_NR
6772 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6773 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006774 return;
6775
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006776 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006777 if (type >= HIST_COUNT)
6778 return;
6779 if (viminfo_hisidx[type] < viminfo_hislen[type])
6780 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006781 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006782 if (val != NULL && *val != NUL)
6783 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006784 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6785 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006786 int idx;
6787 int overwrite = FALSE;
6788
6789 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6790 {
6791 /* If lines were written by an older Vim we need to avoid
6792 * getting duplicates. See if the entry already exists. */
6793 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6794 {
6795 p = viminfo_history[type][idx].hisstr;
6796 if (STRCMP(val, p) == 0
6797 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6798 {
6799 overwrite = TRUE;
6800 break;
6801 }
6802 }
6803
6804 if (!overwrite)
6805 {
6806 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006807 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808 p = lalloc(len + 2, TRUE);
6809 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006810 else
6811 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006812 if (p != NULL)
6813 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006814 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006815 if (!overwrite)
6816 {
6817 mch_memmove(p, val, (size_t)len + 1);
6818 /* Put the separator after the NUL. */
6819 p[len + 1] = sep;
6820 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006821 viminfo_history[type][idx].hisnum = 0;
6822 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006823 viminfo_hisidx[type]++;
6824 }
6825 }
6826 }
6827 }
6828 }
6829}
6830
6831/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006832 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006833 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006834 static void
6835concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
6837 int idx;
6838 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006839
6840 idx = hisidx[type] + viminfo_hisidx[type];
6841 if (idx >= hislen)
6842 idx -= hislen;
6843 else if (idx < 0)
6844 idx = hislen - 1;
6845 if (viminfo_add_at_front)
6846 hisidx[type] = idx;
6847 else
6848 {
6849 if (hisidx[type] == -1)
6850 hisidx[type] = hislen - 1;
6851 do
6852 {
6853 if (history[type][idx].hisstr != NULL
6854 || history[type][idx].viminfo)
6855 break;
6856 if (++idx == hislen)
6857 idx = 0;
6858 } while (idx != hisidx[type]);
6859 if (idx != hisidx[type] && --idx < 0)
6860 idx = hislen - 1;
6861 }
6862 for (i = 0; i < viminfo_hisidx[type]; i++)
6863 {
6864 vim_free(history[type][idx].hisstr);
6865 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6866 history[type][idx].viminfo = TRUE;
6867 history[type][idx].time_set = viminfo_history[type][i].time_set;
6868 if (--idx < 0)
6869 idx = hislen - 1;
6870 }
6871 idx += 1;
6872 idx %= hislen;
6873 for (i = 0; i < viminfo_hisidx[type]; i++)
6874 {
6875 history[type][idx++].hisnum = ++hisnum[type];
6876 idx %= hislen;
6877 }
6878}
6879
6880#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6881 static int
6882#ifdef __BORLANDC__
6883_RTLENTRYF
6884#endif
6885sort_hist(const void *s1, const void *s2)
6886{
6887 histentry_T *p1 = *(histentry_T **)s1;
6888 histentry_T *p2 = *(histentry_T **)s2;
6889
6890 if (p1->time_set < p2->time_set) return -1;
6891 if (p1->time_set > p2->time_set) return 1;
6892 return 0;
6893}
6894#endif
6895
6896/*
6897 * Merge history lines from viminfo and lines typed in this Vim based on the
6898 * timestamp;
6899 */
6900 static void
6901merge_history(int type)
6902{
6903 int max_len;
6904 histentry_T **tot_hist;
6905 histentry_T *new_hist;
6906 int i;
6907 int len;
6908
6909 /* Make one long list with all entries. */
6910 max_len = hislen + viminfo_hisidx[type];
6911 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6912 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6913 if (tot_hist == NULL || new_hist == NULL)
6914 {
6915 vim_free(tot_hist);
6916 vim_free(new_hist);
6917 return;
6918 }
6919 for (i = 0; i < viminfo_hisidx[type]; i++)
6920 tot_hist[i] = &viminfo_history[type][i];
6921 len = i;
6922 for (i = 0; i < hislen; i++)
6923 if (history[type][i].hisstr != NULL)
6924 tot_hist[len++] = &history[type][i];
6925
6926 /* Sort the list on timestamp. */
6927 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6928
6929 /* Keep the newest ones. */
6930 for (i = 0; i < hislen; i++)
6931 {
6932 if (i < len)
6933 {
6934 new_hist[i] = *tot_hist[i];
6935 tot_hist[i]->hisstr = NULL;
6936 if (new_hist[i].hisnum == 0)
6937 new_hist[i].hisnum = ++hisnum[type];
6938 }
6939 else
6940 clear_hist_entry(&new_hist[i]);
6941 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006942 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006943
6944 /* Free what is not kept. */
6945 for (i = 0; i < viminfo_hisidx[type]; i++)
6946 vim_free(viminfo_history[type][i].hisstr);
6947 for (i = 0; i < hislen; i++)
6948 vim_free(history[type][i].hisstr);
6949 vim_free(history[type]);
6950 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006951 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006952}
6953
6954/*
6955 * Finish reading history lines from viminfo. Not used when writing viminfo.
6956 */
6957 void
6958finish_viminfo_history(vir_T *virp)
6959{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006961 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963 for (type = 0; type < HIST_COUNT; ++type)
6964 {
6965 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006966 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006967
6968 if (merge)
6969 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006971 concat_history(type);
6972
Bram Moolenaard23a8232018-02-10 18:45:26 +01006973 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006974 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 }
6976}
6977
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006978/*
6979 * Write history to viminfo file in "fp".
6980 * When "merge" is TRUE merge history lines with a previously read viminfo
6981 * file, data is in viminfo_history[].
6982 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006985write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 int i;
6988 int type;
6989 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006990 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992 init_history();
6993 if (hislen == 0)
6994 return;
6995 for (type = 0; type < HIST_COUNT; ++type)
6996 {
6997 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6998 if (num_saved == 0)
6999 continue;
7000 if (num_saved < 0) /* Use default */
7001 num_saved = hislen;
7002 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7003 type == HIST_CMD ? _("Command Line") :
7004 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007005 type == HIST_EXPR ? _("Expression") :
7006 type == HIST_INPUT ? _("Input Line") :
7007 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (num_saved > hislen)
7009 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007010
7011 /*
7012 * Merge typed and viminfo history:
7013 * round 1: history of typed commands.
7014 * round 2: history from recently read viminfo.
7015 */
7016 for (round = 1; round <= 2; ++round)
7017 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007018 if (round == 1)
7019 /* start at newest entry, somewhere in the list */
7020 i = hisidx[type];
7021 else if (viminfo_hisidx[type] > 0)
7022 /* start at newest entry, first in the list */
7023 i = 0;
7024 else
7025 /* empty list */
7026 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007027 if (i >= 0)
7028 while (num_saved > 0
7029 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007031 char_u *p;
7032 time_t timestamp;
7033 int c = NUL;
7034
7035 if (round == 1)
7036 {
7037 p = history[type][i].hisstr;
7038 timestamp = history[type][i].time_set;
7039 }
7040 else
7041 {
7042 p = viminfo_history[type] == NULL ? NULL
7043 : viminfo_history[type][i].hisstr;
7044 timestamp = viminfo_history[type] == NULL ? 0
7045 : viminfo_history[type][i].time_set;
7046 }
7047
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007048 if (p != NULL && (round == 2
7049 || !merge
7050 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007052 --num_saved;
7053 fputc(hist_type2char(type, TRUE), fp);
7054 /* For the search history: put the separator in the
7055 * second column; use a space if there isn't one. */
7056 if (type == HIST_SEARCH)
7057 {
7058 c = p[STRLEN(p) + 1];
7059 putc(c == NUL ? ' ' : c, fp);
7060 }
7061 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007062
7063 {
7064 char cbuf[NUMBUFLEN];
7065
7066 /* New style history with a bar line. Format:
7067 * |{bartype},{histtype},{timestamp},{separator},"text" */
7068 if (c == NUL)
7069 cbuf[0] = NUL;
7070 else
7071 sprintf(cbuf, "%d", c);
7072 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7073 type, (long)timestamp, cbuf);
7074 barline_writestring(fp, p, LSIZE - 20);
7075 putc('\n', fp);
7076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007078 if (round == 1)
7079 {
7080 /* Decrement index, loop around and stop when back at
7081 * the start. */
7082 if (--i < 0)
7083 i = hislen - 1;
7084 if (i == hisidx[type])
7085 break;
7086 }
7087 else
7088 {
7089 /* Increment index. Stop at the end in the while. */
7090 ++i;
7091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007093 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007094 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007095 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007096 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007097 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007098 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100}
7101#endif /* FEAT_VIMINFO */
7102
7103#if defined(FEAT_FKMAP) || defined(PROTO)
7104/*
7105 * Write a character at the current cursor+offset position.
7106 * It is directly written into the command buffer block.
7107 */
7108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007109cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110{
7111 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7112 {
7113 EMSG(_("E198: cmd_pchar beyond the command length"));
7114 return;
7115 }
7116 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7117 ccline.cmdbuff[ccline.cmdlen] = NUL;
7118}
7119
7120 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007121cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7124 {
7125 /* EMSG(_("cmd_gchar beyond the command length")); */
7126 return NUL;
7127 }
7128 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7129}
7130#endif
7131
7132#if defined(FEAT_CMDWIN) || defined(PROTO)
7133/*
7134 * Open a window on the current command line and history. Allow editing in
7135 * the window. Returns when the window is closed.
7136 * Returns:
7137 * CR if the command is to be executed
7138 * Ctrl_C if it is to be abandoned
7139 * K_IGNORE if editing continues
7140 */
7141 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007142open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143{
7144 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007145 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007147 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 win_T *wp;
7149 int i;
7150 linenr_T lnum;
7151 int histtype;
7152 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 int save_restart_edit = restart_edit;
7154 int save_State = State;
7155 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007156#ifdef FEAT_RIGHTLEFT
7157 int save_cmdmsg_rl = cmdmsg_rl;
7158#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007159#ifdef FEAT_FOLDING
7160 int save_KeyTyped;
7161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
7163 /* Can't do this recursively. Can't do it when typing a password. */
7164 if (cmdwin_type != 0
7165# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7166 || cmdline_star > 0
7167# endif
7168 )
7169 {
7170 beep_flush();
7171 return K_IGNORE;
7172 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007173 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175 /* Save current window sizes. */
7176 win_size_save(&winsizes);
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007179 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007180
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007181 /* don't use a new tab page */
7182 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007183 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 /* Create a window for the command-line buffer. */
7186 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7187 {
7188 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007189 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 return K_IGNORE;
7191 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007192 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
7194 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007195 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007196 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007199#ifdef FEAT_FOLDING
7200 curwin->w_p_fen = FALSE;
7201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007203 curwin->w_p_rl = cmdmsg_rl;
7204 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007206 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007209 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007210 /* But don't allow switching to another buffer. */
7211 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
Bram Moolenaar46152342005-09-07 21:18:43 +00007213 /* Showing the prompt may have set need_wait_return, reset it. */
7214 need_wait_return = FALSE;
7215
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007216 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7218 {
7219 if (p_wc == TAB)
7220 {
7221 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7222 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7223 }
7224 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7225 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007226 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007228 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7229 * sets 'textwidth' to 78). */
7230 curbuf->b_p_tw = 0;
7231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 /* Fill the buffer with the history. */
7233 init_history();
7234 if (hislen > 0)
7235 {
7236 i = hisidx[histtype];
7237 if (i >= 0)
7238 {
7239 lnum = 0;
7240 do
7241 {
7242 if (++i == hislen)
7243 i = 0;
7244 if (history[histtype][i].hisstr != NULL)
7245 ml_append(lnum++, history[histtype][i].hisstr,
7246 (colnr_T)0, FALSE);
7247 }
7248 while (i != hisidx[histtype]);
7249 }
7250 }
7251
7252 /* Replace the empty last line with the current command-line and put the
7253 * cursor there. */
7254 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7255 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7256 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007257 changed_line_abv_curs();
7258 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007259 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007262 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264 /* No Ex mode here! */
7265 exmode_active = 0;
7266
7267 State = NORMAL;
7268# ifdef FEAT_MOUSE
7269 setmouse();
7270# endif
7271
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007273 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007274 if (restart_edit != 0) /* autocmd with ":startinsert" */
7275 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
7277 i = RedrawingDisabled;
7278 RedrawingDisabled = 0;
7279
7280 /*
7281 * Call the main loop until <CR> or CTRL-C is typed.
7282 */
7283 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007284 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
7286 RedrawingDisabled = i;
7287
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007288# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007289 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007290# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007293 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007294
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007295# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007296 /* Restore KeyTyped in case it is modified by autocommands */
7297 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298# endif
7299
Bram Moolenaarccc18222007-05-10 18:25:20 +00007300 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007301 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 cmdwin_type = 0;
7303
7304 exmode_active = save_exmode;
7305
Bram Moolenaarf9821062008-06-20 16:31:07 +00007306 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007308 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 {
7310 cmdwin_result = Ctrl_C;
7311 EMSG(_("E199: Active window or buffer deleted"));
7312 }
7313 else
7314 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007315# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 /* autocmds may abort script processing */
7317 if (aborting() && cmdwin_result != K_IGNORE)
7318 cmdwin_result = Ctrl_C;
7319# endif
7320 /* Set the new command line from the cmdline buffer. */
7321 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007322 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007324 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7325
7326 if (histtype == HIST_CMD)
7327 {
7328 /* Execute the command directly. */
7329 ccline.cmdbuff = vim_strsave((char_u *)p);
7330 cmdwin_result = CAR;
7331 }
7332 else
7333 {
7334 /* First need to cancel what we were doing. */
7335 ccline.cmdbuff = NULL;
7336 stuffcharReadbuff(':');
7337 stuffReadbuff((char_u *)p);
7338 stuffcharReadbuff(CAR);
7339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
7341 else if (cmdwin_result == K_XF2) /* :qa typed */
7342 {
7343 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7344 cmdwin_result = CAR;
7345 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007346 else if (cmdwin_result == Ctrl_C)
7347 {
7348 /* :q or :close, don't execute any command
7349 * and don't modify the cmd window. */
7350 ccline.cmdbuff = NULL;
7351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 else
7353 ccline.cmdbuff = vim_strsave(ml_get_curline());
7354 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007355 {
7356 ccline.cmdbuff = vim_strsave((char_u *)"");
7357 ccline.cmdlen = 0;
7358 ccline.cmdbufflen = 1;
7359 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 else
7363 {
7364 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7365 ccline.cmdbufflen = ccline.cmdlen + 1;
7366 ccline.cmdpos = curwin->w_cursor.col;
7367 if (ccline.cmdpos > ccline.cmdlen)
7368 ccline.cmdpos = ccline.cmdlen;
7369 if (cmdwin_result == K_IGNORE)
7370 {
7371 set_cmdspos_cursor();
7372 redrawcmd();
7373 }
7374 }
7375
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007377 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007378# ifdef FEAT_CONCEAL
7379 /* Avoid command-line window first character being concealed. */
7380 curwin->w_p_cole = 0;
7381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007383 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 win_goto(old_curwin);
7385 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007386
7387 /* win_close() may have already wiped the buffer when 'bh' is
7388 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007389 if (bufref_valid(&bufref))
7390 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
7392 /* Restore window sizes. */
7393 win_size_restore(&winsizes);
7394
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007395 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397
7398 ga_clear(&winsizes);
7399 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007400# ifdef FEAT_RIGHTLEFT
7401 cmdmsg_rl = save_cmdmsg_rl;
7402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 State = save_State;
7405# ifdef FEAT_MOUSE
7406 setmouse();
7407# endif
7408
7409 return cmdwin_result;
7410}
7411#endif /* FEAT_CMDWIN */
7412
7413/*
7414 * Used for commands that either take a simple command string argument, or:
7415 * cmd << endmarker
7416 * {script}
7417 * endmarker
7418 * Returns a pointer to allocated memory with {script} or NULL.
7419 */
7420 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 char_u *theline;
7424 char *end_pattern = NULL;
7425 char dot[] = ".";
7426 garray_T ga;
7427
7428 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7429 return NULL;
7430
7431 ga_init2(&ga, 1, 0x400);
7432
7433 if (cmd[2] != NUL)
7434 end_pattern = (char *)skipwhite(cmd + 2);
7435 else
7436 end_pattern = dot;
7437
7438 for (;;)
7439 {
7440 theline = eap->getline(
7441#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007442 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443#endif
7444 NUL, eap->cookie, 0);
7445
7446 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007447 {
7448 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
7452 ga_concat(&ga, theline);
7453 ga_append(&ga, '\n');
7454 vim_free(theline);
7455 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007456 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 return (char_u *)ga.ga_data;
7459}