blob: d200eab78a9b9a4b5d2dbdc2cf628fff85fc8e33 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
144
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200146 static void
147trigger_cmd_autocmd(int typechar, int evt)
148{
149 char_u typestr[2];
150
151 typestr[0] = typechar;
152 typestr[1] = NUL;
153 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
154}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200157 * Abandon the command line.
158 */
159 static void
160abandon_cmdline(void)
161{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100162 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200163 if (msg_scrolled == 0)
164 compute_cmdrow();
165 MSG("");
166 redraw_cmdline = TRUE;
167}
168
Bram Moolenaaree219b02017-12-17 14:55:01 +0100169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200170/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100171 * Guess that the pattern matches everything. Only finds specific cases, such
172 * as a trailing \|, which can happen while typing a pattern.
173 */
174 static int
175empty_pattern(char_u *p)
176{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100177 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100178
179 /* remove trailing \v and the like */
180 while (n >= 2 && p[n - 2] == '\\'
181 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
182 n -= 2;
183 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
184}
185
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200186// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200187typedef struct {
188 colnr_T vs_curswant;
189 colnr_T vs_leftcol;
190 linenr_T vs_topline;
191# ifdef FEAT_DIFF
192 int vs_topfill;
193# endif
194 linenr_T vs_botline;
195 linenr_T vs_empty_rows;
196} viewstate_T;
197
198 static void
199save_viewstate(viewstate_T *vs)
200{
201 vs->vs_curswant = curwin->w_curswant;
202 vs->vs_leftcol = curwin->w_leftcol;
203 vs->vs_topline = curwin->w_topline;
204# ifdef FEAT_DIFF
205 vs->vs_topfill = curwin->w_topfill;
206# endif
207 vs->vs_botline = curwin->w_botline;
208 vs->vs_empty_rows = curwin->w_empty_rows;
209}
210
211 static void
212restore_viewstate(viewstate_T *vs)
213{
214 curwin->w_curswant = vs->vs_curswant;
215 curwin->w_leftcol = vs->vs_leftcol;
216 curwin->w_topline = vs->vs_topline;
217# ifdef FEAT_DIFF
218 curwin->w_topfill = vs->vs_topfill;
219# endif
220 curwin->w_botline = vs->vs_botline;
221 curwin->w_empty_rows = vs->vs_empty_rows;
222}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200223
224// Struct to store the state of 'incsearch' highlighting.
225typedef struct {
226 pos_T search_start; // where 'incsearch' starts searching
227 pos_T save_cursor;
228 viewstate_T init_viewstate;
229 viewstate_T old_viewstate;
230 pos_T match_start;
231 pos_T match_end;
232 int did_incsearch;
233 int incsearch_postponed;
234} incsearch_state_T;
235
236 static void
237init_incsearch_state(incsearch_state_T *is_state)
238{
239 is_state->match_start = curwin->w_cursor;
240 is_state->did_incsearch = FALSE;
241 is_state->incsearch_postponed = FALSE;
242 CLEAR_POS(&is_state->match_end);
243 is_state->save_cursor = curwin->w_cursor; // may be restored later
244 is_state->search_start = curwin->w_cursor;
245 save_viewstate(&is_state->init_viewstate);
246 save_viewstate(&is_state->old_viewstate);
247}
248
249/*
250 * First move cursor to end of match, then to the start. This
251 * moves the whole match onto the screen when 'nowrap' is set.
252 */
253 static void
254set_search_match(pos_T *t)
255{
256 t->lnum += search_match_lines;
257 t->col = search_match_endcol;
258 if (t->lnum > curbuf->b_ml.ml_line_count)
259 {
260 t->lnum = curbuf->b_ml.ml_line_count;
261 coladvance((colnr_T)MAXCOL);
262 }
263}
264
265/*
266 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200267 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200268 */
269 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200270do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
271 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200272{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
276 if (p_is && !cmd_silent)
277 {
278 // by default search all lines
279 search_first_line = 0;
280 search_last_line = MAXLNUM;
281
282 if (firstc == '/' || firstc == '?')
283 return TRUE;
284 if (firstc == ':')
285 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200286 char_u *cmd;
287 cmdmod_T save_cmdmod = cmdmod;
288 char_u *p;
289 int delim;
290 char_u *end;
291 char_u *dummy;
292 exarg_T ea;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200293
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200294 vim_memset(&ea, 0, sizeof(ea));
295 ea.line1 = 1;
296 ea.line2 = 1;
297 ea.cmd = ccline.cmdbuff;
298 ea.addr_type = ADDR_LINES;
299
300 parse_command_modifiers(&ea, &dummy, TRUE);
301 cmdmod = save_cmdmod;
302
303 cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200304 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
305 {
306 // Skip over "substitute" to find the pattern separator.
307 for (p = cmd; ASCII_ISALPHA(*p); ++p)
308 ;
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200309 if (*skipwhite(p) != NUL
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200310 && (STRNCMP(cmd, "substitute", p - cmd) == 0
311 || STRNCMP(cmd, "global", p - cmd) == 0
312 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200313 {
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200314 // Check for "global!/".
315 if (*cmd == 'g' && *p == '!')
316 {
317 p++;
318 if (*skipwhite(p) == NUL)
319 return FALSE;
320 }
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200321 p = skipwhite(p);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200322 delim = *p++;
323 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200324 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200325 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200326 pos_T save_cursor = curwin->w_cursor;
327
328 // found a non-empty pattern
329 *skiplen = (int)(p - ccline.cmdbuff);
330 *patlen = (int)(end - p);
331
332 // parse the address range
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200333 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200334 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200335 if (ea.addr_count > 0)
336 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200337 // Allow for reverse match.
338 if (ea.line2 < ea.line1)
339 {
340 search_first_line = ea.line2;
341 search_last_line = ea.line1;
342 }
343 else
344 {
345 search_first_line = ea.line1;
346 search_last_line = ea.line2;
347 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200348 }
349 else if (*cmd == 's')
350 {
351 // :s defaults to the current line
352 search_first_line = curwin->w_cursor.lnum;
353 search_last_line = curwin->w_cursor.lnum;
354 }
355
356 curwin->w_cursor = save_cursor;
357 return TRUE;
358 }
359 }
360 }
361 }
362 }
363
364 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200365}
366
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200367 static void
368finish_incsearch_highlighting(
369 int gotesc,
370 incsearch_state_T *is_state,
371 int call_update_screen)
372{
373 if (is_state->did_incsearch)
374 {
375 is_state->did_incsearch = FALSE;
376 if (gotesc)
377 curwin->w_cursor = is_state->save_cursor;
378 else
379 {
380 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
381 {
382 // put the '" mark at the original position
383 curwin->w_cursor = is_state->save_cursor;
384 setpcmark();
385 }
386 curwin->w_cursor = is_state->search_start;
387 }
388 restore_viewstate(&is_state->old_viewstate);
389 highlight_match = FALSE;
390 validate_cursor(); /* needed for TAB */
391 if (call_update_screen)
392 update_screen(SOME_VALID);
393 else
394 redraw_all_later(SOME_VALID);
395 }
396}
397
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200398/*
399 * Do 'incsearch' highlighting if desired.
400 */
401 static void
402may_do_incsearch_highlighting(
403 int firstc,
404 long count,
405 incsearch_state_T *is_state)
406{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200407 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200408 int i;
409 pos_T end_pos;
410 struct cmdline_info save_ccline;
411#ifdef FEAT_RELTIME
412 proftime_T tm;
413#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200414 int next_char;
415 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200416
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200417 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200418 {
419 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200420 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200421 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422
423 // If there is a character waiting, search and redraw later.
424 if (char_avail())
425 {
426 is_state->incsearch_postponed = TRUE;
427 return;
428 }
429 is_state->incsearch_postponed = FALSE;
430
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200431 if (search_first_line == 0)
432 // start at the original cursor position
433 curwin->w_cursor = is_state->search_start;
434 else
435 {
436 // start at the first line in the range
437 curwin->w_cursor.lnum = search_first_line;
438 curwin->w_cursor.col = 0;
439 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200440 save_last_search_pattern();
441
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200442 // Use the previous pattern for ":s//".
443 next_char = ccline.cmdbuff[skiplen + patlen];
444 use_last_pat = patlen == 0 && skiplen > 0
445 && ccline.cmdbuff[skiplen - 1] == next_char;
446
447 // If there is no pattern, don't do anything.
448 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200449 {
450 i = 0;
451 set_no_hlsearch(TRUE); // turn off previous highlight
452 redraw_all_later(SOME_VALID);
453 }
454 else
455 {
456 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
457
458 cursor_off(); // so the user knows we're busy
459 out_flush();
460 ++emsg_off; // so it doesn't beep if bad expr
461#ifdef FEAT_RELTIME
462 // Set the time limit to half a second.
463 profile_setlimit(500L, &tm);
464#endif
465 if (!p_hls)
466 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200467 if (search_first_line != 0)
468 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200469 ccline.cmdbuff[skiplen + patlen] = NUL;
470 i = do_search(NULL, firstc == ':' ? '/' : firstc,
471 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472#ifdef FEAT_RELTIME
473 &tm, NULL
474#else
475 NULL, NULL
476#endif
477 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200478 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 --emsg_off;
480
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200481 if (curwin->w_cursor.lnum < search_first_line
482 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200483 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200484 // match outside of address range
485 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200486 curwin->w_cursor = is_state->search_start;
487 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200488
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200489 // if interrupted while searching, behave like it failed
490 if (got_int)
491 {
492 (void)vpeekc(); // remove <C-C> from input stream
493 got_int = FALSE; // don't abandon the command line
494 i = 0;
495 }
496 else if (char_avail())
497 // cancelled searching because a char was typed
498 is_state->incsearch_postponed = TRUE;
499 }
500 if (i != 0)
501 highlight_match = TRUE; // highlight position
502 else
503 highlight_match = FALSE; // remove highlight
504
505 // First restore the old curwin values, so the screen is positioned in the
506 // same way as the actual search command.
507 restore_viewstate(&is_state->old_viewstate);
508 changed_cline_bef_curs();
509 update_topline();
510
511 if (i != 0)
512 {
513 pos_T save_pos = curwin->w_cursor;
514
515 is_state->match_start = curwin->w_cursor;
516 set_search_match(&curwin->w_cursor);
517 validate_cursor();
518 end_pos = curwin->w_cursor;
519 is_state->match_end = end_pos;
520 curwin->w_cursor = save_pos;
521 }
522 else
523 end_pos = curwin->w_cursor; // shutup gcc 4
524
525 // Disable 'hlsearch' highlighting if the pattern matches everything.
526 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200527 if (!use_last_pat)
528 {
529 next_char = ccline.cmdbuff[skiplen + patlen];
530 ccline.cmdbuff[skiplen + patlen] = NUL;
531 if (empty_pattern(ccline.cmdbuff))
532 set_no_hlsearch(TRUE);
533 ccline.cmdbuff[skiplen + patlen] = next_char;
534 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200535
536 validate_cursor();
537 // May redraw the status line to show the cursor position.
538 if (p_ru && curwin->w_status_height > 0)
539 curwin->w_redr_status = TRUE;
540
541 save_cmdline(&save_ccline);
542 update_screen(SOME_VALID);
543 restore_cmdline(&save_ccline);
544 restore_last_search_pattern();
545
546 // Leave it at the end to make CTRL-R CTRL-W work.
547 if (i != 0)
548 curwin->w_cursor = end_pos;
549
550 msg_starthere();
551 redrawcmdline();
552 is_state->did_incsearch = TRUE;
553}
554
555/*
556 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
557 * or previous match.
558 * Returns FAIL when jumping to cmdline_not_changed;
559 */
560 static int
561may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int firstc,
563 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200565 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200566{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200567 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568 pos_T t;
569 char_u *pat;
570 int search_flags = SEARCH_NOOF;
571 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200572 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200573
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200574 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200575 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200576 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200577 return FAIL;
578
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200579 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200580 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200581 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200582 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200583 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200584 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200585 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200586 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587
588 save_last_search_pattern();
589 cursor_off();
590 out_flush();
591 if (c == Ctrl_G)
592 {
593 t = is_state->match_end;
594 if (LT_POS(is_state->match_start, is_state->match_end))
595 // Start searching at the end of the match not at the beginning of
596 // the next column.
597 (void)decl(&t);
598 search_flags += SEARCH_COL;
599 }
600 else
601 t = is_state->match_start;
602 if (!p_hls)
603 search_flags += SEARCH_KEEP;
604 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200605 save = pat[patlen];
606 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200607 i = searchit(curwin, curbuf, &t,
608 c == Ctrl_G ? FORWARD : BACKWARD,
609 pat, count, search_flags,
610 RE_SEARCH, 0, NULL, NULL);
611 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200612 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200613 if (i)
614 {
615 is_state->search_start = is_state->match_start;
616 is_state->match_end = t;
617 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200618 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200619 {
620 // Move just before the current match, so that when nv_search
621 // finishes the cursor will be put back on the match.
622 is_state->search_start = t;
623 (void)decl(&is_state->search_start);
624 }
625 else if (c == Ctrl_G && firstc == '?')
626 {
627 // Move just after the current match, so that when nv_search
628 // finishes the cursor will be put back on the match.
629 is_state->search_start = t;
630 (void)incl(&is_state->search_start);
631 }
632 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
633 {
634 // wrap around
635 is_state->search_start = t;
636 if (firstc == '?')
637 (void)incl(&is_state->search_start);
638 else
639 (void)decl(&is_state->search_start);
640 }
641
642 set_search_match(&is_state->match_end);
643 curwin->w_cursor = is_state->match_start;
644 changed_cline_bef_curs();
645 update_topline();
646 validate_cursor();
647 highlight_match = TRUE;
648 save_viewstate(&is_state->old_viewstate);
649 update_screen(NOT_VALID);
650 redrawcmdline();
651 }
652 else
653 vim_beep(BO_ERROR);
654 restore_last_search_pattern();
655 return FAIL;
656}
657
658/*
659 * When CTRL-L typed: add character from the match to the pattern.
660 * May set "*c" to the added character.
661 * Return OK when jumping to cmdline_not_changed.
662 */
663 static int
664may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
665{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200666 int skiplen, patlen;
667
668 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200669 return FAIL;
670
671 // Add a character from under the cursor for 'incsearch'.
672 if (is_state->did_incsearch)
673 {
674 curwin->w_cursor = is_state->match_end;
675 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
676 {
677 *c = gchar_cursor();
678
679 // If 'ignorecase' and 'smartcase' are set and the
680 // command line has no uppercase characters, convert
681 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200682 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200683 *c = MB_TOLOWER(*c);
684 if (*c != NUL)
685 {
686 if (*c == firstc || vim_strchr((char_u *)(
687 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
688 {
689 // put a backslash before special characters
690 stuffcharReadbuff(*c);
691 *c = '\\';
692 }
693 return FAIL;
694 }
695 }
696 }
697 return OK;
698}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200699#endif
700
Bram Moolenaar66216052017-12-16 16:33:44 +0100701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 * getcmdline() - accept a command line starting with firstc.
703 *
704 * firstc == ':' get ":" command line.
705 * firstc == '/' or '?' get search pattern
706 * firstc == '=' get expression
707 * firstc == '@' get text for input() function
708 * firstc == '>' get text for debug mode
709 * firstc == NUL get text for :insert command
710 * firstc == -1 like NUL, and break on CTRL-C
711 *
712 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
713 * command line.
714 *
715 * Careful: getcmdline() can be called recursively!
716 *
717 * Return pointer to allocated string if there is a commandline, NULL
718 * otherwise.
719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100721getcmdline(
722 int firstc,
723 long count UNUSED, /* only used for incremental search */
724 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 int c;
727 int i;
728 int j;
729 int gotesc = FALSE; /* TRUE when <ESC> just typed */
730 int do_abbr; /* when TRUE check for abbr. */
731#ifdef FEAT_CMDHIST
732 char_u *lookfor = NULL; /* string to match */
733 int hiscnt; /* current history line in use */
734 int histype; /* history type to be used */
735#endif
736#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200737 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#endif
739 int did_wild_list = FALSE; /* did wild_list() recently */
740 int wim_index = 0; /* index in wim_flags[] */
741 int res;
742 int save_msg_scroll = msg_scroll;
743 int save_State = State; /* remember State when called */
744 int some_key_typed = FALSE; /* one of the keys was typed */
745#ifdef FEAT_MOUSE
746 /* mouse drag and release events are ignored, unless they are
747 * preceded with a mouse down event */
748 int ignore_drag_release = TRUE;
749#endif
750#ifdef FEAT_EVAL
751 int break_ctrl_c = FALSE;
752#endif
753 expand_T xpc;
754 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200755#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000756 /* Everything that may work recursively should save and restore the
757 * current command line in save_ccline. That includes update_screen(), a
758 * custom status line may invoke ":normal". */
759 struct cmdline_info save_ccline;
760#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200761 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#ifdef FEAT_EVAL
764 if (firstc == -1)
765 {
766 firstc = NUL;
767 break_ctrl_c = TRUE;
768 }
769#endif
770#ifdef FEAT_RIGHTLEFT
771 /* start without Hebrew mapping for a command line */
772 if (firstc == ':' || firstc == '=' || firstc == '>')
773 cmd_hkmap = 0;
774#endif
775
776 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200779 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781
782 /*
783 * set some variables for redrawcmd()
784 */
785 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000786 ccline.cmdindent = (firstc > 0 ? indent : 0);
787
788 /* alloc initial ccline.cmdbuff */
789 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (ccline.cmdbuff == NULL)
791 return NULL; /* out of memory */
792 ccline.cmdlen = ccline.cmdpos = 0;
793 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100794 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000796 /* autoindent for :insert and :append */
797 if (firstc <= 0)
798 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200799 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000800 ccline.cmdbuff[indent] = NUL;
801 ccline.cmdpos = indent;
802 ccline.cmdspos = indent;
803 ccline.cmdlen = indent;
804 }
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000807 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809#ifdef FEAT_RIGHTLEFT
810 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
811 && (firstc == '/' || firstc == '?'))
812 cmdmsg_rl = TRUE;
813 else
814 cmdmsg_rl = FALSE;
815#endif
816
817 redir_off = TRUE; /* don't redirect the typed command */
818 if (!cmd_silent)
819 {
820 i = msg_scrolled;
821 msg_scrolled = 0; /* avoid wait_return message */
822 gotocmdline(TRUE);
823 msg_scrolled += i;
824 redrawcmdprompt(); /* draw prompt or indent */
825 set_cmdspos();
826 }
827 xpc.xp_context = EXPAND_NOTHING;
828 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000829#ifndef BACKSLASH_IN_FILENAME
830 xpc.xp_shell = FALSE;
831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000833#if defined(FEAT_EVAL)
834 if (ccline.input_fn)
835 {
836 xpc.xp_context = ccline.xp_context;
837 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000838# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000839 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000840# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000841 }
842#endif
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 /*
845 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
846 * doing ":@0" when register 0 doesn't contain a CR.
847 */
848 msg_scroll = FALSE;
849
850 State = CMDLINE;
851
852 if (firstc == '/' || firstc == '?' || firstc == '@')
853 {
854 /* Use ":lmap" mappings for search pattern and input(). */
855 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
856 b_im_ptr = &curbuf->b_p_iminsert;
857 else
858 b_im_ptr = &curbuf->b_p_imsearch;
859 if (*b_im_ptr == B_IMODE_LMAP)
860 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100861#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 im_set_active(*b_im_ptr == B_IMODE_IM);
863#endif
864 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100865#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 else if (p_imcmdline)
867 im_set_active(TRUE);
868#endif
869
870#ifdef FEAT_MOUSE
871 setmouse();
872#endif
873#ifdef CURSOR_SHAPE
874 ui_cursor_shape(); /* may show different cursor shape */
875#endif
876
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000877 /* When inside an autocommand for writing "exiting" may be set and
878 * terminal mode set to cooked. Need to set raw mode here then. */
879 settmode(TMODE_RAW);
880
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200881 /* Trigger CmdlineEnter autocommands. */
882 cmdline_type = firstc == NUL ? '-' : firstc;
883 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#ifdef FEAT_CMDHIST
886 init_history();
887 hiscnt = hislen; /* set hiscnt to impossible history value */
888 histype = hist_char2type(firstc);
889#endif
890
891#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000892 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200895 /* If something above caused an error, reset the flags, we do want to type
896 * and execute commands. Display may be messed up a bit. */
897 if (did_emsg)
898 redrawcmd();
899 did_emsg = FALSE;
900 got_int = FALSE;
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 /*
903 * Collect the command string, handling editing keys.
904 */
905 for (;;)
906 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000907 redir_off = TRUE; /* Don't redirect the typed command.
908 Repeated, because a ":redir" inside
909 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#ifdef USE_ON_FLY_SCROLL
911 dont_scroll = FALSE; /* allow scrolling here */
912#endif
913 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
914
Bram Moolenaar72532d32018-04-07 19:09:09 +0200915 did_emsg = FALSE; /* There can't really be a reason why an error
916 that occurs while typing a command should
917 cause the command not to be executed. */
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000920
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100921 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
922 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000923 do
924 {
925 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100926 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000927
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (KeyTyped)
929 {
930 some_key_typed = TRUE;
931#ifdef FEAT_RIGHTLEFT
932 if (cmd_hkmap)
933 c = hkmap(c);
934# ifdef FEAT_FKMAP
935 if (cmd_fkmap)
936 c = cmdl_fkmap(c);
937# endif
938 if (cmdmsg_rl && !KeyStuffed)
939 {
940 /* Invert horizontal movements and operations. Only when
941 * typed by the user directly, not when the result of a
942 * mapping. */
943 switch (c)
944 {
945 case K_RIGHT: c = K_LEFT; break;
946 case K_S_RIGHT: c = K_S_LEFT; break;
947 case K_C_RIGHT: c = K_C_LEFT; break;
948 case K_LEFT: c = K_RIGHT; break;
949 case K_S_LEFT: c = K_S_RIGHT; break;
950 case K_C_LEFT: c = K_C_RIGHT; break;
951 }
952 }
953#endif
954 }
955
956 /*
957 * Ignore got_int when CTRL-C was typed here.
958 * Don't ignore it in :global, we really need to break then, e.g., for
959 * ":g/pat/normal /pat" (without the <CR>).
960 * Don't ignore it for the input() function.
961 */
962 if ((c == Ctrl_C
963#ifdef UNIX
964 || c == intr_char
965#endif
966 )
967#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
968 && firstc != '@'
969#endif
970#ifdef FEAT_EVAL
971 && !break_ctrl_c
972#endif
973 && !global_busy)
974 got_int = FALSE;
975
976#ifdef FEAT_CMDHIST
977 /* free old command line when finished moving around in the history
978 * list */
979 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000980 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000981 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 && c != K_PAGEDOWN && c != K_PAGEUP
983 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000984 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100986 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#endif
988
989 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000990 * When there are matching completions to select <S-Tab> works like
991 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000993 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 c = Ctrl_P;
995
996#ifdef FEAT_WILDMENU
997 /* Special translations for 'wildmenu' */
998 if (did_wild_list && p_wmnu)
999 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001000 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001002 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 c = Ctrl_N;
1004 }
1005 /* Hitting CR after "emenu Name.": complete submenu */
1006 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1007 && ccline.cmdpos > 1
1008 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1009 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1010 && (c == '\n' || c == '\r' || c == K_KENTER))
1011 c = K_DOWN;
1012#endif
1013
1014 /* free expanded names when finished walking through matches */
1015 if (xpc.xp_numfiles != -1
1016 && !(c == p_wc && KeyTyped) && c != p_wcm
1017 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1018 && c != Ctrl_L)
1019 {
1020 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1021 did_wild_list = FALSE;
1022#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001023 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#endif
1025 xpc.xp_context = EXPAND_NOTHING;
1026 wim_index = 0;
1027#ifdef FEAT_WILDMENU
1028 if (p_wmnu && wild_menu_showing != 0)
1029 {
1030 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001031 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001032
1033 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001034 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 if (wild_menu_showing == WM_SCROLLED)
1037 {
1038 /* Entered command line, move it up */
1039 cmdline_row--;
1040 redrawcmd();
1041 }
1042 else if (save_p_ls != -1)
1043 {
1044 /* restore 'laststatus' and 'winminheight' */
1045 p_ls = save_p_ls;
1046 p_wmh = save_p_wmh;
1047 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001048 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001050 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 redrawcmd();
1052 save_p_ls = -1;
1053 }
1054 else
1055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 redraw_statuslines();
1058 }
1059 KeyTyped = skt;
1060 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001061 if (ccline.input_fn)
1062 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064#endif
1065 }
1066
1067#ifdef FEAT_WILDMENU
1068 /* Special translations for 'wildmenu' */
1069 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1070 {
1071 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001072 if (c == K_DOWN && ccline.cmdpos > 0
1073 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001075 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
1077 /* Hitting <Up>: Remove one submenu name in front of the
1078 * cursor */
1079 int found = FALSE;
1080
1081 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1082 i = 0;
1083 while (--j > 0)
1084 {
1085 /* check for start of menu name */
1086 if (ccline.cmdbuff[j] == ' '
1087 && ccline.cmdbuff[j - 1] != '\\')
1088 {
1089 i = j + 1;
1090 break;
1091 }
1092 /* check for start of submenu name */
1093 if (ccline.cmdbuff[j] == '.'
1094 && ccline.cmdbuff[j - 1] != '\\')
1095 {
1096 if (found)
1097 {
1098 i = j + 1;
1099 break;
1100 }
1101 else
1102 found = TRUE;
1103 }
1104 }
1105 if (i > 0)
1106 cmdline_del(i);
1107 c = p_wc;
1108 xpc.xp_context = EXPAND_NOTHING;
1109 }
1110 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001111 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001112 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001113 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 char_u upseg[5];
1116
1117 upseg[0] = PATHSEP;
1118 upseg[1] = '.';
1119 upseg[2] = '.';
1120 upseg[3] = PATHSEP;
1121 upseg[4] = NUL;
1122
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001123 if (c == K_DOWN
1124 && ccline.cmdpos > 0
1125 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1126 && (ccline.cmdpos < 3
1127 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1129 {
1130 /* go down a directory */
1131 c = p_wc;
1132 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001133 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
1135 /* If in a direct ancestor, strip off one ../ to go down */
1136 int found = FALSE;
1137
1138 j = ccline.cmdpos;
1139 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1140 while (--j > i)
1141 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001142#ifdef FEAT_MBYTE
1143 if (has_mbyte)
1144 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (vim_ispathsep(ccline.cmdbuff[j]))
1147 {
1148 found = TRUE;
1149 break;
1150 }
1151 }
1152 if (found
1153 && ccline.cmdbuff[j - 1] == '.'
1154 && ccline.cmdbuff[j - 2] == '.'
1155 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1156 {
1157 cmdline_del(j - 2);
1158 c = p_wc;
1159 }
1160 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001161 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {
1163 /* go up a directory */
1164 int found = FALSE;
1165
1166 j = ccline.cmdpos - 1;
1167 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1168 while (--j > i)
1169 {
1170#ifdef FEAT_MBYTE
1171 if (has_mbyte)
1172 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1173#endif
1174 if (vim_ispathsep(ccline.cmdbuff[j])
1175#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001176 && vim_strchr((char_u *)" *?[{`$%#",
1177 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179 )
1180 {
1181 if (found)
1182 {
1183 i = j + 1;
1184 break;
1185 }
1186 else
1187 found = TRUE;
1188 }
1189 }
1190
1191 if (!found)
1192 j = i;
1193 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1194 j += 4;
1195 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1196 && j == i)
1197 j += 3;
1198 else
1199 j = 0;
1200 if (j > 0)
1201 {
1202 /* TODO this is only for DOS/UNIX systems - need to put in
1203 * machine-specific stuff here and in upseg init */
1204 cmdline_del(j);
1205 put_on_cmdline(upseg + 1, 3, FALSE);
1206 }
1207 else if (ccline.cmdpos > i)
1208 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001209
1210 /* Now complete in the new directory. Set KeyTyped in case the
1211 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001213 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
1217#endif /* FEAT_WILDMENU */
1218
1219 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1220 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1221 if (c == Ctrl_BSL)
1222 {
1223 ++no_mapping;
1224 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001225 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 --no_mapping;
1227 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001228 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1229 * is in a mapping. */
1230 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1231 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
1233 vungetc(c);
1234 c = Ctrl_BSL;
1235 }
1236#ifdef FEAT_EVAL
1237 else if (c == 'e')
1238 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001239 char_u *p = NULL;
1240 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242 /*
1243 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001244 * Need to save and restore the current command line, to be
1245 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
1247 if (ccline.cmdpos == ccline.cmdlen)
1248 new_cmdpos = 99999; /* keep it at the end */
1249 else
1250 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001251
1252 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001254 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (c == '=')
1256 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001257 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001258 * to avoid nasty things like going to another buffer when
1259 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001260 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001261 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001263 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001264 restore_cmdline(&save_ccline);
1265
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001266 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001268 len = (int)STRLEN(p);
1269 if (realloc_cmdbuff(len + 1) == OK)
1270 {
1271 ccline.cmdlen = len;
1272 STRCPY(ccline.cmdbuff, p);
1273 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001275 /* Restore the cursor or use the position set with
1276 * set_cmdline_pos(). */
1277 if (new_cmdpos > ccline.cmdlen)
1278 ccline.cmdpos = ccline.cmdlen;
1279 else
1280 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001282 KeyTyped = FALSE; /* Don't do p_wc completion. */
1283 redrawcmd();
1284 goto cmdline_changed;
1285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 }
1288 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001289 got_int = FALSE; /* don't abandon the command line */
1290 did_emsg = FALSE;
1291 emsg_on_display = FALSE;
1292 redrawcmd();
1293 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295#endif
1296 else
1297 {
1298 if (c == Ctrl_G && p_im && restart_edit == 0)
1299 restart_edit = 'a';
1300 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1301 in history */
1302 goto returncmd; /* back to Normal mode */
1303 }
1304 }
1305
1306#ifdef FEAT_CMDWIN
1307 if (c == cedit_key || c == K_CMDWIN)
1308 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001309 if (ex_normal_busy == 0 && got_int == FALSE)
1310 {
1311 /*
1312 * Open a window to edit the command line (and history).
1313 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001314 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001315 some_key_typed = TRUE;
1316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318# ifdef FEAT_DIGRAPHS
1319 else
1320# endif
1321#endif
1322#ifdef FEAT_DIGRAPHS
1323 c = do_digraph(c);
1324#endif
1325
1326 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1327 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1328 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001329 /* In Ex mode a backslash escapes a newline. */
1330 if (exmode_active
1331 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001332 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001333 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001334 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001336 if (c == K_KENTER)
1337 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001339 else
1340 {
1341 gotesc = FALSE; /* Might have typed ESC previously, don't
1342 truncate the cmdline now. */
1343 if (ccheck_abbr(c + ABBR_OFF))
1344 goto cmdline_changed;
1345 if (!cmd_silent)
1346 {
1347 windgoto(msg_row, 0);
1348 out_flush();
1349 }
1350 break;
1351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353
1354 /*
1355 * Completion for 'wildchar' or 'wildcharm' key.
1356 * - hitting <ESC> twice means: abandon command line.
1357 * - wildcard expansion is only done when the 'wildchar' key is really
1358 * typed, not when it comes from a macro
1359 */
1360 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1361 {
1362 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1363 {
1364 /* if 'wildmode' contains "list" may still need to list */
1365 if (xpc.xp_numfiles > 1
1366 && !did_wild_list
1367 && (wim_flags[wim_index] & WIM_LIST))
1368 {
1369 (void)showmatches(&xpc, FALSE);
1370 redrawcmd();
1371 did_wild_list = TRUE;
1372 }
1373 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001374 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1375 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001377 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1378 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 else
1380 res = OK; /* don't insert 'wildchar' now */
1381 }
1382 else /* typed p_wc first time */
1383 {
1384 wim_index = 0;
1385 j = ccline.cmdpos;
1386 /* if 'wildmode' first contains "longest", get longest
1387 * common part */
1388 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001389 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1390 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001392 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1393 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 /* if interrupted while completing, behave like it failed */
1396 if (got_int)
1397 {
1398 (void)vpeekc(); /* remove <C-C> from input stream */
1399 got_int = FALSE; /* don't abandon the command line */
1400 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1401#ifdef FEAT_WILDMENU
1402 xpc.xp_context = EXPAND_NOTHING;
1403#endif
1404 goto cmdline_changed;
1405 }
1406
1407 /* when more than one match, and 'wildmode' first contains
1408 * "list", or no change and 'wildmode' contains "longest,list",
1409 * list all matches */
1410 if (res == OK && xpc.xp_numfiles > 1)
1411 {
1412 /* a "longest" that didn't do anything is skipped (but not
1413 * "list:longest") */
1414 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1415 wim_index = 1;
1416 if ((wim_flags[wim_index] & WIM_LIST)
1417#ifdef FEAT_WILDMENU
1418 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1419#endif
1420 )
1421 {
1422 if (!(wim_flags[0] & WIM_LONGEST))
1423 {
1424#ifdef FEAT_WILDMENU
1425 int p_wmnu_save = p_wmnu;
1426 p_wmnu = 0;
1427#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001428 /* remove match */
1429 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430#ifdef FEAT_WILDMENU
1431 p_wmnu = p_wmnu_save;
1432#endif
1433 }
1434#ifdef FEAT_WILDMENU
1435 (void)showmatches(&xpc, p_wmnu
1436 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1437#else
1438 (void)showmatches(&xpc, FALSE);
1439#endif
1440 redrawcmd();
1441 did_wild_list = TRUE;
1442 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001443 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1444 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001446 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1447 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001450 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452#ifdef FEAT_WILDMENU
1453 else if (xpc.xp_numfiles == -1)
1454 xpc.xp_context = EXPAND_NOTHING;
1455#endif
1456 }
1457 if (wim_index < 3)
1458 ++wim_index;
1459 if (c == ESC)
1460 gotesc = TRUE;
1461 if (res == OK)
1462 goto cmdline_changed;
1463 }
1464
1465 gotesc = FALSE;
1466
1467 /* <S-Tab> goes to last match, in a clumsy way */
1468 if (c == K_S_TAB && KeyTyped)
1469 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001470 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1471 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1472 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 goto cmdline_changed;
1474 }
1475
1476 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1477 c = NL;
1478
1479 do_abbr = TRUE; /* default: check for abbreviation */
1480
1481 /*
1482 * Big switch for a typed command line character.
1483 */
1484 switch (c)
1485 {
1486 case K_BS:
1487 case Ctrl_H:
1488 case K_DEL:
1489 case K_KDEL:
1490 case Ctrl_W:
1491#ifdef FEAT_FKMAP
1492 if (cmd_fkmap && c == K_BS)
1493 c = K_DEL;
1494#endif
1495 if (c == K_KDEL)
1496 c = K_DEL;
1497
1498 /*
1499 * delete current character is the same as backspace on next
1500 * character, except at end of line
1501 */
1502 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1503 ++ccline.cmdpos;
1504#ifdef FEAT_MBYTE
1505 if (has_mbyte && c == K_DEL)
1506 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1507 ccline.cmdbuff + ccline.cmdpos);
1508#endif
1509 if (ccline.cmdpos > 0)
1510 {
1511 char_u *p;
1512
1513 j = ccline.cmdpos;
1514 p = ccline.cmdbuff + j;
1515#ifdef FEAT_MBYTE
1516 if (has_mbyte)
1517 {
1518 p = mb_prevptr(ccline.cmdbuff, p);
1519 if (c == Ctrl_W)
1520 {
1521 while (p > ccline.cmdbuff && vim_isspace(*p))
1522 p = mb_prevptr(ccline.cmdbuff, p);
1523 i = mb_get_class(p);
1524 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1525 p = mb_prevptr(ccline.cmdbuff, p);
1526 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001527 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529 }
1530 else
1531#endif
1532 if (c == Ctrl_W)
1533 {
1534 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1535 --p;
1536 i = vim_iswordc(p[-1]);
1537 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1538 && vim_iswordc(p[-1]) == i)
1539 --p;
1540 }
1541 else
1542 --p;
1543 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1544 ccline.cmdlen -= j - ccline.cmdpos;
1545 i = ccline.cmdpos;
1546 while (i < ccline.cmdlen)
1547 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1548
1549 /* Truncate at the end, required for multi-byte chars. */
1550 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001551#ifdef FEAT_SEARCH_EXTRA
1552 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001553 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001554 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001555 /* save view settings, so that the screen
1556 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001557 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001558 }
1559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 redrawcmd();
1561 }
1562 else if (ccline.cmdlen == 0 && c != Ctrl_W
1563 && ccline.cmdprompt == NULL && indent == 0)
1564 {
1565 /* In ex and debug mode it doesn't make sense to return. */
1566 if (exmode_active
1567#ifdef FEAT_EVAL
1568 || ccline.cmdfirstc == '>'
1569#endif
1570 )
1571 goto cmdline_not_changed;
1572
Bram Moolenaard23a8232018-02-10 18:45:26 +01001573 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (!cmd_silent)
1575 {
1576#ifdef FEAT_RIGHTLEFT
1577 if (cmdmsg_rl)
1578 msg_col = Columns;
1579 else
1580#endif
1581 msg_col = 0;
1582 msg_putchar(' '); /* delete ':' */
1583 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001584#ifdef FEAT_SEARCH_EXTRA
1585 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001586 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 redraw_cmdline = TRUE;
1589 goto returncmd; /* back to cmd mode */
1590 }
1591 goto cmdline_changed;
1592
1593 case K_INS:
1594 case K_KINS:
1595#ifdef FEAT_FKMAP
1596 /* if Farsi mode set, we are in reverse insert mode -
1597 Do not change the mode */
1598 if (cmd_fkmap)
1599 beep_flush();
1600 else
1601#endif
1602 ccline.overstrike = !ccline.overstrike;
1603#ifdef CURSOR_SHAPE
1604 ui_cursor_shape(); /* may show different cursor shape */
1605#endif
1606 goto cmdline_not_changed;
1607
1608 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001609 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 /* ":lmap" mappings exists, toggle use of mappings. */
1612 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001613#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 im_set_active(FALSE); /* Disable input method */
1615#endif
1616 if (b_im_ptr != NULL)
1617 {
1618 if (State & LANGMAP)
1619 *b_im_ptr = B_IMODE_LMAP;
1620 else
1621 *b_im_ptr = B_IMODE_NONE;
1622 }
1623 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001624#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 else
1626 {
1627 /* There are no ":lmap" mappings, toggle IM. When
1628 * 'imdisable' is set don't try getting the status, it's
1629 * always off. */
1630 if ((p_imdisable && b_im_ptr != NULL)
1631 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1632 {
1633 im_set_active(FALSE); /* Disable input method */
1634 if (b_im_ptr != NULL)
1635 *b_im_ptr = B_IMODE_NONE;
1636 }
1637 else
1638 {
1639 im_set_active(TRUE); /* Enable input method */
1640 if (b_im_ptr != NULL)
1641 *b_im_ptr = B_IMODE_IM;
1642 }
1643 }
1644#endif
1645 if (b_im_ptr != NULL)
1646 {
1647 if (b_im_ptr == &curbuf->b_p_iminsert)
1648 set_iminsert_global();
1649 else
1650 set_imsearch_global();
1651 }
1652#ifdef CURSOR_SHAPE
1653 ui_cursor_shape(); /* may show different cursor shape */
1654#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001655#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 /* Show/unshow value of 'keymap' in status lines later. */
1657 status_redraw_curbuf();
1658#endif
1659 goto cmdline_not_changed;
1660
1661/* case '@': only in very old vi */
1662 case Ctrl_U:
1663 /* delete all characters left of the cursor */
1664 j = ccline.cmdpos;
1665 ccline.cmdlen -= j;
1666 i = ccline.cmdpos = 0;
1667 while (i < ccline.cmdlen)
1668 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1669 /* Truncate at the end, required for multi-byte chars. */
1670 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001671#ifdef FEAT_SEARCH_EXTRA
1672 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001673 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 redrawcmd();
1676 goto cmdline_changed;
1677
1678#ifdef FEAT_CLIPBOARD
1679 case Ctrl_Y:
1680 /* Copy the modeless selection, if there is one. */
1681 if (clip_star.state != SELECT_CLEARED)
1682 {
1683 if (clip_star.state == SELECT_DONE)
1684 clip_copy_modeless_selection(TRUE);
1685 goto cmdline_not_changed;
1686 }
1687 break;
1688#endif
1689
1690 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1691 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001692 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001693 * ":normal" runs out of characters. */
1694 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001695 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 goto cmdline_not_changed;
1697
1698 gotesc = TRUE; /* will free ccline.cmdbuff after
1699 putting it in history */
1700 goto returncmd; /* back to cmd mode */
1701
1702 case Ctrl_R: /* insert register */
1703#ifdef USE_ON_FLY_SCROLL
1704 dont_scroll = TRUE; /* disallow scrolling here */
1705#endif
1706 putcmdline('"', TRUE);
1707 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001708 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if (i == Ctrl_O)
1710 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1711 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001712 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001713 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 --no_mapping;
1715#ifdef FEAT_EVAL
1716 /*
1717 * Insert the result of an expression.
1718 * Need to save the current command line, to be able to enter
1719 * a new one...
1720 */
1721 new_cmdpos = -1;
1722 if (c == '=')
1723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1725 {
1726 beep_flush();
1727 c = ESC;
1728 }
1729 else
1730 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001731 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001733 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
1735 }
1736#endif
1737 if (c != ESC) /* use ESC to cancel inserting register */
1738 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001739 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001740
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001741#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001742 /* When there was a serious error abort getting the
1743 * command line. */
1744 if (aborting())
1745 {
1746 gotesc = TRUE; /* will free ccline.cmdbuff after
1747 putting it in history */
1748 goto returncmd; /* back to cmd mode */
1749 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 KeyTyped = FALSE; /* Don't do p_wc completion. */
1752#ifdef FEAT_EVAL
1753 if (new_cmdpos >= 0)
1754 {
1755 /* set_cmdline_pos() was used */
1756 if (new_cmdpos > ccline.cmdlen)
1757 ccline.cmdpos = ccline.cmdlen;
1758 else
1759 ccline.cmdpos = new_cmdpos;
1760 }
1761#endif
1762 }
1763 redrawcmd();
1764 goto cmdline_changed;
1765
1766 case Ctrl_D:
1767 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1768 break; /* Use ^D as normal char instead */
1769
1770 redrawcmd();
1771 continue; /* don't do incremental search now */
1772
1773 case K_RIGHT:
1774 case K_S_RIGHT:
1775 case K_C_RIGHT:
1776 do
1777 {
1778 if (ccline.cmdpos >= ccline.cmdlen)
1779 break;
1780 i = cmdline_charsize(ccline.cmdpos);
1781 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1782 break;
1783 ccline.cmdspos += i;
1784#ifdef FEAT_MBYTE
1785 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001786 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 + ccline.cmdpos);
1788 else
1789#endif
1790 ++ccline.cmdpos;
1791 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001792 while ((c == K_S_RIGHT || c == K_C_RIGHT
1793 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1795#ifdef FEAT_MBYTE
1796 if (has_mbyte)
1797 set_cmdspos_cursor();
1798#endif
1799 goto cmdline_not_changed;
1800
1801 case K_LEFT:
1802 case K_S_LEFT:
1803 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001804 if (ccline.cmdpos == 0)
1805 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 do
1807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 --ccline.cmdpos;
1809#ifdef FEAT_MBYTE
1810 if (has_mbyte) /* move to first byte of char */
1811 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1812 ccline.cmdbuff + ccline.cmdpos);
1813#endif
1814 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1815 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001816 while (ccline.cmdpos > 0
1817 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001818 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1820#ifdef FEAT_MBYTE
1821 if (has_mbyte)
1822 set_cmdspos_cursor();
1823#endif
1824 goto cmdline_not_changed;
1825
1826 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001827 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001828 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar4770d092006-01-12 23:22:24 +00001830#ifdef FEAT_GUI_W32
1831 /* On Win32 ignore <M-F4>, we get it when closing the window was
1832 * cancelled. */
1833 case K_F4:
1834 if (mod_mask == MOD_MASK_ALT)
1835 {
1836 redrawcmd(); /* somehow the cmdline is cleared */
1837 goto cmdline_not_changed;
1838 }
1839 break;
1840#endif
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#ifdef FEAT_MOUSE
1843 case K_MIDDLEDRAG:
1844 case K_MIDDLERELEASE:
1845 goto cmdline_not_changed; /* Ignore mouse */
1846
1847 case K_MIDDLEMOUSE:
1848# ifdef FEAT_GUI
1849 /* When GUI is active, also paste when 'mouse' is empty */
1850 if (!gui.in_use)
1851# endif
1852 if (!mouse_has(MOUSE_COMMAND))
1853 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001854# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001856 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001858# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001859 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 redrawcmd();
1861 goto cmdline_changed;
1862
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001863# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001865 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 redrawcmd();
1867 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
1870 case K_LEFTDRAG:
1871 case K_LEFTRELEASE:
1872 case K_RIGHTDRAG:
1873 case K_RIGHTRELEASE:
1874 /* Ignore drag and release events when the button-down wasn't
1875 * seen before. */
1876 if (ignore_drag_release)
1877 goto cmdline_not_changed;
1878 /* FALLTHROUGH */
1879 case K_LEFTMOUSE:
1880 case K_RIGHTMOUSE:
1881 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1882 ignore_drag_release = TRUE;
1883 else
1884 ignore_drag_release = FALSE;
1885# ifdef FEAT_GUI
1886 /* When GUI is active, also move when 'mouse' is empty */
1887 if (!gui.in_use)
1888# endif
1889 if (!mouse_has(MOUSE_COMMAND))
1890 goto cmdline_not_changed; /* Ignore mouse */
1891# ifdef FEAT_CLIPBOARD
1892 if (mouse_row < cmdline_row && clip_star.available)
1893 {
1894 int button, is_click, is_drag;
1895
1896 /*
1897 * Handle modeless selection.
1898 */
1899 button = get_mouse_button(KEY2TERMCAP1(c),
1900 &is_click, &is_drag);
1901 if (mouse_model_popup() && button == MOUSE_LEFT
1902 && (mod_mask & MOD_MASK_SHIFT))
1903 {
1904 /* Translate shift-left to right button. */
1905 button = MOUSE_RIGHT;
1906 mod_mask &= ~MOD_MASK_SHIFT;
1907 }
1908 clip_modeless(button, is_click, is_drag);
1909 goto cmdline_not_changed;
1910 }
1911# endif
1912
1913 set_cmdspos();
1914 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1915 ++ccline.cmdpos)
1916 {
1917 i = cmdline_charsize(ccline.cmdpos);
1918 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1919 && mouse_col < ccline.cmdspos % Columns + i)
1920 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001921# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (has_mbyte)
1923 {
1924 /* Count ">" for double-wide char that doesn't fit. */
1925 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001926 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 + ccline.cmdpos) - 1;
1928 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 ccline.cmdspos += i;
1931 }
1932 goto cmdline_not_changed;
1933
1934 /* Mouse scroll wheel: ignored here */
1935 case K_MOUSEDOWN:
1936 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001937 case K_MOUSELEFT:
1938 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 /* Alternate buttons ignored here */
1940 case K_X1MOUSE:
1941 case K_X1DRAG:
1942 case K_X1RELEASE:
1943 case K_X2MOUSE:
1944 case K_X2DRAG:
1945 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001946 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 goto cmdline_not_changed;
1948
1949#endif /* FEAT_MOUSE */
1950
1951#ifdef FEAT_GUI
1952 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1953 case K_LEFTRELEASE_NM:
1954 goto cmdline_not_changed;
1955
1956 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001957 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 gui_do_scroll();
1960 redrawcmd();
1961 }
1962 goto cmdline_not_changed;
1963
1964 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001965 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001967 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 redrawcmd();
1969 }
1970 goto cmdline_not_changed;
1971#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001972#ifdef FEAT_GUI_TABLINE
1973 case K_TABLINE:
1974 case K_TABMENU:
1975 /* Don't want to change any tabs here. Make sure the same tab
1976 * is still selected. */
1977 if (gui_use_tabline())
1978 gui_mch_set_curtab(tabpage_index(curtab));
1979 goto cmdline_not_changed;
1980#endif
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 case K_SELECT: /* end of Select mode mapping - ignore */
1983 goto cmdline_not_changed;
1984
1985 case Ctrl_B: /* begin of command line */
1986 case K_HOME:
1987 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 case K_S_HOME:
1989 case K_C_HOME:
1990 ccline.cmdpos = 0;
1991 set_cmdspos();
1992 goto cmdline_not_changed;
1993
1994 case Ctrl_E: /* end of command line */
1995 case K_END:
1996 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 case K_S_END:
1998 case K_C_END:
1999 ccline.cmdpos = ccline.cmdlen;
2000 set_cmdspos_cursor();
2001 goto cmdline_not_changed;
2002
2003 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002004 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 break;
2006 goto cmdline_changed;
2007
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002008 case Ctrl_L:
2009#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002010 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002011 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002012#endif
2013
2014 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002015 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 break;
2017 goto cmdline_changed;
2018
2019 case Ctrl_N: /* next match */
2020 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002021 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002023 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2024 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002026 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002029 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 case K_UP:
2031 case K_DOWN:
2032 case K_S_UP:
2033 case K_S_DOWN:
2034 case K_PAGEUP:
2035 case K_KPAGEUP:
2036 case K_PAGEDOWN:
2037 case K_KPAGEDOWN:
2038 if (hislen == 0 || firstc == NUL) /* no history */
2039 goto cmdline_not_changed;
2040
2041 i = hiscnt;
2042
2043 /* save current command string so it can be restored later */
2044 if (lookfor == NULL)
2045 {
2046 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2047 goto cmdline_not_changed;
2048 lookfor[ccline.cmdpos] = NUL;
2049 }
2050
2051 j = (int)STRLEN(lookfor);
2052 for (;;)
2053 {
2054 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002055 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002056 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 if (hiscnt == hislen) /* first time */
2059 hiscnt = hisidx[histype];
2060 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2061 hiscnt = hislen - 1;
2062 else if (hiscnt != hisidx[histype] + 1)
2063 --hiscnt;
2064 else /* at top of list */
2065 {
2066 hiscnt = i;
2067 break;
2068 }
2069 }
2070 else /* one step forwards */
2071 {
2072 /* on last entry, clear the line */
2073 if (hiscnt == hisidx[histype])
2074 {
2075 hiscnt = hislen;
2076 break;
2077 }
2078
2079 /* not on a history line, nothing to do */
2080 if (hiscnt == hislen)
2081 break;
2082 if (hiscnt == hislen - 1) /* wrap around */
2083 hiscnt = 0;
2084 else
2085 ++hiscnt;
2086 }
2087 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2088 {
2089 hiscnt = i;
2090 break;
2091 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002092 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002093 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 || STRNCMP(history[histype][hiscnt].hisstr,
2095 lookfor, (size_t)j) == 0)
2096 break;
2097 }
2098
2099 if (hiscnt != i) /* jumped to other entry */
2100 {
2101 char_u *p;
2102 int len;
2103 int old_firstc;
2104
2105 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002106 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (hiscnt == hislen)
2108 p = lookfor; /* back to the old one */
2109 else
2110 p = history[histype][hiscnt].hisstr;
2111
2112 if (histype == HIST_SEARCH
2113 && p != lookfor
2114 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2115 {
2116 /* Correct for the separator character used when
2117 * adding the history entry vs the one used now.
2118 * First loop: count length.
2119 * Second loop: copy the characters. */
2120 for (i = 0; i <= 1; ++i)
2121 {
2122 len = 0;
2123 for (j = 0; p[j] != NUL; ++j)
2124 {
2125 /* Replace old sep with new sep, unless it is
2126 * escaped. */
2127 if (p[j] == old_firstc
2128 && (j == 0 || p[j - 1] != '\\'))
2129 {
2130 if (i > 0)
2131 ccline.cmdbuff[len] = firstc;
2132 }
2133 else
2134 {
2135 /* Escape new sep, unless it is already
2136 * escaped. */
2137 if (p[j] == firstc
2138 && (j == 0 || p[j - 1] != '\\'))
2139 {
2140 if (i > 0)
2141 ccline.cmdbuff[len] = '\\';
2142 ++len;
2143 }
2144 if (i > 0)
2145 ccline.cmdbuff[len] = p[j];
2146 }
2147 ++len;
2148 }
2149 if (i == 0)
2150 {
2151 alloc_cmdbuff(len);
2152 if (ccline.cmdbuff == NULL)
2153 goto returncmd;
2154 }
2155 }
2156 ccline.cmdbuff[len] = NUL;
2157 }
2158 else
2159 {
2160 alloc_cmdbuff((int)STRLEN(p));
2161 if (ccline.cmdbuff == NULL)
2162 goto returncmd;
2163 STRCPY(ccline.cmdbuff, p);
2164 }
2165
2166 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2167 redrawcmd();
2168 goto cmdline_changed;
2169 }
2170 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002171#endif
2172 goto cmdline_not_changed;
2173
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002174#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002175 case Ctrl_G: /* next match */
2176 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002177 if (may_adjust_incsearch_highlighting(
2178 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002179 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181#endif
2182
2183 case Ctrl_V:
2184 case Ctrl_Q:
2185#ifdef FEAT_MOUSE
2186 ignore_drag_release = TRUE;
2187#endif
2188 putcmdline('^', TRUE);
2189 c = get_literal(); /* get next (two) character(s) */
2190 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002191 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#ifdef FEAT_MBYTE
2193 /* may need to remove ^ when composing char was typed */
2194 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2195 {
2196 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2197 msg_putchar(' ');
2198 cursorcmd();
2199 }
2200#endif
2201 break;
2202
2203#ifdef FEAT_DIGRAPHS
2204 case Ctrl_K:
2205#ifdef FEAT_MOUSE
2206 ignore_drag_release = TRUE;
2207#endif
2208 putcmdline('?', TRUE);
2209#ifdef USE_ON_FLY_SCROLL
2210 dont_scroll = TRUE; /* disallow scrolling here */
2211#endif
2212 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002213 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (c != NUL)
2215 break;
2216
2217 redrawcmd();
2218 goto cmdline_not_changed;
2219#endif /* FEAT_DIGRAPHS */
2220
2221#ifdef FEAT_RIGHTLEFT
2222 case Ctrl__: /* CTRL-_: switch language mode */
2223 if (!p_ari)
2224 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002225# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 if (p_altkeymap)
2227 {
2228 cmd_fkmap = !cmd_fkmap;
2229 if (cmd_fkmap) /* in Farsi always in Insert mode */
2230 ccline.overstrike = FALSE;
2231 }
2232 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 cmd_hkmap = !cmd_hkmap;
2235 goto cmdline_not_changed;
2236#endif
2237
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002238 case K_PS:
2239 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2240 goto cmdline_changed;
2241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 default:
2243#ifdef UNIX
2244 if (c == intr_char)
2245 {
2246 gotesc = TRUE; /* will free ccline.cmdbuff after
2247 putting it in history */
2248 goto returncmd; /* back to Normal mode */
2249 }
2250#endif
2251 /*
2252 * Normal character with no special meaning. Just set mod_mask
2253 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2254 * the string <S-Space>. This should only happen after ^V.
2255 */
2256 if (!IS_SPECIAL(c))
2257 mod_mask = 0x0;
2258 break;
2259 }
2260 /*
2261 * End of switch on command line character.
2262 * We come here if we have a normal character.
2263 */
2264
Bram Moolenaarede3e632013-06-23 16:16:19 +02002265 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#ifdef FEAT_MBYTE
2267 /* Add ABBR_OFF for characters above 0x100, this is
2268 * what check_abbr() expects. */
2269 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2270#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002271 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 goto cmdline_changed;
2273
2274 /*
2275 * put the character in the command line
2276 */
2277 if (IS_SPECIAL(c) || mod_mask != 0)
2278 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2279 else
2280 {
2281#ifdef FEAT_MBYTE
2282 if (has_mbyte)
2283 {
2284 j = (*mb_char2bytes)(c, IObuff);
2285 IObuff[j] = NUL; /* exclude composing chars */
2286 put_on_cmdline(IObuff, j, TRUE);
2287 }
2288 else
2289#endif
2290 {
2291 IObuff[0] = c;
2292 put_on_cmdline(IObuff, 1, TRUE);
2293 }
2294 }
2295 goto cmdline_changed;
2296
2297/*
2298 * This part implements incremental searches for "/" and "?"
2299 * Jump to cmdline_not_changed when a character has been read but the command
2300 * line did not change. Then we only search and redraw if something changed in
2301 * the past.
2302 * Jump to cmdline_changed when the command line did change.
2303 * (Sorry for the goto's, I know it is ugly).
2304 */
2305cmdline_not_changed:
2306#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002307 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 continue;
2309#endif
2310
2311cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002312 /* Trigger CmdlineChanged autocommands. */
2313 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002316 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#endif
2318
2319#ifdef FEAT_RIGHTLEFT
2320 if (cmdmsg_rl
2321# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002322 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323# endif
2324 )
2325 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002326 * right-left typing. Not efficient, but it works.
2327 * Do it only when there are no characters left to read
2328 * to avoid useless intermediate redraws. */
2329 if (vpeekc() == NUL)
2330 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332 }
2333
2334returncmd:
2335
2336#ifdef FEAT_RIGHTLEFT
2337 cmdmsg_rl = FALSE;
2338#endif
2339
2340#ifdef FEAT_FKMAP
2341 cmd_fkmap = 0;
2342#endif
2343
2344 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002345 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002348 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#endif
2350
2351 if (ccline.cmdbuff != NULL)
2352 {
2353 /*
2354 * Put line in history buffer (":" and "=" only when it was typed).
2355 */
2356#ifdef FEAT_CMDHIST
2357 if (ccline.cmdlen && firstc != NUL
2358 && (some_key_typed || histype == HIST_SEARCH))
2359 {
2360 add_to_history(histype, ccline.cmdbuff, TRUE,
2361 histype == HIST_SEARCH ? firstc : NUL);
2362 if (firstc == ':')
2363 {
2364 vim_free(new_last_cmdline);
2365 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2366 }
2367 }
2368#endif
2369
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002370 if (gotesc)
2371 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373
2374 /*
2375 * If the screen was shifted up, redraw the whole screen (later).
2376 * If the line is too long, clear it, so ruler and shown command do
2377 * not get printed in the middle of it.
2378 */
2379 msg_check();
2380 msg_scroll = save_msg_scroll;
2381 redir_off = FALSE;
2382
2383 /* When the command line was typed, no need for a wait-return prompt. */
2384 if (some_key_typed)
2385 need_wait_return = FALSE;
2386
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002387 /* Trigger CmdlineLeave autocommands. */
2388 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002391#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2393 im_save_status(b_im_ptr);
2394 im_set_active(FALSE);
2395#endif
2396#ifdef FEAT_MOUSE
2397 setmouse();
2398#endif
2399#ifdef CURSOR_SHAPE
2400 ui_cursor_shape(); /* may show different cursor shape */
2401#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002402 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002404 {
2405 char_u *p = ccline.cmdbuff;
2406
2407 /* Make ccline empty, getcmdline() may try to use it. */
2408 ccline.cmdbuff = NULL;
2409 return p;
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411}
2412
2413#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2414/*
2415 * Get a command line with a prompt.
2416 * This is prepared to be called recursively from getcmdline() (e.g. by
2417 * f_input() when evaluating an expression from CTRL-R =).
2418 * Returns the command line in allocated memory, or NULL.
2419 */
2420 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002421getcmdline_prompt(
2422 int firstc,
2423 char_u *prompt, /* command line prompt */
2424 int attr, /* attributes for prompt */
2425 int xp_context, /* type of expansion */
2426 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 char_u *s;
2429 struct cmdline_info save_ccline;
2430 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002431 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002433 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 ccline.cmdprompt = prompt;
2435 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002436# ifdef FEAT_EVAL
2437 ccline.xp_context = xp_context;
2438 ccline.xp_arg = xp_arg;
2439 ccline.input_fn = (firstc == '@');
2440# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002441 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002443 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002444 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002445 /* Restore msg_col, the prompt from input() may have changed it.
2446 * But only if called recursively and the commandline is therefore being
2447 * restored to an old one; if not, the input() prompt stays on the screen,
2448 * so we need its modified msg_col left intact. */
2449 if (ccline.cmdbuff != NULL)
2450 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 return s;
2453}
2454#endif
2455
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002456/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002457 * Return TRUE when the text must not be changed and we can't switch to
2458 * another window or buffer. Used when editing the command line, evaluating
2459 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002460 */
2461 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002462text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002463{
2464#ifdef FEAT_CMDWIN
2465 if (cmdwin_type != 0)
2466 return TRUE;
2467#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002468 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002469}
2470
2471/*
2472 * Give an error message for a command that isn't allowed while the cmdline
2473 * window is open or editing the cmdline in another way.
2474 */
2475 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002476text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002477{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002478 EMSG(_(get_text_locked_msg()));
2479}
2480
2481 char_u *
2482get_text_locked_msg(void)
2483{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002484#ifdef FEAT_CMDWIN
2485 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002486 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002487#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002488 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002489}
2490
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002491/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002492 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2493 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002494 */
2495 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002496curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002497{
2498 if (curbuf_lock > 0)
2499 {
2500 EMSG(_("E788: Not allowed to edit another buffer now"));
2501 return TRUE;
2502 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002503 return allbuf_locked();
2504}
2505
2506/*
2507 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2508 * message.
2509 */
2510 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002511allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002512{
2513 if (allbuf_lock > 0)
2514 {
2515 EMSG(_("E811: Not allowed to change buffer information now"));
2516 return TRUE;
2517 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002518 return FALSE;
2519}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002522cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2525 if (cmdline_star > 0) /* showing '*', always 1 position */
2526 return 1;
2527#endif
2528 return ptr2cells(ccline.cmdbuff + idx);
2529}
2530
2531/*
2532 * Compute the offset of the cursor on the command line for the prompt and
2533 * indent.
2534 */
2535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002536set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002538 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 ccline.cmdspos = 1 + ccline.cmdindent;
2540 else
2541 ccline.cmdspos = 0 + ccline.cmdindent;
2542}
2543
2544/*
2545 * Compute the screen position for the cursor on the command line.
2546 */
2547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002548set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550 int i, m, c;
2551
2552 set_cmdspos();
2553 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002556 if (m < 0) /* overflow, Columns or Rows at weird value */
2557 m = MAXCOL;
2558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 else
2560 m = MAXCOL;
2561 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2562 {
2563 c = cmdline_charsize(i);
2564#ifdef FEAT_MBYTE
2565 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2566 if (has_mbyte)
2567 correct_cmdspos(i, c);
2568#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002569 /* If the cmdline doesn't fit, show cursor on last visible char.
2570 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if ((ccline.cmdspos += c) >= m)
2572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 ccline.cmdspos -= c;
2574 break;
2575 }
2576#ifdef FEAT_MBYTE
2577 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002578 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579#endif
2580 }
2581}
2582
2583#ifdef FEAT_MBYTE
2584/*
2585 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2586 * character that doesn't fit, so that a ">" must be displayed.
2587 */
2588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002589correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002591 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2593 && ccline.cmdspos % Columns + cells > Columns)
2594 ccline.cmdspos++;
2595}
2596#endif
2597
2598/*
2599 * Get an Ex command line for the ":" command.
2600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602getexline(
2603 int c, /* normally ':', NUL for ":append" */
2604 void *cookie UNUSED,
2605 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 /* When executing a register, remove ':' that's in front of each line. */
2608 if (exec_from_reg && vpeekc() == ':')
2609 (void)vgetc();
2610 return getcmdline(c, 1L, indent);
2611}
2612
2613/*
2614 * Get an Ex command line for Ex mode.
2615 * In Ex mode we only use the OS supplied line editing features and no
2616 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002617 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002620getexmodeline(
2621 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002622 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002623 void *cookie UNUSED,
2624 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 garray_T line_ga;
2627 char_u *pend;
2628 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002629 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002630 int escaped = FALSE; /* CTRL-V typed */
2631 int vcol = 0;
2632 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002633 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002634 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
2636 /* Switch cursor on now. This avoids that it happens after the "\n", which
2637 * confuses the system function that computes tabstops. */
2638 cursor_on();
2639
2640 /* always start in column 0; write a newline if necessary */
2641 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002642 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002646 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002647 if (p_prompt)
2648 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 while (indent-- > 0)
2650 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
2654 ga_init2(&line_ga, 1, 30);
2655
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002656 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002657 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002658 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002659 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002660 while (indent >= 8)
2661 {
2662 ga_append(&line_ga, TAB);
2663 msg_puts((char_u *)" ");
2664 indent -= 8;
2665 }
2666 while (indent-- > 0)
2667 {
2668 ga_append(&line_ga, ' ');
2669 msg_putchar(' ');
2670 }
2671 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672 ++no_mapping;
2673 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 /*
2676 * Get the line, one character at a time.
2677 */
2678 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002679 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002681 long sw;
2682 char_u *s;
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (ga_grow(&line_ga, 40) == FAIL)
2685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
Bram Moolenaarba748c82017-02-26 14:00:07 +01002687 /*
2688 * Get one character at a time.
2689 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002690 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002691
2692 /* Check for a ":normal" command and no more characters left. */
2693 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2694 c1 = '\n';
2695 else
2696 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002699 * Handle line editing.
2700 * Previously this was left to the system, putting the terminal in
2701 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002703 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002705 msg_putchar('\n');
2706 break;
2707 }
2708
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002709 if (c1 == K_PS)
2710 {
2711 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2712 goto redraw;
2713 }
2714
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 if (!escaped)
2716 {
2717 /* CR typed means "enter", which is NL */
2718 if (c1 == '\r')
2719 c1 = '\n';
2720
2721 if (c1 == BS || c1 == K_BS
2722 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 if (line_ga.ga_len > 0)
2725 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002726#ifdef FEAT_MBYTE
2727 if (has_mbyte)
2728 {
2729 p = (char_u *)line_ga.ga_data;
2730 p[line_ga.ga_len] = NUL;
2731 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2732 line_ga.ga_len -= len;
2733 }
2734 else
2735#endif
2736 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 goto redraw;
2738 }
2739 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002742 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 msg_col = startcol;
2745 msg_clr_eos();
2746 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002747 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748 }
2749
2750 if (c1 == Ctrl_T)
2751 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002752 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 p = (char_u *)line_ga.ga_data;
2754 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002755 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002756 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002758 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002760 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002761 p = (char_u *)line_ga.ga_data;
2762 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2764 *s = ' ';
2765 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767redraw:
2768 /* redraw the line */
2769 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002771 p = (char_u *)line_ga.ga_data;
2772 p[line_ga.ga_len] = NUL;
2773 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002775 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002777 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 msg_putchar(' ');
2780 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002781 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002785 len = MB_PTR2LEN(p);
2786 msg_outtrans_len(p, len);
2787 vcol += ptr2cells(p);
2788 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002792 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 continue;
2794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002796 if (c1 == Ctrl_D)
2797 {
2798 /* Delete one shiftwidth. */
2799 p = (char_u *)line_ga.ga_data;
2800 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 if (prev_char == '^')
2803 ex_keep_indent = TRUE;
2804 indent = 0;
2805 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 else
2808 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002810 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002811 if (indent > 0)
2812 {
2813 --indent;
2814 indent -= indent % get_sw_value(curbuf);
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002817 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002818 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002819 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2821 --line_ga.ga_len;
2822 }
2823 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002825
2826 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2827 {
2828 escaped = TRUE;
2829 continue;
2830 }
2831
2832 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2833 if (IS_SPECIAL(c1))
2834 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002836
2837 if (IS_SPECIAL(c1))
2838 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002839#ifdef FEAT_MBYTE
2840 if (has_mbyte)
2841 len = (*mb_char2bytes)(c1,
2842 (char_u *)line_ga.ga_data + line_ga.ga_len);
2843 else
2844#endif
2845 {
2846 len = 1;
2847 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2848 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 if (c1 == '\n')
2850 msg_putchar('\n');
2851 else if (c1 == TAB)
2852 {
2853 /* Don't use chartabsize(), 'ts' can be different */
2854 do
2855 {
2856 msg_putchar(' ');
2857 } while (++vcol % 8);
2858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002862 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002865 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002866 escaped = FALSE;
2867
2868 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002869 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002871 /* We are done when a NL is entered, but not when it comes after an
2872 * odd number of backslashes, that results in a NUL. */
2873 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002875 int bcount = 0;
2876
2877 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2878 ++bcount;
2879
2880 if (bcount > 0)
2881 {
2882 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2883 * "\NL", etc. */
2884 line_ga.ga_len -= (bcount + 1) / 2;
2885 pend -= (bcount + 1) / 2;
2886 pend[-1] = '\n';
2887 }
2888
2889 if ((bcount & 1) == 0)
2890 {
2891 --line_ga.ga_len;
2892 --pend;
2893 *pend = NUL;
2894 break;
2895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897 }
2898
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002899 --no_mapping;
2900 --allow_keys;
2901
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 /* make following messages go to the next line */
2903 msg_didout = FALSE;
2904 msg_col = 0;
2905 if (msg_row < Rows - 1)
2906 ++msg_row;
2907 emsg_on_display = FALSE; /* don't want ui_delay() */
2908
2909 if (got_int)
2910 ga_clear(&line_ga);
2911
2912 return (char_u *)line_ga.ga_data;
2913}
2914
Bram Moolenaare344bea2005-09-01 20:46:49 +00002915# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2916 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917/*
2918 * Return TRUE if ccline.overstrike is on.
2919 */
2920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002921cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 return ccline.overstrike;
2924}
2925
2926/*
2927 * Return TRUE if the cursor is at the end of the cmdline.
2928 */
2929 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002930cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 return (ccline.cmdpos >= ccline.cmdlen);
2933}
2934#endif
2935
Bram Moolenaar9372a112005-12-06 19:59:18 +00002936#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937/*
2938 * Return the virtual column number at the current cursor position.
2939 * This is used by the IM code to obtain the start of the preedit string.
2940 */
2941 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002942cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2945 return MAXCOL;
2946
2947# ifdef FEAT_MBYTE
2948 if (has_mbyte)
2949 {
2950 colnr_T col;
2951 int i = 0;
2952
2953 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002954 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
2956 return col;
2957 }
2958 else
2959# endif
2960 return ccline.cmdpos;
2961}
2962#endif
2963
2964#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2965/*
2966 * If part of the command line is an IM preedit string, redraw it with
2967 * IM feedback attributes. The cursor position is restored after drawing.
2968 */
2969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002970redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 if ((State & CMDLINE)
2973 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002974 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 && !p_imdisable
2976 && im_is_preediting())
2977 {
2978 int cmdpos = 0;
2979 int cmdspos;
2980 int old_row;
2981 int old_col;
2982 colnr_T col;
2983
2984 old_row = msg_row;
2985 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002986 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987
2988# ifdef FEAT_MBYTE
2989 if (has_mbyte)
2990 {
2991 for (col = 0; col < preedit_start_col
2992 && cmdpos < ccline.cmdlen; ++col)
2993 {
2994 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002995 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997 }
2998 else
2999# endif
3000 {
3001 cmdspos += preedit_start_col;
3002 cmdpos += preedit_start_col;
3003 }
3004
3005 msg_row = cmdline_row + (cmdspos / (int)Columns);
3006 msg_col = cmdspos % (int)Columns;
3007 if (msg_row >= Rows)
3008 msg_row = Rows - 1;
3009
3010 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3011 {
3012 int char_len;
3013 int char_attr;
3014
3015 char_attr = im_get_feedback_attr(col);
3016 if (char_attr < 0)
3017 break; /* end of preedit string */
3018
3019# ifdef FEAT_MBYTE
3020 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003021 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 else
3023# endif
3024 char_len = 1;
3025
3026 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3027 cmdpos += char_len;
3028 }
3029
3030 msg_row = old_row;
3031 msg_col = old_col;
3032 }
3033}
3034#endif /* FEAT_XIM && FEAT_GUI_GTK */
3035
3036/*
3037 * Allocate a new command line buffer.
3038 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3039 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3040 */
3041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 /*
3045 * give some extra space to avoid having to allocate all the time
3046 */
3047 if (len < 80)
3048 len = 100;
3049 else
3050 len += 20;
3051
3052 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3053 ccline.cmdbufflen = len;
3054}
3055
3056/*
3057 * Re-allocate the command line to length len + something extra.
3058 * return FAIL for failure, OK otherwise
3059 */
3060 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 char_u *p;
3064
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003065 if (len < ccline.cmdbufflen)
3066 return OK; /* no need to resize */
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 p = ccline.cmdbuff;
3069 alloc_cmdbuff(len); /* will get some more */
3070 if (ccline.cmdbuff == NULL) /* out of memory */
3071 {
3072 ccline.cmdbuff = p; /* keep the old one */
3073 return FAIL;
3074 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003075 /* There isn't always a NUL after the command, but it may need to be
3076 * there, thus copy up to the NUL and add a NUL. */
3077 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3078 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003080
3081 if (ccline.xpc != NULL
3082 && ccline.xpc->xp_pattern != NULL
3083 && ccline.xpc->xp_context != EXPAND_NOTHING
3084 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3085 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003086 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003087
3088 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3089 * to point into the newly allocated memory. */
3090 if (i >= 0 && i <= ccline.cmdlen)
3091 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3092 }
3093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 return OK;
3095}
3096
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003097#if defined(FEAT_ARABIC) || defined(PROTO)
3098static char_u *arshape_buf = NULL;
3099
3100# if defined(EXITFREE) || defined(PROTO)
3101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003102free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003103{
3104 vim_free(arshape_buf);
3105}
3106# endif
3107#endif
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
3110 * Draw part of the cmdline at the current cursor position. But draw stars
3111 * when cmdline_star is TRUE.
3112 */
3113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003114draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3117 int i;
3118
3119 if (cmdline_star > 0)
3120 for (i = 0; i < len; ++i)
3121 {
3122 msg_putchar('*');
3123# ifdef FEAT_MBYTE
3124 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003125 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126# endif
3127 }
3128 else
3129#endif
3130#ifdef FEAT_ARABIC
3131 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 static int buflen = 0;
3134 char_u *p;
3135 int j;
3136 int newlen = 0;
3137 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003138 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 int prev_c = 0;
3140 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003141 int u8c;
3142 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 /*
3146 * Do arabic shaping into a temporary buffer. This is very
3147 * inefficient!
3148 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003149 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
3151 /* Re-allocate the buffer. We keep it around to avoid a lot of
3152 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003153 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003154 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003155 arshape_buf = alloc(buflen);
3156 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return; /* out of memory */
3158 }
3159
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003160 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3161 {
3162 /* Prepend a space to draw the leading composing char on. */
3163 arshape_buf[0] = ' ';
3164 newlen = 1;
3165 }
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 for (j = start; j < start + len; j += mb_l)
3168 {
3169 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003170 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003171 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (ARABIC_CHAR(u8c))
3173 {
3174 /* Do Arabic shaping. */
3175 if (cmdmsg_rl)
3176 {
3177 /* displaying from right to left */
3178 pc = prev_c;
3179 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003180 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (j + mb_l >= start + len)
3182 nc = NUL;
3183 else
3184 nc = utf_ptr2char(p + mb_l);
3185 }
3186 else
3187 {
3188 /* displaying from left to right */
3189 if (j + mb_l >= start + len)
3190 pc = NUL;
3191 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003192 {
3193 int pcc[MAX_MCO];
3194
3195 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003197 pc1 = pcc[0];
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 nc = prev_c;
3200 }
3201 prev_c = u8c;
3202
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003203 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003206 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003208 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3209 if (u8cc[1] != 0)
3210 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003211 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
3213 }
3214 else
3215 {
3216 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003217 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 newlen += mb_l;
3219 }
3220 }
3221
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003222 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224 else
3225#endif
3226 msg_outtrans_len(ccline.cmdbuff + start, len);
3227}
3228
3229/*
3230 * Put a character on the command line. Shifts the following text to the
3231 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3232 * "c" must be printable (fit in one display cell)!
3233 */
3234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003235putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 if (cmd_silent)
3238 return;
3239 msg_no_more = TRUE;
3240 msg_putchar(c);
3241 if (shift)
3242 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3243 msg_no_more = FALSE;
3244 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003245 extra_char = c;
3246 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
3248
3249/*
3250 * Undo a putcmdline(c, FALSE).
3251 */
3252 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003253unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 if (cmd_silent)
3256 return;
3257 msg_no_more = TRUE;
3258 if (ccline.cmdlen == ccline.cmdpos)
3259 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003260#ifdef FEAT_MBYTE
3261 else if (has_mbyte)
3262 draw_cmdline(ccline.cmdpos,
3263 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 else
3266 draw_cmdline(ccline.cmdpos, 1);
3267 msg_no_more = FALSE;
3268 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003269 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270}
3271
3272/*
3273 * Put the given string, of the given length, onto the command line.
3274 * If len is -1, then STRLEN() is used to calculate the length.
3275 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3276 * part will be redrawn, otherwise it will not. If this function is called
3277 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3278 * called afterwards.
3279 */
3280 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003281put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 int retval;
3284 int i;
3285 int m;
3286 int c;
3287
3288 if (len < 0)
3289 len = (int)STRLEN(str);
3290
3291 /* Check if ccline.cmdbuff needs to be longer */
3292 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003293 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 else
3295 retval = OK;
3296 if (retval == OK)
3297 {
3298 if (!ccline.overstrike)
3299 {
3300 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3301 ccline.cmdbuff + ccline.cmdpos,
3302 (size_t)(ccline.cmdlen - ccline.cmdpos));
3303 ccline.cmdlen += len;
3304 }
3305 else
3306 {
3307#ifdef FEAT_MBYTE
3308 if (has_mbyte)
3309 {
3310 /* Count nr of characters in the new string. */
3311 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003312 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 ++m;
3314 /* Count nr of bytes in cmdline that are overwritten by these
3315 * characters. */
3316 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003317 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 --m;
3319 if (i < ccline.cmdlen)
3320 {
3321 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3322 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3323 ccline.cmdlen += ccline.cmdpos + len - i;
3324 }
3325 else
3326 ccline.cmdlen = ccline.cmdpos + len;
3327 }
3328 else
3329#endif
3330 if (ccline.cmdpos + len > ccline.cmdlen)
3331 ccline.cmdlen = ccline.cmdpos + len;
3332 }
3333 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3334 ccline.cmdbuff[ccline.cmdlen] = NUL;
3335
3336#ifdef FEAT_MBYTE
3337 if (enc_utf8)
3338 {
3339 /* When the inserted text starts with a composing character,
3340 * backup to the character before it. There could be two of them.
3341 */
3342 i = 0;
3343 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3344 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3345 {
3346 i = (*mb_head_off)(ccline.cmdbuff,
3347 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3348 ccline.cmdpos -= i;
3349 len += i;
3350 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3351 }
3352# ifdef FEAT_ARABIC
3353 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3354 {
3355 /* Check the previous character for Arabic combining pair. */
3356 i = (*mb_head_off)(ccline.cmdbuff,
3357 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3358 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3359 + ccline.cmdpos - i), c))
3360 {
3361 ccline.cmdpos -= i;
3362 len += i;
3363 }
3364 else
3365 i = 0;
3366 }
3367# endif
3368 if (i != 0)
3369 {
3370 /* Also backup the cursor position. */
3371 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3372 ccline.cmdspos -= i;
3373 msg_col -= i;
3374 if (msg_col < 0)
3375 {
3376 msg_col += Columns;
3377 --msg_row;
3378 }
3379 }
3380 }
3381#endif
3382
3383 if (redraw && !cmd_silent)
3384 {
3385 msg_no_more = TRUE;
3386 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003387 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3389 /* Avoid clearing the rest of the line too often. */
3390 if (cmdline_row != i || ccline.overstrike)
3391 msg_clr_eos();
3392 msg_no_more = FALSE;
3393 }
3394#ifdef FEAT_FKMAP
3395 /*
3396 * If we are in Farsi command mode, the character input must be in
3397 * Insert mode. So do not advance the cmdpos.
3398 */
3399 if (!cmd_fkmap)
3400#endif
3401 {
3402 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003405 if (m < 0) /* overflow, Columns or Rows at weird value */
3406 m = MAXCOL;
3407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 else
3409 m = MAXCOL;
3410 for (i = 0; i < len; ++i)
3411 {
3412 c = cmdline_charsize(ccline.cmdpos);
3413#ifdef FEAT_MBYTE
3414 /* count ">" for a double-wide char that doesn't fit. */
3415 if (has_mbyte)
3416 correct_cmdspos(ccline.cmdpos, c);
3417#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003418 /* Stop cursor at the end of the screen, but do increment the
3419 * insert position, so that entering a very long command
3420 * works, even though you can't see it. */
3421 if (ccline.cmdspos + c < m)
3422 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#ifdef FEAT_MBYTE
3424 if (has_mbyte)
3425 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003426 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (c > len - i - 1)
3428 c = len - i - 1;
3429 ccline.cmdpos += c;
3430 i += c;
3431 }
3432#endif
3433 ++ccline.cmdpos;
3434 }
3435 }
3436 }
3437 if (redraw)
3438 msg_check();
3439 return retval;
3440}
3441
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003442static struct cmdline_info prev_ccline;
3443static int prev_ccline_used = FALSE;
3444
3445/*
3446 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3447 * and overwrite it. But get_cmdline_str() may need it, thus make it
3448 * available globally in prev_ccline.
3449 */
3450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003451save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003452{
3453 if (!prev_ccline_used)
3454 {
3455 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3456 prev_ccline_used = TRUE;
3457 }
3458 *ccp = prev_ccline;
3459 prev_ccline = ccline;
3460 ccline.cmdbuff = NULL;
3461 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003462 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003463}
3464
3465/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003466 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003467 */
3468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003469restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003470{
3471 ccline = prev_ccline;
3472 prev_ccline = *ccp;
3473}
3474
Bram Moolenaar5a305422006-04-28 22:38:25 +00003475#if defined(FEAT_EVAL) || defined(PROTO)
3476/*
3477 * Save the command line into allocated memory. Returns a pointer to be
3478 * passed to restore_cmdline_alloc() later.
3479 * Returns NULL when failed.
3480 */
3481 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003482save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003483{
3484 struct cmdline_info *p;
3485
3486 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3487 if (p != NULL)
3488 save_cmdline(p);
3489 return (char_u *)p;
3490}
3491
3492/*
3493 * Restore the command line from the return value of save_cmdline_alloc().
3494 */
3495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003496restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003497{
3498 if (p != NULL)
3499 {
3500 restore_cmdline((struct cmdline_info *)p);
3501 vim_free(p);
3502 }
3503}
3504#endif
3505
Bram Moolenaar8299df92004-07-10 09:47:34 +00003506/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003507 * Paste a yank register into the command line.
3508 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003509 * insert_reg() can't be used here, because special characters from the
3510 * register contents will be interpreted as commands.
3511 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003512 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003513 */
3514 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003515cmdline_paste(
3516 int regname,
3517 int literally, /* Insert text literally instead of "as typed" */
3518 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003519{
3520 long i;
3521 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003522 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003523 int allocated;
3524 struct cmdline_info save_ccline;
3525
3526 /* check for valid regname; also accept special characters for CTRL-R in
3527 * the command line */
3528 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003529 && regname != Ctrl_A && regname != Ctrl_L
3530 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531 return FAIL;
3532
3533 /* A register containing CTRL-R can cause an endless loop. Allow using
3534 * CTRL-C to break the loop. */
3535 line_breakcheck();
3536 if (got_int)
3537 return FAIL;
3538
3539#ifdef FEAT_CLIPBOARD
3540 regname = may_get_selection(regname);
3541#endif
3542
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003543 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003544 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003545 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003546 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003547 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003548 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003549 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003550
3551 if (i)
3552 {
3553 /* Got the value of a special register in "arg". */
3554 if (arg == NULL)
3555 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003556
3557 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3558 * part of the word. */
3559 p = arg;
3560 if (p_is && regname == Ctrl_W)
3561 {
3562 char_u *w;
3563 int len;
3564
3565 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003566 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003567 {
3568#ifdef FEAT_MBYTE
3569 if (has_mbyte)
3570 {
3571 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3572 if (!vim_iswordc(mb_ptr2char(w - len)))
3573 break;
3574 w -= len;
3575 }
3576 else
3577#endif
3578 {
3579 if (!vim_iswordc(w[-1]))
3580 break;
3581 --w;
3582 }
3583 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003584 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003585 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3586 p += len;
3587 }
3588
3589 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003590 if (allocated)
3591 vim_free(arg);
3592 return OK;
3593 }
3594
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003595 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596}
3597
3598/*
3599 * Put a string on the command line.
3600 * When "literally" is TRUE, insert literally.
3601 * When "literally" is FALSE, insert as typed, but don't leave the command
3602 * line.
3603 */
3604 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003605cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003606{
3607 int c, cv;
3608
3609 if (literally)
3610 put_on_cmdline(s, -1, TRUE);
3611 else
3612 while (*s != NUL)
3613 {
3614 cv = *s;
3615 if (cv == Ctrl_V && s[1])
3616 ++s;
3617#ifdef FEAT_MBYTE
3618 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003619 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003620 else
3621#endif
3622 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003623 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3624 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003625#ifdef UNIX
3626 || c == intr_char
3627#endif
3628 || (c == Ctrl_BSL && *s == Ctrl_N))
3629 stuffcharReadbuff(Ctrl_V);
3630 stuffcharReadbuff(c);
3631 }
3632}
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#ifdef FEAT_WILDMENU
3635/*
3636 * Delete characters on the command line, from "from" to the current
3637 * position.
3638 */
3639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003640cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3643 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3644 ccline.cmdlen -= ccline.cmdpos - from;
3645 ccline.cmdpos = from;
3646}
3647#endif
3648
3649/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003650 * This function is called when the screen size changes and with incremental
3651 * search and in other situations where the command line may have been
3652 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
3654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003655redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003657 redrawcmdline_ex(TRUE);
3658}
3659
3660 void
3661redrawcmdline_ex(int do_compute_cmdrow)
3662{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (cmd_silent)
3664 return;
3665 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003666 if (do_compute_cmdrow)
3667 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 redrawcmd();
3669 cursorcmd();
3670}
3671
3672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003673redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 int i;
3676
3677 if (cmd_silent)
3678 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003679 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 msg_putchar(ccline.cmdfirstc);
3681 if (ccline.cmdprompt != NULL)
3682 {
3683 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3684 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3685 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003686 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 --ccline.cmdindent;
3688 }
3689 else
3690 for (i = ccline.cmdindent; i > 0; --i)
3691 msg_putchar(' ');
3692}
3693
3694/*
3695 * Redraw what is currently on the command line.
3696 */
3697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003698redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
3700 if (cmd_silent)
3701 return;
3702
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003703 /* when 'incsearch' is set there may be no command line while redrawing */
3704 if (ccline.cmdbuff == NULL)
3705 {
3706 windgoto(cmdline_row, 0);
3707 msg_clr_eos();
3708 return;
3709 }
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 msg_start();
3712 redrawcmdprompt();
3713
3714 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3715 msg_no_more = TRUE;
3716 draw_cmdline(0, ccline.cmdlen);
3717 msg_clr_eos();
3718 msg_no_more = FALSE;
3719
3720 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003721 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003722 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
3724 /*
3725 * An emsg() before may have set msg_scroll. This is used in normal mode,
3726 * in cmdline mode we can reset them now.
3727 */
3728 msg_scroll = FALSE; /* next message overwrites cmdline */
3729
3730 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3731 * in cmdline mode */
3732 skip_redraw = FALSE;
3733}
3734
3735 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003736compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003738 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 cmdline_row = Rows - 1;
3740 else
3741 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003742 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743}
3744
3745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003746cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
3748 if (cmd_silent)
3749 return;
3750
3751#ifdef FEAT_RIGHTLEFT
3752 if (cmdmsg_rl)
3753 {
3754 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3755 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3756 if (msg_row <= 0)
3757 msg_row = Rows - 1;
3758 }
3759 else
3760#endif
3761 {
3762 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3763 msg_col = ccline.cmdspos % (int)Columns;
3764 if (msg_row >= Rows)
3765 msg_row = Rows - 1;
3766 }
3767
3768 windgoto(msg_row, msg_col);
3769#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003770 if (p_imst == IM_ON_THE_SPOT)
3771 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#endif
3773#ifdef MCH_CURSOR_SHAPE
3774 mch_update_cursor();
3775#endif
3776}
3777
3778 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003779gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
3781 msg_start();
3782#ifdef FEAT_RIGHTLEFT
3783 if (cmdmsg_rl)
3784 msg_col = Columns - 1;
3785 else
3786#endif
3787 msg_col = 0; /* always start in column 0 */
3788 if (clr) /* clear the bottom line(s) */
3789 msg_clr_eos(); /* will reset clear_cmdline */
3790 windgoto(cmdline_row, 0);
3791}
3792
3793/*
3794 * Check the word in front of the cursor for an abbreviation.
3795 * Called when the non-id character "c" has been entered.
3796 * When an abbreviation is recognized it is removed from the text with
3797 * backspaces and the replacement string is inserted, followed by "c".
3798 */
3799 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003800ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003802 int spos = 0;
3803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3805 return FALSE;
3806
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003807 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3808 * Actually accepts any mark. */
3809 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3810 spos++;
3811 if (ccline.cmdlen - spos > 5
3812 && ccline.cmdbuff[spos] == '\''
3813 && ccline.cmdbuff[spos + 2] == ','
3814 && ccline.cmdbuff[spos + 3] == '\'')
3815 spos += 5;
3816 else
3817 /* check abbreviation from the beginning of the commandline */
3818 spos = 0;
3819
3820 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821}
3822
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003823#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3824 static int
3825#ifdef __BORLANDC__
3826_RTLENTRYF
3827#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003828sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003829{
3830 char_u *p1 = *(char_u **)s1;
3831 char_u *p2 = *(char_u **)s2;
3832
3833 if (*p1 != '<' && *p2 == '<') return -1;
3834 if (*p1 == '<' && *p2 != '<') return 1;
3835 return STRCMP(p1, p2);
3836}
3837#endif
3838
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839/*
3840 * Return FAIL if this is not an appropriate context in which to do
3841 * completion of anything, return OK if it is (even if there are no matches).
3842 * For the caller, this means that the character is just passed through like a
3843 * normal character (instead of being expanded). This allows :s/^I^D etc.
3844 */
3845 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003846nextwild(
3847 expand_T *xp,
3848 int type,
3849 int options, /* extra options for ExpandOne() */
3850 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 int i, j;
3853 char_u *p1;
3854 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 int difflen;
3856 int v;
3857
3858 if (xp->xp_numfiles == -1)
3859 {
3860 set_expand_context(xp);
3861 cmd_showtail = expand_showtail(xp);
3862 }
3863
3864 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3865 {
3866 beep_flush();
3867 return OK; /* Something illegal on command line */
3868 }
3869 if (xp->xp_context == EXPAND_NOTHING)
3870 {
3871 /* Caller can use the character as a normal char instead */
3872 return FAIL;
3873 }
3874
3875 MSG_PUTS("..."); /* show that we are busy */
3876 out_flush();
3877
3878 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003879 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 if (type == WILD_NEXT || type == WILD_PREV)
3882 {
3883 /*
3884 * Get next/previous match for a previous expanded pattern.
3885 */
3886 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3887 }
3888 else
3889 {
3890 /*
3891 * Translate string into pattern and expand it.
3892 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003893 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3894 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 p2 = NULL;
3896 else
3897 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003898 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003899 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3900 if (escape)
3901 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003902
3903 if (p_wic)
3904 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003905 p2 = ExpandOne(xp, p1,
3906 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003907 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003909 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if (p2 != NULL && type == WILD_LONGEST)
3911 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003912 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (ccline.cmdbuff[i + j] == '*'
3914 || ccline.cmdbuff[i + j] == '?')
3915 break;
3916 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003917 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919 }
3920 }
3921
3922 if (p2 != NULL && !got_int)
3923 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003924 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003925 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003927 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 xp->xp_pattern = ccline.cmdbuff + i;
3929 }
3930 else
3931 v = OK;
3932 if (v == OK)
3933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003934 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3935 &ccline.cmdbuff[ccline.cmdpos],
3936 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3937 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 ccline.cmdlen += difflen;
3939 ccline.cmdpos += difflen;
3940 }
3941 }
3942 vim_free(p2);
3943
3944 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003945 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947 /* When expanding a ":map" command and no matches are found, assume that
3948 * the key is supposed to be inserted literally */
3949 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3950 return FAIL;
3951
3952 if (xp->xp_numfiles <= 0 && p2 == NULL)
3953 beep_flush();
3954 else if (xp->xp_numfiles == 1)
3955 /* free expanded pattern */
3956 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3957
3958 return OK;
3959}
3960
3961/*
3962 * Do wildcard expansion on the string 'str'.
3963 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003964 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 * Return NULL for failure.
3966 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003967 * "orig" is the originally expanded string, copied to allocated memory. It
3968 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3969 * WILD_PREV "orig" should be NULL.
3970 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003971 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3972 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *
3974 * mode = WILD_FREE: just free previously expanded matches
3975 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3976 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3977 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3978 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3979 * mode = WILD_ALL: return all matches concatenated
3980 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003981 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 *
3983 * options = WILD_LIST_NOTFOUND: list entries without a match
3984 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3985 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3986 * options = WILD_NO_BEEP: Don't beep for multiple matches
3987 * options = WILD_ADD_SLASH: add a slash after directory names
3988 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3989 * options = WILD_SILENT: don't print warning messages
3990 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003991 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 *
3993 * The variables xp->xp_context and xp->xp_backslash must have been set!
3994 */
3995 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003996ExpandOne(
3997 expand_T *xp,
3998 char_u *str,
3999 char_u *orig, /* allocated copy of original of expanded string */
4000 int options,
4001 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 char_u *ss = NULL;
4004 static int findex;
4005 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004006 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 int i;
4008 long_u len;
4009 int non_suf_match; /* number without matching suffix */
4010
4011 /*
4012 * first handle the case of using an old match
4013 */
4014 if (mode == WILD_NEXT || mode == WILD_PREV)
4015 {
4016 if (xp->xp_numfiles > 0)
4017 {
4018 if (mode == WILD_PREV)
4019 {
4020 if (findex == -1)
4021 findex = xp->xp_numfiles;
4022 --findex;
4023 }
4024 else /* mode == WILD_NEXT */
4025 ++findex;
4026
4027 /*
4028 * When wrapping around, return the original string, set findex to
4029 * -1.
4030 */
4031 if (findex < 0)
4032 {
4033 if (orig_save == NULL)
4034 findex = xp->xp_numfiles - 1;
4035 else
4036 findex = -1;
4037 }
4038 if (findex >= xp->xp_numfiles)
4039 {
4040 if (orig_save == NULL)
4041 findex = 0;
4042 else
4043 findex = -1;
4044 }
4045#ifdef FEAT_WILDMENU
4046 if (p_wmnu)
4047 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4048 findex, cmd_showtail);
4049#endif
4050 if (findex == -1)
4051 return vim_strsave(orig_save);
4052 return vim_strsave(xp->xp_files[findex]);
4053 }
4054 else
4055 return NULL;
4056 }
4057
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004058 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4060 {
4061 FreeWild(xp->xp_numfiles, xp->xp_files);
4062 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004063 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 findex = 0;
4066
4067 if (mode == WILD_FREE) /* only release file name */
4068 return NULL;
4069
4070 if (xp->xp_numfiles == -1)
4071 {
4072 vim_free(orig_save);
4073 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004074 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 /*
4077 * Do the expansion.
4078 */
4079 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4080 options) == FAIL)
4081 {
4082#ifdef FNAME_ILLEGAL
4083 /* Illegal file name has been silently skipped. But when there
4084 * are wildcards, the real problem is that there was no match,
4085 * causing the pattern to be added, which has illegal characters.
4086 */
4087 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4088 EMSG2(_(e_nomatch2), str);
4089#endif
4090 }
4091 else if (xp->xp_numfiles == 0)
4092 {
4093 if (!(options & WILD_SILENT))
4094 EMSG2(_(e_nomatch2), str);
4095 }
4096 else
4097 {
4098 /* Escape the matches for use on the command line. */
4099 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4100
4101 /*
4102 * Check for matching suffixes in file names.
4103 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004104 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4105 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107 if (xp->xp_numfiles)
4108 non_suf_match = xp->xp_numfiles;
4109 else
4110 non_suf_match = 1;
4111 if ((xp->xp_context == EXPAND_FILES
4112 || xp->xp_context == EXPAND_DIRECTORIES)
4113 && xp->xp_numfiles > 1)
4114 {
4115 /*
4116 * More than one match; check suffix.
4117 * The files will have been sorted on matching suffix in
4118 * expand_wildcards, only need to check the first two.
4119 */
4120 non_suf_match = 0;
4121 for (i = 0; i < 2; ++i)
4122 if (match_suffix(xp->xp_files[i]))
4123 ++non_suf_match;
4124 }
4125 if (non_suf_match != 1)
4126 {
4127 /* Can we ever get here unless it's while expanding
4128 * interactively? If not, we can get rid of this all
4129 * together. Don't really want to wait for this message
4130 * (and possibly have to hit return to continue!).
4131 */
4132 if (!(options & WILD_SILENT))
4133 EMSG(_(e_toomany));
4134 else if (!(options & WILD_NO_BEEP))
4135 beep_flush();
4136 }
4137 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4138 ss = vim_strsave(xp->xp_files[0]);
4139 }
4140 }
4141 }
4142
4143 /* Find longest common part */
4144 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4145 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004146 int mb_len = 1;
4147 int c0, ci;
4148
4149 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004151#ifdef FEAT_MBYTE
4152 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004154 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4155 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4156 }
4157 else
4158#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004159 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004160 for (i = 1; i < xp->xp_numfiles; ++i)
4161 {
4162#ifdef FEAT_MBYTE
4163 if (has_mbyte)
4164 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4165 else
4166#endif
4167 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004168 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004170 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004171 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004173 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 break;
4175 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004176 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 break;
4178 }
4179 if (i < xp->xp_numfiles)
4180 {
4181 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004182 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 break;
4184 }
4185 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 ss = alloc((unsigned)len + 1);
4188 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004189 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 findex = -1; /* next p_wc gets first one */
4191 }
4192
4193 /* Concatenate all matching names */
4194 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4195 {
4196 len = 0;
4197 for (i = 0; i < xp->xp_numfiles; ++i)
4198 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4199 ss = lalloc(len, TRUE);
4200 if (ss != NULL)
4201 {
4202 *ss = NUL;
4203 for (i = 0; i < xp->xp_numfiles; ++i)
4204 {
4205 STRCAT(ss, xp->xp_files[i]);
4206 if (i != xp->xp_numfiles - 1)
4207 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4208 }
4209 }
4210 }
4211
4212 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4213 ExpandCleanup(xp);
4214
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004215 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004216 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004217 vim_free(orig);
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return ss;
4220}
4221
4222/*
4223 * Prepare an expand structure for use.
4224 */
4225 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004226ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004228 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004229 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004231#ifndef BACKSLASH_IN_FILENAME
4232 xp->xp_shell = FALSE;
4233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 xp->xp_numfiles = -1;
4235 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004236#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4237 xp->xp_arg = NULL;
4238#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004239 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240}
4241
4242/*
4243 * Cleanup an expand structure after use.
4244 */
4245 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004246ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 if (xp->xp_numfiles >= 0)
4249 {
4250 FreeWild(xp->xp_numfiles, xp->xp_files);
4251 xp->xp_numfiles = -1;
4252 }
4253}
4254
4255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004256ExpandEscape(
4257 expand_T *xp,
4258 char_u *str,
4259 int numfiles,
4260 char_u **files,
4261 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
4263 int i;
4264 char_u *p;
4265
4266 /*
4267 * May change home directory back to "~"
4268 */
4269 if (options & WILD_HOME_REPLACE)
4270 tilde_replace(str, numfiles, files);
4271
4272 if (options & WILD_ESCAPE)
4273 {
4274 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004275 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004276 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 || xp->xp_context == EXPAND_BUFFERS
4278 || xp->xp_context == EXPAND_DIRECTORIES)
4279 {
4280 /*
4281 * Insert a backslash into a file name before a space, \, %, #
4282 * and wildmatch characters, except '~'.
4283 */
4284 for (i = 0; i < numfiles; ++i)
4285 {
4286 /* for ":set path=" we need to escape spaces twice */
4287 if (xp->xp_backslash == XP_BS_THREE)
4288 {
4289 p = vim_strsave_escaped(files[i], (char_u *)" ");
4290 if (p != NULL)
4291 {
4292 vim_free(files[i]);
4293 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004294#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 p = vim_strsave_escaped(files[i], (char_u *)" ");
4296 if (p != NULL)
4297 {
4298 vim_free(files[i]);
4299 files[i] = p;
4300 }
4301#endif
4302 }
4303 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004304#ifdef BACKSLASH_IN_FILENAME
4305 p = vim_strsave_fnameescape(files[i], FALSE);
4306#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004307 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 if (p != NULL)
4310 {
4311 vim_free(files[i]);
4312 files[i] = p;
4313 }
4314
4315 /* If 'str' starts with "\~", replace "~" at start of
4316 * files[i] with "\~". */
4317 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004318 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004321
4322 /* If the first file starts with a '+' escape it. Otherwise it
4323 * could be seen as "+cmd". */
4324 if (*files[0] == '+')
4325 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 else if (xp->xp_context == EXPAND_TAGS)
4328 {
4329 /*
4330 * Insert a backslash before characters in a tag name that
4331 * would terminate the ":tag" command.
4332 */
4333 for (i = 0; i < numfiles; ++i)
4334 {
4335 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4336 if (p != NULL)
4337 {
4338 vim_free(files[i]);
4339 files[i] = p;
4340 }
4341 }
4342 }
4343 }
4344}
4345
4346/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004347 * Escape special characters in "fname" for when used as a file name argument
4348 * after a Vim command, or, when "shell" is non-zero, a shell command.
4349 * Returns the result in allocated memory.
4350 */
4351 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004352vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004353{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004354 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004355#ifdef BACKSLASH_IN_FILENAME
4356 char_u buf[20];
4357 int j = 0;
4358
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004359 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004360 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004361 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004362 buf[j++] = *p;
4363 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004364 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004365#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004366 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4367 if (shell && csh_like_shell() && p != NULL)
4368 {
4369 char_u *s;
4370
4371 /* For csh and similar shells need to put two backslashes before '!'.
4372 * One is taken by Vim, one by the shell. */
4373 s = vim_strsave_escaped(p, (char_u *)"!");
4374 vim_free(p);
4375 p = s;
4376 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004377#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004378
4379 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4380 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004381 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004382 escape_fname(&p);
4383
4384 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004385}
4386
4387/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004388 * Put a backslash before the file name in "pp", which is in allocated memory.
4389 */
4390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004391escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004392{
4393 char_u *p;
4394
4395 p = alloc((unsigned)(STRLEN(*pp) + 2));
4396 if (p != NULL)
4397 {
4398 p[0] = '\\';
4399 STRCPY(p + 1, *pp);
4400 vim_free(*pp);
4401 *pp = p;
4402 }
4403}
4404
4405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 * For each file name in files[num_files]:
4407 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4408 */
4409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004410tilde_replace(
4411 char_u *orig_pat,
4412 int num_files,
4413 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
4415 int i;
4416 char_u *p;
4417
4418 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4419 {
4420 for (i = 0; i < num_files; ++i)
4421 {
4422 p = home_replace_save(NULL, files[i]);
4423 if (p != NULL)
4424 {
4425 vim_free(files[i]);
4426 files[i] = p;
4427 }
4428 }
4429 }
4430}
4431
4432/*
4433 * Show all matches for completion on the command line.
4434 * Returns EXPAND_NOTHING when the character that triggered expansion should
4435 * be inserted like a normal character.
4436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004438showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
4440#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4441 int num_files;
4442 char_u **files_found;
4443 int i, j, k;
4444 int maxlen;
4445 int lines;
4446 int columns;
4447 char_u *p;
4448 int lastlen;
4449 int attr;
4450 int showtail;
4451
4452 if (xp->xp_numfiles == -1)
4453 {
4454 set_expand_context(xp);
4455 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4456 &num_files, &files_found);
4457 showtail = expand_showtail(xp);
4458 if (i != EXPAND_OK)
4459 return i;
4460
4461 }
4462 else
4463 {
4464 num_files = xp->xp_numfiles;
4465 files_found = xp->xp_files;
4466 showtail = cmd_showtail;
4467 }
4468
4469#ifdef FEAT_WILDMENU
4470 if (!wildmenu)
4471 {
4472#endif
4473 msg_didany = FALSE; /* lines_left will be set */
4474 msg_start(); /* prepare for paging */
4475 msg_putchar('\n');
4476 out_flush();
4477 cmdline_row = msg_row;
4478 msg_didany = FALSE; /* lines_left will be set again */
4479 msg_start(); /* prepare for paging */
4480#ifdef FEAT_WILDMENU
4481 }
4482#endif
4483
4484 if (got_int)
4485 got_int = FALSE; /* only int. the completion, not the cmd line */
4486#ifdef FEAT_WILDMENU
4487 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004488 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489#endif
4490 else
4491 {
4492 /* find the length of the longest file name */
4493 maxlen = 0;
4494 for (i = 0; i < num_files; ++i)
4495 {
4496 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 || xp->xp_context == EXPAND_BUFFERS))
4499 {
4500 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4501 j = vim_strsize(NameBuff);
4502 }
4503 else
4504 j = vim_strsize(L_SHOWFILE(i));
4505 if (j > maxlen)
4506 maxlen = j;
4507 }
4508
4509 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4510 lines = num_files;
4511 else
4512 {
4513 /* compute the number of columns and lines for the listing */
4514 maxlen += 2; /* two spaces between file names */
4515 columns = ((int)Columns + 2) / maxlen;
4516 if (columns < 1)
4517 columns = 1;
4518 lines = (num_files + columns - 1) / columns;
4519 }
4520
Bram Moolenaar8820b482017-03-16 17:23:31 +01004521 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
4523 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4524 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004525 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 msg_clr_eos();
4527 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004528 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530
4531 /* list the files line by line */
4532 for (i = 0; i < lines; ++i)
4533 {
4534 lastlen = 999;
4535 for (k = i; k < num_files; k += lines)
4536 {
4537 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4538 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004539 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 p = files_found[k] + STRLEN(files_found[k]) + 1;
4541 msg_advance(maxlen + 1);
4542 msg_puts(p);
4543 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004544 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 break;
4546 }
4547 for (j = maxlen - lastlen; --j >= 0; )
4548 msg_putchar(' ');
4549 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004550 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 || xp->xp_context == EXPAND_BUFFERS)
4552 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004553 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004554 if (xp->xp_numfiles != -1)
4555 {
4556 char_u *halved_slash;
4557 char_u *exp_path;
4558
4559 /* Expansion was done before and special characters
4560 * were escaped, need to halve backslashes. Also
4561 * $HOME has been replaced with ~/. */
4562 exp_path = expand_env_save_opt(files_found[k], TRUE);
4563 halved_slash = backslash_halve_save(
4564 exp_path != NULL ? exp_path : files_found[k]);
4565 j = mch_isdir(halved_slash != NULL ? halved_slash
4566 : files_found[k]);
4567 vim_free(exp_path);
4568 vim_free(halved_slash);
4569 }
4570 else
4571 /* Expansion was done here, file names are literal. */
4572 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (showtail)
4574 p = L_SHOWFILE(k);
4575 else
4576 {
4577 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4578 TRUE);
4579 p = NameBuff;
4580 }
4581 }
4582 else
4583 {
4584 j = FALSE;
4585 p = L_SHOWFILE(k);
4586 }
4587 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4588 }
4589 if (msg_col > 0) /* when not wrapped around */
4590 {
4591 msg_clr_eos();
4592 msg_putchar('\n');
4593 }
4594 out_flush(); /* show one line at a time */
4595 if (got_int)
4596 {
4597 got_int = FALSE;
4598 break;
4599 }
4600 }
4601
4602 /*
4603 * we redraw the command below the lines that we have just listed
4604 * This is a bit tricky, but it saves a lot of screen updating.
4605 */
4606 cmdline_row = msg_row; /* will put it back later */
4607 }
4608
4609 if (xp->xp_numfiles == -1)
4610 FreeWild(num_files, files_found);
4611
4612 return EXPAND_OK;
4613}
4614
4615/*
4616 * Private gettail for showmatches() (and win_redr_status_matches()):
4617 * Find tail of file name path, but ignore trailing "/".
4618 */
4619 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004620sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622 char_u *p;
4623 char_u *t = s;
4624 int had_sep = FALSE;
4625
4626 for (p = s; *p != NUL; )
4627 {
4628 if (vim_ispathsep(*p)
4629#ifdef BACKSLASH_IN_FILENAME
4630 && !rem_backslash(p)
4631#endif
4632 )
4633 had_sep = TRUE;
4634 else if (had_sep)
4635 {
4636 t = p;
4637 had_sep = FALSE;
4638 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004639 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 return t;
4642}
4643
4644/*
4645 * Return TRUE if we only need to show the tail of completion matches.
4646 * When not completing file names or there is a wildcard in the path FALSE is
4647 * returned.
4648 */
4649 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004650expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
4652 char_u *s;
4653 char_u *end;
4654
4655 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004656 if (xp->xp_context != EXPAND_FILES
4657 && xp->xp_context != EXPAND_SHELLCMD
4658 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 return FALSE;
4660
4661 end = gettail(xp->xp_pattern);
4662 if (end == xp->xp_pattern) /* there is no path separator */
4663 return FALSE;
4664
4665 for (s = xp->xp_pattern; s < end; s++)
4666 {
4667 /* Skip escaped wildcards. Only when the backslash is not a path
4668 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4669 if (rem_backslash(s))
4670 ++s;
4671 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4672 return FALSE;
4673 }
4674 return TRUE;
4675}
4676
4677/*
4678 * Prepare a string for expansion.
4679 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004680 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 * When expanding other names: The string will be used with regcomp(). Copy
4682 * the name into allocated memory and prepend "^".
4683 */
4684 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004685addstar(
4686 char_u *fname,
4687 int len,
4688 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 char_u *retval;
4691 int i, j;
4692 int new_len;
4693 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004694 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004696 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004697 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004698 && context != EXPAND_SHELLCMD
4699 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701 /*
4702 * Matching will be done internally (on something other than files).
4703 * So we convert the file-matching-type wildcards into our kind for
4704 * use with vim_regcomp(). First work out how long it will be:
4705 */
4706
4707 /* For help tags the translation is done in find_help_tags().
4708 * For a tag pattern starting with "/" no translation is needed. */
4709 if (context == EXPAND_HELP
4710 || context == EXPAND_COLORS
4711 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004712 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004713 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004714 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004715 || ((context == EXPAND_TAGS_LISTFILES
4716 || context == EXPAND_TAGS)
4717 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 retval = vim_strnsave(fname, len);
4719 else
4720 {
4721 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4722 for (i = 0; i < len; i++)
4723 {
4724 if (fname[i] == '*' || fname[i] == '~')
4725 new_len++; /* '*' needs to be replaced by ".*"
4726 '~' needs to be replaced by "\~" */
4727
4728 /* Buffer names are like file names. "." should be literal */
4729 if (context == EXPAND_BUFFERS && fname[i] == '.')
4730 new_len++; /* "." becomes "\." */
4731
4732 /* Custom expansion takes care of special things, match
4733 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004734 if ((context == EXPAND_USER_DEFINED
4735 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 new_len++; /* '\' becomes "\\" */
4737 }
4738 retval = alloc(new_len);
4739 if (retval != NULL)
4740 {
4741 retval[0] = '^';
4742 j = 1;
4743 for (i = 0; i < len; i++, j++)
4744 {
4745 /* Skip backslash. But why? At least keep it for custom
4746 * expansion. */
4747 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004748 && context != EXPAND_USER_LIST
4749 && fname[i] == '\\'
4750 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 break;
4752
4753 switch (fname[i])
4754 {
4755 case '*': retval[j++] = '.';
4756 break;
4757 case '~': retval[j++] = '\\';
4758 break;
4759 case '?': retval[j] = '.';
4760 continue;
4761 case '.': if (context == EXPAND_BUFFERS)
4762 retval[j++] = '\\';
4763 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004764 case '\\': if (context == EXPAND_USER_DEFINED
4765 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 retval[j++] = '\\';
4767 break;
4768 }
4769 retval[j] = fname[i];
4770 }
4771 retval[j] = NUL;
4772 }
4773 }
4774 }
4775 else
4776 {
4777 retval = alloc(len + 4);
4778 if (retval != NULL)
4779 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004780 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004783 * Don't add a star to *, ~, ~user, $var or `cmd`.
4784 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 * ~ would be at the start of the file name, but not the tail.
4786 * $ could be anywhere in the tail.
4787 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004788 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 */
4790 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004791 ends_in_star = (len > 0 && retval[len - 1] == '*');
4792#ifndef BACKSLASH_IN_FILENAME
4793 for (i = len - 2; i >= 0; --i)
4794 {
4795 if (retval[i] != '\\')
4796 break;
4797 ends_in_star = !ends_in_star;
4798 }
4799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004801 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 && vim_strchr(tail, '$') == NULL
4803 && vim_strchr(retval, '`') == NULL)
4804 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004805 else if (len > 0 && retval[len - 1] == '$')
4806 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 retval[len] = NUL;
4808 }
4809 }
4810 return retval;
4811}
4812
4813/*
4814 * Must parse the command line so far to work out what context we are in.
4815 * Completion can then be done based on that context.
4816 * This routine sets the variables:
4817 * xp->xp_pattern The start of the pattern to be expanded within
4818 * the command line (ends at the cursor).
4819 * xp->xp_context The type of thing to expand. Will be one of:
4820 *
4821 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4822 * the command line, like an unknown command. Caller
4823 * should beep.
4824 * EXPAND_NOTHING Unrecognised context for completion, use char like
4825 * a normal char, rather than for completion. eg
4826 * :s/^I/
4827 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4828 * it.
4829 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4830 * EXPAND_FILES After command with XFILE set, or after setting
4831 * with P_EXPAND set. eg :e ^I, :w>>^I
4832 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4833 * when we know only directories are of interest. eg
4834 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004835 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4837 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4838 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4839 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4840 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4841 * EXPAND_EVENTS Complete event names
4842 * EXPAND_SYNTAX Complete :syntax command arguments
4843 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4844 * EXPAND_AUGROUP Complete autocommand group names
4845 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4846 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4847 * eg :unmap a^I , :cunab x^I
4848 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4849 * eg :call sub^I
4850 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4851 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4852 * names in expressions, eg :while s^I
4853 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004854 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 */
4856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004859 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (ccline.cmdfirstc != ':'
4861#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004862 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004863 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#endif
4865 )
4866 {
4867 xp->xp_context = EXPAND_NOTHING;
4868 return;
4869 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004870 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871}
4872
4873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004874set_cmd_context(
4875 expand_T *xp,
4876 char_u *str, /* start of command line */
4877 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004878 int col, /* position of cursor */
4879 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 int old_char = NUL;
4882 char_u *nextcomm;
4883
4884 /*
4885 * Avoid a UMR warning from Purify, only save the character if it has been
4886 * written before.
4887 */
4888 if (col < len)
4889 old_char = str[col];
4890 str[col] = NUL;
4891 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004892
4893#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004894 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004895 {
4896# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004897 /* pass CMD_SIZE because there is no real command */
4898 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004899# endif
4900 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004901 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004902 {
4903 xp->xp_context = ccline.xp_context;
4904 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004905# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004906 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004907# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004908 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004909 else
4910#endif
4911 while (nextcomm != NULL)
4912 nextcomm = set_one_cmd_context(xp, nextcomm);
4913
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004914 /* Store the string here so that call_user_expand_func() can get to them
4915 * easily. */
4916 xp->xp_line = str;
4917 xp->xp_col = col;
4918
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 str[col] = old_char;
4920}
4921
4922/*
4923 * Expand the command line "str" from context "xp".
4924 * "xp" must have been set by set_cmd_context().
4925 * xp->xp_pattern points into "str", to where the text that is to be expanded
4926 * starts.
4927 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4928 * cursor.
4929 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4930 * key that triggered expansion literally.
4931 * Returns EXPAND_OK otherwise.
4932 */
4933 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934expand_cmdline(
4935 expand_T *xp,
4936 char_u *str, /* start of command line */
4937 int col, /* position of cursor */
4938 int *matchcount, /* return: nr of matches */
4939 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004942 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4945 {
4946 beep_flush();
4947 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4948 }
4949 if (xp->xp_context == EXPAND_NOTHING)
4950 {
4951 /* Caller can use the character as a normal char instead */
4952 return EXPAND_NOTHING;
4953 }
4954
4955 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004956 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4957 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (file_str == NULL)
4959 return EXPAND_UNSUCCESSFUL;
4960
Bram Moolenaar94950a92010-12-02 16:01:29 +01004961 if (p_wic)
4962 options += WILD_ICASE;
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004965 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 *matchcount = 0;
4968 *matches = NULL;
4969 }
4970 vim_free(file_str);
4971
4972 return EXPAND_OK;
4973}
4974
4975#ifdef FEAT_MULTI_LANG
4976/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004977 * Cleanup matches for help tags:
4978 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4979 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004981static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004984cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
4986 int i, j;
4987 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004988 char_u buf[4];
4989 char_u *p = buf;
4990
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004991 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004992 {
4993 *p++ = '@';
4994 *p++ = p_hlg[0];
4995 *p++ = p_hlg[1];
4996 }
4997 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 for (i = 0; i < num_file; ++i)
5000 {
5001 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005002 if (len <= 0)
5003 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005004 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 /* Sorting on priority means the same item in another language may
5007 * be anywhere. Search all items for a match up to the "@en". */
5008 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005009 if (j != i && (int)STRLEN(file[j]) == len + 3
5010 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 break;
5012 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005013 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 file[i][len] = NUL;
5015 }
5016 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005017
5018 if (*buf != NUL)
5019 for (i = 0; i < num_file; ++i)
5020 {
5021 len = (int)STRLEN(file[i]) - 3;
5022 if (len <= 0)
5023 continue;
5024 if (STRCMP(file[i] + len, buf) == 0)
5025 {
5026 /* remove the default language */
5027 file[i][len] = NUL;
5028 }
5029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030}
5031#endif
5032
5033/*
5034 * Do the expansion based on xp->xp_context and "pat".
5035 */
5036 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005037ExpandFromContext(
5038 expand_T *xp,
5039 char_u *pat,
5040 int *num_file,
5041 char_u ***file,
5042 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044#ifdef FEAT_CMDL_COMPL
5045 regmatch_T regmatch;
5046#endif
5047 int ret;
5048 int flags;
5049
5050 flags = EW_DIR; /* include directories */
5051 if (options & WILD_LIST_NOTFOUND)
5052 flags |= EW_NOTFOUND;
5053 if (options & WILD_ADD_SLASH)
5054 flags |= EW_ADDSLASH;
5055 if (options & WILD_KEEP_ALL)
5056 flags |= EW_KEEPALL;
5057 if (options & WILD_SILENT)
5058 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005059 if (options & WILD_ALLLINKS)
5060 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005062 if (xp->xp_context == EXPAND_FILES
5063 || xp->xp_context == EXPAND_DIRECTORIES
5064 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 /*
5067 * Expand file or directory names.
5068 */
5069 int free_pat = FALSE;
5070 int i;
5071
5072 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5073 * space */
5074 if (xp->xp_backslash != XP_BS_NONE)
5075 {
5076 free_pat = TRUE;
5077 pat = vim_strsave(pat);
5078 for (i = 0; pat[i]; ++i)
5079 if (pat[i] == '\\')
5080 {
5081 if (xp->xp_backslash == XP_BS_THREE
5082 && pat[i + 1] == '\\'
5083 && pat[i + 2] == '\\'
5084 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005085 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (xp->xp_backslash == XP_BS_ONE
5087 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005088 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090 }
5091
5092 if (xp->xp_context == EXPAND_FILES)
5093 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005094 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5095 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 else
5097 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005098 if (options & WILD_ICASE)
5099 flags |= EW_ICASE;
5100
Bram Moolenaard7834d32009-12-02 16:14:36 +00005101 /* Expand wildcards, supporting %:h and the like. */
5102 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (free_pat)
5104 vim_free(pat);
5105 return ret;
5106 }
5107
5108 *file = (char_u **)"";
5109 *num_file = 0;
5110 if (xp->xp_context == EXPAND_HELP)
5111 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005112 /* With an empty argument we would get all the help tags, which is
5113 * very slow. Get matches for "help" instead. */
5114 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5115 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117#ifdef FEAT_MULTI_LANG
5118 cleanup_help_tags(*num_file, *file);
5119#endif
5120 return OK;
5121 }
5122 return FAIL;
5123 }
5124
5125#ifndef FEAT_CMDL_COMPL
5126 return FAIL;
5127#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005128 if (xp->xp_context == EXPAND_SHELLCMD)
5129 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (xp->xp_context == EXPAND_OLD_SETTING)
5131 return ExpandOldSetting(num_file, file);
5132 if (xp->xp_context == EXPAND_BUFFERS)
5133 return ExpandBufnames(pat, num_file, file, options);
5134 if (xp->xp_context == EXPAND_TAGS
5135 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5136 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5137 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005138 {
5139 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005140 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5141 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005145 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005146 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005147 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005148 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005149 {
5150 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005151 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005152 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005153 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005154 {
5155 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005156 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005157 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005158# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5159 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005160 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005161# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005162 if (xp->xp_context == EXPAND_PACKADD)
5163 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5166 if (regmatch.regprog == NULL)
5167 return FAIL;
5168
5169 /* set ignore-case according to p_ic, p_scs and pat */
5170 regmatch.rm_ic = ignorecase(pat);
5171
5172 if (xp->xp_context == EXPAND_SETTINGS
5173 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5174 ret = ExpandSettings(xp, &regmatch, num_file, file);
5175 else if (xp->xp_context == EXPAND_MAPPINGS)
5176 ret = ExpandMappings(&regmatch, num_file, file);
5177# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5178 else if (xp->xp_context == EXPAND_USER_DEFINED)
5179 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5180# endif
5181 else
5182 {
5183 static struct expgen
5184 {
5185 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005186 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005188 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 } tab[] =
5190 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005191 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5192 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005193 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005194 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005195#ifdef FEAT_CMDHIST
5196 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005199 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005200 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005201 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5202 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5203 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#endif
5205#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005206 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5207 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5208 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5209 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#endif
5211#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005212 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5213 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
5215#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005218#ifdef FEAT_PROFILE
5219 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5220#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005221 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5223 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005224#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005225 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005226#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005227#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005228 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005229#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005230#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005231 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5234 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005235 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5236 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005238 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005239 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005240 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 };
5242 int i;
5243
5244 /*
5245 * Find a context in the table and call the ExpandGeneric() with the
5246 * right function to do the expansion.
5247 */
5248 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005249 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (xp->xp_context == tab[i].context)
5251 {
5252 if (tab[i].ic)
5253 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005254 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005255 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 break;
5257 }
5258 }
5259
Bram Moolenaar473de612013-06-08 18:19:48 +02005260 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262 return ret;
5263#endif /* FEAT_CMDL_COMPL */
5264}
5265
5266#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5267/*
5268 * Expand a list of names.
5269 *
5270 * Generic function for command line completion. It calls a function to
5271 * obtain strings, one by one. The strings are matched against a regexp
5272 * program. Matching strings are copied into an array, which is returned.
5273 *
5274 * Returns OK when no problems encountered, FAIL for error (out of memory).
5275 */
5276 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005277ExpandGeneric(
5278 expand_T *xp,
5279 regmatch_T *regmatch,
5280 int *num_file,
5281 char_u ***file,
5282 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005284 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 int i;
5287 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005288 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 char_u *str;
5290
5291 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005292 * round == 0: count the number of matching names
5293 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005295 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
5297 for (i = 0; ; ++i)
5298 {
5299 str = (*func)(xp, i);
5300 if (str == NULL) /* end of list */
5301 break;
5302 if (*str == NUL) /* skip empty strings */
5303 continue;
5304
5305 if (vim_regexec(regmatch, str, (colnr_T)0))
5306 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005307 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005309 if (escaped)
5310 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5311 else
5312 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 (*file)[count] = str;
5314#ifdef FEAT_MENU
5315 if (func == get_menu_names && str != NULL)
5316 {
5317 /* test for separator added by get_menu_names() */
5318 str += STRLEN(str) - 1;
5319 if (*str == '\001')
5320 *str = '.';
5321 }
5322#endif
5323 }
5324 ++count;
5325 }
5326 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005327 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
5329 if (count == 0)
5330 return OK;
5331 *num_file = count;
5332 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5333 if (*file == NULL)
5334 {
5335 *file = (char_u **)"";
5336 return FAIL;
5337 }
5338 count = 0;
5339 }
5340 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005341
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005342 /* Sort the results. Keep menu's in the specified order. */
5343 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005344 {
5345 if (xp->xp_context == EXPAND_EXPRESSION
5346 || xp->xp_context == EXPAND_FUNCTIONS
5347 || xp->xp_context == EXPAND_USER_FUNC)
5348 /* <SNR> functions should be sorted to the end. */
5349 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5350 sort_func_compare);
5351 else
5352 sort_strings(*file, *num_file);
5353 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005354
Bram Moolenaar4f688582007-07-24 12:34:30 +00005355#ifdef FEAT_CMDL_COMPL
5356 /* Reset the variables used for special highlight names expansion, so that
5357 * they don't show up when getting normal highlight names by ID. */
5358 reset_expand_highlight();
5359#endif
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 return OK;
5362}
5363
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005364/*
5365 * Complete a shell command.
5366 * Returns FAIL or OK;
5367 */
5368 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005369expand_shellcmd(
5370 char_u *filepat, /* pattern to match with command names */
5371 int *num_file, /* return: number of matches */
5372 char_u ***file, /* return: array with matches */
5373 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005374{
5375 char_u *pat;
5376 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005377 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005378 int mustfree = FALSE;
5379 garray_T ga;
5380 char_u *buf = alloc(MAXPATHL);
5381 size_t l;
5382 char_u *s, *e;
5383 int flags = flagsarg;
5384 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005385 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005386 hashtab_T found_ht;
5387 hashitem_T *hi;
5388 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005389
5390 if (buf == NULL)
5391 return FAIL;
5392
5393 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5394 * space */
5395 pat = vim_strsave(filepat);
5396 for (i = 0; pat[i]; ++i)
5397 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005398 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005399
Bram Moolenaarb5971142015-03-21 17:32:19 +01005400 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005401
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005402 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5403 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005404 path = (char_u *)".";
5405 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005406 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005407 /* For an absolute name we don't use $PATH. */
5408 if (!mch_isFullName(pat))
5409 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005410 if (path == NULL)
5411 path = (char_u *)"";
5412 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005413
5414 /*
5415 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005416 * collect them in "ga". When "." is not in $PATH also expand for the
5417 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005418 */
5419 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005420 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005421 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005422 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005423#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005424 e = vim_strchr(s, ';');
5425#else
5426 e = vim_strchr(s, ':');
5427#endif
5428 if (e == NULL)
5429 e = s + STRLEN(s);
5430
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005431 if (*s == NUL)
5432 {
5433 if (did_curdir)
5434 break;
5435 // Find directories in the current directory, path is empty.
5436 did_curdir = TRUE;
5437 flags |= EW_DIR;
5438 }
5439 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5440 {
5441 did_curdir = TRUE;
5442 flags |= EW_DIR;
5443 }
5444 else
5445 // Do not match directories inside a $PATH item.
5446 flags &= ~EW_DIR;
5447
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005448 l = e - s;
5449 if (l > MAXPATHL - 5)
5450 break;
5451 vim_strncpy(buf, s, l);
5452 add_pathsep(buf);
5453 l = STRLEN(buf);
5454 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5455
5456 /* Expand matches in one directory of $PATH. */
5457 ret = expand_wildcards(1, &buf, num_file, file, flags);
5458 if (ret == OK)
5459 {
5460 if (ga_grow(&ga, *num_file) == FAIL)
5461 FreeWild(*num_file, *file);
5462 else
5463 {
5464 for (i = 0; i < *num_file; ++i)
5465 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005466 char_u *name = (*file)[i];
5467
5468 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005469 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005470 // Check if this name was already found.
5471 hash = hash_hash(name + l);
5472 hi = hash_lookup(&found_ht, name + l, hash);
5473 if (HASHITEM_EMPTY(hi))
5474 {
5475 // Remove the path that was prepended.
5476 STRMOVE(name, name + l);
5477 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5478 hash_add_item(&found_ht, hi, name, hash);
5479 name = NULL;
5480 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005481 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005482 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005483 }
5484 vim_free(*file);
5485 }
5486 }
5487 if (*e != NUL)
5488 ++e;
5489 }
5490 *file = ga.ga_data;
5491 *num_file = ga.ga_len;
5492
5493 vim_free(buf);
5494 vim_free(pat);
5495 if (mustfree)
5496 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005497 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005498 return OK;
5499}
5500
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5503/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005504 * Call "user_expand_func()" to invoke a user defined Vim script function and
5505 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005507 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005509 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510 expand_T *xp,
5511 int *num_file,
5512 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005514 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005515 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005517 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005518 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005519 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaarb7515462013-06-29 12:58:33 +02005521 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 *num_file = 0;
5524 *file = NULL;
5525
Bram Moolenaarb7515462013-06-29 12:58:33 +02005526 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005527 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005528 keep = ccline.cmdbuff[ccline.cmdlen];
5529 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005530 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005531
Bram Moolenaarffa96842018-06-12 22:05:14 +02005532 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5533
5534 args[0].v_type = VAR_STRING;
5535 args[0].vval.v_string = pat;
5536 args[1].v_type = VAR_STRING;
5537 args[1].vval.v_string = xp->xp_line;
5538 args[2].v_type = VAR_NUMBER;
5539 args[2].vval.v_number = xp->xp_col;
5540 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005542 /* Save the cmdline, we don't know what the function may do. */
5543 save_ccline = ccline;
5544 ccline.cmdbuff = NULL;
5545 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005547
Bram Moolenaarded27a12018-08-01 19:06:03 +02005548 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005549
5550 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005552 if (ccline.cmdbuff != NULL)
5553 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005554
Bram Moolenaarffa96842018-06-12 22:05:14 +02005555 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005556 return ret;
5557}
5558
5559/*
5560 * Expand names with a function defined by the user.
5561 */
5562 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563ExpandUserDefined(
5564 expand_T *xp,
5565 regmatch_T *regmatch,
5566 int *num_file,
5567 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005568{
5569 char_u *retstr;
5570 char_u *s;
5571 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005572 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005573 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005574 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005575
5576 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5577 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 return FAIL;
5579
5580 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005581 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
5583 e = vim_strchr(s, '\n');
5584 if (e == NULL)
5585 e = s + STRLEN(s);
5586 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005587 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005589 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5590 *e = keep;
5591
5592 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005594 if (ga_grow(&ga, 1) == FAIL)
5595 break;
5596 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5597 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (*e != NUL)
5601 ++e;
5602 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005603 vim_free(retstr);
5604 *file = ga.ga_data;
5605 *num_file = ga.ga_len;
5606 return OK;
5607}
5608
5609/*
5610 * Expand names with a list returned by a function defined by the user.
5611 */
5612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613ExpandUserList(
5614 expand_T *xp,
5615 int *num_file,
5616 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005617{
5618 list_T *retlist;
5619 listitem_T *li;
5620 garray_T ga;
5621
5622 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5623 if (retlist == NULL)
5624 return FAIL;
5625
5626 ga_init2(&ga, (int)sizeof(char *), 3);
5627 /* Loop over the items in the list. */
5628 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5629 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005630 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5631 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005632
5633 if (ga_grow(&ga, 1) == FAIL)
5634 break;
5635
5636 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005637 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005638 ++ga.ga_len;
5639 }
5640 list_unref(retlist);
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 *file = ga.ga_data;
5643 *num_file = ga.ga_len;
5644 return OK;
5645}
5646#endif
5647
5648/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005649 * Expand color scheme, compiler or filetype names.
5650 * Search from 'runtimepath':
5651 * 'runtimepath'/{dirnames}/{pat}.vim
5652 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5653 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5654 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5655 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005656 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 */
5658 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659ExpandRTDir(
5660 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005661 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662 int *num_file,
5663 char_u ***file,
5664 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 char_u *s;
5667 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005668 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005670 int i;
5671 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
5673 *num_file = 0;
5674 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005675 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005676 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005678 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005680 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5681 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005683 ga_clear_strings(&ga);
5684 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005686 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005687 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005688 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005690
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005691 if (flags & DIP_START) {
5692 for (i = 0; dirnames[i] != NULL; ++i)
5693 {
5694 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5695 if (s == NULL)
5696 {
5697 ga_clear_strings(&ga);
5698 return FAIL;
5699 }
5700 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5701 globpath(p_pp, s, &ga, 0);
5702 vim_free(s);
5703 }
5704 }
5705
5706 if (flags & DIP_OPT) {
5707 for (i = 0; dirnames[i] != NULL; ++i)
5708 {
5709 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5710 if (s == NULL)
5711 {
5712 ga_clear_strings(&ga);
5713 return FAIL;
5714 }
5715 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5716 globpath(p_pp, s, &ga, 0);
5717 vim_free(s);
5718 }
5719 }
5720
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005721 for (i = 0; i < ga.ga_len; ++i)
5722 {
5723 match = ((char_u **)ga.ga_data)[i];
5724 s = match;
5725 e = s + STRLEN(s);
5726 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5727 {
5728 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005729 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005730 if (s < match || vim_ispathsep(*s))
5731 break;
5732 ++s;
5733 *e = NUL;
5734 mch_memmove(match, s, e - s + 1);
5735 }
5736 }
5737
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005738 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005739 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005740
5741 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005742 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005743 remove_duplicates(&ga);
5744
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 *file = ga.ga_data;
5746 *num_file = ga.ga_len;
5747 return OK;
5748}
5749
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005750/*
5751 * Expand loadplugin names:
5752 * 'packpath'/pack/ * /opt/{pat}
5753 */
5754 static int
5755ExpandPackAddDir(
5756 char_u *pat,
5757 int *num_file,
5758 char_u ***file)
5759{
5760 char_u *s;
5761 char_u *e;
5762 char_u *match;
5763 garray_T ga;
5764 int i;
5765 int pat_len;
5766
5767 *num_file = 0;
5768 *file = NULL;
5769 pat_len = (int)STRLEN(pat);
5770 ga_init2(&ga, (int)sizeof(char *), 10);
5771
5772 s = alloc((unsigned)(pat_len + 26));
5773 if (s == NULL)
5774 {
5775 ga_clear_strings(&ga);
5776 return FAIL;
5777 }
5778 sprintf((char *)s, "pack/*/opt/%s*", pat);
5779 globpath(p_pp, s, &ga, 0);
5780 vim_free(s);
5781
5782 for (i = 0; i < ga.ga_len; ++i)
5783 {
5784 match = ((char_u **)ga.ga_data)[i];
5785 s = gettail(match);
5786 e = s + STRLEN(s);
5787 mch_memmove(match, s, e - s + 1);
5788 }
5789
5790 if (ga.ga_len == 0)
5791 return FAIL;
5792
5793 /* Sort and remove duplicates which can happen when specifying multiple
5794 * directories in dirnames. */
5795 remove_duplicates(&ga);
5796
5797 *file = ga.ga_data;
5798 *num_file = ga.ga_len;
5799 return OK;
5800}
5801
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802#endif
5803
5804#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5805/*
5806 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005807 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005810globpath(
5811 char_u *path,
5812 char_u *file,
5813 garray_T *ga,
5814 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 expand_T xpc;
5817 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 int num_p;
5820 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821
5822 buf = alloc(MAXPATHL);
5823 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005824 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005826 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 /* Loop over all entries in {path}. */
5830 while (*path != NUL)
5831 {
5832 /* Copy one item of the path to buf[] and concatenate the file name. */
5833 copy_option_part(&path, buf, MAXPATHL, ",");
5834 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5835 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005836# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005837 /* Using the platform's path separator (\) makes vim incorrectly
5838 * treat it as an escape character, use '/' instead. */
5839 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5840 STRCAT(buf, "/");
5841# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005845 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5846 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005848 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005850 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 for (i = 0; i < num_p; ++i)
5853 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005854 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005855 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005856 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 FreeWild(num_p, p);
5861 }
5862 }
5863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
5865 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866}
5867
5868#endif
5869
5870#if defined(FEAT_CMDHIST) || defined(PROTO)
5871
5872/*********************************
5873 * Command line history stuff *
5874 *********************************/
5875
5876/*
5877 * Translate a history character to the associated type number.
5878 */
5879 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005880hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 if (c == ':')
5883 return HIST_CMD;
5884 if (c == '=')
5885 return HIST_EXPR;
5886 if (c == '@')
5887 return HIST_INPUT;
5888 if (c == '>')
5889 return HIST_DEBUG;
5890 return HIST_SEARCH; /* must be '?' or '/' */
5891}
5892
5893/*
5894 * Table of history names.
5895 * These names are used in :history and various hist...() functions.
5896 * It is sufficient to give the significant prefix of a history name.
5897 */
5898
5899static char *(history_names[]) =
5900{
5901 "cmd",
5902 "search",
5903 "expr",
5904 "input",
5905 "debug",
5906 NULL
5907};
5908
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005909#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5910/*
5911 * Function given to ExpandGeneric() to obtain the possible first
5912 * arguments of the ":history command.
5913 */
5914 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005915get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005916{
5917 static char_u compl[2] = { NUL, NUL };
5918 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005919 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005920 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5921
5922 if (idx < short_names_count)
5923 {
5924 compl[0] = (char_u)short_names[idx];
5925 return compl;
5926 }
5927 if (idx < short_names_count + history_name_count)
5928 return (char_u *)history_names[idx - short_names_count];
5929 if (idx == short_names_count + history_name_count)
5930 return (char_u *)"all";
5931 return NULL;
5932}
5933#endif
5934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935/*
5936 * init_history() - Initialize the command line history.
5937 * Also used to re-allocate the history when the size changes.
5938 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005939 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005940init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 int newlen; /* new length of history table */
5943 histentry_T *temp;
5944 int i;
5945 int j;
5946 int type;
5947
5948 /*
5949 * If size of history table changed, reallocate it
5950 */
5951 newlen = (int)p_hi;
5952 if (newlen != hislen) /* history length changed */
5953 {
5954 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5955 {
5956 if (newlen)
5957 {
5958 temp = (histentry_T *)lalloc(
5959 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5960 if (temp == NULL) /* out of memory! */
5961 {
5962 if (type == 0) /* first one: just keep the old length */
5963 {
5964 newlen = hislen;
5965 break;
5966 }
5967 /* Already changed one table, now we can only have zero
5968 * length for all tables. */
5969 newlen = 0;
5970 type = -1;
5971 continue;
5972 }
5973 }
5974 else
5975 temp = NULL;
5976 if (newlen == 0 || temp != NULL)
5977 {
5978 if (hisidx[type] < 0) /* there are no entries yet */
5979 {
5980 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005981 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983 else if (newlen > hislen) /* array becomes bigger */
5984 {
5985 for (i = 0; i <= hisidx[type]; ++i)
5986 temp[i] = history[type][i];
5987 j = i;
5988 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005989 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 for ( ; j < hislen; ++i, ++j)
5991 temp[i] = history[type][j];
5992 }
5993 else /* array becomes smaller or 0 */
5994 {
5995 j = hisidx[type];
5996 for (i = newlen - 1; ; --i)
5997 {
5998 if (i >= 0) /* copy newest entries */
5999 temp[i] = history[type][j];
6000 else /* remove older entries */
6001 vim_free(history[type][j].hisstr);
6002 if (--j < 0)
6003 j = hislen - 1;
6004 if (j == hisidx[type])
6005 break;
6006 }
6007 hisidx[type] = newlen - 1;
6008 }
6009 vim_free(history[type]);
6010 history[type] = temp;
6011 }
6012 }
6013 hislen = newlen;
6014 }
6015}
6016
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006018clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006019{
6020 hisptr->hisnum = 0;
6021 hisptr->viminfo = FALSE;
6022 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006023 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006024}
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026/*
6027 * Check if command line 'str' is already in history.
6028 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6029 */
6030 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006031in_history(
6032 int type,
6033 char_u *str,
6034 int move_to_front, /* Move the entry to the front if it exists */
6035 int sep,
6036 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 int i;
6039 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006040 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041
6042 if (hisidx[type] < 0)
6043 return FALSE;
6044 i = hisidx[type];
6045 do
6046 {
6047 if (history[type][i].hisstr == NULL)
6048 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006049
6050 /* For search history, check that the separator character matches as
6051 * well. */
6052 p = history[type][i].hisstr;
6053 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006054 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006055 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 {
6057 if (!move_to_front)
6058 return TRUE;
6059 last_i = i;
6060 break;
6061 }
6062 if (--i < 0)
6063 i = hislen - 1;
6064 } while (i != hisidx[type]);
6065
6066 if (last_i >= 0)
6067 {
6068 str = history[type][i].hisstr;
6069 while (i != hisidx[type])
6070 {
6071 if (++i >= hislen)
6072 i = 0;
6073 history[type][last_i] = history[type][i];
6074 last_i = i;
6075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006077 history[type][i].viminfo = FALSE;
6078 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006079 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 return TRUE;
6081 }
6082 return FALSE;
6083}
6084
6085/*
6086 * Convert history name (from table above) to its HIST_ equivalent.
6087 * When "name" is empty, return "cmd" history.
6088 * Returns -1 for unknown history name.
6089 */
6090 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006091get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
6093 int i;
6094 int len = (int)STRLEN(name);
6095
6096 /* No argument: use current history. */
6097 if (len == 0)
6098 return hist_char2type(ccline.cmdfirstc);
6099
6100 for (i = 0; history_names[i] != NULL; ++i)
6101 if (STRNICMP(name, history_names[i], len) == 0)
6102 return i;
6103
6104 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6105 return hist_char2type(name[0]);
6106
6107 return -1;
6108}
6109
6110static int last_maptick = -1; /* last seen maptick */
6111
6112/*
6113 * Add the given string to the given history. If the string is already in the
6114 * history then it is moved to the front. "histype" may be one of he HIST_
6115 * values.
6116 */
6117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006118add_to_history(
6119 int histype,
6120 char_u *new_entry,
6121 int in_map, /* consider maptick when inside a mapping */
6122 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124 histentry_T *hisptr;
6125 int len;
6126
6127 if (hislen == 0) /* no history */
6128 return;
6129
Bram Moolenaara939e432013-11-09 05:30:26 +01006130 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6131 return;
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 /*
6134 * Searches inside the same mapping overwrite each other, so that only
6135 * the last line is kept. Be careful not to remove a line that was moved
6136 * down, only lines that were added.
6137 */
6138 if (histype == HIST_SEARCH && in_map)
6139 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006140 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
6142 /* Current line is from the same mapping, remove it */
6143 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6144 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006145 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 --hisnum[histype];
6147 if (--hisidx[HIST_SEARCH] < 0)
6148 hisidx[HIST_SEARCH] = hislen - 1;
6149 }
6150 last_maptick = -1;
6151 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006152 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
6154 if (++hisidx[histype] == hislen)
6155 hisidx[histype] = 0;
6156 hisptr = &history[histype][hisidx[histype]];
6157 vim_free(hisptr->hisstr);
6158
6159 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006160 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6162 if (hisptr->hisstr != NULL)
6163 hisptr->hisstr[len + 1] = sep;
6164
6165 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006166 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006167 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (histype == HIST_SEARCH && in_map)
6169 last_maptick = maptick;
6170 }
6171}
6172
6173#if defined(FEAT_EVAL) || defined(PROTO)
6174
6175/*
6176 * Get identifier of newest history entry.
6177 * "histype" may be one of the HIST_ values.
6178 */
6179 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6183 || hisidx[histype] < 0)
6184 return -1;
6185
6186 return history[histype][hisidx[histype]].hisnum;
6187}
6188
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 * Calculate history index from a number:
6191 * num > 0: seen as identifying number of a history entry
6192 * num < 0: relative position in history wrt newest entry
6193 * "histype" may be one of the HIST_ values.
6194 */
6195 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006196calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
6198 int i;
6199 histentry_T *hist;
6200 int wrapped = FALSE;
6201
6202 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6203 || (i = hisidx[histype]) < 0 || num == 0)
6204 return -1;
6205
6206 hist = history[histype];
6207 if (num > 0)
6208 {
6209 while (hist[i].hisnum > num)
6210 if (--i < 0)
6211 {
6212 if (wrapped)
6213 break;
6214 i += hislen;
6215 wrapped = TRUE;
6216 }
6217 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6218 return i;
6219 }
6220 else if (-num <= hislen)
6221 {
6222 i += num + 1;
6223 if (i < 0)
6224 i += hislen;
6225 if (hist[i].hisstr != NULL)
6226 return i;
6227 }
6228 return -1;
6229}
6230
6231/*
6232 * Get a history entry by its index.
6233 * "histype" may be one of the HIST_ values.
6234 */
6235 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006236get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 idx = calc_hist_idx(histype, idx);
6239 if (idx >= 0)
6240 return history[histype][idx].hisstr;
6241 else
6242 return (char_u *)"";
6243}
6244
6245/*
6246 * Clear all entries of a history.
6247 * "histype" may be one of the HIST_ values.
6248 */
6249 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 int i;
6253 histentry_T *hisptr;
6254
6255 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6256 {
6257 hisptr = history[histype];
6258 for (i = hislen; i--;)
6259 {
6260 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006261 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006262 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 }
6264 hisidx[histype] = -1; /* mark history as cleared */
6265 hisnum[histype] = 0; /* reset identifier counter */
6266 return OK;
6267 }
6268 return FAIL;
6269}
6270
6271/*
6272 * Remove all entries matching {str} from a history.
6273 * "histype" may be one of the HIST_ values.
6274 */
6275 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006276del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 regmatch_T regmatch;
6279 histentry_T *hisptr;
6280 int idx;
6281 int i;
6282 int last;
6283 int found = FALSE;
6284
6285 regmatch.regprog = NULL;
6286 regmatch.rm_ic = FALSE; /* always match case */
6287 if (hislen != 0
6288 && histype >= 0
6289 && histype < HIST_COUNT
6290 && *str != NUL
6291 && (idx = hisidx[histype]) >= 0
6292 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6293 != NULL)
6294 {
6295 i = last = idx;
6296 do
6297 {
6298 hisptr = &history[histype][i];
6299 if (hisptr->hisstr == NULL)
6300 break;
6301 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6302 {
6303 found = TRUE;
6304 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006305 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307 else
6308 {
6309 if (i != last)
6310 {
6311 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006312 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 }
6314 if (--last < 0)
6315 last += hislen;
6316 }
6317 if (--i < 0)
6318 i += hislen;
6319 } while (i != idx);
6320 if (history[histype][idx].hisstr == NULL)
6321 hisidx[histype] = -1;
6322 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006323 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 return found;
6325}
6326
6327/*
6328 * Remove an indexed entry from a history.
6329 * "histype" may be one of the HIST_ values.
6330 */
6331 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 int i, j;
6335
6336 i = calc_hist_idx(histype, idx);
6337 if (i < 0)
6338 return FALSE;
6339 idx = hisidx[histype];
6340 vim_free(history[histype][i].hisstr);
6341
6342 /* When deleting the last added search string in a mapping, reset
6343 * last_maptick, so that the last added search string isn't deleted again.
6344 */
6345 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6346 last_maptick = -1;
6347
6348 while (i != idx)
6349 {
6350 j = (i + 1) % hislen;
6351 history[histype][i] = history[histype][j];
6352 i = j;
6353 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006354 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 if (--i < 0)
6356 i += hislen;
6357 hisidx[histype] = i;
6358 return TRUE;
6359}
6360
6361#endif /* FEAT_EVAL */
6362
6363#if defined(FEAT_CRYPT) || defined(PROTO)
6364/*
6365 * Very specific function to remove the value in ":set key=val" from the
6366 * history.
6367 */
6368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006369remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 char_u *p;
6372 int i;
6373
6374 i = hisidx[HIST_CMD];
6375 if (i < 0)
6376 return;
6377 p = history[HIST_CMD][i].hisstr;
6378 if (p != NULL)
6379 for ( ; *p; ++p)
6380 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6381 {
6382 p = vim_strchr(p + 3, '=');
6383 if (p == NULL)
6384 break;
6385 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006386 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 if (p[i] == '\\' && p[i + 1])
6388 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006389 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 --p;
6391 }
6392}
6393#endif
6394
6395#endif /* FEAT_CMDHIST */
6396
Bram Moolenaar064154c2016-03-19 22:50:43 +01006397#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006398/*
6399 * Get pointer to the command line info to use. cmdline_paste() may clear
6400 * ccline and put the previous value in prev_ccline.
6401 */
6402 static struct cmdline_info *
6403get_ccline_ptr(void)
6404{
6405 if ((State & CMDLINE) == 0)
6406 return NULL;
6407 if (ccline.cmdbuff != NULL)
6408 return &ccline;
6409 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6410 return &prev_ccline;
6411 return NULL;
6412}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006413#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006414
Bram Moolenaar064154c2016-03-19 22:50:43 +01006415#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006416/*
6417 * Get the current command line in allocated memory.
6418 * Only works when the command line is being edited.
6419 * Returns NULL when something is wrong.
6420 */
6421 char_u *
6422get_cmdline_str(void)
6423{
6424 struct cmdline_info *p = get_ccline_ptr();
6425
6426 if (p == NULL)
6427 return NULL;
6428 return vim_strnsave(p->cmdbuff, p->cmdlen);
6429}
6430
6431/*
6432 * Get the current command line position, counted in bytes.
6433 * Zero is the first position.
6434 * Only works when the command line is being edited.
6435 * Returns -1 when something is wrong.
6436 */
6437 int
6438get_cmdline_pos(void)
6439{
6440 struct cmdline_info *p = get_ccline_ptr();
6441
6442 if (p == NULL)
6443 return -1;
6444 return p->cmdpos;
6445}
6446
6447/*
6448 * Set the command line byte position to "pos". Zero is the first position.
6449 * Only works when the command line is being edited.
6450 * Returns 1 when failed, 0 when OK.
6451 */
6452 int
6453set_cmdline_pos(
6454 int pos)
6455{
6456 struct cmdline_info *p = get_ccline_ptr();
6457
6458 if (p == NULL)
6459 return 1;
6460
6461 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6462 * changed the command line. */
6463 if (pos < 0)
6464 new_cmdpos = 0;
6465 else
6466 new_cmdpos = pos;
6467 return 0;
6468}
6469#endif
6470
6471#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6472/*
6473 * Get the current command-line type.
6474 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6475 * Only works when the command line is being edited.
6476 * Returns NUL when something is wrong.
6477 */
6478 int
6479get_cmdline_type(void)
6480{
6481 struct cmdline_info *p = get_ccline_ptr();
6482
6483 if (p == NULL)
6484 return NUL;
6485 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006486 return
6487# ifdef FEAT_EVAL
6488 (p->input_fn) ? '@' :
6489# endif
6490 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006491 return p->cmdfirstc;
6492}
6493#endif
6494
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6496/*
6497 * Get indices "num1,num2" that specify a range within a list (not a range of
6498 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6499 * Returns OK if parsed successfully, otherwise FAIL.
6500 */
6501 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006502get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 int len;
6505 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006506 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 *str = skipwhite(*str);
6509 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6510 {
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 *str += len;
6513 *num1 = (int)num;
6514 first = TRUE;
6515 }
6516 *str = skipwhite(*str);
6517 if (**str == ',') /* parse "to" part of range */
6518 {
6519 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006520 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 if (len > 0)
6522 {
6523 *num2 = (int)num;
6524 *str = skipwhite(*str + len);
6525 }
6526 else if (!first) /* no number given at all */
6527 return FAIL;
6528 }
6529 else if (first) /* only one number given */
6530 *num2 = *num1;
6531 return OK;
6532}
6533#endif
6534
6535#if defined(FEAT_CMDHIST) || defined(PROTO)
6536/*
6537 * :history command - print a history
6538 */
6539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006540ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 histentry_T *hist;
6543 int histype1 = HIST_CMD;
6544 int histype2 = HIST_CMD;
6545 int hisidx1 = 1;
6546 int hisidx2 = -1;
6547 int idx;
6548 int i, j, k;
6549 char_u *end;
6550 char_u *arg = eap->arg;
6551
6552 if (hislen == 0)
6553 {
6554 MSG(_("'history' option is zero"));
6555 return;
6556 }
6557
6558 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6559 {
6560 end = arg;
6561 while (ASCII_ISALPHA(*end)
6562 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6563 end++;
6564 i = *end;
6565 *end = NUL;
6566 histype1 = get_histtype(arg);
6567 if (histype1 == -1)
6568 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006569 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {
6571 histype1 = 0;
6572 histype2 = HIST_COUNT-1;
6573 }
6574 else
6575 {
6576 *end = i;
6577 EMSG(_(e_trailing));
6578 return;
6579 }
6580 }
6581 else
6582 histype2 = histype1;
6583 *end = i;
6584 }
6585 else
6586 end = arg;
6587 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6588 {
6589 EMSG(_(e_trailing));
6590 return;
6591 }
6592
6593 for (; !got_int && histype1 <= histype2; ++histype1)
6594 {
6595 STRCPY(IObuff, "\n # ");
6596 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6597 MSG_PUTS_TITLE(IObuff);
6598 idx = hisidx[histype1];
6599 hist = history[histype1];
6600 j = hisidx1;
6601 k = hisidx2;
6602 if (j < 0)
6603 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6604 if (k < 0)
6605 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6606 if (idx >= 0 && j <= k)
6607 for (i = idx + 1; !got_int; ++i)
6608 {
6609 if (i == hislen)
6610 i = 0;
6611 if (hist[i].hisstr != NULL
6612 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6613 {
6614 msg_putchar('\n');
6615 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6616 hist[i].hisnum);
6617 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6618 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006619 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 else
6621 STRCAT(IObuff, hist[i].hisstr);
6622 msg_outtrans(IObuff);
6623 out_flush();
6624 }
6625 if (i == idx)
6626 break;
6627 }
6628 }
6629}
6630#endif
6631
6632#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006633/*
6634 * Buffers for history read from a viminfo file. Only valid while reading.
6635 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006636static histentry_T *viminfo_history[HIST_COUNT] =
6637 {NULL, NULL, NULL, NULL, NULL};
6638static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6639static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640static int viminfo_add_at_front = FALSE;
6641
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006642static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644/*
6645 * Translate a history type number to the associated character.
6646 */
6647 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006648hist_type2char(
6649 int type,
6650 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 if (type == HIST_CMD)
6653 return ':';
6654 if (type == HIST_SEARCH)
6655 {
6656 if (use_question)
6657 return '?';
6658 else
6659 return '/';
6660 }
6661 if (type == HIST_EXPR)
6662 return '=';
6663 return '@';
6664}
6665
6666/*
6667 * Prepare for reading the history from the viminfo file.
6668 * This allocates history arrays to store the read history lines.
6669 */
6670 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006671prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 int i;
6674 int num;
6675 int type;
6676 int len;
6677
6678 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006679 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (asklen > hislen)
6681 asklen = hislen;
6682
6683 for (type = 0; type < HIST_COUNT; ++type)
6684 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006685 /* Count the number of empty spaces in the history list. Entries read
6686 * from viminfo previously are also considered empty. If there are
6687 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006689 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 num++;
6691 len = asklen;
6692 if (num > len)
6693 len = num;
6694 if (len <= 0)
6695 viminfo_history[type] = NULL;
6696 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006697 viminfo_history[type] = (histentry_T *)lalloc(
6698 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (viminfo_history[type] == NULL)
6700 len = 0;
6701 viminfo_hislen[type] = len;
6702 viminfo_hisidx[type] = 0;
6703 }
6704}
6705
6706/*
6707 * Accept a line from the viminfo, store it in the history array when it's
6708 * new.
6709 */
6710 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006711read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 int type;
6714 long_u len;
6715 char_u *val;
6716 char_u *p;
6717
6718 type = hist_char2type(virp->vir_line[0]);
6719 if (viminfo_hisidx[type] < viminfo_hislen[type])
6720 {
6721 val = viminfo_readstring(virp, 1, TRUE);
6722 if (val != NULL && *val != NUL)
6723 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006724 int sep = (*val == ' ' ? NUL : *val);
6725
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006727 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {
6729 /* Need to re-allocate to append the separator byte. */
6730 len = STRLEN(val);
6731 p = lalloc(len + 2, TRUE);
6732 if (p != NULL)
6733 {
6734 if (type == HIST_SEARCH)
6735 {
6736 /* Search entry: Move the separator from the first
6737 * column to after the NUL. */
6738 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006739 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741 else
6742 {
6743 /* Not a search entry: No separator in the viminfo
6744 * file, add a NUL separator. */
6745 mch_memmove(p, val, (size_t)len + 1);
6746 p[len + 1] = NUL;
6747 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006748 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6749 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006750 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6751 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006752 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
6754 }
6755 }
6756 vim_free(val);
6757 }
6758 return viminfo_readline(virp);
6759}
6760
Bram Moolenaar07219f92013-04-14 23:19:36 +02006761/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006762 * Accept a new style history line from the viminfo, store it in the history
6763 * array when it's new.
6764 */
6765 void
6766handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006767 garray_T *values,
6768 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006769{
6770 int type;
6771 long_u len;
6772 char_u *val;
6773 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006774 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006775
6776 /* Check the format:
6777 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006778 if (values->ga_len < 4
6779 || vp[0].bv_type != BVAL_NR
6780 || vp[1].bv_type != BVAL_NR
6781 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6782 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006783 return;
6784
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006785 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006786 if (type >= HIST_COUNT)
6787 return;
6788 if (viminfo_hisidx[type] < viminfo_hislen[type])
6789 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006790 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006791 if (val != NULL && *val != NUL)
6792 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006793 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6794 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006795 int idx;
6796 int overwrite = FALSE;
6797
6798 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6799 {
6800 /* If lines were written by an older Vim we need to avoid
6801 * getting duplicates. See if the entry already exists. */
6802 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6803 {
6804 p = viminfo_history[type][idx].hisstr;
6805 if (STRCMP(val, p) == 0
6806 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6807 {
6808 overwrite = TRUE;
6809 break;
6810 }
6811 }
6812
6813 if (!overwrite)
6814 {
6815 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006816 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006817 p = lalloc(len + 2, TRUE);
6818 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006819 else
6820 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006821 if (p != NULL)
6822 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006823 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006824 if (!overwrite)
6825 {
6826 mch_memmove(p, val, (size_t)len + 1);
6827 /* Put the separator after the NUL. */
6828 p[len + 1] = sep;
6829 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006830 viminfo_history[type][idx].hisnum = 0;
6831 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006832 viminfo_hisidx[type]++;
6833 }
6834 }
6835 }
6836 }
6837 }
6838}
6839
6840/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006841 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006842 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006843 static void
6844concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 int idx;
6847 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006848
6849 idx = hisidx[type] + viminfo_hisidx[type];
6850 if (idx >= hislen)
6851 idx -= hislen;
6852 else if (idx < 0)
6853 idx = hislen - 1;
6854 if (viminfo_add_at_front)
6855 hisidx[type] = idx;
6856 else
6857 {
6858 if (hisidx[type] == -1)
6859 hisidx[type] = hislen - 1;
6860 do
6861 {
6862 if (history[type][idx].hisstr != NULL
6863 || history[type][idx].viminfo)
6864 break;
6865 if (++idx == hislen)
6866 idx = 0;
6867 } while (idx != hisidx[type]);
6868 if (idx != hisidx[type] && --idx < 0)
6869 idx = hislen - 1;
6870 }
6871 for (i = 0; i < viminfo_hisidx[type]; i++)
6872 {
6873 vim_free(history[type][idx].hisstr);
6874 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6875 history[type][idx].viminfo = TRUE;
6876 history[type][idx].time_set = viminfo_history[type][i].time_set;
6877 if (--idx < 0)
6878 idx = hislen - 1;
6879 }
6880 idx += 1;
6881 idx %= hislen;
6882 for (i = 0; i < viminfo_hisidx[type]; i++)
6883 {
6884 history[type][idx++].hisnum = ++hisnum[type];
6885 idx %= hislen;
6886 }
6887}
6888
6889#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6890 static int
6891#ifdef __BORLANDC__
6892_RTLENTRYF
6893#endif
6894sort_hist(const void *s1, const void *s2)
6895{
6896 histentry_T *p1 = *(histentry_T **)s1;
6897 histentry_T *p2 = *(histentry_T **)s2;
6898
6899 if (p1->time_set < p2->time_set) return -1;
6900 if (p1->time_set > p2->time_set) return 1;
6901 return 0;
6902}
6903#endif
6904
6905/*
6906 * Merge history lines from viminfo and lines typed in this Vim based on the
6907 * timestamp;
6908 */
6909 static void
6910merge_history(int type)
6911{
6912 int max_len;
6913 histentry_T **tot_hist;
6914 histentry_T *new_hist;
6915 int i;
6916 int len;
6917
6918 /* Make one long list with all entries. */
6919 max_len = hislen + viminfo_hisidx[type];
6920 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6921 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6922 if (tot_hist == NULL || new_hist == NULL)
6923 {
6924 vim_free(tot_hist);
6925 vim_free(new_hist);
6926 return;
6927 }
6928 for (i = 0; i < viminfo_hisidx[type]; i++)
6929 tot_hist[i] = &viminfo_history[type][i];
6930 len = i;
6931 for (i = 0; i < hislen; i++)
6932 if (history[type][i].hisstr != NULL)
6933 tot_hist[len++] = &history[type][i];
6934
6935 /* Sort the list on timestamp. */
6936 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6937
6938 /* Keep the newest ones. */
6939 for (i = 0; i < hislen; i++)
6940 {
6941 if (i < len)
6942 {
6943 new_hist[i] = *tot_hist[i];
6944 tot_hist[i]->hisstr = NULL;
6945 if (new_hist[i].hisnum == 0)
6946 new_hist[i].hisnum = ++hisnum[type];
6947 }
6948 else
6949 clear_hist_entry(&new_hist[i]);
6950 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006951 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006952
6953 /* Free what is not kept. */
6954 for (i = 0; i < viminfo_hisidx[type]; i++)
6955 vim_free(viminfo_history[type][i].hisstr);
6956 for (i = 0; i < hislen; i++)
6957 vim_free(history[type][i].hisstr);
6958 vim_free(history[type]);
6959 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006960 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006961}
6962
6963/*
6964 * Finish reading history lines from viminfo. Not used when writing viminfo.
6965 */
6966 void
6967finish_viminfo_history(vir_T *virp)
6968{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006970 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972 for (type = 0; type < HIST_COUNT; ++type)
6973 {
6974 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006975 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006976
6977 if (merge)
6978 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006980 concat_history(type);
6981
Bram Moolenaard23a8232018-02-10 18:45:26 +01006982 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006983 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985}
6986
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006987/*
6988 * Write history to viminfo file in "fp".
6989 * When "merge" is TRUE merge history lines with a previously read viminfo
6990 * file, data is in viminfo_history[].
6991 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006994write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 int i;
6997 int type;
6998 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006999 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 init_history();
7002 if (hislen == 0)
7003 return;
7004 for (type = 0; type < HIST_COUNT; ++type)
7005 {
7006 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7007 if (num_saved == 0)
7008 continue;
7009 if (num_saved < 0) /* Use default */
7010 num_saved = hislen;
7011 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7012 type == HIST_CMD ? _("Command Line") :
7013 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007014 type == HIST_EXPR ? _("Expression") :
7015 type == HIST_INPUT ? _("Input Line") :
7016 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (num_saved > hislen)
7018 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007019
7020 /*
7021 * Merge typed and viminfo history:
7022 * round 1: history of typed commands.
7023 * round 2: history from recently read viminfo.
7024 */
7025 for (round = 1; round <= 2; ++round)
7026 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007027 if (round == 1)
7028 /* start at newest entry, somewhere in the list */
7029 i = hisidx[type];
7030 else if (viminfo_hisidx[type] > 0)
7031 /* start at newest entry, first in the list */
7032 i = 0;
7033 else
7034 /* empty list */
7035 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007036 if (i >= 0)
7037 while (num_saved > 0
7038 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007040 char_u *p;
7041 time_t timestamp;
7042 int c = NUL;
7043
7044 if (round == 1)
7045 {
7046 p = history[type][i].hisstr;
7047 timestamp = history[type][i].time_set;
7048 }
7049 else
7050 {
7051 p = viminfo_history[type] == NULL ? NULL
7052 : viminfo_history[type][i].hisstr;
7053 timestamp = viminfo_history[type] == NULL ? 0
7054 : viminfo_history[type][i].time_set;
7055 }
7056
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007057 if (p != NULL && (round == 2
7058 || !merge
7059 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007061 --num_saved;
7062 fputc(hist_type2char(type, TRUE), fp);
7063 /* For the search history: put the separator in the
7064 * second column; use a space if there isn't one. */
7065 if (type == HIST_SEARCH)
7066 {
7067 c = p[STRLEN(p) + 1];
7068 putc(c == NUL ? ' ' : c, fp);
7069 }
7070 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007071
7072 {
7073 char cbuf[NUMBUFLEN];
7074
7075 /* New style history with a bar line. Format:
7076 * |{bartype},{histtype},{timestamp},{separator},"text" */
7077 if (c == NUL)
7078 cbuf[0] = NUL;
7079 else
7080 sprintf(cbuf, "%d", c);
7081 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7082 type, (long)timestamp, cbuf);
7083 barline_writestring(fp, p, LSIZE - 20);
7084 putc('\n', fp);
7085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007087 if (round == 1)
7088 {
7089 /* Decrement index, loop around and stop when back at
7090 * the start. */
7091 if (--i < 0)
7092 i = hislen - 1;
7093 if (i == hisidx[type])
7094 break;
7095 }
7096 else
7097 {
7098 /* Increment index. Stop at the end in the while. */
7099 ++i;
7100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007102 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007103 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007104 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007105 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007106 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007107 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
7109}
7110#endif /* FEAT_VIMINFO */
7111
7112#if defined(FEAT_FKMAP) || defined(PROTO)
7113/*
7114 * Write a character at the current cursor+offset position.
7115 * It is directly written into the command buffer block.
7116 */
7117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007118cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7121 {
7122 EMSG(_("E198: cmd_pchar beyond the command length"));
7123 return;
7124 }
7125 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7126 ccline.cmdbuff[ccline.cmdlen] = NUL;
7127}
7128
7129 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007130cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7133 {
7134 /* EMSG(_("cmd_gchar beyond the command length")); */
7135 return NUL;
7136 }
7137 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7138}
7139#endif
7140
7141#if defined(FEAT_CMDWIN) || defined(PROTO)
7142/*
7143 * Open a window on the current command line and history. Allow editing in
7144 * the window. Returns when the window is closed.
7145 * Returns:
7146 * CR if the command is to be executed
7147 * Ctrl_C if it is to be abandoned
7148 * K_IGNORE if editing continues
7149 */
7150 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007151open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
7153 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007154 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007156 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 win_T *wp;
7158 int i;
7159 linenr_T lnum;
7160 int histtype;
7161 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 int save_restart_edit = restart_edit;
7163 int save_State = State;
7164 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007165#ifdef FEAT_RIGHTLEFT
7166 int save_cmdmsg_rl = cmdmsg_rl;
7167#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007168#ifdef FEAT_FOLDING
7169 int save_KeyTyped;
7170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
7172 /* Can't do this recursively. Can't do it when typing a password. */
7173 if (cmdwin_type != 0
7174# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7175 || cmdline_star > 0
7176# endif
7177 )
7178 {
7179 beep_flush();
7180 return K_IGNORE;
7181 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007182 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
7184 /* Save current window sizes. */
7185 win_size_save(&winsizes);
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007188 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007189
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007190 /* don't use a new tab page */
7191 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007192 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 /* Create a window for the command-line buffer. */
7195 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7196 {
7197 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007198 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 return K_IGNORE;
7200 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007201 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
7203 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007204 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007205 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007208#ifdef FEAT_FOLDING
7209 curwin->w_p_fen = FALSE;
7210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007212 curwin->w_p_rl = cmdmsg_rl;
7213 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007215 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007218 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007219 /* But don't allow switching to another buffer. */
7220 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar46152342005-09-07 21:18:43 +00007222 /* Showing the prompt may have set need_wait_return, reset it. */
7223 need_wait_return = FALSE;
7224
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007225 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7227 {
7228 if (p_wc == TAB)
7229 {
7230 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7231 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7232 }
7233 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7234 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007235 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007237 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7238 * sets 'textwidth' to 78). */
7239 curbuf->b_p_tw = 0;
7240
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 /* Fill the buffer with the history. */
7242 init_history();
7243 if (hislen > 0)
7244 {
7245 i = hisidx[histtype];
7246 if (i >= 0)
7247 {
7248 lnum = 0;
7249 do
7250 {
7251 if (++i == hislen)
7252 i = 0;
7253 if (history[histtype][i].hisstr != NULL)
7254 ml_append(lnum++, history[histtype][i].hisstr,
7255 (colnr_T)0, FALSE);
7256 }
7257 while (i != hisidx[histtype]);
7258 }
7259 }
7260
7261 /* Replace the empty last line with the current command-line and put the
7262 * cursor there. */
7263 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7264 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7265 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007266 changed_line_abv_curs();
7267 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007268 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007271 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
7273 /* No Ex mode here! */
7274 exmode_active = 0;
7275
7276 State = NORMAL;
7277# ifdef FEAT_MOUSE
7278 setmouse();
7279# endif
7280
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007282 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007283 if (restart_edit != 0) /* autocmd with ":startinsert" */
7284 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
7286 i = RedrawingDisabled;
7287 RedrawingDisabled = 0;
7288
7289 /*
7290 * Call the main loop until <CR> or CTRL-C is typed.
7291 */
7292 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007293 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
7295 RedrawingDisabled = i;
7296
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007297# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007298 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007299# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007302 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007303
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007304# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007305 /* Restore KeyTyped in case it is modified by autocommands */
7306 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307# endif
7308
Bram Moolenaarccc18222007-05-10 18:25:20 +00007309 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007310 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 cmdwin_type = 0;
7312
7313 exmode_active = save_exmode;
7314
Bram Moolenaarf9821062008-06-20 16:31:07 +00007315 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007317 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 {
7319 cmdwin_result = Ctrl_C;
7320 EMSG(_("E199: Active window or buffer deleted"));
7321 }
7322 else
7323 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007324# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 /* autocmds may abort script processing */
7326 if (aborting() && cmdwin_result != K_IGNORE)
7327 cmdwin_result = Ctrl_C;
7328# endif
7329 /* Set the new command line from the cmdline buffer. */
7330 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007331 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007333 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7334
7335 if (histtype == HIST_CMD)
7336 {
7337 /* Execute the command directly. */
7338 ccline.cmdbuff = vim_strsave((char_u *)p);
7339 cmdwin_result = CAR;
7340 }
7341 else
7342 {
7343 /* First need to cancel what we were doing. */
7344 ccline.cmdbuff = NULL;
7345 stuffcharReadbuff(':');
7346 stuffReadbuff((char_u *)p);
7347 stuffcharReadbuff(CAR);
7348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350 else if (cmdwin_result == K_XF2) /* :qa typed */
7351 {
7352 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7353 cmdwin_result = CAR;
7354 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007355 else if (cmdwin_result == Ctrl_C)
7356 {
7357 /* :q or :close, don't execute any command
7358 * and don't modify the cmd window. */
7359 ccline.cmdbuff = NULL;
7360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 else
7362 ccline.cmdbuff = vim_strsave(ml_get_curline());
7363 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007364 {
7365 ccline.cmdbuff = vim_strsave((char_u *)"");
7366 ccline.cmdlen = 0;
7367 ccline.cmdbufflen = 1;
7368 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 else
7372 {
7373 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7374 ccline.cmdbufflen = ccline.cmdlen + 1;
7375 ccline.cmdpos = curwin->w_cursor.col;
7376 if (ccline.cmdpos > ccline.cmdlen)
7377 ccline.cmdpos = ccline.cmdlen;
7378 if (cmdwin_result == K_IGNORE)
7379 {
7380 set_cmdspos_cursor();
7381 redrawcmd();
7382 }
7383 }
7384
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007386 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007387# ifdef FEAT_CONCEAL
7388 /* Avoid command-line window first character being concealed. */
7389 curwin->w_p_cole = 0;
7390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007392 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 win_goto(old_curwin);
7394 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007395
7396 /* win_close() may have already wiped the buffer when 'bh' is
7397 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007398 if (bufref_valid(&bufref))
7399 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
7401 /* Restore window sizes. */
7402 win_size_restore(&winsizes);
7403
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007404 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406
7407 ga_clear(&winsizes);
7408 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007409# ifdef FEAT_RIGHTLEFT
7410 cmdmsg_rl = save_cmdmsg_rl;
7411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
7413 State = save_State;
7414# ifdef FEAT_MOUSE
7415 setmouse();
7416# endif
7417
7418 return cmdwin_result;
7419}
7420#endif /* FEAT_CMDWIN */
7421
7422/*
7423 * Used for commands that either take a simple command string argument, or:
7424 * cmd << endmarker
7425 * {script}
7426 * endmarker
7427 * Returns a pointer to allocated memory with {script} or NULL.
7428 */
7429 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007430script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 char_u *theline;
7433 char *end_pattern = NULL;
7434 char dot[] = ".";
7435 garray_T ga;
7436
7437 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7438 return NULL;
7439
7440 ga_init2(&ga, 1, 0x400);
7441
7442 if (cmd[2] != NUL)
7443 end_pattern = (char *)skipwhite(cmd + 2);
7444 else
7445 end_pattern = dot;
7446
7447 for (;;)
7448 {
7449 theline = eap->getline(
7450#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007451 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452#endif
7453 NUL, eap->cookie, 0);
7454
7455 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007456 {
7457 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 ga_concat(&ga, theline);
7462 ga_append(&ga, '\n');
7463 vim_free(theline);
7464 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007465 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466
7467 return (char_u *)ga.ga_data;
7468}