blob: 4af390440988f84e18a2ccc893e185fa35270f09 [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;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200234 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200235} incsearch_state_T;
236
237 static void
238init_incsearch_state(incsearch_state_T *is_state)
239{
240 is_state->match_start = curwin->w_cursor;
241 is_state->did_incsearch = FALSE;
242 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200243 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200244 CLEAR_POS(&is_state->match_end);
245 is_state->save_cursor = curwin->w_cursor; // may be restored later
246 is_state->search_start = curwin->w_cursor;
247 save_viewstate(&is_state->init_viewstate);
248 save_viewstate(&is_state->old_viewstate);
249}
250
251/*
252 * First move cursor to end of match, then to the start. This
253 * moves the whole match onto the screen when 'nowrap' is set.
254 */
255 static void
256set_search_match(pos_T *t)
257{
258 t->lnum += search_match_lines;
259 t->col = search_match_endcol;
260 if (t->lnum > curbuf->b_ml.ml_line_count)
261 {
262 t->lnum = curbuf->b_ml.ml_line_count;
263 coladvance((colnr_T)MAXCOL);
264 }
265}
266
267/*
268 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200269 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200270 */
271 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200272do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
273 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200274{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200275 *skiplen = 0;
276 *patlen = ccline.cmdlen;
277
278 if (p_is && !cmd_silent)
279 {
280 // by default search all lines
281 search_first_line = 0;
282 search_last_line = MAXLNUM;
283
284 if (firstc == '/' || firstc == '?')
285 return TRUE;
286 if (firstc == ':')
287 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200288 char_u *cmd;
289 cmdmod_T save_cmdmod = cmdmod;
290 char_u *p;
291 int delim;
292 char_u *end;
293 char_u *dummy;
294 exarg_T ea;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200295
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200296 vim_memset(&ea, 0, sizeof(ea));
297 ea.line1 = 1;
298 ea.line2 = 1;
299 ea.cmd = ccline.cmdbuff;
300 ea.addr_type = ADDR_LINES;
301
302 parse_command_modifiers(&ea, &dummy, TRUE);
303 cmdmod = save_cmdmod;
304
305 cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200306 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
307 {
308 // Skip over "substitute" to find the pattern separator.
309 for (p = cmd; ASCII_ISALPHA(*p); ++p)
310 ;
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200311 if (*skipwhite(p) != NUL
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200312 && (STRNCMP(cmd, "substitute", p - cmd) == 0
Bram Moolenaar167ae422018-08-14 21:32:21 +0200313 || STRNCMP(cmd, "smagic", p - cmd) == 0
314 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200315 || STRNCMP(cmd, "global", p - cmd) == 0
316 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200317 {
Bram Moolenaar167ae422018-08-14 21:32:21 +0200318 if (*cmd == 's' && cmd[1] == 'm')
319 p_magic = TRUE;
320 else if (*cmd == 's' && cmd[1] == 'n')
321 p_magic = FALSE;
322
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200323 // Check for "global!/".
324 if (*cmd == 'g' && *p == '!')
325 {
326 p++;
327 if (*skipwhite(p) == NUL)
328 return FALSE;
329 }
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200330 p = skipwhite(p);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200331 delim = *p++;
332 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200333 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200334 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200335 pos_T save_cursor = curwin->w_cursor;
336
337 // found a non-empty pattern
338 *skiplen = (int)(p - ccline.cmdbuff);
339 *patlen = (int)(end - p);
340
341 // parse the address range
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200342 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200343 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200344 if (ea.addr_count > 0)
345 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200346 // Allow for reverse match.
347 if (ea.line2 < ea.line1)
348 {
349 search_first_line = ea.line2;
350 search_last_line = ea.line1;
351 }
352 else
353 {
354 search_first_line = ea.line1;
355 search_last_line = ea.line2;
356 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200357 }
358 else if (*cmd == 's')
359 {
360 // :s defaults to the current line
361 search_first_line = curwin->w_cursor.lnum;
362 search_last_line = curwin->w_cursor.lnum;
363 }
364
365 curwin->w_cursor = save_cursor;
366 return TRUE;
367 }
368 }
369 }
370 }
371 }
372
373 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200374}
375
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200376 static void
377finish_incsearch_highlighting(
378 int gotesc,
379 incsearch_state_T *is_state,
380 int call_update_screen)
381{
382 if (is_state->did_incsearch)
383 {
384 is_state->did_incsearch = FALSE;
385 if (gotesc)
386 curwin->w_cursor = is_state->save_cursor;
387 else
388 {
389 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
390 {
391 // put the '" mark at the original position
392 curwin->w_cursor = is_state->save_cursor;
393 setpcmark();
394 }
395 curwin->w_cursor = is_state->search_start;
396 }
397 restore_viewstate(&is_state->old_viewstate);
398 highlight_match = FALSE;
399 validate_cursor(); /* needed for TAB */
400 if (call_update_screen)
401 update_screen(SOME_VALID);
402 else
403 redraw_all_later(SOME_VALID);
Bram Moolenaar167ae422018-08-14 21:32:21 +0200404 p_magic = is_state->magic_save;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200405 }
406}
407
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200408/*
409 * Do 'incsearch' highlighting if desired.
410 */
411 static void
412may_do_incsearch_highlighting(
413 int firstc,
414 long count,
415 incsearch_state_T *is_state)
416{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200417 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200418 int i;
419 pos_T end_pos;
420 struct cmdline_info save_ccline;
421#ifdef FEAT_RELTIME
422 proftime_T tm;
423#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200424 int next_char;
425 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200426
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200427 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200428 {
429 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200430 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200431 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200432
433 // If there is a character waiting, search and redraw later.
434 if (char_avail())
435 {
436 is_state->incsearch_postponed = TRUE;
437 return;
438 }
439 is_state->incsearch_postponed = FALSE;
440
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200441 if (search_first_line == 0)
442 // start at the original cursor position
443 curwin->w_cursor = is_state->search_start;
444 else
445 {
446 // start at the first line in the range
447 curwin->w_cursor.lnum = search_first_line;
448 curwin->w_cursor.col = 0;
449 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200450 save_last_search_pattern();
451
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200452 // Use the previous pattern for ":s//".
453 next_char = ccline.cmdbuff[skiplen + patlen];
454 use_last_pat = patlen == 0 && skiplen > 0
455 && ccline.cmdbuff[skiplen - 1] == next_char;
456
457 // If there is no pattern, don't do anything.
458 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200459 {
460 i = 0;
461 set_no_hlsearch(TRUE); // turn off previous highlight
462 redraw_all_later(SOME_VALID);
463 }
464 else
465 {
466 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
467
468 cursor_off(); // so the user knows we're busy
469 out_flush();
470 ++emsg_off; // so it doesn't beep if bad expr
471#ifdef FEAT_RELTIME
472 // Set the time limit to half a second.
473 profile_setlimit(500L, &tm);
474#endif
475 if (!p_hls)
476 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200477 if (search_first_line != 0)
478 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200479 ccline.cmdbuff[skiplen + patlen] = NUL;
480 i = do_search(NULL, firstc == ':' ? '/' : firstc,
481 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200482#ifdef FEAT_RELTIME
483 &tm, NULL
484#else
485 NULL, NULL
486#endif
487 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200488 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200489 --emsg_off;
490
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200491 if (curwin->w_cursor.lnum < search_first_line
492 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200493 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200494 // match outside of address range
495 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200496 curwin->w_cursor = is_state->search_start;
497 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200498
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200499 // if interrupted while searching, behave like it failed
500 if (got_int)
501 {
502 (void)vpeekc(); // remove <C-C> from input stream
503 got_int = FALSE; // don't abandon the command line
504 i = 0;
505 }
506 else if (char_avail())
507 // cancelled searching because a char was typed
508 is_state->incsearch_postponed = TRUE;
509 }
510 if (i != 0)
511 highlight_match = TRUE; // highlight position
512 else
513 highlight_match = FALSE; // remove highlight
514
515 // First restore the old curwin values, so the screen is positioned in the
516 // same way as the actual search command.
517 restore_viewstate(&is_state->old_viewstate);
518 changed_cline_bef_curs();
519 update_topline();
520
521 if (i != 0)
522 {
523 pos_T save_pos = curwin->w_cursor;
524
525 is_state->match_start = curwin->w_cursor;
526 set_search_match(&curwin->w_cursor);
527 validate_cursor();
528 end_pos = curwin->w_cursor;
529 is_state->match_end = end_pos;
530 curwin->w_cursor = save_pos;
531 }
532 else
533 end_pos = curwin->w_cursor; // shutup gcc 4
534
535 // Disable 'hlsearch' highlighting if the pattern matches everything.
536 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200537 if (!use_last_pat)
538 {
539 next_char = ccline.cmdbuff[skiplen + patlen];
540 ccline.cmdbuff[skiplen + patlen] = NUL;
541 if (empty_pattern(ccline.cmdbuff))
542 set_no_hlsearch(TRUE);
543 ccline.cmdbuff[skiplen + patlen] = next_char;
544 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200545
546 validate_cursor();
547 // May redraw the status line to show the cursor position.
548 if (p_ru && curwin->w_status_height > 0)
549 curwin->w_redr_status = TRUE;
550
551 save_cmdline(&save_ccline);
552 update_screen(SOME_VALID);
553 restore_cmdline(&save_ccline);
554 restore_last_search_pattern();
555
556 // Leave it at the end to make CTRL-R CTRL-W work.
557 if (i != 0)
558 curwin->w_cursor = end_pos;
559
560 msg_starthere();
561 redrawcmdline();
562 is_state->did_incsearch = TRUE;
563}
564
565/*
566 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
567 * or previous match.
568 * Returns FAIL when jumping to cmdline_not_changed;
569 */
570 static int
571may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200572 int firstc,
573 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200575 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200577 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578 pos_T t;
579 char_u *pat;
580 int search_flags = SEARCH_NOOF;
581 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200582 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200583
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200584 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200585 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200586 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587 return FAIL;
588
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200589 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200590 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200592 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200593 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200594 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200595 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200596 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597
598 save_last_search_pattern();
599 cursor_off();
600 out_flush();
601 if (c == Ctrl_G)
602 {
603 t = is_state->match_end;
604 if (LT_POS(is_state->match_start, is_state->match_end))
605 // Start searching at the end of the match not at the beginning of
606 // the next column.
607 (void)decl(&t);
608 search_flags += SEARCH_COL;
609 }
610 else
611 t = is_state->match_start;
612 if (!p_hls)
613 search_flags += SEARCH_KEEP;
614 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 save = pat[patlen];
616 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200617 i = searchit(curwin, curbuf, &t,
618 c == Ctrl_G ? FORWARD : BACKWARD,
619 pat, count, search_flags,
620 RE_SEARCH, 0, NULL, NULL);
621 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200622 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200623 if (i)
624 {
625 is_state->search_start = is_state->match_start;
626 is_state->match_end = t;
627 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629 {
630 // Move just before the current match, so that when nv_search
631 // finishes the cursor will be put back on the match.
632 is_state->search_start = t;
633 (void)decl(&is_state->search_start);
634 }
635 else if (c == Ctrl_G && firstc == '?')
636 {
637 // Move just after the current match, so that when nv_search
638 // finishes the cursor will be put back on the match.
639 is_state->search_start = t;
640 (void)incl(&is_state->search_start);
641 }
642 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
643 {
644 // wrap around
645 is_state->search_start = t;
646 if (firstc == '?')
647 (void)incl(&is_state->search_start);
648 else
649 (void)decl(&is_state->search_start);
650 }
651
652 set_search_match(&is_state->match_end);
653 curwin->w_cursor = is_state->match_start;
654 changed_cline_bef_curs();
655 update_topline();
656 validate_cursor();
657 highlight_match = TRUE;
658 save_viewstate(&is_state->old_viewstate);
659 update_screen(NOT_VALID);
660 redrawcmdline();
661 }
662 else
663 vim_beep(BO_ERROR);
664 restore_last_search_pattern();
665 return FAIL;
666}
667
668/*
669 * When CTRL-L typed: add character from the match to the pattern.
670 * May set "*c" to the added character.
671 * Return OK when jumping to cmdline_not_changed.
672 */
673 static int
674may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
675{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200676 int skiplen, patlen;
677
678 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200679 return FAIL;
680
681 // Add a character from under the cursor for 'incsearch'.
682 if (is_state->did_incsearch)
683 {
684 curwin->w_cursor = is_state->match_end;
685 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
686 {
687 *c = gchar_cursor();
688
689 // If 'ignorecase' and 'smartcase' are set and the
690 // command line has no uppercase characters, convert
691 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200692 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200693 *c = MB_TOLOWER(*c);
694 if (*c != NUL)
695 {
696 if (*c == firstc || vim_strchr((char_u *)(
697 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
698 {
699 // put a backslash before special characters
700 stuffcharReadbuff(*c);
701 *c = '\\';
702 }
703 return FAIL;
704 }
705 }
706 }
707 return OK;
708}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200709#endif
710
Bram Moolenaar66216052017-12-16 16:33:44 +0100711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 * getcmdline() - accept a command line starting with firstc.
713 *
714 * firstc == ':' get ":" command line.
715 * firstc == '/' or '?' get search pattern
716 * firstc == '=' get expression
717 * firstc == '@' get text for input() function
718 * firstc == '>' get text for debug mode
719 * firstc == NUL get text for :insert command
720 * firstc == -1 like NUL, and break on CTRL-C
721 *
722 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
723 * command line.
724 *
725 * Careful: getcmdline() can be called recursively!
726 *
727 * Return pointer to allocated string if there is a commandline, NULL
728 * otherwise.
729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100731getcmdline(
732 int firstc,
733 long count UNUSED, /* only used for incremental search */
734 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 int c;
737 int i;
738 int j;
739 int gotesc = FALSE; /* TRUE when <ESC> just typed */
740 int do_abbr; /* when TRUE check for abbr. */
741#ifdef FEAT_CMDHIST
742 char_u *lookfor = NULL; /* string to match */
743 int hiscnt; /* current history line in use */
744 int histype; /* history type to be used */
745#endif
746#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200747 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748#endif
749 int did_wild_list = FALSE; /* did wild_list() recently */
750 int wim_index = 0; /* index in wim_flags[] */
751 int res;
752 int save_msg_scroll = msg_scroll;
753 int save_State = State; /* remember State when called */
754 int some_key_typed = FALSE; /* one of the keys was typed */
755#ifdef FEAT_MOUSE
756 /* mouse drag and release events are ignored, unless they are
757 * preceded with a mouse down event */
758 int ignore_drag_release = TRUE;
759#endif
760#ifdef FEAT_EVAL
761 int break_ctrl_c = FALSE;
762#endif
763 expand_T xpc;
764 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200765#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000766 /* Everything that may work recursively should save and restore the
767 * current command line in save_ccline. That includes update_screen(), a
768 * custom status line may invoke ":normal". */
769 struct cmdline_info save_ccline;
770#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200771 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#ifdef FEAT_EVAL
774 if (firstc == -1)
775 {
776 firstc = NUL;
777 break_ctrl_c = TRUE;
778 }
779#endif
780#ifdef FEAT_RIGHTLEFT
781 /* start without Hebrew mapping for a command line */
782 if (firstc == ':' || firstc == '=' || firstc == '>')
783 cmd_hkmap = 0;
784#endif
785
786 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200789 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790#endif
791
792 /*
793 * set some variables for redrawcmd()
794 */
795 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000796 ccline.cmdindent = (firstc > 0 ? indent : 0);
797
798 /* alloc initial ccline.cmdbuff */
799 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (ccline.cmdbuff == NULL)
801 return NULL; /* out of memory */
802 ccline.cmdlen = ccline.cmdpos = 0;
803 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100804 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000806 /* autoindent for :insert and :append */
807 if (firstc <= 0)
808 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200809 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000810 ccline.cmdbuff[indent] = NUL;
811 ccline.cmdpos = indent;
812 ccline.cmdspos = indent;
813 ccline.cmdlen = indent;
814 }
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000817 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819#ifdef FEAT_RIGHTLEFT
820 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
821 && (firstc == '/' || firstc == '?'))
822 cmdmsg_rl = TRUE;
823 else
824 cmdmsg_rl = FALSE;
825#endif
826
827 redir_off = TRUE; /* don't redirect the typed command */
828 if (!cmd_silent)
829 {
830 i = msg_scrolled;
831 msg_scrolled = 0; /* avoid wait_return message */
832 gotocmdline(TRUE);
833 msg_scrolled += i;
834 redrawcmdprompt(); /* draw prompt or indent */
835 set_cmdspos();
836 }
837 xpc.xp_context = EXPAND_NOTHING;
838 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000839#ifndef BACKSLASH_IN_FILENAME
840 xpc.xp_shell = FALSE;
841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000843#if defined(FEAT_EVAL)
844 if (ccline.input_fn)
845 {
846 xpc.xp_context = ccline.xp_context;
847 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000848# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000849 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000850# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000851 }
852#endif
853
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 /*
855 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
856 * doing ":@0" when register 0 doesn't contain a CR.
857 */
858 msg_scroll = FALSE;
859
860 State = CMDLINE;
861
862 if (firstc == '/' || firstc == '?' || firstc == '@')
863 {
864 /* Use ":lmap" mappings for search pattern and input(). */
865 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
866 b_im_ptr = &curbuf->b_p_iminsert;
867 else
868 b_im_ptr = &curbuf->b_p_imsearch;
869 if (*b_im_ptr == B_IMODE_LMAP)
870 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100871#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 im_set_active(*b_im_ptr == B_IMODE_IM);
873#endif
874 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100875#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 else if (p_imcmdline)
877 im_set_active(TRUE);
878#endif
879
880#ifdef FEAT_MOUSE
881 setmouse();
882#endif
883#ifdef CURSOR_SHAPE
884 ui_cursor_shape(); /* may show different cursor shape */
885#endif
886
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000887 /* When inside an autocommand for writing "exiting" may be set and
888 * terminal mode set to cooked. Need to set raw mode here then. */
889 settmode(TMODE_RAW);
890
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200891 /* Trigger CmdlineEnter autocommands. */
892 cmdline_type = firstc == NUL ? '-' : firstc;
893 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200894
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#ifdef FEAT_CMDHIST
896 init_history();
897 hiscnt = hislen; /* set hiscnt to impossible history value */
898 histype = hist_char2type(firstc);
899#endif
900
901#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000902 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903#endif
904
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200905 /* If something above caused an error, reset the flags, we do want to type
906 * and execute commands. Display may be messed up a bit. */
907 if (did_emsg)
908 redrawcmd();
909 did_emsg = FALSE;
910 got_int = FALSE;
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 /*
913 * Collect the command string, handling editing keys.
914 */
915 for (;;)
916 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000917 redir_off = TRUE; /* Don't redirect the typed command.
918 Repeated, because a ":redir" inside
919 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920#ifdef USE_ON_FLY_SCROLL
921 dont_scroll = FALSE; /* allow scrolling here */
922#endif
923 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
924
Bram Moolenaar72532d32018-04-07 19:09:09 +0200925 did_emsg = FALSE; /* There can't really be a reason why an error
926 that occurs while typing a command should
927 cause the command not to be executed. */
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000930
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100931 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
932 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000933 do
934 {
935 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100936 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 if (KeyTyped)
939 {
940 some_key_typed = TRUE;
941#ifdef FEAT_RIGHTLEFT
942 if (cmd_hkmap)
943 c = hkmap(c);
944# ifdef FEAT_FKMAP
945 if (cmd_fkmap)
946 c = cmdl_fkmap(c);
947# endif
948 if (cmdmsg_rl && !KeyStuffed)
949 {
950 /* Invert horizontal movements and operations. Only when
951 * typed by the user directly, not when the result of a
952 * mapping. */
953 switch (c)
954 {
955 case K_RIGHT: c = K_LEFT; break;
956 case K_S_RIGHT: c = K_S_LEFT; break;
957 case K_C_RIGHT: c = K_C_LEFT; break;
958 case K_LEFT: c = K_RIGHT; break;
959 case K_S_LEFT: c = K_S_RIGHT; break;
960 case K_C_LEFT: c = K_C_RIGHT; break;
961 }
962 }
963#endif
964 }
965
966 /*
967 * Ignore got_int when CTRL-C was typed here.
968 * Don't ignore it in :global, we really need to break then, e.g., for
969 * ":g/pat/normal /pat" (without the <CR>).
970 * Don't ignore it for the input() function.
971 */
972 if ((c == Ctrl_C
973#ifdef UNIX
974 || c == intr_char
975#endif
976 )
977#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
978 && firstc != '@'
979#endif
980#ifdef FEAT_EVAL
981 && !break_ctrl_c
982#endif
983 && !global_busy)
984 got_int = FALSE;
985
986#ifdef FEAT_CMDHIST
987 /* free old command line when finished moving around in the history
988 * list */
989 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000990 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000991 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 && c != K_PAGEDOWN && c != K_PAGEUP
993 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100996 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#endif
998
999 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001000 * When there are matching completions to select <S-Tab> works like
1001 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001003 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 c = Ctrl_P;
1005
1006#ifdef FEAT_WILDMENU
1007 /* Special translations for 'wildmenu' */
1008 if (did_wild_list && p_wmnu)
1009 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001010 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001012 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 c = Ctrl_N;
1014 }
1015 /* Hitting CR after "emenu Name.": complete submenu */
1016 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1017 && ccline.cmdpos > 1
1018 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1019 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1020 && (c == '\n' || c == '\r' || c == K_KENTER))
1021 c = K_DOWN;
1022#endif
1023
1024 /* free expanded names when finished walking through matches */
1025 if (xpc.xp_numfiles != -1
1026 && !(c == p_wc && KeyTyped) && c != p_wcm
1027 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1028 && c != Ctrl_L)
1029 {
1030 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1031 did_wild_list = FALSE;
1032#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001033 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#endif
1035 xpc.xp_context = EXPAND_NOTHING;
1036 wim_index = 0;
1037#ifdef FEAT_WILDMENU
1038 if (p_wmnu && wild_menu_showing != 0)
1039 {
1040 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001041 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001042
1043 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001044 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 if (wild_menu_showing == WM_SCROLLED)
1047 {
1048 /* Entered command line, move it up */
1049 cmdline_row--;
1050 redrawcmd();
1051 }
1052 else if (save_p_ls != -1)
1053 {
1054 /* restore 'laststatus' and 'winminheight' */
1055 p_ls = save_p_ls;
1056 p_wmh = save_p_wmh;
1057 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001058 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001060 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 redrawcmd();
1062 save_p_ls = -1;
1063 }
1064 else
1065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 redraw_statuslines();
1068 }
1069 KeyTyped = skt;
1070 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001071 if (ccline.input_fn)
1072 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
1074#endif
1075 }
1076
1077#ifdef FEAT_WILDMENU
1078 /* Special translations for 'wildmenu' */
1079 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1080 {
1081 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001082 if (c == K_DOWN && ccline.cmdpos > 0
1083 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001085 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
1087 /* Hitting <Up>: Remove one submenu name in front of the
1088 * cursor */
1089 int found = FALSE;
1090
1091 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1092 i = 0;
1093 while (--j > 0)
1094 {
1095 /* check for start of menu name */
1096 if (ccline.cmdbuff[j] == ' '
1097 && ccline.cmdbuff[j - 1] != '\\')
1098 {
1099 i = j + 1;
1100 break;
1101 }
1102 /* check for start of submenu name */
1103 if (ccline.cmdbuff[j] == '.'
1104 && ccline.cmdbuff[j - 1] != '\\')
1105 {
1106 if (found)
1107 {
1108 i = j + 1;
1109 break;
1110 }
1111 else
1112 found = TRUE;
1113 }
1114 }
1115 if (i > 0)
1116 cmdline_del(i);
1117 c = p_wc;
1118 xpc.xp_context = EXPAND_NOTHING;
1119 }
1120 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001121 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001122 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001123 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
1125 char_u upseg[5];
1126
1127 upseg[0] = PATHSEP;
1128 upseg[1] = '.';
1129 upseg[2] = '.';
1130 upseg[3] = PATHSEP;
1131 upseg[4] = NUL;
1132
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001133 if (c == K_DOWN
1134 && ccline.cmdpos > 0
1135 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1136 && (ccline.cmdpos < 3
1137 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1139 {
1140 /* go down a directory */
1141 c = p_wc;
1142 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001143 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 /* If in a direct ancestor, strip off one ../ to go down */
1146 int found = FALSE;
1147
1148 j = ccline.cmdpos;
1149 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1150 while (--j > i)
1151 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001152#ifdef FEAT_MBYTE
1153 if (has_mbyte)
1154 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (vim_ispathsep(ccline.cmdbuff[j]))
1157 {
1158 found = TRUE;
1159 break;
1160 }
1161 }
1162 if (found
1163 && ccline.cmdbuff[j - 1] == '.'
1164 && ccline.cmdbuff[j - 2] == '.'
1165 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1166 {
1167 cmdline_del(j - 2);
1168 c = p_wc;
1169 }
1170 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001171 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 /* go up a directory */
1174 int found = FALSE;
1175
1176 j = ccline.cmdpos - 1;
1177 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1178 while (--j > i)
1179 {
1180#ifdef FEAT_MBYTE
1181 if (has_mbyte)
1182 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1183#endif
1184 if (vim_ispathsep(ccline.cmdbuff[j])
1185#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001186 && vim_strchr((char_u *)" *?[{`$%#",
1187 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#endif
1189 )
1190 {
1191 if (found)
1192 {
1193 i = j + 1;
1194 break;
1195 }
1196 else
1197 found = TRUE;
1198 }
1199 }
1200
1201 if (!found)
1202 j = i;
1203 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1204 j += 4;
1205 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1206 && j == i)
1207 j += 3;
1208 else
1209 j = 0;
1210 if (j > 0)
1211 {
1212 /* TODO this is only for DOS/UNIX systems - need to put in
1213 * machine-specific stuff here and in upseg init */
1214 cmdline_del(j);
1215 put_on_cmdline(upseg + 1, 3, FALSE);
1216 }
1217 else if (ccline.cmdpos > i)
1218 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001219
1220 /* Now complete in the new directory. Set KeyTyped in case the
1221 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001223 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
1227#endif /* FEAT_WILDMENU */
1228
1229 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1230 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1231 if (c == Ctrl_BSL)
1232 {
1233 ++no_mapping;
1234 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001235 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 --no_mapping;
1237 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001238 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1239 * is in a mapping. */
1240 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1241 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 vungetc(c);
1244 c = Ctrl_BSL;
1245 }
1246#ifdef FEAT_EVAL
1247 else if (c == 'e')
1248 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001249 char_u *p = NULL;
1250 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 /*
1253 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001254 * Need to save and restore the current command line, to be
1255 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 */
1257 if (ccline.cmdpos == ccline.cmdlen)
1258 new_cmdpos = 99999; /* keep it at the end */
1259 else
1260 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001261
1262 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001264 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (c == '=')
1266 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001267 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001268 * to avoid nasty things like going to another buffer when
1269 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001270 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001271 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001273 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001274 restore_cmdline(&save_ccline);
1275
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001276 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001278 len = (int)STRLEN(p);
1279 if (realloc_cmdbuff(len + 1) == OK)
1280 {
1281 ccline.cmdlen = len;
1282 STRCPY(ccline.cmdbuff, p);
1283 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001285 /* Restore the cursor or use the position set with
1286 * set_cmdline_pos(). */
1287 if (new_cmdpos > ccline.cmdlen)
1288 ccline.cmdpos = ccline.cmdlen;
1289 else
1290 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001292 KeyTyped = FALSE; /* Don't do p_wc completion. */
1293 redrawcmd();
1294 goto cmdline_changed;
1295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
1297 }
1298 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001299 got_int = FALSE; /* don't abandon the command line */
1300 did_emsg = FALSE;
1301 emsg_on_display = FALSE;
1302 redrawcmd();
1303 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305#endif
1306 else
1307 {
1308 if (c == Ctrl_G && p_im && restart_edit == 0)
1309 restart_edit = 'a';
1310 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1311 in history */
1312 goto returncmd; /* back to Normal mode */
1313 }
1314 }
1315
1316#ifdef FEAT_CMDWIN
1317 if (c == cedit_key || c == K_CMDWIN)
1318 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001319 if (ex_normal_busy == 0 && got_int == FALSE)
1320 {
1321 /*
1322 * Open a window to edit the command line (and history).
1323 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001324 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001325 some_key_typed = TRUE;
1326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328# ifdef FEAT_DIGRAPHS
1329 else
1330# endif
1331#endif
1332#ifdef FEAT_DIGRAPHS
1333 c = do_digraph(c);
1334#endif
1335
1336 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1337 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1338 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001339 /* In Ex mode a backslash escapes a newline. */
1340 if (exmode_active
1341 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001342 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001343 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001344 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001346 if (c == K_KENTER)
1347 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001349 else
1350 {
1351 gotesc = FALSE; /* Might have typed ESC previously, don't
1352 truncate the cmdline now. */
1353 if (ccheck_abbr(c + ABBR_OFF))
1354 goto cmdline_changed;
1355 if (!cmd_silent)
1356 {
1357 windgoto(msg_row, 0);
1358 out_flush();
1359 }
1360 break;
1361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363
1364 /*
1365 * Completion for 'wildchar' or 'wildcharm' key.
1366 * - hitting <ESC> twice means: abandon command line.
1367 * - wildcard expansion is only done when the 'wildchar' key is really
1368 * typed, not when it comes from a macro
1369 */
1370 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1371 {
1372 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1373 {
1374 /* if 'wildmode' contains "list" may still need to list */
1375 if (xpc.xp_numfiles > 1
1376 && !did_wild_list
1377 && (wim_flags[wim_index] & WIM_LIST))
1378 {
1379 (void)showmatches(&xpc, FALSE);
1380 redrawcmd();
1381 did_wild_list = TRUE;
1382 }
1383 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001384 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1385 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001387 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1388 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 else
1390 res = OK; /* don't insert 'wildchar' now */
1391 }
1392 else /* typed p_wc first time */
1393 {
1394 wim_index = 0;
1395 j = ccline.cmdpos;
1396 /* if 'wildmode' first contains "longest", get longest
1397 * common part */
1398 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001399 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1400 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001402 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1403 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405 /* if interrupted while completing, behave like it failed */
1406 if (got_int)
1407 {
1408 (void)vpeekc(); /* remove <C-C> from input stream */
1409 got_int = FALSE; /* don't abandon the command line */
1410 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1411#ifdef FEAT_WILDMENU
1412 xpc.xp_context = EXPAND_NOTHING;
1413#endif
1414 goto cmdline_changed;
1415 }
1416
1417 /* when more than one match, and 'wildmode' first contains
1418 * "list", or no change and 'wildmode' contains "longest,list",
1419 * list all matches */
1420 if (res == OK && xpc.xp_numfiles > 1)
1421 {
1422 /* a "longest" that didn't do anything is skipped (but not
1423 * "list:longest") */
1424 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1425 wim_index = 1;
1426 if ((wim_flags[wim_index] & WIM_LIST)
1427#ifdef FEAT_WILDMENU
1428 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1429#endif
1430 )
1431 {
1432 if (!(wim_flags[0] & WIM_LONGEST))
1433 {
1434#ifdef FEAT_WILDMENU
1435 int p_wmnu_save = p_wmnu;
1436 p_wmnu = 0;
1437#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001438 /* remove match */
1439 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#ifdef FEAT_WILDMENU
1441 p_wmnu = p_wmnu_save;
1442#endif
1443 }
1444#ifdef FEAT_WILDMENU
1445 (void)showmatches(&xpc, p_wmnu
1446 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1447#else
1448 (void)showmatches(&xpc, FALSE);
1449#endif
1450 redrawcmd();
1451 did_wild_list = TRUE;
1452 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001453 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1454 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001456 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1457 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001460 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462#ifdef FEAT_WILDMENU
1463 else if (xpc.xp_numfiles == -1)
1464 xpc.xp_context = EXPAND_NOTHING;
1465#endif
1466 }
1467 if (wim_index < 3)
1468 ++wim_index;
1469 if (c == ESC)
1470 gotesc = TRUE;
1471 if (res == OK)
1472 goto cmdline_changed;
1473 }
1474
1475 gotesc = FALSE;
1476
1477 /* <S-Tab> goes to last match, in a clumsy way */
1478 if (c == K_S_TAB && KeyTyped)
1479 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001480 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1481 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1482 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 goto cmdline_changed;
1484 }
1485
1486 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1487 c = NL;
1488
1489 do_abbr = TRUE; /* default: check for abbreviation */
1490
1491 /*
1492 * Big switch for a typed command line character.
1493 */
1494 switch (c)
1495 {
1496 case K_BS:
1497 case Ctrl_H:
1498 case K_DEL:
1499 case K_KDEL:
1500 case Ctrl_W:
1501#ifdef FEAT_FKMAP
1502 if (cmd_fkmap && c == K_BS)
1503 c = K_DEL;
1504#endif
1505 if (c == K_KDEL)
1506 c = K_DEL;
1507
1508 /*
1509 * delete current character is the same as backspace on next
1510 * character, except at end of line
1511 */
1512 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1513 ++ccline.cmdpos;
1514#ifdef FEAT_MBYTE
1515 if (has_mbyte && c == K_DEL)
1516 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1517 ccline.cmdbuff + ccline.cmdpos);
1518#endif
1519 if (ccline.cmdpos > 0)
1520 {
1521 char_u *p;
1522
1523 j = ccline.cmdpos;
1524 p = ccline.cmdbuff + j;
1525#ifdef FEAT_MBYTE
1526 if (has_mbyte)
1527 {
1528 p = mb_prevptr(ccline.cmdbuff, p);
1529 if (c == Ctrl_W)
1530 {
1531 while (p > ccline.cmdbuff && vim_isspace(*p))
1532 p = mb_prevptr(ccline.cmdbuff, p);
1533 i = mb_get_class(p);
1534 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1535 p = mb_prevptr(ccline.cmdbuff, p);
1536 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001537 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
1539 }
1540 else
1541#endif
1542 if (c == Ctrl_W)
1543 {
1544 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1545 --p;
1546 i = vim_iswordc(p[-1]);
1547 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1548 && vim_iswordc(p[-1]) == i)
1549 --p;
1550 }
1551 else
1552 --p;
1553 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1554 ccline.cmdlen -= j - ccline.cmdpos;
1555 i = ccline.cmdpos;
1556 while (i < ccline.cmdlen)
1557 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1558
1559 /* Truncate at the end, required for multi-byte chars. */
1560 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001561#ifdef FEAT_SEARCH_EXTRA
1562 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001563 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001564 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001565 /* save view settings, so that the screen
1566 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001567 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001568 }
1569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 redrawcmd();
1571 }
1572 else if (ccline.cmdlen == 0 && c != Ctrl_W
1573 && ccline.cmdprompt == NULL && indent == 0)
1574 {
1575 /* In ex and debug mode it doesn't make sense to return. */
1576 if (exmode_active
1577#ifdef FEAT_EVAL
1578 || ccline.cmdfirstc == '>'
1579#endif
1580 )
1581 goto cmdline_not_changed;
1582
Bram Moolenaard23a8232018-02-10 18:45:26 +01001583 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (!cmd_silent)
1585 {
1586#ifdef FEAT_RIGHTLEFT
1587 if (cmdmsg_rl)
1588 msg_col = Columns;
1589 else
1590#endif
1591 msg_col = 0;
1592 msg_putchar(' '); /* delete ':' */
1593 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001594#ifdef FEAT_SEARCH_EXTRA
1595 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001596 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 redraw_cmdline = TRUE;
1599 goto returncmd; /* back to cmd mode */
1600 }
1601 goto cmdline_changed;
1602
1603 case K_INS:
1604 case K_KINS:
1605#ifdef FEAT_FKMAP
1606 /* if Farsi mode set, we are in reverse insert mode -
1607 Do not change the mode */
1608 if (cmd_fkmap)
1609 beep_flush();
1610 else
1611#endif
1612 ccline.overstrike = !ccline.overstrike;
1613#ifdef CURSOR_SHAPE
1614 ui_cursor_shape(); /* may show different cursor shape */
1615#endif
1616 goto cmdline_not_changed;
1617
1618 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001619 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 /* ":lmap" mappings exists, toggle use of mappings. */
1622 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001623#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 im_set_active(FALSE); /* Disable input method */
1625#endif
1626 if (b_im_ptr != NULL)
1627 {
1628 if (State & LANGMAP)
1629 *b_im_ptr = B_IMODE_LMAP;
1630 else
1631 *b_im_ptr = B_IMODE_NONE;
1632 }
1633 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001634#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 else
1636 {
1637 /* There are no ":lmap" mappings, toggle IM. When
1638 * 'imdisable' is set don't try getting the status, it's
1639 * always off. */
1640 if ((p_imdisable && b_im_ptr != NULL)
1641 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1642 {
1643 im_set_active(FALSE); /* Disable input method */
1644 if (b_im_ptr != NULL)
1645 *b_im_ptr = B_IMODE_NONE;
1646 }
1647 else
1648 {
1649 im_set_active(TRUE); /* Enable input method */
1650 if (b_im_ptr != NULL)
1651 *b_im_ptr = B_IMODE_IM;
1652 }
1653 }
1654#endif
1655 if (b_im_ptr != NULL)
1656 {
1657 if (b_im_ptr == &curbuf->b_p_iminsert)
1658 set_iminsert_global();
1659 else
1660 set_imsearch_global();
1661 }
1662#ifdef CURSOR_SHAPE
1663 ui_cursor_shape(); /* may show different cursor shape */
1664#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001665#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 /* Show/unshow value of 'keymap' in status lines later. */
1667 status_redraw_curbuf();
1668#endif
1669 goto cmdline_not_changed;
1670
1671/* case '@': only in very old vi */
1672 case Ctrl_U:
1673 /* delete all characters left of the cursor */
1674 j = ccline.cmdpos;
1675 ccline.cmdlen -= j;
1676 i = ccline.cmdpos = 0;
1677 while (i < ccline.cmdlen)
1678 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1679 /* Truncate at the end, required for multi-byte chars. */
1680 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001681#ifdef FEAT_SEARCH_EXTRA
1682 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001683 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 redrawcmd();
1686 goto cmdline_changed;
1687
1688#ifdef FEAT_CLIPBOARD
1689 case Ctrl_Y:
1690 /* Copy the modeless selection, if there is one. */
1691 if (clip_star.state != SELECT_CLEARED)
1692 {
1693 if (clip_star.state == SELECT_DONE)
1694 clip_copy_modeless_selection(TRUE);
1695 goto cmdline_not_changed;
1696 }
1697 break;
1698#endif
1699
1700 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1701 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001702 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001703 * ":normal" runs out of characters. */
1704 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001705 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 goto cmdline_not_changed;
1707
1708 gotesc = TRUE; /* will free ccline.cmdbuff after
1709 putting it in history */
1710 goto returncmd; /* back to cmd mode */
1711
1712 case Ctrl_R: /* insert register */
1713#ifdef USE_ON_FLY_SCROLL
1714 dont_scroll = TRUE; /* disallow scrolling here */
1715#endif
1716 putcmdline('"', TRUE);
1717 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001718 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (i == Ctrl_O)
1720 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1721 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001722 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001723 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 --no_mapping;
1725#ifdef FEAT_EVAL
1726 /*
1727 * Insert the result of an expression.
1728 * Need to save the current command line, to be able to enter
1729 * a new one...
1730 */
1731 new_cmdpos = -1;
1732 if (c == '=')
1733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1735 {
1736 beep_flush();
1737 c = ESC;
1738 }
1739 else
1740 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001741 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001743 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
1745 }
1746#endif
1747 if (c != ESC) /* use ESC to cancel inserting register */
1748 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001749 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001750
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001751#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001752 /* When there was a serious error abort getting the
1753 * command line. */
1754 if (aborting())
1755 {
1756 gotesc = TRUE; /* will free ccline.cmdbuff after
1757 putting it in history */
1758 goto returncmd; /* back to cmd mode */
1759 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 KeyTyped = FALSE; /* Don't do p_wc completion. */
1762#ifdef FEAT_EVAL
1763 if (new_cmdpos >= 0)
1764 {
1765 /* set_cmdline_pos() was used */
1766 if (new_cmdpos > ccline.cmdlen)
1767 ccline.cmdpos = ccline.cmdlen;
1768 else
1769 ccline.cmdpos = new_cmdpos;
1770 }
1771#endif
1772 }
1773 redrawcmd();
1774 goto cmdline_changed;
1775
1776 case Ctrl_D:
1777 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1778 break; /* Use ^D as normal char instead */
1779
1780 redrawcmd();
1781 continue; /* don't do incremental search now */
1782
1783 case K_RIGHT:
1784 case K_S_RIGHT:
1785 case K_C_RIGHT:
1786 do
1787 {
1788 if (ccline.cmdpos >= ccline.cmdlen)
1789 break;
1790 i = cmdline_charsize(ccline.cmdpos);
1791 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1792 break;
1793 ccline.cmdspos += i;
1794#ifdef FEAT_MBYTE
1795 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001796 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 + ccline.cmdpos);
1798 else
1799#endif
1800 ++ccline.cmdpos;
1801 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001802 while ((c == K_S_RIGHT || c == K_C_RIGHT
1803 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1805#ifdef FEAT_MBYTE
1806 if (has_mbyte)
1807 set_cmdspos_cursor();
1808#endif
1809 goto cmdline_not_changed;
1810
1811 case K_LEFT:
1812 case K_S_LEFT:
1813 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001814 if (ccline.cmdpos == 0)
1815 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 do
1817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 --ccline.cmdpos;
1819#ifdef FEAT_MBYTE
1820 if (has_mbyte) /* move to first byte of char */
1821 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1822 ccline.cmdbuff + ccline.cmdpos);
1823#endif
1824 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1825 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001826 while (ccline.cmdpos > 0
1827 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001828 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1830#ifdef FEAT_MBYTE
1831 if (has_mbyte)
1832 set_cmdspos_cursor();
1833#endif
1834 goto cmdline_not_changed;
1835
1836 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001837 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001838 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaar4770d092006-01-12 23:22:24 +00001840#ifdef FEAT_GUI_W32
1841 /* On Win32 ignore <M-F4>, we get it when closing the window was
1842 * cancelled. */
1843 case K_F4:
1844 if (mod_mask == MOD_MASK_ALT)
1845 {
1846 redrawcmd(); /* somehow the cmdline is cleared */
1847 goto cmdline_not_changed;
1848 }
1849 break;
1850#endif
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852#ifdef FEAT_MOUSE
1853 case K_MIDDLEDRAG:
1854 case K_MIDDLERELEASE:
1855 goto cmdline_not_changed; /* Ignore mouse */
1856
1857 case K_MIDDLEMOUSE:
1858# ifdef FEAT_GUI
1859 /* When GUI is active, also paste when 'mouse' is empty */
1860 if (!gui.in_use)
1861# endif
1862 if (!mouse_has(MOUSE_COMMAND))
1863 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001864# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001866 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001868# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001869 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 redrawcmd();
1871 goto cmdline_changed;
1872
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001873# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001875 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 redrawcmd();
1877 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880 case K_LEFTDRAG:
1881 case K_LEFTRELEASE:
1882 case K_RIGHTDRAG:
1883 case K_RIGHTRELEASE:
1884 /* Ignore drag and release events when the button-down wasn't
1885 * seen before. */
1886 if (ignore_drag_release)
1887 goto cmdline_not_changed;
1888 /* FALLTHROUGH */
1889 case K_LEFTMOUSE:
1890 case K_RIGHTMOUSE:
1891 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1892 ignore_drag_release = TRUE;
1893 else
1894 ignore_drag_release = FALSE;
1895# ifdef FEAT_GUI
1896 /* When GUI is active, also move when 'mouse' is empty */
1897 if (!gui.in_use)
1898# endif
1899 if (!mouse_has(MOUSE_COMMAND))
1900 goto cmdline_not_changed; /* Ignore mouse */
1901# ifdef FEAT_CLIPBOARD
1902 if (mouse_row < cmdline_row && clip_star.available)
1903 {
1904 int button, is_click, is_drag;
1905
1906 /*
1907 * Handle modeless selection.
1908 */
1909 button = get_mouse_button(KEY2TERMCAP1(c),
1910 &is_click, &is_drag);
1911 if (mouse_model_popup() && button == MOUSE_LEFT
1912 && (mod_mask & MOD_MASK_SHIFT))
1913 {
1914 /* Translate shift-left to right button. */
1915 button = MOUSE_RIGHT;
1916 mod_mask &= ~MOD_MASK_SHIFT;
1917 }
1918 clip_modeless(button, is_click, is_drag);
1919 goto cmdline_not_changed;
1920 }
1921# endif
1922
1923 set_cmdspos();
1924 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1925 ++ccline.cmdpos)
1926 {
1927 i = cmdline_charsize(ccline.cmdpos);
1928 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1929 && mouse_col < ccline.cmdspos % Columns + i)
1930 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001931# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 if (has_mbyte)
1933 {
1934 /* Count ">" for double-wide char that doesn't fit. */
1935 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001936 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 + ccline.cmdpos) - 1;
1938 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 ccline.cmdspos += i;
1941 }
1942 goto cmdline_not_changed;
1943
1944 /* Mouse scroll wheel: ignored here */
1945 case K_MOUSEDOWN:
1946 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001947 case K_MOUSELEFT:
1948 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 /* Alternate buttons ignored here */
1950 case K_X1MOUSE:
1951 case K_X1DRAG:
1952 case K_X1RELEASE:
1953 case K_X2MOUSE:
1954 case K_X2DRAG:
1955 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001956 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 goto cmdline_not_changed;
1958
1959#endif /* FEAT_MOUSE */
1960
1961#ifdef FEAT_GUI
1962 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1963 case K_LEFTRELEASE_NM:
1964 goto cmdline_not_changed;
1965
1966 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001967 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 gui_do_scroll();
1970 redrawcmd();
1971 }
1972 goto cmdline_not_changed;
1973
1974 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001975 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001977 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 redrawcmd();
1979 }
1980 goto cmdline_not_changed;
1981#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001982#ifdef FEAT_GUI_TABLINE
1983 case K_TABLINE:
1984 case K_TABMENU:
1985 /* Don't want to change any tabs here. Make sure the same tab
1986 * is still selected. */
1987 if (gui_use_tabline())
1988 gui_mch_set_curtab(tabpage_index(curtab));
1989 goto cmdline_not_changed;
1990#endif
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 case K_SELECT: /* end of Select mode mapping - ignore */
1993 goto cmdline_not_changed;
1994
1995 case Ctrl_B: /* begin of command line */
1996 case K_HOME:
1997 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 case K_S_HOME:
1999 case K_C_HOME:
2000 ccline.cmdpos = 0;
2001 set_cmdspos();
2002 goto cmdline_not_changed;
2003
2004 case Ctrl_E: /* end of command line */
2005 case K_END:
2006 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 case K_S_END:
2008 case K_C_END:
2009 ccline.cmdpos = ccline.cmdlen;
2010 set_cmdspos_cursor();
2011 goto cmdline_not_changed;
2012
2013 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002014 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 break;
2016 goto cmdline_changed;
2017
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002018 case Ctrl_L:
2019#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002020 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002021 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002022#endif
2023
2024 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002025 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 break;
2027 goto cmdline_changed;
2028
2029 case Ctrl_N: /* next match */
2030 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002031 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002033 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2034 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002036 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002039 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 case K_UP:
2041 case K_DOWN:
2042 case K_S_UP:
2043 case K_S_DOWN:
2044 case K_PAGEUP:
2045 case K_KPAGEUP:
2046 case K_PAGEDOWN:
2047 case K_KPAGEDOWN:
2048 if (hislen == 0 || firstc == NUL) /* no history */
2049 goto cmdline_not_changed;
2050
2051 i = hiscnt;
2052
2053 /* save current command string so it can be restored later */
2054 if (lookfor == NULL)
2055 {
2056 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2057 goto cmdline_not_changed;
2058 lookfor[ccline.cmdpos] = NUL;
2059 }
2060
2061 j = (int)STRLEN(lookfor);
2062 for (;;)
2063 {
2064 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002065 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002066 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 if (hiscnt == hislen) /* first time */
2069 hiscnt = hisidx[histype];
2070 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2071 hiscnt = hislen - 1;
2072 else if (hiscnt != hisidx[histype] + 1)
2073 --hiscnt;
2074 else /* at top of list */
2075 {
2076 hiscnt = i;
2077 break;
2078 }
2079 }
2080 else /* one step forwards */
2081 {
2082 /* on last entry, clear the line */
2083 if (hiscnt == hisidx[histype])
2084 {
2085 hiscnt = hislen;
2086 break;
2087 }
2088
2089 /* not on a history line, nothing to do */
2090 if (hiscnt == hislen)
2091 break;
2092 if (hiscnt == hislen - 1) /* wrap around */
2093 hiscnt = 0;
2094 else
2095 ++hiscnt;
2096 }
2097 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2098 {
2099 hiscnt = i;
2100 break;
2101 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002102 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002103 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 || STRNCMP(history[histype][hiscnt].hisstr,
2105 lookfor, (size_t)j) == 0)
2106 break;
2107 }
2108
2109 if (hiscnt != i) /* jumped to other entry */
2110 {
2111 char_u *p;
2112 int len;
2113 int old_firstc;
2114
2115 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002116 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (hiscnt == hislen)
2118 p = lookfor; /* back to the old one */
2119 else
2120 p = history[histype][hiscnt].hisstr;
2121
2122 if (histype == HIST_SEARCH
2123 && p != lookfor
2124 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2125 {
2126 /* Correct for the separator character used when
2127 * adding the history entry vs the one used now.
2128 * First loop: count length.
2129 * Second loop: copy the characters. */
2130 for (i = 0; i <= 1; ++i)
2131 {
2132 len = 0;
2133 for (j = 0; p[j] != NUL; ++j)
2134 {
2135 /* Replace old sep with new sep, unless it is
2136 * escaped. */
2137 if (p[j] == old_firstc
2138 && (j == 0 || p[j - 1] != '\\'))
2139 {
2140 if (i > 0)
2141 ccline.cmdbuff[len] = firstc;
2142 }
2143 else
2144 {
2145 /* Escape new sep, unless it is already
2146 * escaped. */
2147 if (p[j] == firstc
2148 && (j == 0 || p[j - 1] != '\\'))
2149 {
2150 if (i > 0)
2151 ccline.cmdbuff[len] = '\\';
2152 ++len;
2153 }
2154 if (i > 0)
2155 ccline.cmdbuff[len] = p[j];
2156 }
2157 ++len;
2158 }
2159 if (i == 0)
2160 {
2161 alloc_cmdbuff(len);
2162 if (ccline.cmdbuff == NULL)
2163 goto returncmd;
2164 }
2165 }
2166 ccline.cmdbuff[len] = NUL;
2167 }
2168 else
2169 {
2170 alloc_cmdbuff((int)STRLEN(p));
2171 if (ccline.cmdbuff == NULL)
2172 goto returncmd;
2173 STRCPY(ccline.cmdbuff, p);
2174 }
2175
2176 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2177 redrawcmd();
2178 goto cmdline_changed;
2179 }
2180 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002181#endif
2182 goto cmdline_not_changed;
2183
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002184#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002185 case Ctrl_G: /* next match */
2186 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002187 if (may_adjust_incsearch_highlighting(
2188 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002189 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002190 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#endif
2192
2193 case Ctrl_V:
2194 case Ctrl_Q:
2195#ifdef FEAT_MOUSE
2196 ignore_drag_release = TRUE;
2197#endif
2198 putcmdline('^', TRUE);
2199 c = get_literal(); /* get next (two) character(s) */
2200 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002201 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#ifdef FEAT_MBYTE
2203 /* may need to remove ^ when composing char was typed */
2204 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2205 {
2206 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2207 msg_putchar(' ');
2208 cursorcmd();
2209 }
2210#endif
2211 break;
2212
2213#ifdef FEAT_DIGRAPHS
2214 case Ctrl_K:
2215#ifdef FEAT_MOUSE
2216 ignore_drag_release = TRUE;
2217#endif
2218 putcmdline('?', TRUE);
2219#ifdef USE_ON_FLY_SCROLL
2220 dont_scroll = TRUE; /* disallow scrolling here */
2221#endif
2222 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002223 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (c != NUL)
2225 break;
2226
2227 redrawcmd();
2228 goto cmdline_not_changed;
2229#endif /* FEAT_DIGRAPHS */
2230
2231#ifdef FEAT_RIGHTLEFT
2232 case Ctrl__: /* CTRL-_: switch language mode */
2233 if (!p_ari)
2234 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002235# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (p_altkeymap)
2237 {
2238 cmd_fkmap = !cmd_fkmap;
2239 if (cmd_fkmap) /* in Farsi always in Insert mode */
2240 ccline.overstrike = FALSE;
2241 }
2242 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 cmd_hkmap = !cmd_hkmap;
2245 goto cmdline_not_changed;
2246#endif
2247
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002248 case K_PS:
2249 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2250 goto cmdline_changed;
2251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 default:
2253#ifdef UNIX
2254 if (c == intr_char)
2255 {
2256 gotesc = TRUE; /* will free ccline.cmdbuff after
2257 putting it in history */
2258 goto returncmd; /* back to Normal mode */
2259 }
2260#endif
2261 /*
2262 * Normal character with no special meaning. Just set mod_mask
2263 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2264 * the string <S-Space>. This should only happen after ^V.
2265 */
2266 if (!IS_SPECIAL(c))
2267 mod_mask = 0x0;
2268 break;
2269 }
2270 /*
2271 * End of switch on command line character.
2272 * We come here if we have a normal character.
2273 */
2274
Bram Moolenaarede3e632013-06-23 16:16:19 +02002275 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#ifdef FEAT_MBYTE
2277 /* Add ABBR_OFF for characters above 0x100, this is
2278 * what check_abbr() expects. */
2279 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2280#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002281 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 goto cmdline_changed;
2283
2284 /*
2285 * put the character in the command line
2286 */
2287 if (IS_SPECIAL(c) || mod_mask != 0)
2288 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2289 else
2290 {
2291#ifdef FEAT_MBYTE
2292 if (has_mbyte)
2293 {
2294 j = (*mb_char2bytes)(c, IObuff);
2295 IObuff[j] = NUL; /* exclude composing chars */
2296 put_on_cmdline(IObuff, j, TRUE);
2297 }
2298 else
2299#endif
2300 {
2301 IObuff[0] = c;
2302 put_on_cmdline(IObuff, 1, TRUE);
2303 }
2304 }
2305 goto cmdline_changed;
2306
2307/*
2308 * This part implements incremental searches for "/" and "?"
2309 * Jump to cmdline_not_changed when a character has been read but the command
2310 * line did not change. Then we only search and redraw if something changed in
2311 * the past.
2312 * Jump to cmdline_changed when the command line did change.
2313 * (Sorry for the goto's, I know it is ugly).
2314 */
2315cmdline_not_changed:
2316#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002317 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 continue;
2319#endif
2320
2321cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002322 /* Trigger CmdlineChanged autocommands. */
2323 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002326 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#endif
2328
2329#ifdef FEAT_RIGHTLEFT
2330 if (cmdmsg_rl
2331# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002332 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333# endif
2334 )
2335 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002336 * right-left typing. Not efficient, but it works.
2337 * Do it only when there are no characters left to read
2338 * to avoid useless intermediate redraws. */
2339 if (vpeekc() == NUL)
2340 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#endif
2342 }
2343
2344returncmd:
2345
2346#ifdef FEAT_RIGHTLEFT
2347 cmdmsg_rl = FALSE;
2348#endif
2349
2350#ifdef FEAT_FKMAP
2351 cmd_fkmap = 0;
2352#endif
2353
2354 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002355 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002358 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#endif
2360
2361 if (ccline.cmdbuff != NULL)
2362 {
2363 /*
2364 * Put line in history buffer (":" and "=" only when it was typed).
2365 */
2366#ifdef FEAT_CMDHIST
2367 if (ccline.cmdlen && firstc != NUL
2368 && (some_key_typed || histype == HIST_SEARCH))
2369 {
2370 add_to_history(histype, ccline.cmdbuff, TRUE,
2371 histype == HIST_SEARCH ? firstc : NUL);
2372 if (firstc == ':')
2373 {
2374 vim_free(new_last_cmdline);
2375 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2376 }
2377 }
2378#endif
2379
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002380 if (gotesc)
2381 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
2383
2384 /*
2385 * If the screen was shifted up, redraw the whole screen (later).
2386 * If the line is too long, clear it, so ruler and shown command do
2387 * not get printed in the middle of it.
2388 */
2389 msg_check();
2390 msg_scroll = save_msg_scroll;
2391 redir_off = FALSE;
2392
2393 /* When the command line was typed, no need for a wait-return prompt. */
2394 if (some_key_typed)
2395 need_wait_return = FALSE;
2396
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002397 /* Trigger CmdlineLeave autocommands. */
2398 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002401#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2403 im_save_status(b_im_ptr);
2404 im_set_active(FALSE);
2405#endif
2406#ifdef FEAT_MOUSE
2407 setmouse();
2408#endif
2409#ifdef CURSOR_SHAPE
2410 ui_cursor_shape(); /* may show different cursor shape */
2411#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002412 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002414 {
2415 char_u *p = ccline.cmdbuff;
2416
2417 /* Make ccline empty, getcmdline() may try to use it. */
2418 ccline.cmdbuff = NULL;
2419 return p;
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421}
2422
2423#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2424/*
2425 * Get a command line with a prompt.
2426 * This is prepared to be called recursively from getcmdline() (e.g. by
2427 * f_input() when evaluating an expression from CTRL-R =).
2428 * Returns the command line in allocated memory, or NULL.
2429 */
2430 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002431getcmdline_prompt(
2432 int firstc,
2433 char_u *prompt, /* command line prompt */
2434 int attr, /* attributes for prompt */
2435 int xp_context, /* type of expansion */
2436 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 char_u *s;
2439 struct cmdline_info save_ccline;
2440 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002441 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002443 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 ccline.cmdprompt = prompt;
2445 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002446# ifdef FEAT_EVAL
2447 ccline.xp_context = xp_context;
2448 ccline.xp_arg = xp_arg;
2449 ccline.input_fn = (firstc == '@');
2450# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002451 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002453 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002454 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002455 /* Restore msg_col, the prompt from input() may have changed it.
2456 * But only if called recursively and the commandline is therefore being
2457 * restored to an old one; if not, the input() prompt stays on the screen,
2458 * so we need its modified msg_col left intact. */
2459 if (ccline.cmdbuff != NULL)
2460 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 return s;
2463}
2464#endif
2465
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002466/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002467 * Return TRUE when the text must not be changed and we can't switch to
2468 * another window or buffer. Used when editing the command line, evaluating
2469 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002470 */
2471 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002472text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002473{
2474#ifdef FEAT_CMDWIN
2475 if (cmdwin_type != 0)
2476 return TRUE;
2477#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002478 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002479}
2480
2481/*
2482 * Give an error message for a command that isn't allowed while the cmdline
2483 * window is open or editing the cmdline in another way.
2484 */
2485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002486text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002487{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002488 EMSG(_(get_text_locked_msg()));
2489}
2490
2491 char_u *
2492get_text_locked_msg(void)
2493{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002494#ifdef FEAT_CMDWIN
2495 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002496 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002497#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002498 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002499}
2500
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002501/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002502 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2503 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002504 */
2505 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002506curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002507{
2508 if (curbuf_lock > 0)
2509 {
2510 EMSG(_("E788: Not allowed to edit another buffer now"));
2511 return TRUE;
2512 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002513 return allbuf_locked();
2514}
2515
2516/*
2517 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2518 * message.
2519 */
2520 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002521allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002522{
2523 if (allbuf_lock > 0)
2524 {
2525 EMSG(_("E811: Not allowed to change buffer information now"));
2526 return TRUE;
2527 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002528 return FALSE;
2529}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002530
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002532cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2535 if (cmdline_star > 0) /* showing '*', always 1 position */
2536 return 1;
2537#endif
2538 return ptr2cells(ccline.cmdbuff + idx);
2539}
2540
2541/*
2542 * Compute the offset of the cursor on the command line for the prompt and
2543 * indent.
2544 */
2545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002546set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002548 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 ccline.cmdspos = 1 + ccline.cmdindent;
2550 else
2551 ccline.cmdspos = 0 + ccline.cmdindent;
2552}
2553
2554/*
2555 * Compute the screen position for the cursor on the command line.
2556 */
2557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002558set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 int i, m, c;
2561
2562 set_cmdspos();
2563 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002566 if (m < 0) /* overflow, Columns or Rows at weird value */
2567 m = MAXCOL;
2568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 else
2570 m = MAXCOL;
2571 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2572 {
2573 c = cmdline_charsize(i);
2574#ifdef FEAT_MBYTE
2575 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2576 if (has_mbyte)
2577 correct_cmdspos(i, c);
2578#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002579 /* If the cmdline doesn't fit, show cursor on last visible char.
2580 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if ((ccline.cmdspos += c) >= m)
2582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 ccline.cmdspos -= c;
2584 break;
2585 }
2586#ifdef FEAT_MBYTE
2587 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002588 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589#endif
2590 }
2591}
2592
2593#ifdef FEAT_MBYTE
2594/*
2595 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2596 * character that doesn't fit, so that a ">" must be displayed.
2597 */
2598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002599correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002601 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2603 && ccline.cmdspos % Columns + cells > Columns)
2604 ccline.cmdspos++;
2605}
2606#endif
2607
2608/*
2609 * Get an Ex command line for the ":" command.
2610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002612getexline(
2613 int c, /* normally ':', NUL for ":append" */
2614 void *cookie UNUSED,
2615 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 /* When executing a register, remove ':' that's in front of each line. */
2618 if (exec_from_reg && vpeekc() == ':')
2619 (void)vgetc();
2620 return getcmdline(c, 1L, indent);
2621}
2622
2623/*
2624 * Get an Ex command line for Ex mode.
2625 * In Ex mode we only use the OS supplied line editing features and no
2626 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002627 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002630getexmodeline(
2631 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002632 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002633 void *cookie UNUSED,
2634 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002636 garray_T line_ga;
2637 char_u *pend;
2638 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002639 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640 int escaped = FALSE; /* CTRL-V typed */
2641 int vcol = 0;
2642 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002643 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002644 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 /* Switch cursor on now. This avoids that it happens after the "\n", which
2647 * confuses the system function that computes tabstops. */
2648 cursor_on();
2649
2650 /* always start in column 0; write a newline if necessary */
2651 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002652 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002654 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002656 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002657 if (p_prompt)
2658 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 while (indent-- > 0)
2660 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663
2664 ga_init2(&line_ga, 1, 30);
2665
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002666 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002667 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002668 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002669 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002670 while (indent >= 8)
2671 {
2672 ga_append(&line_ga, TAB);
2673 msg_puts((char_u *)" ");
2674 indent -= 8;
2675 }
2676 while (indent-- > 0)
2677 {
2678 ga_append(&line_ga, ' ');
2679 msg_putchar(' ');
2680 }
2681 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002682 ++no_mapping;
2683 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 /*
2686 * Get the line, one character at a time.
2687 */
2688 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002689 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002691 long sw;
2692 char_u *s;
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (ga_grow(&line_ga, 40) == FAIL)
2695 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaarba748c82017-02-26 14:00:07 +01002697 /*
2698 * Get one character at a time.
2699 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002700 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002701
2702 /* Check for a ":normal" command and no more characters left. */
2703 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2704 c1 = '\n';
2705 else
2706 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002709 * Handle line editing.
2710 * Previously this was left to the system, putting the terminal in
2711 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 msg_putchar('\n');
2716 break;
2717 }
2718
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002719 if (c1 == K_PS)
2720 {
2721 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2722 goto redraw;
2723 }
2724
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002725 if (!escaped)
2726 {
2727 /* CR typed means "enter", which is NL */
2728 if (c1 == '\r')
2729 c1 = '\n';
2730
2731 if (c1 == BS || c1 == K_BS
2732 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002734 if (line_ga.ga_len > 0)
2735 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002736#ifdef FEAT_MBYTE
2737 if (has_mbyte)
2738 {
2739 p = (char_u *)line_ga.ga_data;
2740 p[line_ga.ga_len] = NUL;
2741 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2742 line_ga.ga_len -= len;
2743 }
2744 else
2745#endif
2746 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 goto redraw;
2748 }
2749 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
2751
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 msg_col = startcol;
2755 msg_clr_eos();
2756 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002757 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 }
2759
2760 if (c1 == Ctrl_T)
2761 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002762 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 p = (char_u *)line_ga.ga_data;
2764 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002765 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002766 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002768 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002770 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002771 p = (char_u *)line_ga.ga_data;
2772 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002773 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2774 *s = ' ';
2775 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002777redraw:
2778 /* redraw the line */
2779 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002781 p = (char_u *)line_ga.ga_data;
2782 p[line_ga.ga_len] = NUL;
2783 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002785 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002789 msg_putchar(' ');
2790 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002791 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002795 len = MB_PTR2LEN(p);
2796 msg_outtrans_len(p, len);
2797 vcol += ptr2cells(p);
2798 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002802 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 continue;
2804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806 if (c1 == Ctrl_D)
2807 {
2808 /* Delete one shiftwidth. */
2809 p = (char_u *)line_ga.ga_data;
2810 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002812 if (prev_char == '^')
2813 ex_keep_indent = TRUE;
2814 indent = 0;
2815 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817 else
2818 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002820 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002821 if (indent > 0)
2822 {
2823 --indent;
2824 indent -= indent % get_sw_value(curbuf);
2825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002827 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002829 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2831 --line_ga.ga_len;
2832 }
2833 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835
2836 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2837 {
2838 escaped = TRUE;
2839 continue;
2840 }
2841
2842 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2843 if (IS_SPECIAL(c1))
2844 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846
2847 if (IS_SPECIAL(c1))
2848 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002849#ifdef FEAT_MBYTE
2850 if (has_mbyte)
2851 len = (*mb_char2bytes)(c1,
2852 (char_u *)line_ga.ga_data + line_ga.ga_len);
2853 else
2854#endif
2855 {
2856 len = 1;
2857 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2858 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 if (c1 == '\n')
2860 msg_putchar('\n');
2861 else if (c1 == TAB)
2862 {
2863 /* Don't use chartabsize(), 'ts' can be different */
2864 do
2865 {
2866 msg_putchar(' ');
2867 } while (++vcol % 8);
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002872 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002875 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 escaped = FALSE;
2877
2878 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002879 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002881 /* We are done when a NL is entered, but not when it comes after an
2882 * odd number of backslashes, that results in a NUL. */
2883 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002885 int bcount = 0;
2886
2887 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2888 ++bcount;
2889
2890 if (bcount > 0)
2891 {
2892 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2893 * "\NL", etc. */
2894 line_ga.ga_len -= (bcount + 1) / 2;
2895 pend -= (bcount + 1) / 2;
2896 pend[-1] = '\n';
2897 }
2898
2899 if ((bcount & 1) == 0)
2900 {
2901 --line_ga.ga_len;
2902 --pend;
2903 *pend = NUL;
2904 break;
2905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
2907 }
2908
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909 --no_mapping;
2910 --allow_keys;
2911
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 /* make following messages go to the next line */
2913 msg_didout = FALSE;
2914 msg_col = 0;
2915 if (msg_row < Rows - 1)
2916 ++msg_row;
2917 emsg_on_display = FALSE; /* don't want ui_delay() */
2918
2919 if (got_int)
2920 ga_clear(&line_ga);
2921
2922 return (char_u *)line_ga.ga_data;
2923}
2924
Bram Moolenaare344bea2005-09-01 20:46:49 +00002925# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2926 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927/*
2928 * Return TRUE if ccline.overstrike is on.
2929 */
2930 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002931cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
2933 return ccline.overstrike;
2934}
2935
2936/*
2937 * Return TRUE if the cursor is at the end of the cmdline.
2938 */
2939 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002940cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
2942 return (ccline.cmdpos >= ccline.cmdlen);
2943}
2944#endif
2945
Bram Moolenaar9372a112005-12-06 19:59:18 +00002946#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947/*
2948 * Return the virtual column number at the current cursor position.
2949 * This is used by the IM code to obtain the start of the preedit string.
2950 */
2951 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002952cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
2954 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2955 return MAXCOL;
2956
2957# ifdef FEAT_MBYTE
2958 if (has_mbyte)
2959 {
2960 colnr_T col;
2961 int i = 0;
2962
2963 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002964 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 return col;
2967 }
2968 else
2969# endif
2970 return ccline.cmdpos;
2971}
2972#endif
2973
2974#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2975/*
2976 * If part of the command line is an IM preedit string, redraw it with
2977 * IM feedback attributes. The cursor position is restored after drawing.
2978 */
2979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002980redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 if ((State & CMDLINE)
2983 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002984 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 && !p_imdisable
2986 && im_is_preediting())
2987 {
2988 int cmdpos = 0;
2989 int cmdspos;
2990 int old_row;
2991 int old_col;
2992 colnr_T col;
2993
2994 old_row = msg_row;
2995 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002996 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998# ifdef FEAT_MBYTE
2999 if (has_mbyte)
3000 {
3001 for (col = 0; col < preedit_start_col
3002 && cmdpos < ccline.cmdlen; ++col)
3003 {
3004 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003005 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007 }
3008 else
3009# endif
3010 {
3011 cmdspos += preedit_start_col;
3012 cmdpos += preedit_start_col;
3013 }
3014
3015 msg_row = cmdline_row + (cmdspos / (int)Columns);
3016 msg_col = cmdspos % (int)Columns;
3017 if (msg_row >= Rows)
3018 msg_row = Rows - 1;
3019
3020 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3021 {
3022 int char_len;
3023 int char_attr;
3024
3025 char_attr = im_get_feedback_attr(col);
3026 if (char_attr < 0)
3027 break; /* end of preedit string */
3028
3029# ifdef FEAT_MBYTE
3030 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003031 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else
3033# endif
3034 char_len = 1;
3035
3036 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3037 cmdpos += char_len;
3038 }
3039
3040 msg_row = old_row;
3041 msg_col = old_col;
3042 }
3043}
3044#endif /* FEAT_XIM && FEAT_GUI_GTK */
3045
3046/*
3047 * Allocate a new command line buffer.
3048 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3049 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3050 */
3051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003052alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 /*
3055 * give some extra space to avoid having to allocate all the time
3056 */
3057 if (len < 80)
3058 len = 100;
3059 else
3060 len += 20;
3061
3062 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3063 ccline.cmdbufflen = len;
3064}
3065
3066/*
3067 * Re-allocate the command line to length len + something extra.
3068 * return FAIL for failure, OK otherwise
3069 */
3070 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003071realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 char_u *p;
3074
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003075 if (len < ccline.cmdbufflen)
3076 return OK; /* no need to resize */
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 p = ccline.cmdbuff;
3079 alloc_cmdbuff(len); /* will get some more */
3080 if (ccline.cmdbuff == NULL) /* out of memory */
3081 {
3082 ccline.cmdbuff = p; /* keep the old one */
3083 return FAIL;
3084 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003085 /* There isn't always a NUL after the command, but it may need to be
3086 * there, thus copy up to the NUL and add a NUL. */
3087 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3088 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003090
3091 if (ccline.xpc != NULL
3092 && ccline.xpc->xp_pattern != NULL
3093 && ccline.xpc->xp_context != EXPAND_NOTHING
3094 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3095 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003096 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003097
3098 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3099 * to point into the newly allocated memory. */
3100 if (i >= 0 && i <= ccline.cmdlen)
3101 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3102 }
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 return OK;
3105}
3106
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003107#if defined(FEAT_ARABIC) || defined(PROTO)
3108static char_u *arshape_buf = NULL;
3109
3110# if defined(EXITFREE) || defined(PROTO)
3111 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003112free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003113{
3114 vim_free(arshape_buf);
3115}
3116# endif
3117#endif
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119/*
3120 * Draw part of the cmdline at the current cursor position. But draw stars
3121 * when cmdline_star is TRUE.
3122 */
3123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003124draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3127 int i;
3128
3129 if (cmdline_star > 0)
3130 for (i = 0; i < len; ++i)
3131 {
3132 msg_putchar('*');
3133# ifdef FEAT_MBYTE
3134 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003135 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136# endif
3137 }
3138 else
3139#endif
3140#ifdef FEAT_ARABIC
3141 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 static int buflen = 0;
3144 char_u *p;
3145 int j;
3146 int newlen = 0;
3147 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003148 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 int prev_c = 0;
3150 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003151 int u8c;
3152 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 /*
3156 * Do arabic shaping into a temporary buffer. This is very
3157 * inefficient!
3158 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003159 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
3161 /* Re-allocate the buffer. We keep it around to avoid a lot of
3162 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003163 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003164 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003165 arshape_buf = alloc(buflen);
3166 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 return; /* out of memory */
3168 }
3169
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003170 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3171 {
3172 /* Prepend a space to draw the leading composing char on. */
3173 arshape_buf[0] = ' ';
3174 newlen = 1;
3175 }
3176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 for (j = start; j < start + len; j += mb_l)
3178 {
3179 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003180 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003181 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (ARABIC_CHAR(u8c))
3183 {
3184 /* Do Arabic shaping. */
3185 if (cmdmsg_rl)
3186 {
3187 /* displaying from right to left */
3188 pc = prev_c;
3189 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003190 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (j + mb_l >= start + len)
3192 nc = NUL;
3193 else
3194 nc = utf_ptr2char(p + mb_l);
3195 }
3196 else
3197 {
3198 /* displaying from left to right */
3199 if (j + mb_l >= start + len)
3200 pc = NUL;
3201 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003202 {
3203 int pcc[MAX_MCO];
3204
3205 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003207 pc1 = pcc[0];
3208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 nc = prev_c;
3210 }
3211 prev_c = u8c;
3212
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003213 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003215 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003216 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003218 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3219 if (u8cc[1] != 0)
3220 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223 }
3224 else
3225 {
3226 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003227 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 newlen += mb_l;
3229 }
3230 }
3231
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003232 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 else
3235#endif
3236 msg_outtrans_len(ccline.cmdbuff + start, len);
3237}
3238
3239/*
3240 * Put a character on the command line. Shifts the following text to the
3241 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3242 * "c" must be printable (fit in one display cell)!
3243 */
3244 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003245putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 if (cmd_silent)
3248 return;
3249 msg_no_more = TRUE;
3250 msg_putchar(c);
3251 if (shift)
3252 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3253 msg_no_more = FALSE;
3254 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003255 extra_char = c;
3256 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
3259/*
3260 * Undo a putcmdline(c, FALSE).
3261 */
3262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003263unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 if (cmd_silent)
3266 return;
3267 msg_no_more = TRUE;
3268 if (ccline.cmdlen == ccline.cmdpos)
3269 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003270#ifdef FEAT_MBYTE
3271 else if (has_mbyte)
3272 draw_cmdline(ccline.cmdpos,
3273 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 else
3276 draw_cmdline(ccline.cmdpos, 1);
3277 msg_no_more = FALSE;
3278 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003279 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280}
3281
3282/*
3283 * Put the given string, of the given length, onto the command line.
3284 * If len is -1, then STRLEN() is used to calculate the length.
3285 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3286 * part will be redrawn, otherwise it will not. If this function is called
3287 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3288 * called afterwards.
3289 */
3290 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003291put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 int retval;
3294 int i;
3295 int m;
3296 int c;
3297
3298 if (len < 0)
3299 len = (int)STRLEN(str);
3300
3301 /* Check if ccline.cmdbuff needs to be longer */
3302 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003303 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 else
3305 retval = OK;
3306 if (retval == OK)
3307 {
3308 if (!ccline.overstrike)
3309 {
3310 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3311 ccline.cmdbuff + ccline.cmdpos,
3312 (size_t)(ccline.cmdlen - ccline.cmdpos));
3313 ccline.cmdlen += len;
3314 }
3315 else
3316 {
3317#ifdef FEAT_MBYTE
3318 if (has_mbyte)
3319 {
3320 /* Count nr of characters in the new string. */
3321 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003322 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 ++m;
3324 /* Count nr of bytes in cmdline that are overwritten by these
3325 * characters. */
3326 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003327 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 --m;
3329 if (i < ccline.cmdlen)
3330 {
3331 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3332 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3333 ccline.cmdlen += ccline.cmdpos + len - i;
3334 }
3335 else
3336 ccline.cmdlen = ccline.cmdpos + len;
3337 }
3338 else
3339#endif
3340 if (ccline.cmdpos + len > ccline.cmdlen)
3341 ccline.cmdlen = ccline.cmdpos + len;
3342 }
3343 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3344 ccline.cmdbuff[ccline.cmdlen] = NUL;
3345
3346#ifdef FEAT_MBYTE
3347 if (enc_utf8)
3348 {
3349 /* When the inserted text starts with a composing character,
3350 * backup to the character before it. There could be two of them.
3351 */
3352 i = 0;
3353 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3354 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3355 {
3356 i = (*mb_head_off)(ccline.cmdbuff,
3357 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3358 ccline.cmdpos -= i;
3359 len += i;
3360 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3361 }
3362# ifdef FEAT_ARABIC
3363 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3364 {
3365 /* Check the previous character for Arabic combining pair. */
3366 i = (*mb_head_off)(ccline.cmdbuff,
3367 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3368 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3369 + ccline.cmdpos - i), c))
3370 {
3371 ccline.cmdpos -= i;
3372 len += i;
3373 }
3374 else
3375 i = 0;
3376 }
3377# endif
3378 if (i != 0)
3379 {
3380 /* Also backup the cursor position. */
3381 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3382 ccline.cmdspos -= i;
3383 msg_col -= i;
3384 if (msg_col < 0)
3385 {
3386 msg_col += Columns;
3387 --msg_row;
3388 }
3389 }
3390 }
3391#endif
3392
3393 if (redraw && !cmd_silent)
3394 {
3395 msg_no_more = TRUE;
3396 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003397 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3399 /* Avoid clearing the rest of the line too often. */
3400 if (cmdline_row != i || ccline.overstrike)
3401 msg_clr_eos();
3402 msg_no_more = FALSE;
3403 }
3404#ifdef FEAT_FKMAP
3405 /*
3406 * If we are in Farsi command mode, the character input must be in
3407 * Insert mode. So do not advance the cmdpos.
3408 */
3409 if (!cmd_fkmap)
3410#endif
3411 {
3412 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003415 if (m < 0) /* overflow, Columns or Rows at weird value */
3416 m = MAXCOL;
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else
3419 m = MAXCOL;
3420 for (i = 0; i < len; ++i)
3421 {
3422 c = cmdline_charsize(ccline.cmdpos);
3423#ifdef FEAT_MBYTE
3424 /* count ">" for a double-wide char that doesn't fit. */
3425 if (has_mbyte)
3426 correct_cmdspos(ccline.cmdpos, c);
3427#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003428 /* Stop cursor at the end of the screen, but do increment the
3429 * insert position, so that entering a very long command
3430 * works, even though you can't see it. */
3431 if (ccline.cmdspos + c < m)
3432 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433#ifdef FEAT_MBYTE
3434 if (has_mbyte)
3435 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003436 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 if (c > len - i - 1)
3438 c = len - i - 1;
3439 ccline.cmdpos += c;
3440 i += c;
3441 }
3442#endif
3443 ++ccline.cmdpos;
3444 }
3445 }
3446 }
3447 if (redraw)
3448 msg_check();
3449 return retval;
3450}
3451
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003452static struct cmdline_info prev_ccline;
3453static int prev_ccline_used = FALSE;
3454
3455/*
3456 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3457 * and overwrite it. But get_cmdline_str() may need it, thus make it
3458 * available globally in prev_ccline.
3459 */
3460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003461save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003462{
3463 if (!prev_ccline_used)
3464 {
3465 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3466 prev_ccline_used = TRUE;
3467 }
3468 *ccp = prev_ccline;
3469 prev_ccline = ccline;
3470 ccline.cmdbuff = NULL;
3471 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003472 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003473}
3474
3475/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003476 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003477 */
3478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003479restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003480{
3481 ccline = prev_ccline;
3482 prev_ccline = *ccp;
3483}
3484
Bram Moolenaar5a305422006-04-28 22:38:25 +00003485#if defined(FEAT_EVAL) || defined(PROTO)
3486/*
3487 * Save the command line into allocated memory. Returns a pointer to be
3488 * passed to restore_cmdline_alloc() later.
3489 * Returns NULL when failed.
3490 */
3491 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003492save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003493{
3494 struct cmdline_info *p;
3495
3496 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3497 if (p != NULL)
3498 save_cmdline(p);
3499 return (char_u *)p;
3500}
3501
3502/*
3503 * Restore the command line from the return value of save_cmdline_alloc().
3504 */
3505 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003506restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003507{
3508 if (p != NULL)
3509 {
3510 restore_cmdline((struct cmdline_info *)p);
3511 vim_free(p);
3512 }
3513}
3514#endif
3515
Bram Moolenaar8299df92004-07-10 09:47:34 +00003516/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003517 * Paste a yank register into the command line.
3518 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003519 * insert_reg() can't be used here, because special characters from the
3520 * register contents will be interpreted as commands.
3521 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003522 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003523 */
3524 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525cmdline_paste(
3526 int regname,
3527 int literally, /* Insert text literally instead of "as typed" */
3528 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003529{
3530 long i;
3531 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003532 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003533 int allocated;
3534 struct cmdline_info save_ccline;
3535
3536 /* check for valid regname; also accept special characters for CTRL-R in
3537 * the command line */
3538 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003539 && regname != Ctrl_A && regname != Ctrl_L
3540 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541 return FAIL;
3542
3543 /* A register containing CTRL-R can cause an endless loop. Allow using
3544 * CTRL-C to break the loop. */
3545 line_breakcheck();
3546 if (got_int)
3547 return FAIL;
3548
3549#ifdef FEAT_CLIPBOARD
3550 regname = may_get_selection(regname);
3551#endif
3552
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003553 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003554 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003555 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003556 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003557 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003558 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003559 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003560
3561 if (i)
3562 {
3563 /* Got the value of a special register in "arg". */
3564 if (arg == NULL)
3565 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003566
3567 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3568 * part of the word. */
3569 p = arg;
3570 if (p_is && regname == Ctrl_W)
3571 {
3572 char_u *w;
3573 int len;
3574
3575 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003576 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003577 {
3578#ifdef FEAT_MBYTE
3579 if (has_mbyte)
3580 {
3581 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3582 if (!vim_iswordc(mb_ptr2char(w - len)))
3583 break;
3584 w -= len;
3585 }
3586 else
3587#endif
3588 {
3589 if (!vim_iswordc(w[-1]))
3590 break;
3591 --w;
3592 }
3593 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003594 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003595 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3596 p += len;
3597 }
3598
3599 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003600 if (allocated)
3601 vim_free(arg);
3602 return OK;
3603 }
3604
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003605 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003606}
3607
3608/*
3609 * Put a string on the command line.
3610 * When "literally" is TRUE, insert literally.
3611 * When "literally" is FALSE, insert as typed, but don't leave the command
3612 * line.
3613 */
3614 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003615cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003616{
3617 int c, cv;
3618
3619 if (literally)
3620 put_on_cmdline(s, -1, TRUE);
3621 else
3622 while (*s != NUL)
3623 {
3624 cv = *s;
3625 if (cv == Ctrl_V && s[1])
3626 ++s;
3627#ifdef FEAT_MBYTE
3628 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003629 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003630 else
3631#endif
3632 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003633 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3634 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003635#ifdef UNIX
3636 || c == intr_char
3637#endif
3638 || (c == Ctrl_BSL && *s == Ctrl_N))
3639 stuffcharReadbuff(Ctrl_V);
3640 stuffcharReadbuff(c);
3641 }
3642}
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644#ifdef FEAT_WILDMENU
3645/*
3646 * Delete characters on the command line, from "from" to the current
3647 * position.
3648 */
3649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003650cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
3652 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3653 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3654 ccline.cmdlen -= ccline.cmdpos - from;
3655 ccline.cmdpos = from;
3656}
3657#endif
3658
3659/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003660 * This function is called when the screen size changes and with incremental
3661 * search and in other situations where the command line may have been
3662 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 */
3664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003665redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003667 redrawcmdline_ex(TRUE);
3668}
3669
3670 void
3671redrawcmdline_ex(int do_compute_cmdrow)
3672{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (cmd_silent)
3674 return;
3675 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003676 if (do_compute_cmdrow)
3677 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 redrawcmd();
3679 cursorcmd();
3680}
3681
3682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003683redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
3685 int i;
3686
3687 if (cmd_silent)
3688 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003689 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 msg_putchar(ccline.cmdfirstc);
3691 if (ccline.cmdprompt != NULL)
3692 {
3693 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3694 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3695 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003696 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 --ccline.cmdindent;
3698 }
3699 else
3700 for (i = ccline.cmdindent; i > 0; --i)
3701 msg_putchar(' ');
3702}
3703
3704/*
3705 * Redraw what is currently on the command line.
3706 */
3707 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003708redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 if (cmd_silent)
3711 return;
3712
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003713 /* when 'incsearch' is set there may be no command line while redrawing */
3714 if (ccline.cmdbuff == NULL)
3715 {
3716 windgoto(cmdline_row, 0);
3717 msg_clr_eos();
3718 return;
3719 }
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 msg_start();
3722 redrawcmdprompt();
3723
3724 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3725 msg_no_more = TRUE;
3726 draw_cmdline(0, ccline.cmdlen);
3727 msg_clr_eos();
3728 msg_no_more = FALSE;
3729
3730 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003731 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003732 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 /*
3735 * An emsg() before may have set msg_scroll. This is used in normal mode,
3736 * in cmdline mode we can reset them now.
3737 */
3738 msg_scroll = FALSE; /* next message overwrites cmdline */
3739
3740 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3741 * in cmdline mode */
3742 skip_redraw = FALSE;
3743}
3744
3745 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003746compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003748 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 cmdline_row = Rows - 1;
3750 else
3751 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003752 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753}
3754
3755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003756cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 if (cmd_silent)
3759 return;
3760
3761#ifdef FEAT_RIGHTLEFT
3762 if (cmdmsg_rl)
3763 {
3764 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3765 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3766 if (msg_row <= 0)
3767 msg_row = Rows - 1;
3768 }
3769 else
3770#endif
3771 {
3772 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3773 msg_col = ccline.cmdspos % (int)Columns;
3774 if (msg_row >= Rows)
3775 msg_row = Rows - 1;
3776 }
3777
3778 windgoto(msg_row, msg_col);
3779#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003780 if (p_imst == IM_ON_THE_SPOT)
3781 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#endif
3783#ifdef MCH_CURSOR_SHAPE
3784 mch_update_cursor();
3785#endif
3786}
3787
3788 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003789gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
3791 msg_start();
3792#ifdef FEAT_RIGHTLEFT
3793 if (cmdmsg_rl)
3794 msg_col = Columns - 1;
3795 else
3796#endif
3797 msg_col = 0; /* always start in column 0 */
3798 if (clr) /* clear the bottom line(s) */
3799 msg_clr_eos(); /* will reset clear_cmdline */
3800 windgoto(cmdline_row, 0);
3801}
3802
3803/*
3804 * Check the word in front of the cursor for an abbreviation.
3805 * Called when the non-id character "c" has been entered.
3806 * When an abbreviation is recognized it is removed from the text with
3807 * backspaces and the replacement string is inserted, followed by "c".
3808 */
3809 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003810ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003812 int spos = 0;
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3815 return FALSE;
3816
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003817 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3818 * Actually accepts any mark. */
3819 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3820 spos++;
3821 if (ccline.cmdlen - spos > 5
3822 && ccline.cmdbuff[spos] == '\''
3823 && ccline.cmdbuff[spos + 2] == ','
3824 && ccline.cmdbuff[spos + 3] == '\'')
3825 spos += 5;
3826 else
3827 /* check abbreviation from the beginning of the commandline */
3828 spos = 0;
3829
3830 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831}
3832
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003833#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3834 static int
3835#ifdef __BORLANDC__
3836_RTLENTRYF
3837#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003838sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003839{
3840 char_u *p1 = *(char_u **)s1;
3841 char_u *p2 = *(char_u **)s2;
3842
3843 if (*p1 != '<' && *p2 == '<') return -1;
3844 if (*p1 == '<' && *p2 != '<') return 1;
3845 return STRCMP(p1, p2);
3846}
3847#endif
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849/*
3850 * Return FAIL if this is not an appropriate context in which to do
3851 * completion of anything, return OK if it is (even if there are no matches).
3852 * For the caller, this means that the character is just passed through like a
3853 * normal character (instead of being expanded). This allows :s/^I^D etc.
3854 */
3855 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003856nextwild(
3857 expand_T *xp,
3858 int type,
3859 int options, /* extra options for ExpandOne() */
3860 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 int i, j;
3863 char_u *p1;
3864 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 int difflen;
3866 int v;
3867
3868 if (xp->xp_numfiles == -1)
3869 {
3870 set_expand_context(xp);
3871 cmd_showtail = expand_showtail(xp);
3872 }
3873
3874 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3875 {
3876 beep_flush();
3877 return OK; /* Something illegal on command line */
3878 }
3879 if (xp->xp_context == EXPAND_NOTHING)
3880 {
3881 /* Caller can use the character as a normal char instead */
3882 return FAIL;
3883 }
3884
3885 MSG_PUTS("..."); /* show that we are busy */
3886 out_flush();
3887
3888 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003889 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 if (type == WILD_NEXT || type == WILD_PREV)
3892 {
3893 /*
3894 * Get next/previous match for a previous expanded pattern.
3895 */
3896 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3897 }
3898 else
3899 {
3900 /*
3901 * Translate string into pattern and expand it.
3902 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003903 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3904 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 p2 = NULL;
3906 else
3907 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003908 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003909 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3910 if (escape)
3911 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003912
3913 if (p_wic)
3914 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003915 p2 = ExpandOne(xp, p1,
3916 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003917 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003919 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (p2 != NULL && type == WILD_LONGEST)
3921 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003922 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (ccline.cmdbuff[i + j] == '*'
3924 || ccline.cmdbuff[i + j] == '?')
3925 break;
3926 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003927 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929 }
3930 }
3931
3932 if (p2 != NULL && !got_int)
3933 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003934 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003935 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003937 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 xp->xp_pattern = ccline.cmdbuff + i;
3939 }
3940 else
3941 v = OK;
3942 if (v == OK)
3943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3945 &ccline.cmdbuff[ccline.cmdpos],
3946 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3947 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 ccline.cmdlen += difflen;
3949 ccline.cmdpos += difflen;
3950 }
3951 }
3952 vim_free(p2);
3953
3954 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003955 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
3957 /* When expanding a ":map" command and no matches are found, assume that
3958 * the key is supposed to be inserted literally */
3959 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3960 return FAIL;
3961
3962 if (xp->xp_numfiles <= 0 && p2 == NULL)
3963 beep_flush();
3964 else if (xp->xp_numfiles == 1)
3965 /* free expanded pattern */
3966 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3967
3968 return OK;
3969}
3970
3971/*
3972 * Do wildcard expansion on the string 'str'.
3973 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003974 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * Return NULL for failure.
3976 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003977 * "orig" is the originally expanded string, copied to allocated memory. It
3978 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3979 * WILD_PREV "orig" should be NULL.
3980 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003981 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3982 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 *
3984 * mode = WILD_FREE: just free previously expanded matches
3985 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3986 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3987 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3988 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3989 * mode = WILD_ALL: return all matches concatenated
3990 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003991 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 *
3993 * options = WILD_LIST_NOTFOUND: list entries without a match
3994 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3995 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3996 * options = WILD_NO_BEEP: Don't beep for multiple matches
3997 * options = WILD_ADD_SLASH: add a slash after directory names
3998 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3999 * options = WILD_SILENT: don't print warning messages
4000 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004001 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 *
4003 * The variables xp->xp_context and xp->xp_backslash must have been set!
4004 */
4005 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004006ExpandOne(
4007 expand_T *xp,
4008 char_u *str,
4009 char_u *orig, /* allocated copy of original of expanded string */
4010 int options,
4011 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
4013 char_u *ss = NULL;
4014 static int findex;
4015 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004016 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 int i;
4018 long_u len;
4019 int non_suf_match; /* number without matching suffix */
4020
4021 /*
4022 * first handle the case of using an old match
4023 */
4024 if (mode == WILD_NEXT || mode == WILD_PREV)
4025 {
4026 if (xp->xp_numfiles > 0)
4027 {
4028 if (mode == WILD_PREV)
4029 {
4030 if (findex == -1)
4031 findex = xp->xp_numfiles;
4032 --findex;
4033 }
4034 else /* mode == WILD_NEXT */
4035 ++findex;
4036
4037 /*
4038 * When wrapping around, return the original string, set findex to
4039 * -1.
4040 */
4041 if (findex < 0)
4042 {
4043 if (orig_save == NULL)
4044 findex = xp->xp_numfiles - 1;
4045 else
4046 findex = -1;
4047 }
4048 if (findex >= xp->xp_numfiles)
4049 {
4050 if (orig_save == NULL)
4051 findex = 0;
4052 else
4053 findex = -1;
4054 }
4055#ifdef FEAT_WILDMENU
4056 if (p_wmnu)
4057 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4058 findex, cmd_showtail);
4059#endif
4060 if (findex == -1)
4061 return vim_strsave(orig_save);
4062 return vim_strsave(xp->xp_files[findex]);
4063 }
4064 else
4065 return NULL;
4066 }
4067
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004068 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4070 {
4071 FreeWild(xp->xp_numfiles, xp->xp_files);
4072 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004073 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 findex = 0;
4076
4077 if (mode == WILD_FREE) /* only release file name */
4078 return NULL;
4079
4080 if (xp->xp_numfiles == -1)
4081 {
4082 vim_free(orig_save);
4083 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004084 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 /*
4087 * Do the expansion.
4088 */
4089 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4090 options) == FAIL)
4091 {
4092#ifdef FNAME_ILLEGAL
4093 /* Illegal file name has been silently skipped. But when there
4094 * are wildcards, the real problem is that there was no match,
4095 * causing the pattern to be added, which has illegal characters.
4096 */
4097 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4098 EMSG2(_(e_nomatch2), str);
4099#endif
4100 }
4101 else if (xp->xp_numfiles == 0)
4102 {
4103 if (!(options & WILD_SILENT))
4104 EMSG2(_(e_nomatch2), str);
4105 }
4106 else
4107 {
4108 /* Escape the matches for use on the command line. */
4109 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4110
4111 /*
4112 * Check for matching suffixes in file names.
4113 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004114 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4115 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
4117 if (xp->xp_numfiles)
4118 non_suf_match = xp->xp_numfiles;
4119 else
4120 non_suf_match = 1;
4121 if ((xp->xp_context == EXPAND_FILES
4122 || xp->xp_context == EXPAND_DIRECTORIES)
4123 && xp->xp_numfiles > 1)
4124 {
4125 /*
4126 * More than one match; check suffix.
4127 * The files will have been sorted on matching suffix in
4128 * expand_wildcards, only need to check the first two.
4129 */
4130 non_suf_match = 0;
4131 for (i = 0; i < 2; ++i)
4132 if (match_suffix(xp->xp_files[i]))
4133 ++non_suf_match;
4134 }
4135 if (non_suf_match != 1)
4136 {
4137 /* Can we ever get here unless it's while expanding
4138 * interactively? If not, we can get rid of this all
4139 * together. Don't really want to wait for this message
4140 * (and possibly have to hit return to continue!).
4141 */
4142 if (!(options & WILD_SILENT))
4143 EMSG(_(e_toomany));
4144 else if (!(options & WILD_NO_BEEP))
4145 beep_flush();
4146 }
4147 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4148 ss = vim_strsave(xp->xp_files[0]);
4149 }
4150 }
4151 }
4152
4153 /* Find longest common part */
4154 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4155 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004156 int mb_len = 1;
4157 int c0, ci;
4158
4159 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004161#ifdef FEAT_MBYTE
4162 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004164 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4165 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4166 }
4167 else
4168#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004169 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004170 for (i = 1; i < xp->xp_numfiles; ++i)
4171 {
4172#ifdef FEAT_MBYTE
4173 if (has_mbyte)
4174 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4175 else
4176#endif
4177 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004178 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004180 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004181 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004183 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 break;
4185 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004186 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 break;
4188 }
4189 if (i < xp->xp_numfiles)
4190 {
4191 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004192 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 break;
4194 }
4195 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 ss = alloc((unsigned)len + 1);
4198 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004199 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 findex = -1; /* next p_wc gets first one */
4201 }
4202
4203 /* Concatenate all matching names */
4204 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4205 {
4206 len = 0;
4207 for (i = 0; i < xp->xp_numfiles; ++i)
4208 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4209 ss = lalloc(len, TRUE);
4210 if (ss != NULL)
4211 {
4212 *ss = NUL;
4213 for (i = 0; i < xp->xp_numfiles; ++i)
4214 {
4215 STRCAT(ss, xp->xp_files[i]);
4216 if (i != xp->xp_numfiles - 1)
4217 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4218 }
4219 }
4220 }
4221
4222 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4223 ExpandCleanup(xp);
4224
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004225 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004226 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004227 vim_free(orig);
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return ss;
4230}
4231
4232/*
4233 * Prepare an expand structure for use.
4234 */
4235 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004236ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004238 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004239 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004241#ifndef BACKSLASH_IN_FILENAME
4242 xp->xp_shell = FALSE;
4243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 xp->xp_numfiles = -1;
4245 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004246#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4247 xp->xp_arg = NULL;
4248#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004249 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250}
4251
4252/*
4253 * Cleanup an expand structure after use.
4254 */
4255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004256ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
4258 if (xp->xp_numfiles >= 0)
4259 {
4260 FreeWild(xp->xp_numfiles, xp->xp_files);
4261 xp->xp_numfiles = -1;
4262 }
4263}
4264
4265 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004266ExpandEscape(
4267 expand_T *xp,
4268 char_u *str,
4269 int numfiles,
4270 char_u **files,
4271 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273 int i;
4274 char_u *p;
4275
4276 /*
4277 * May change home directory back to "~"
4278 */
4279 if (options & WILD_HOME_REPLACE)
4280 tilde_replace(str, numfiles, files);
4281
4282 if (options & WILD_ESCAPE)
4283 {
4284 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004285 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004286 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 || xp->xp_context == EXPAND_BUFFERS
4288 || xp->xp_context == EXPAND_DIRECTORIES)
4289 {
4290 /*
4291 * Insert a backslash into a file name before a space, \, %, #
4292 * and wildmatch characters, except '~'.
4293 */
4294 for (i = 0; i < numfiles; ++i)
4295 {
4296 /* for ":set path=" we need to escape spaces twice */
4297 if (xp->xp_backslash == XP_BS_THREE)
4298 {
4299 p = vim_strsave_escaped(files[i], (char_u *)" ");
4300 if (p != NULL)
4301 {
4302 vim_free(files[i]);
4303 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004304#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 p = vim_strsave_escaped(files[i], (char_u *)" ");
4306 if (p != NULL)
4307 {
4308 vim_free(files[i]);
4309 files[i] = p;
4310 }
4311#endif
4312 }
4313 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004314#ifdef BACKSLASH_IN_FILENAME
4315 p = vim_strsave_fnameescape(files[i], FALSE);
4316#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004317 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (p != NULL)
4320 {
4321 vim_free(files[i]);
4322 files[i] = p;
4323 }
4324
4325 /* If 'str' starts with "\~", replace "~" at start of
4326 * files[i] with "\~". */
4327 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004328 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004331
4332 /* If the first file starts with a '+' escape it. Otherwise it
4333 * could be seen as "+cmd". */
4334 if (*files[0] == '+')
4335 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337 else if (xp->xp_context == EXPAND_TAGS)
4338 {
4339 /*
4340 * Insert a backslash before characters in a tag name that
4341 * would terminate the ":tag" command.
4342 */
4343 for (i = 0; i < numfiles; ++i)
4344 {
4345 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4346 if (p != NULL)
4347 {
4348 vim_free(files[i]);
4349 files[i] = p;
4350 }
4351 }
4352 }
4353 }
4354}
4355
4356/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004357 * Escape special characters in "fname" for when used as a file name argument
4358 * after a Vim command, or, when "shell" is non-zero, a shell command.
4359 * Returns the result in allocated memory.
4360 */
4361 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004362vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004363{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004364 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004365#ifdef BACKSLASH_IN_FILENAME
4366 char_u buf[20];
4367 int j = 0;
4368
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004369 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004370 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004371 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004372 buf[j++] = *p;
4373 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004374 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004375#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004376 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4377 if (shell && csh_like_shell() && p != NULL)
4378 {
4379 char_u *s;
4380
4381 /* For csh and similar shells need to put two backslashes before '!'.
4382 * One is taken by Vim, one by the shell. */
4383 s = vim_strsave_escaped(p, (char_u *)"!");
4384 vim_free(p);
4385 p = s;
4386 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004387#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004388
4389 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4390 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004391 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004392 escape_fname(&p);
4393
4394 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004395}
4396
4397/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004398 * Put a backslash before the file name in "pp", which is in allocated memory.
4399 */
4400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004401escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004402{
4403 char_u *p;
4404
4405 p = alloc((unsigned)(STRLEN(*pp) + 2));
4406 if (p != NULL)
4407 {
4408 p[0] = '\\';
4409 STRCPY(p + 1, *pp);
4410 vim_free(*pp);
4411 *pp = p;
4412 }
4413}
4414
4415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 * For each file name in files[num_files]:
4417 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4418 */
4419 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004420tilde_replace(
4421 char_u *orig_pat,
4422 int num_files,
4423 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 int i;
4426 char_u *p;
4427
4428 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4429 {
4430 for (i = 0; i < num_files; ++i)
4431 {
4432 p = home_replace_save(NULL, files[i]);
4433 if (p != NULL)
4434 {
4435 vim_free(files[i]);
4436 files[i] = p;
4437 }
4438 }
4439 }
4440}
4441
4442/*
4443 * Show all matches for completion on the command line.
4444 * Returns EXPAND_NOTHING when the character that triggered expansion should
4445 * be inserted like a normal character.
4446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004448showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4451 int num_files;
4452 char_u **files_found;
4453 int i, j, k;
4454 int maxlen;
4455 int lines;
4456 int columns;
4457 char_u *p;
4458 int lastlen;
4459 int attr;
4460 int showtail;
4461
4462 if (xp->xp_numfiles == -1)
4463 {
4464 set_expand_context(xp);
4465 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4466 &num_files, &files_found);
4467 showtail = expand_showtail(xp);
4468 if (i != EXPAND_OK)
4469 return i;
4470
4471 }
4472 else
4473 {
4474 num_files = xp->xp_numfiles;
4475 files_found = xp->xp_files;
4476 showtail = cmd_showtail;
4477 }
4478
4479#ifdef FEAT_WILDMENU
4480 if (!wildmenu)
4481 {
4482#endif
4483 msg_didany = FALSE; /* lines_left will be set */
4484 msg_start(); /* prepare for paging */
4485 msg_putchar('\n');
4486 out_flush();
4487 cmdline_row = msg_row;
4488 msg_didany = FALSE; /* lines_left will be set again */
4489 msg_start(); /* prepare for paging */
4490#ifdef FEAT_WILDMENU
4491 }
4492#endif
4493
4494 if (got_int)
4495 got_int = FALSE; /* only int. the completion, not the cmd line */
4496#ifdef FEAT_WILDMENU
4497 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004498 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499#endif
4500 else
4501 {
4502 /* find the length of the longest file name */
4503 maxlen = 0;
4504 for (i = 0; i < num_files; ++i)
4505 {
4506 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004507 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 || xp->xp_context == EXPAND_BUFFERS))
4509 {
4510 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4511 j = vim_strsize(NameBuff);
4512 }
4513 else
4514 j = vim_strsize(L_SHOWFILE(i));
4515 if (j > maxlen)
4516 maxlen = j;
4517 }
4518
4519 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4520 lines = num_files;
4521 else
4522 {
4523 /* compute the number of columns and lines for the listing */
4524 maxlen += 2; /* two spaces between file names */
4525 columns = ((int)Columns + 2) / maxlen;
4526 if (columns < 1)
4527 columns = 1;
4528 lines = (num_files + columns - 1) / columns;
4529 }
4530
Bram Moolenaar8820b482017-03-16 17:23:31 +01004531 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4534 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004535 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 msg_clr_eos();
4537 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004538 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540
4541 /* list the files line by line */
4542 for (i = 0; i < lines; ++i)
4543 {
4544 lastlen = 999;
4545 for (k = i; k < num_files; k += lines)
4546 {
4547 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4548 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004549 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 p = files_found[k] + STRLEN(files_found[k]) + 1;
4551 msg_advance(maxlen + 1);
4552 msg_puts(p);
4553 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004554 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 break;
4556 }
4557 for (j = maxlen - lastlen; --j >= 0; )
4558 msg_putchar(' ');
4559 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004560 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 || xp->xp_context == EXPAND_BUFFERS)
4562 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004563 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004564 if (xp->xp_numfiles != -1)
4565 {
4566 char_u *halved_slash;
4567 char_u *exp_path;
4568
4569 /* Expansion was done before and special characters
4570 * were escaped, need to halve backslashes. Also
4571 * $HOME has been replaced with ~/. */
4572 exp_path = expand_env_save_opt(files_found[k], TRUE);
4573 halved_slash = backslash_halve_save(
4574 exp_path != NULL ? exp_path : files_found[k]);
4575 j = mch_isdir(halved_slash != NULL ? halved_slash
4576 : files_found[k]);
4577 vim_free(exp_path);
4578 vim_free(halved_slash);
4579 }
4580 else
4581 /* Expansion was done here, file names are literal. */
4582 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (showtail)
4584 p = L_SHOWFILE(k);
4585 else
4586 {
4587 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4588 TRUE);
4589 p = NameBuff;
4590 }
4591 }
4592 else
4593 {
4594 j = FALSE;
4595 p = L_SHOWFILE(k);
4596 }
4597 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4598 }
4599 if (msg_col > 0) /* when not wrapped around */
4600 {
4601 msg_clr_eos();
4602 msg_putchar('\n');
4603 }
4604 out_flush(); /* show one line at a time */
4605 if (got_int)
4606 {
4607 got_int = FALSE;
4608 break;
4609 }
4610 }
4611
4612 /*
4613 * we redraw the command below the lines that we have just listed
4614 * This is a bit tricky, but it saves a lot of screen updating.
4615 */
4616 cmdline_row = msg_row; /* will put it back later */
4617 }
4618
4619 if (xp->xp_numfiles == -1)
4620 FreeWild(num_files, files_found);
4621
4622 return EXPAND_OK;
4623}
4624
4625/*
4626 * Private gettail for showmatches() (and win_redr_status_matches()):
4627 * Find tail of file name path, but ignore trailing "/".
4628 */
4629 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 char_u *p;
4633 char_u *t = s;
4634 int had_sep = FALSE;
4635
4636 for (p = s; *p != NUL; )
4637 {
4638 if (vim_ispathsep(*p)
4639#ifdef BACKSLASH_IN_FILENAME
4640 && !rem_backslash(p)
4641#endif
4642 )
4643 had_sep = TRUE;
4644 else if (had_sep)
4645 {
4646 t = p;
4647 had_sep = FALSE;
4648 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004649 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651 return t;
4652}
4653
4654/*
4655 * Return TRUE if we only need to show the tail of completion matches.
4656 * When not completing file names or there is a wildcard in the path FALSE is
4657 * returned.
4658 */
4659 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004660expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
4662 char_u *s;
4663 char_u *end;
4664
4665 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004666 if (xp->xp_context != EXPAND_FILES
4667 && xp->xp_context != EXPAND_SHELLCMD
4668 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return FALSE;
4670
4671 end = gettail(xp->xp_pattern);
4672 if (end == xp->xp_pattern) /* there is no path separator */
4673 return FALSE;
4674
4675 for (s = xp->xp_pattern; s < end; s++)
4676 {
4677 /* Skip escaped wildcards. Only when the backslash is not a path
4678 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4679 if (rem_backslash(s))
4680 ++s;
4681 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4682 return FALSE;
4683 }
4684 return TRUE;
4685}
4686
4687/*
4688 * Prepare a string for expansion.
4689 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004690 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 * When expanding other names: The string will be used with regcomp(). Copy
4692 * the name into allocated memory and prepend "^".
4693 */
4694 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004695addstar(
4696 char_u *fname,
4697 int len,
4698 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 char_u *retval;
4701 int i, j;
4702 int new_len;
4703 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004704 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004706 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004707 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004708 && context != EXPAND_SHELLCMD
4709 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 /*
4712 * Matching will be done internally (on something other than files).
4713 * So we convert the file-matching-type wildcards into our kind for
4714 * use with vim_regcomp(). First work out how long it will be:
4715 */
4716
4717 /* For help tags the translation is done in find_help_tags().
4718 * For a tag pattern starting with "/" no translation is needed. */
4719 if (context == EXPAND_HELP
4720 || context == EXPAND_COLORS
4721 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004722 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004723 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004724 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004725 || ((context == EXPAND_TAGS_LISTFILES
4726 || context == EXPAND_TAGS)
4727 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 retval = vim_strnsave(fname, len);
4729 else
4730 {
4731 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4732 for (i = 0; i < len; i++)
4733 {
4734 if (fname[i] == '*' || fname[i] == '~')
4735 new_len++; /* '*' needs to be replaced by ".*"
4736 '~' needs to be replaced by "\~" */
4737
4738 /* Buffer names are like file names. "." should be literal */
4739 if (context == EXPAND_BUFFERS && fname[i] == '.')
4740 new_len++; /* "." becomes "\." */
4741
4742 /* Custom expansion takes care of special things, match
4743 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004744 if ((context == EXPAND_USER_DEFINED
4745 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 new_len++; /* '\' becomes "\\" */
4747 }
4748 retval = alloc(new_len);
4749 if (retval != NULL)
4750 {
4751 retval[0] = '^';
4752 j = 1;
4753 for (i = 0; i < len; i++, j++)
4754 {
4755 /* Skip backslash. But why? At least keep it for custom
4756 * expansion. */
4757 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004758 && context != EXPAND_USER_LIST
4759 && fname[i] == '\\'
4760 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 break;
4762
4763 switch (fname[i])
4764 {
4765 case '*': retval[j++] = '.';
4766 break;
4767 case '~': retval[j++] = '\\';
4768 break;
4769 case '?': retval[j] = '.';
4770 continue;
4771 case '.': if (context == EXPAND_BUFFERS)
4772 retval[j++] = '\\';
4773 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004774 case '\\': if (context == EXPAND_USER_DEFINED
4775 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 retval[j++] = '\\';
4777 break;
4778 }
4779 retval[j] = fname[i];
4780 }
4781 retval[j] = NUL;
4782 }
4783 }
4784 }
4785 else
4786 {
4787 retval = alloc(len + 4);
4788 if (retval != NULL)
4789 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004790 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004793 * Don't add a star to *, ~, ~user, $var or `cmd`.
4794 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * ~ would be at the start of the file name, but not the tail.
4796 * $ could be anywhere in the tail.
4797 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004798 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 */
4800 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004801 ends_in_star = (len > 0 && retval[len - 1] == '*');
4802#ifndef BACKSLASH_IN_FILENAME
4803 for (i = len - 2; i >= 0; --i)
4804 {
4805 if (retval[i] != '\\')
4806 break;
4807 ends_in_star = !ends_in_star;
4808 }
4809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004811 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 && vim_strchr(tail, '$') == NULL
4813 && vim_strchr(retval, '`') == NULL)
4814 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004815 else if (len > 0 && retval[len - 1] == '$')
4816 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 retval[len] = NUL;
4818 }
4819 }
4820 return retval;
4821}
4822
4823/*
4824 * Must parse the command line so far to work out what context we are in.
4825 * Completion can then be done based on that context.
4826 * This routine sets the variables:
4827 * xp->xp_pattern The start of the pattern to be expanded within
4828 * the command line (ends at the cursor).
4829 * xp->xp_context The type of thing to expand. Will be one of:
4830 *
4831 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4832 * the command line, like an unknown command. Caller
4833 * should beep.
4834 * EXPAND_NOTHING Unrecognised context for completion, use char like
4835 * a normal char, rather than for completion. eg
4836 * :s/^I/
4837 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4838 * it.
4839 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4840 * EXPAND_FILES After command with XFILE set, or after setting
4841 * with P_EXPAND set. eg :e ^I, :w>>^I
4842 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4843 * when we know only directories are of interest. eg
4844 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004845 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4847 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4848 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4849 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4850 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4851 * EXPAND_EVENTS Complete event names
4852 * EXPAND_SYNTAX Complete :syntax command arguments
4853 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4854 * EXPAND_AUGROUP Complete autocommand group names
4855 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4856 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4857 * eg :unmap a^I , :cunab x^I
4858 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4859 * eg :call sub^I
4860 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4861 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4862 * names in expressions, eg :while s^I
4863 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004864 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 */
4866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004867set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004869 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (ccline.cmdfirstc != ':'
4871#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004872 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004873 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#endif
4875 )
4876 {
4877 xp->xp_context = EXPAND_NOTHING;
4878 return;
4879 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004880 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881}
4882
4883 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004884set_cmd_context(
4885 expand_T *xp,
4886 char_u *str, /* start of command line */
4887 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004888 int col, /* position of cursor */
4889 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
4891 int old_char = NUL;
4892 char_u *nextcomm;
4893
4894 /*
4895 * Avoid a UMR warning from Purify, only save the character if it has been
4896 * written before.
4897 */
4898 if (col < len)
4899 old_char = str[col];
4900 str[col] = NUL;
4901 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004902
4903#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004904 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004905 {
4906# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004907 /* pass CMD_SIZE because there is no real command */
4908 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004909# endif
4910 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004911 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004912 {
4913 xp->xp_context = ccline.xp_context;
4914 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004915# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004916 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004917# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004918 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004919 else
4920#endif
4921 while (nextcomm != NULL)
4922 nextcomm = set_one_cmd_context(xp, nextcomm);
4923
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004924 /* Store the string here so that call_user_expand_func() can get to them
4925 * easily. */
4926 xp->xp_line = str;
4927 xp->xp_col = col;
4928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 str[col] = old_char;
4930}
4931
4932/*
4933 * Expand the command line "str" from context "xp".
4934 * "xp" must have been set by set_cmd_context().
4935 * xp->xp_pattern points into "str", to where the text that is to be expanded
4936 * starts.
4937 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4938 * cursor.
4939 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4940 * key that triggered expansion literally.
4941 * Returns EXPAND_OK otherwise.
4942 */
4943 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004944expand_cmdline(
4945 expand_T *xp,
4946 char_u *str, /* start of command line */
4947 int col, /* position of cursor */
4948 int *matchcount, /* return: nr of matches */
4949 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004952 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4955 {
4956 beep_flush();
4957 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4958 }
4959 if (xp->xp_context == EXPAND_NOTHING)
4960 {
4961 /* Caller can use the character as a normal char instead */
4962 return EXPAND_NOTHING;
4963 }
4964
4965 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004966 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4967 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (file_str == NULL)
4969 return EXPAND_UNSUCCESSFUL;
4970
Bram Moolenaar94950a92010-12-02 16:01:29 +01004971 if (p_wic)
4972 options += WILD_ICASE;
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004975 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 *matchcount = 0;
4978 *matches = NULL;
4979 }
4980 vim_free(file_str);
4981
4982 return EXPAND_OK;
4983}
4984
4985#ifdef FEAT_MULTI_LANG
4986/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004987 * Cleanup matches for help tags:
4988 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4989 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004991static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004994cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
4996 int i, j;
4997 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004998 char_u buf[4];
4999 char_u *p = buf;
5000
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005001 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005002 {
5003 *p++ = '@';
5004 *p++ = p_hlg[0];
5005 *p++ = p_hlg[1];
5006 }
5007 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 for (i = 0; i < num_file; ++i)
5010 {
5011 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005012 if (len <= 0)
5013 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005014 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
5016 /* Sorting on priority means the same item in another language may
5017 * be anywhere. Search all items for a match up to the "@en". */
5018 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005019 if (j != i && (int)STRLEN(file[j]) == len + 3
5020 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 break;
5022 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005023 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 file[i][len] = NUL;
5025 }
5026 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005027
5028 if (*buf != NUL)
5029 for (i = 0; i < num_file; ++i)
5030 {
5031 len = (int)STRLEN(file[i]) - 3;
5032 if (len <= 0)
5033 continue;
5034 if (STRCMP(file[i] + len, buf) == 0)
5035 {
5036 /* remove the default language */
5037 file[i][len] = NUL;
5038 }
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040}
5041#endif
5042
5043/*
5044 * Do the expansion based on xp->xp_context and "pat".
5045 */
5046 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005047ExpandFromContext(
5048 expand_T *xp,
5049 char_u *pat,
5050 int *num_file,
5051 char_u ***file,
5052 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
5054#ifdef FEAT_CMDL_COMPL
5055 regmatch_T regmatch;
5056#endif
5057 int ret;
5058 int flags;
5059
5060 flags = EW_DIR; /* include directories */
5061 if (options & WILD_LIST_NOTFOUND)
5062 flags |= EW_NOTFOUND;
5063 if (options & WILD_ADD_SLASH)
5064 flags |= EW_ADDSLASH;
5065 if (options & WILD_KEEP_ALL)
5066 flags |= EW_KEEPALL;
5067 if (options & WILD_SILENT)
5068 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005069 if (options & WILD_ALLLINKS)
5070 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005072 if (xp->xp_context == EXPAND_FILES
5073 || xp->xp_context == EXPAND_DIRECTORIES
5074 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 /*
5077 * Expand file or directory names.
5078 */
5079 int free_pat = FALSE;
5080 int i;
5081
5082 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5083 * space */
5084 if (xp->xp_backslash != XP_BS_NONE)
5085 {
5086 free_pat = TRUE;
5087 pat = vim_strsave(pat);
5088 for (i = 0; pat[i]; ++i)
5089 if (pat[i] == '\\')
5090 {
5091 if (xp->xp_backslash == XP_BS_THREE
5092 && pat[i + 1] == '\\'
5093 && pat[i + 2] == '\\'
5094 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005095 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (xp->xp_backslash == XP_BS_ONE
5097 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005098 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100 }
5101
5102 if (xp->xp_context == EXPAND_FILES)
5103 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005104 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5105 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 else
5107 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005108 if (options & WILD_ICASE)
5109 flags |= EW_ICASE;
5110
Bram Moolenaard7834d32009-12-02 16:14:36 +00005111 /* Expand wildcards, supporting %:h and the like. */
5112 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (free_pat)
5114 vim_free(pat);
5115 return ret;
5116 }
5117
5118 *file = (char_u **)"";
5119 *num_file = 0;
5120 if (xp->xp_context == EXPAND_HELP)
5121 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005122 /* With an empty argument we would get all the help tags, which is
5123 * very slow. Get matches for "help" instead. */
5124 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5125 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127#ifdef FEAT_MULTI_LANG
5128 cleanup_help_tags(*num_file, *file);
5129#endif
5130 return OK;
5131 }
5132 return FAIL;
5133 }
5134
5135#ifndef FEAT_CMDL_COMPL
5136 return FAIL;
5137#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005138 if (xp->xp_context == EXPAND_SHELLCMD)
5139 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (xp->xp_context == EXPAND_OLD_SETTING)
5141 return ExpandOldSetting(num_file, file);
5142 if (xp->xp_context == EXPAND_BUFFERS)
5143 return ExpandBufnames(pat, num_file, file, options);
5144 if (xp->xp_context == EXPAND_TAGS
5145 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5146 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5147 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005148 {
5149 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005150 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5151 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005154 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005155 char *directories[] = {"compiler", 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 Moolenaar1587a1e2010-07-29 20:59:59 +02005158 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005159 {
5160 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005161 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005162 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005163 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005164 {
5165 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005166 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005167 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005168# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5169 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005170 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005171# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005172 if (xp->xp_context == EXPAND_PACKADD)
5173 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
5175 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5176 if (regmatch.regprog == NULL)
5177 return FAIL;
5178
5179 /* set ignore-case according to p_ic, p_scs and pat */
5180 regmatch.rm_ic = ignorecase(pat);
5181
5182 if (xp->xp_context == EXPAND_SETTINGS
5183 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5184 ret = ExpandSettings(xp, &regmatch, num_file, file);
5185 else if (xp->xp_context == EXPAND_MAPPINGS)
5186 ret = ExpandMappings(&regmatch, num_file, file);
5187# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5188 else if (xp->xp_context == EXPAND_USER_DEFINED)
5189 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5190# endif
5191 else
5192 {
5193 static struct expgen
5194 {
5195 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005196 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005198 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 } tab[] =
5200 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005201 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5202 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005203 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005204 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005205#ifdef FEAT_CMDHIST
5206 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005209 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005210 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005211 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5212 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5213 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
5215#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5217 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5218 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5219 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#endif
5221#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5223 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#endif
5225#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005226 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005228#ifdef FEAT_PROFILE
5229 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5230#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005231 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005232 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5233 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005234#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005235 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005236#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005237#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005238 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005239#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005240#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005241 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5244 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005245 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5246 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005248 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005249 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005250 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 };
5252 int i;
5253
5254 /*
5255 * Find a context in the table and call the ExpandGeneric() with the
5256 * right function to do the expansion.
5257 */
5258 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005259 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (xp->xp_context == tab[i].context)
5261 {
5262 if (tab[i].ic)
5263 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005264 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005265 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 break;
5267 }
5268 }
5269
Bram Moolenaar473de612013-06-08 18:19:48 +02005270 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 return ret;
5273#endif /* FEAT_CMDL_COMPL */
5274}
5275
5276#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5277/*
5278 * Expand a list of names.
5279 *
5280 * Generic function for command line completion. It calls a function to
5281 * obtain strings, one by one. The strings are matched against a regexp
5282 * program. Matching strings are copied into an array, which is returned.
5283 *
5284 * Returns OK when no problems encountered, FAIL for error (out of memory).
5285 */
5286 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005287ExpandGeneric(
5288 expand_T *xp,
5289 regmatch_T *regmatch,
5290 int *num_file,
5291 char_u ***file,
5292 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005294 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 int i;
5297 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005298 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 char_u *str;
5300
5301 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005302 * round == 0: count the number of matching names
5303 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005305 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 for (i = 0; ; ++i)
5308 {
5309 str = (*func)(xp, i);
5310 if (str == NULL) /* end of list */
5311 break;
5312 if (*str == NUL) /* skip empty strings */
5313 continue;
5314
5315 if (vim_regexec(regmatch, str, (colnr_T)0))
5316 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005317 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005319 if (escaped)
5320 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5321 else
5322 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 (*file)[count] = str;
5324#ifdef FEAT_MENU
5325 if (func == get_menu_names && str != NULL)
5326 {
5327 /* test for separator added by get_menu_names() */
5328 str += STRLEN(str) - 1;
5329 if (*str == '\001')
5330 *str = '.';
5331 }
5332#endif
5333 }
5334 ++count;
5335 }
5336 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005337 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
5339 if (count == 0)
5340 return OK;
5341 *num_file = count;
5342 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5343 if (*file == NULL)
5344 {
5345 *file = (char_u **)"";
5346 return FAIL;
5347 }
5348 count = 0;
5349 }
5350 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005351
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005352 /* Sort the results. Keep menu's in the specified order. */
5353 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005354 {
5355 if (xp->xp_context == EXPAND_EXPRESSION
5356 || xp->xp_context == EXPAND_FUNCTIONS
5357 || xp->xp_context == EXPAND_USER_FUNC)
5358 /* <SNR> functions should be sorted to the end. */
5359 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5360 sort_func_compare);
5361 else
5362 sort_strings(*file, *num_file);
5363 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005364
Bram Moolenaar4f688582007-07-24 12:34:30 +00005365#ifdef FEAT_CMDL_COMPL
5366 /* Reset the variables used for special highlight names expansion, so that
5367 * they don't show up when getting normal highlight names by ID. */
5368 reset_expand_highlight();
5369#endif
5370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 return OK;
5372}
5373
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005374/*
5375 * Complete a shell command.
5376 * Returns FAIL or OK;
5377 */
5378 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005379expand_shellcmd(
5380 char_u *filepat, /* pattern to match with command names */
5381 int *num_file, /* return: number of matches */
5382 char_u ***file, /* return: array with matches */
5383 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005384{
5385 char_u *pat;
5386 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005387 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005388 int mustfree = FALSE;
5389 garray_T ga;
5390 char_u *buf = alloc(MAXPATHL);
5391 size_t l;
5392 char_u *s, *e;
5393 int flags = flagsarg;
5394 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005395 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005396 hashtab_T found_ht;
5397 hashitem_T *hi;
5398 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005399
5400 if (buf == NULL)
5401 return FAIL;
5402
5403 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5404 * space */
5405 pat = vim_strsave(filepat);
5406 for (i = 0; pat[i]; ++i)
5407 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005408 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005409
Bram Moolenaarb5971142015-03-21 17:32:19 +01005410 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005411
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005412 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5413 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005414 path = (char_u *)".";
5415 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005416 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005417 /* For an absolute name we don't use $PATH. */
5418 if (!mch_isFullName(pat))
5419 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005420 if (path == NULL)
5421 path = (char_u *)"";
5422 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005423
5424 /*
5425 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005426 * collect them in "ga". When "." is not in $PATH also expand for the
5427 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005428 */
5429 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005430 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005431 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005432 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005433#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005434 e = vim_strchr(s, ';');
5435#else
5436 e = vim_strchr(s, ':');
5437#endif
5438 if (e == NULL)
5439 e = s + STRLEN(s);
5440
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005441 if (*s == NUL)
5442 {
5443 if (did_curdir)
5444 break;
5445 // Find directories in the current directory, path is empty.
5446 did_curdir = TRUE;
5447 flags |= EW_DIR;
5448 }
5449 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5450 {
5451 did_curdir = TRUE;
5452 flags |= EW_DIR;
5453 }
5454 else
5455 // Do not match directories inside a $PATH item.
5456 flags &= ~EW_DIR;
5457
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005458 l = e - s;
5459 if (l > MAXPATHL - 5)
5460 break;
5461 vim_strncpy(buf, s, l);
5462 add_pathsep(buf);
5463 l = STRLEN(buf);
5464 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5465
5466 /* Expand matches in one directory of $PATH. */
5467 ret = expand_wildcards(1, &buf, num_file, file, flags);
5468 if (ret == OK)
5469 {
5470 if (ga_grow(&ga, *num_file) == FAIL)
5471 FreeWild(*num_file, *file);
5472 else
5473 {
5474 for (i = 0; i < *num_file; ++i)
5475 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005476 char_u *name = (*file)[i];
5477
5478 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005479 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005480 // Check if this name was already found.
5481 hash = hash_hash(name + l);
5482 hi = hash_lookup(&found_ht, name + l, hash);
5483 if (HASHITEM_EMPTY(hi))
5484 {
5485 // Remove the path that was prepended.
5486 STRMOVE(name, name + l);
5487 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5488 hash_add_item(&found_ht, hi, name, hash);
5489 name = NULL;
5490 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005491 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005492 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005493 }
5494 vim_free(*file);
5495 }
5496 }
5497 if (*e != NUL)
5498 ++e;
5499 }
5500 *file = ga.ga_data;
5501 *num_file = ga.ga_len;
5502
5503 vim_free(buf);
5504 vim_free(pat);
5505 if (mustfree)
5506 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005507 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005508 return OK;
5509}
5510
5511
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5513/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005514 * Call "user_expand_func()" to invoke a user defined Vim script function and
5515 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005517 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005518call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005519 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520 expand_T *xp,
5521 int *num_file,
5522 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005524 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005525 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005527 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005528 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005529 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaarb7515462013-06-29 12:58:33 +02005531 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005532 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 *num_file = 0;
5534 *file = NULL;
5535
Bram Moolenaarb7515462013-06-29 12:58:33 +02005536 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005537 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005538 keep = ccline.cmdbuff[ccline.cmdlen];
5539 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005540 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005541
Bram Moolenaarffa96842018-06-12 22:05:14 +02005542 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5543
5544 args[0].v_type = VAR_STRING;
5545 args[0].vval.v_string = pat;
5546 args[1].v_type = VAR_STRING;
5547 args[1].vval.v_string = xp->xp_line;
5548 args[2].v_type = VAR_NUMBER;
5549 args[2].vval.v_number = xp->xp_col;
5550 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005552 /* Save the cmdline, we don't know what the function may do. */
5553 save_ccline = ccline;
5554 ccline.cmdbuff = NULL;
5555 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005557
Bram Moolenaarded27a12018-08-01 19:06:03 +02005558 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005559
5560 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005562 if (ccline.cmdbuff != NULL)
5563 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005564
Bram Moolenaarffa96842018-06-12 22:05:14 +02005565 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005566 return ret;
5567}
5568
5569/*
5570 * Expand names with a function defined by the user.
5571 */
5572 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005573ExpandUserDefined(
5574 expand_T *xp,
5575 regmatch_T *regmatch,
5576 int *num_file,
5577 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005578{
5579 char_u *retstr;
5580 char_u *s;
5581 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005582 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005583 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005584 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005585
5586 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5587 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 return FAIL;
5589
5590 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005591 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
5593 e = vim_strchr(s, '\n');
5594 if (e == NULL)
5595 e = s + STRLEN(s);
5596 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005597 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005599 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5600 *e = keep;
5601
5602 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005604 if (ga_grow(&ga, 1) == FAIL)
5605 break;
5606 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5607 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (*e != NUL)
5611 ++e;
5612 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005613 vim_free(retstr);
5614 *file = ga.ga_data;
5615 *num_file = ga.ga_len;
5616 return OK;
5617}
5618
5619/*
5620 * Expand names with a list returned by a function defined by the user.
5621 */
5622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623ExpandUserList(
5624 expand_T *xp,
5625 int *num_file,
5626 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005627{
5628 list_T *retlist;
5629 listitem_T *li;
5630 garray_T ga;
5631
5632 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5633 if (retlist == NULL)
5634 return FAIL;
5635
5636 ga_init2(&ga, (int)sizeof(char *), 3);
5637 /* Loop over the items in the list. */
5638 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5639 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005640 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5641 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005642
5643 if (ga_grow(&ga, 1) == FAIL)
5644 break;
5645
5646 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005647 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005648 ++ga.ga_len;
5649 }
5650 list_unref(retlist);
5651
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 *file = ga.ga_data;
5653 *num_file = ga.ga_len;
5654 return OK;
5655}
5656#endif
5657
5658/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005659 * Expand color scheme, compiler or filetype names.
5660 * Search from 'runtimepath':
5661 * 'runtimepath'/{dirnames}/{pat}.vim
5662 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5663 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5664 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5665 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005666 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 */
5668 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005669ExpandRTDir(
5670 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005671 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672 int *num_file,
5673 char_u ***file,
5674 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 char_u *s;
5677 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005678 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005680 int i;
5681 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 *num_file = 0;
5684 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005685 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005686 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005688 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005690 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5691 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005693 ga_clear_strings(&ga);
5694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005696 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005697 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005698 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005700
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005701 if (flags & DIP_START) {
5702 for (i = 0; dirnames[i] != NULL; ++i)
5703 {
5704 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5705 if (s == NULL)
5706 {
5707 ga_clear_strings(&ga);
5708 return FAIL;
5709 }
5710 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5711 globpath(p_pp, s, &ga, 0);
5712 vim_free(s);
5713 }
5714 }
5715
5716 if (flags & DIP_OPT) {
5717 for (i = 0; dirnames[i] != NULL; ++i)
5718 {
5719 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5720 if (s == NULL)
5721 {
5722 ga_clear_strings(&ga);
5723 return FAIL;
5724 }
5725 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5726 globpath(p_pp, s, &ga, 0);
5727 vim_free(s);
5728 }
5729 }
5730
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005731 for (i = 0; i < ga.ga_len; ++i)
5732 {
5733 match = ((char_u **)ga.ga_data)[i];
5734 s = match;
5735 e = s + STRLEN(s);
5736 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5737 {
5738 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005739 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005740 if (s < match || vim_ispathsep(*s))
5741 break;
5742 ++s;
5743 *e = NUL;
5744 mch_memmove(match, s, e - s + 1);
5745 }
5746 }
5747
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005748 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005749 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005750
5751 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005752 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005753 remove_duplicates(&ga);
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 *file = ga.ga_data;
5756 *num_file = ga.ga_len;
5757 return OK;
5758}
5759
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005760/*
5761 * Expand loadplugin names:
5762 * 'packpath'/pack/ * /opt/{pat}
5763 */
5764 static int
5765ExpandPackAddDir(
5766 char_u *pat,
5767 int *num_file,
5768 char_u ***file)
5769{
5770 char_u *s;
5771 char_u *e;
5772 char_u *match;
5773 garray_T ga;
5774 int i;
5775 int pat_len;
5776
5777 *num_file = 0;
5778 *file = NULL;
5779 pat_len = (int)STRLEN(pat);
5780 ga_init2(&ga, (int)sizeof(char *), 10);
5781
5782 s = alloc((unsigned)(pat_len + 26));
5783 if (s == NULL)
5784 {
5785 ga_clear_strings(&ga);
5786 return FAIL;
5787 }
5788 sprintf((char *)s, "pack/*/opt/%s*", pat);
5789 globpath(p_pp, s, &ga, 0);
5790 vim_free(s);
5791
5792 for (i = 0; i < ga.ga_len; ++i)
5793 {
5794 match = ((char_u **)ga.ga_data)[i];
5795 s = gettail(match);
5796 e = s + STRLEN(s);
5797 mch_memmove(match, s, e - s + 1);
5798 }
5799
5800 if (ga.ga_len == 0)
5801 return FAIL;
5802
5803 /* Sort and remove duplicates which can happen when specifying multiple
5804 * directories in dirnames. */
5805 remove_duplicates(&ga);
5806
5807 *file = ga.ga_data;
5808 *num_file = ga.ga_len;
5809 return OK;
5810}
5811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812#endif
5813
5814#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5815/*
5816 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005817 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005819 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820globpath(
5821 char_u *path,
5822 char_u *file,
5823 garray_T *ga,
5824 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 expand_T xpc;
5827 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 int num_p;
5830 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
5832 buf = alloc(MAXPATHL);
5833 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005834 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005836 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 /* Loop over all entries in {path}. */
5840 while (*path != NUL)
5841 {
5842 /* Copy one item of the path to buf[] and concatenate the file name. */
5843 copy_option_part(&path, buf, MAXPATHL, ",");
5844 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5845 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005846# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005847 /* Using the platform's path separator (\) makes vim incorrectly
5848 * treat it as an escape character, use '/' instead. */
5849 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5850 STRCAT(buf, "/");
5851# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005855 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5856 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005858 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005860 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 for (i = 0; i < num_p; ++i)
5863 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005864 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005865 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005866 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 FreeWild(num_p, p);
5871 }
5872 }
5873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876}
5877
5878#endif
5879
5880#if defined(FEAT_CMDHIST) || defined(PROTO)
5881
5882/*********************************
5883 * Command line history stuff *
5884 *********************************/
5885
5886/*
5887 * Translate a history character to the associated type number.
5888 */
5889 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005890hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 if (c == ':')
5893 return HIST_CMD;
5894 if (c == '=')
5895 return HIST_EXPR;
5896 if (c == '@')
5897 return HIST_INPUT;
5898 if (c == '>')
5899 return HIST_DEBUG;
5900 return HIST_SEARCH; /* must be '?' or '/' */
5901}
5902
5903/*
5904 * Table of history names.
5905 * These names are used in :history and various hist...() functions.
5906 * It is sufficient to give the significant prefix of a history name.
5907 */
5908
5909static char *(history_names[]) =
5910{
5911 "cmd",
5912 "search",
5913 "expr",
5914 "input",
5915 "debug",
5916 NULL
5917};
5918
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005919#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5920/*
5921 * Function given to ExpandGeneric() to obtain the possible first
5922 * arguments of the ":history command.
5923 */
5924 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005925get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005926{
5927 static char_u compl[2] = { NUL, NUL };
5928 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005929 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005930 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5931
5932 if (idx < short_names_count)
5933 {
5934 compl[0] = (char_u)short_names[idx];
5935 return compl;
5936 }
5937 if (idx < short_names_count + history_name_count)
5938 return (char_u *)history_names[idx - short_names_count];
5939 if (idx == short_names_count + history_name_count)
5940 return (char_u *)"all";
5941 return NULL;
5942}
5943#endif
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945/*
5946 * init_history() - Initialize the command line history.
5947 * Also used to re-allocate the history when the size changes.
5948 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005949 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005950init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952 int newlen; /* new length of history table */
5953 histentry_T *temp;
5954 int i;
5955 int j;
5956 int type;
5957
5958 /*
5959 * If size of history table changed, reallocate it
5960 */
5961 newlen = (int)p_hi;
5962 if (newlen != hislen) /* history length changed */
5963 {
5964 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5965 {
5966 if (newlen)
5967 {
5968 temp = (histentry_T *)lalloc(
5969 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5970 if (temp == NULL) /* out of memory! */
5971 {
5972 if (type == 0) /* first one: just keep the old length */
5973 {
5974 newlen = hislen;
5975 break;
5976 }
5977 /* Already changed one table, now we can only have zero
5978 * length for all tables. */
5979 newlen = 0;
5980 type = -1;
5981 continue;
5982 }
5983 }
5984 else
5985 temp = NULL;
5986 if (newlen == 0 || temp != NULL)
5987 {
5988 if (hisidx[type] < 0) /* there are no entries yet */
5989 {
5990 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005991 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
5993 else if (newlen > hislen) /* array becomes bigger */
5994 {
5995 for (i = 0; i <= hisidx[type]; ++i)
5996 temp[i] = history[type][i];
5997 j = i;
5998 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005999 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 for ( ; j < hislen; ++i, ++j)
6001 temp[i] = history[type][j];
6002 }
6003 else /* array becomes smaller or 0 */
6004 {
6005 j = hisidx[type];
6006 for (i = newlen - 1; ; --i)
6007 {
6008 if (i >= 0) /* copy newest entries */
6009 temp[i] = history[type][j];
6010 else /* remove older entries */
6011 vim_free(history[type][j].hisstr);
6012 if (--j < 0)
6013 j = hislen - 1;
6014 if (j == hisidx[type])
6015 break;
6016 }
6017 hisidx[type] = newlen - 1;
6018 }
6019 vim_free(history[type]);
6020 history[type] = temp;
6021 }
6022 }
6023 hislen = newlen;
6024 }
6025}
6026
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006028clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006029{
6030 hisptr->hisnum = 0;
6031 hisptr->viminfo = FALSE;
6032 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006033 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006034}
6035
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036/*
6037 * Check if command line 'str' is already in history.
6038 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6039 */
6040 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006041in_history(
6042 int type,
6043 char_u *str,
6044 int move_to_front, /* Move the entry to the front if it exists */
6045 int sep,
6046 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 int i;
6049 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006050 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051
6052 if (hisidx[type] < 0)
6053 return FALSE;
6054 i = hisidx[type];
6055 do
6056 {
6057 if (history[type][i].hisstr == NULL)
6058 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006059
6060 /* For search history, check that the separator character matches as
6061 * well. */
6062 p = history[type][i].hisstr;
6063 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006064 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006065 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 {
6067 if (!move_to_front)
6068 return TRUE;
6069 last_i = i;
6070 break;
6071 }
6072 if (--i < 0)
6073 i = hislen - 1;
6074 } while (i != hisidx[type]);
6075
6076 if (last_i >= 0)
6077 {
6078 str = history[type][i].hisstr;
6079 while (i != hisidx[type])
6080 {
6081 if (++i >= hislen)
6082 i = 0;
6083 history[type][last_i] = history[type][i];
6084 last_i = i;
6085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006087 history[type][i].viminfo = FALSE;
6088 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006089 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 return TRUE;
6091 }
6092 return FALSE;
6093}
6094
6095/*
6096 * Convert history name (from table above) to its HIST_ equivalent.
6097 * When "name" is empty, return "cmd" history.
6098 * Returns -1 for unknown history name.
6099 */
6100 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 int i;
6104 int len = (int)STRLEN(name);
6105
6106 /* No argument: use current history. */
6107 if (len == 0)
6108 return hist_char2type(ccline.cmdfirstc);
6109
6110 for (i = 0; history_names[i] != NULL; ++i)
6111 if (STRNICMP(name, history_names[i], len) == 0)
6112 return i;
6113
6114 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6115 return hist_char2type(name[0]);
6116
6117 return -1;
6118}
6119
6120static int last_maptick = -1; /* last seen maptick */
6121
6122/*
6123 * Add the given string to the given history. If the string is already in the
6124 * history then it is moved to the front. "histype" may be one of he HIST_
6125 * values.
6126 */
6127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128add_to_history(
6129 int histype,
6130 char_u *new_entry,
6131 int in_map, /* consider maptick when inside a mapping */
6132 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 histentry_T *hisptr;
6135 int len;
6136
6137 if (hislen == 0) /* no history */
6138 return;
6139
Bram Moolenaara939e432013-11-09 05:30:26 +01006140 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6141 return;
6142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 /*
6144 * Searches inside the same mapping overwrite each other, so that only
6145 * the last line is kept. Be careful not to remove a line that was moved
6146 * down, only lines that were added.
6147 */
6148 if (histype == HIST_SEARCH && in_map)
6149 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006150 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
6152 /* Current line is from the same mapping, remove it */
6153 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6154 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006155 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 --hisnum[histype];
6157 if (--hisidx[HIST_SEARCH] < 0)
6158 hisidx[HIST_SEARCH] = hislen - 1;
6159 }
6160 last_maptick = -1;
6161 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006162 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
6164 if (++hisidx[histype] == hislen)
6165 hisidx[histype] = 0;
6166 hisptr = &history[histype][hisidx[histype]];
6167 vim_free(hisptr->hisstr);
6168
6169 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006170 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6172 if (hisptr->hisstr != NULL)
6173 hisptr->hisstr[len + 1] = sep;
6174
6175 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006176 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006177 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (histype == HIST_SEARCH && in_map)
6179 last_maptick = maptick;
6180 }
6181}
6182
6183#if defined(FEAT_EVAL) || defined(PROTO)
6184
6185/*
6186 * Get identifier of newest history entry.
6187 * "histype" may be one of the HIST_ values.
6188 */
6189 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006190get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6193 || hisidx[histype] < 0)
6194 return -1;
6195
6196 return history[histype][hisidx[histype]].hisnum;
6197}
6198
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 * Calculate history index from a number:
6201 * num > 0: seen as identifying number of a history entry
6202 * num < 0: relative position in history wrt newest entry
6203 * "histype" may be one of the HIST_ values.
6204 */
6205 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006206calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 int i;
6209 histentry_T *hist;
6210 int wrapped = FALSE;
6211
6212 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6213 || (i = hisidx[histype]) < 0 || num == 0)
6214 return -1;
6215
6216 hist = history[histype];
6217 if (num > 0)
6218 {
6219 while (hist[i].hisnum > num)
6220 if (--i < 0)
6221 {
6222 if (wrapped)
6223 break;
6224 i += hislen;
6225 wrapped = TRUE;
6226 }
6227 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6228 return i;
6229 }
6230 else if (-num <= hislen)
6231 {
6232 i += num + 1;
6233 if (i < 0)
6234 i += hislen;
6235 if (hist[i].hisstr != NULL)
6236 return i;
6237 }
6238 return -1;
6239}
6240
6241/*
6242 * Get a history entry by its index.
6243 * "histype" may be one of the HIST_ values.
6244 */
6245 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006246get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 idx = calc_hist_idx(histype, idx);
6249 if (idx >= 0)
6250 return history[histype][idx].hisstr;
6251 else
6252 return (char_u *)"";
6253}
6254
6255/*
6256 * Clear all entries of a history.
6257 * "histype" may be one of the HIST_ values.
6258 */
6259 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 int i;
6263 histentry_T *hisptr;
6264
6265 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6266 {
6267 hisptr = history[histype];
6268 for (i = hislen; i--;)
6269 {
6270 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006271 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006272 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
6274 hisidx[histype] = -1; /* mark history as cleared */
6275 hisnum[histype] = 0; /* reset identifier counter */
6276 return OK;
6277 }
6278 return FAIL;
6279}
6280
6281/*
6282 * Remove all entries matching {str} from a history.
6283 * "histype" may be one of the HIST_ values.
6284 */
6285 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006286del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
6288 regmatch_T regmatch;
6289 histentry_T *hisptr;
6290 int idx;
6291 int i;
6292 int last;
6293 int found = FALSE;
6294
6295 regmatch.regprog = NULL;
6296 regmatch.rm_ic = FALSE; /* always match case */
6297 if (hislen != 0
6298 && histype >= 0
6299 && histype < HIST_COUNT
6300 && *str != NUL
6301 && (idx = hisidx[histype]) >= 0
6302 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6303 != NULL)
6304 {
6305 i = last = idx;
6306 do
6307 {
6308 hisptr = &history[histype][i];
6309 if (hisptr->hisstr == NULL)
6310 break;
6311 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6312 {
6313 found = TRUE;
6314 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006315 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317 else
6318 {
6319 if (i != last)
6320 {
6321 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006322 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
6324 if (--last < 0)
6325 last += hislen;
6326 }
6327 if (--i < 0)
6328 i += hislen;
6329 } while (i != idx);
6330 if (history[histype][idx].hisstr == NULL)
6331 hisidx[histype] = -1;
6332 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006333 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 return found;
6335}
6336
6337/*
6338 * Remove an indexed entry from a history.
6339 * "histype" may be one of the HIST_ values.
6340 */
6341 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006342del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 int i, j;
6345
6346 i = calc_hist_idx(histype, idx);
6347 if (i < 0)
6348 return FALSE;
6349 idx = hisidx[histype];
6350 vim_free(history[histype][i].hisstr);
6351
6352 /* When deleting the last added search string in a mapping, reset
6353 * last_maptick, so that the last added search string isn't deleted again.
6354 */
6355 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6356 last_maptick = -1;
6357
6358 while (i != idx)
6359 {
6360 j = (i + 1) % hislen;
6361 history[histype][i] = history[histype][j];
6362 i = j;
6363 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006364 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (--i < 0)
6366 i += hislen;
6367 hisidx[histype] = i;
6368 return TRUE;
6369}
6370
6371#endif /* FEAT_EVAL */
6372
6373#if defined(FEAT_CRYPT) || defined(PROTO)
6374/*
6375 * Very specific function to remove the value in ":set key=val" from the
6376 * history.
6377 */
6378 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006379remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381 char_u *p;
6382 int i;
6383
6384 i = hisidx[HIST_CMD];
6385 if (i < 0)
6386 return;
6387 p = history[HIST_CMD][i].hisstr;
6388 if (p != NULL)
6389 for ( ; *p; ++p)
6390 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6391 {
6392 p = vim_strchr(p + 3, '=');
6393 if (p == NULL)
6394 break;
6395 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006396 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (p[i] == '\\' && p[i + 1])
6398 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006399 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 --p;
6401 }
6402}
6403#endif
6404
6405#endif /* FEAT_CMDHIST */
6406
Bram Moolenaar064154c2016-03-19 22:50:43 +01006407#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006408/*
6409 * Get pointer to the command line info to use. cmdline_paste() may clear
6410 * ccline and put the previous value in prev_ccline.
6411 */
6412 static struct cmdline_info *
6413get_ccline_ptr(void)
6414{
6415 if ((State & CMDLINE) == 0)
6416 return NULL;
6417 if (ccline.cmdbuff != NULL)
6418 return &ccline;
6419 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6420 return &prev_ccline;
6421 return NULL;
6422}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006423#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006424
Bram Moolenaar064154c2016-03-19 22:50:43 +01006425#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006426/*
6427 * Get the current command line in allocated memory.
6428 * Only works when the command line is being edited.
6429 * Returns NULL when something is wrong.
6430 */
6431 char_u *
6432get_cmdline_str(void)
6433{
6434 struct cmdline_info *p = get_ccline_ptr();
6435
6436 if (p == NULL)
6437 return NULL;
6438 return vim_strnsave(p->cmdbuff, p->cmdlen);
6439}
6440
6441/*
6442 * Get the current command line position, counted in bytes.
6443 * Zero is the first position.
6444 * Only works when the command line is being edited.
6445 * Returns -1 when something is wrong.
6446 */
6447 int
6448get_cmdline_pos(void)
6449{
6450 struct cmdline_info *p = get_ccline_ptr();
6451
6452 if (p == NULL)
6453 return -1;
6454 return p->cmdpos;
6455}
6456
6457/*
6458 * Set the command line byte position to "pos". Zero is the first position.
6459 * Only works when the command line is being edited.
6460 * Returns 1 when failed, 0 when OK.
6461 */
6462 int
6463set_cmdline_pos(
6464 int pos)
6465{
6466 struct cmdline_info *p = get_ccline_ptr();
6467
6468 if (p == NULL)
6469 return 1;
6470
6471 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6472 * changed the command line. */
6473 if (pos < 0)
6474 new_cmdpos = 0;
6475 else
6476 new_cmdpos = pos;
6477 return 0;
6478}
6479#endif
6480
6481#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6482/*
6483 * Get the current command-line type.
6484 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6485 * Only works when the command line is being edited.
6486 * Returns NUL when something is wrong.
6487 */
6488 int
6489get_cmdline_type(void)
6490{
6491 struct cmdline_info *p = get_ccline_ptr();
6492
6493 if (p == NULL)
6494 return NUL;
6495 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006496 return
6497# ifdef FEAT_EVAL
6498 (p->input_fn) ? '@' :
6499# endif
6500 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006501 return p->cmdfirstc;
6502}
6503#endif
6504
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6506/*
6507 * Get indices "num1,num2" that specify a range within a list (not a range of
6508 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6509 * Returns OK if parsed successfully, otherwise FAIL.
6510 */
6511 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006512get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 int len;
6515 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006516 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
6518 *str = skipwhite(*str);
6519 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6520 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006521 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 *str += len;
6523 *num1 = (int)num;
6524 first = TRUE;
6525 }
6526 *str = skipwhite(*str);
6527 if (**str == ',') /* parse "to" part of range */
6528 {
6529 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006530 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 if (len > 0)
6532 {
6533 *num2 = (int)num;
6534 *str = skipwhite(*str + len);
6535 }
6536 else if (!first) /* no number given at all */
6537 return FAIL;
6538 }
6539 else if (first) /* only one number given */
6540 *num2 = *num1;
6541 return OK;
6542}
6543#endif
6544
6545#if defined(FEAT_CMDHIST) || defined(PROTO)
6546/*
6547 * :history command - print a history
6548 */
6549 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006550ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551{
6552 histentry_T *hist;
6553 int histype1 = HIST_CMD;
6554 int histype2 = HIST_CMD;
6555 int hisidx1 = 1;
6556 int hisidx2 = -1;
6557 int idx;
6558 int i, j, k;
6559 char_u *end;
6560 char_u *arg = eap->arg;
6561
6562 if (hislen == 0)
6563 {
6564 MSG(_("'history' option is zero"));
6565 return;
6566 }
6567
6568 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6569 {
6570 end = arg;
6571 while (ASCII_ISALPHA(*end)
6572 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6573 end++;
6574 i = *end;
6575 *end = NUL;
6576 histype1 = get_histtype(arg);
6577 if (histype1 == -1)
6578 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006579 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
6581 histype1 = 0;
6582 histype2 = HIST_COUNT-1;
6583 }
6584 else
6585 {
6586 *end = i;
6587 EMSG(_(e_trailing));
6588 return;
6589 }
6590 }
6591 else
6592 histype2 = histype1;
6593 *end = i;
6594 }
6595 else
6596 end = arg;
6597 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6598 {
6599 EMSG(_(e_trailing));
6600 return;
6601 }
6602
6603 for (; !got_int && histype1 <= histype2; ++histype1)
6604 {
6605 STRCPY(IObuff, "\n # ");
6606 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6607 MSG_PUTS_TITLE(IObuff);
6608 idx = hisidx[histype1];
6609 hist = history[histype1];
6610 j = hisidx1;
6611 k = hisidx2;
6612 if (j < 0)
6613 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6614 if (k < 0)
6615 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6616 if (idx >= 0 && j <= k)
6617 for (i = idx + 1; !got_int; ++i)
6618 {
6619 if (i == hislen)
6620 i = 0;
6621 if (hist[i].hisstr != NULL
6622 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6623 {
6624 msg_putchar('\n');
6625 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6626 hist[i].hisnum);
6627 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6628 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006629 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 else
6631 STRCAT(IObuff, hist[i].hisstr);
6632 msg_outtrans(IObuff);
6633 out_flush();
6634 }
6635 if (i == idx)
6636 break;
6637 }
6638 }
6639}
6640#endif
6641
6642#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006643/*
6644 * Buffers for history read from a viminfo file. Only valid while reading.
6645 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006646static histentry_T *viminfo_history[HIST_COUNT] =
6647 {NULL, NULL, NULL, NULL, NULL};
6648static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6649static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650static int viminfo_add_at_front = FALSE;
6651
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006652static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654/*
6655 * Translate a history type number to the associated character.
6656 */
6657 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006658hist_type2char(
6659 int type,
6660 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 if (type == HIST_CMD)
6663 return ':';
6664 if (type == HIST_SEARCH)
6665 {
6666 if (use_question)
6667 return '?';
6668 else
6669 return '/';
6670 }
6671 if (type == HIST_EXPR)
6672 return '=';
6673 return '@';
6674}
6675
6676/*
6677 * Prepare for reading the history from the viminfo file.
6678 * This allocates history arrays to store the read history lines.
6679 */
6680 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006681prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 int i;
6684 int num;
6685 int type;
6686 int len;
6687
6688 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006689 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (asklen > hislen)
6691 asklen = hislen;
6692
6693 for (type = 0; type < HIST_COUNT; ++type)
6694 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006695 /* Count the number of empty spaces in the history list. Entries read
6696 * from viminfo previously are also considered empty. If there are
6697 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006699 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 num++;
6701 len = asklen;
6702 if (num > len)
6703 len = num;
6704 if (len <= 0)
6705 viminfo_history[type] = NULL;
6706 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006707 viminfo_history[type] = (histentry_T *)lalloc(
6708 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (viminfo_history[type] == NULL)
6710 len = 0;
6711 viminfo_hislen[type] = len;
6712 viminfo_hisidx[type] = 0;
6713 }
6714}
6715
6716/*
6717 * Accept a line from the viminfo, store it in the history array when it's
6718 * new.
6719 */
6720 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006721read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 int type;
6724 long_u len;
6725 char_u *val;
6726 char_u *p;
6727
6728 type = hist_char2type(virp->vir_line[0]);
6729 if (viminfo_hisidx[type] < viminfo_hislen[type])
6730 {
6731 val = viminfo_readstring(virp, 1, TRUE);
6732 if (val != NULL && *val != NUL)
6733 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006734 int sep = (*val == ' ' ? NUL : *val);
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006737 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {
6739 /* Need to re-allocate to append the separator byte. */
6740 len = STRLEN(val);
6741 p = lalloc(len + 2, TRUE);
6742 if (p != NULL)
6743 {
6744 if (type == HIST_SEARCH)
6745 {
6746 /* Search entry: Move the separator from the first
6747 * column to after the NUL. */
6748 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006749 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 }
6751 else
6752 {
6753 /* Not a search entry: No separator in the viminfo
6754 * file, add a NUL separator. */
6755 mch_memmove(p, val, (size_t)len + 1);
6756 p[len + 1] = NUL;
6757 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006758 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6759 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006760 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6761 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006762 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
6764 }
6765 }
6766 vim_free(val);
6767 }
6768 return viminfo_readline(virp);
6769}
6770
Bram Moolenaar07219f92013-04-14 23:19:36 +02006771/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006772 * Accept a new style history line from the viminfo, store it in the history
6773 * array when it's new.
6774 */
6775 void
6776handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006777 garray_T *values,
6778 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006779{
6780 int type;
6781 long_u len;
6782 char_u *val;
6783 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006784 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006785
6786 /* Check the format:
6787 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006788 if (values->ga_len < 4
6789 || vp[0].bv_type != BVAL_NR
6790 || vp[1].bv_type != BVAL_NR
6791 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6792 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006793 return;
6794
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006795 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006796 if (type >= HIST_COUNT)
6797 return;
6798 if (viminfo_hisidx[type] < viminfo_hislen[type])
6799 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006800 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006801 if (val != NULL && *val != NUL)
6802 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006803 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6804 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006805 int idx;
6806 int overwrite = FALSE;
6807
6808 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6809 {
6810 /* If lines were written by an older Vim we need to avoid
6811 * getting duplicates. See if the entry already exists. */
6812 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6813 {
6814 p = viminfo_history[type][idx].hisstr;
6815 if (STRCMP(val, p) == 0
6816 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6817 {
6818 overwrite = TRUE;
6819 break;
6820 }
6821 }
6822
6823 if (!overwrite)
6824 {
6825 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006826 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006827 p = lalloc(len + 2, TRUE);
6828 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006829 else
6830 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006831 if (p != NULL)
6832 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006833 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006834 if (!overwrite)
6835 {
6836 mch_memmove(p, val, (size_t)len + 1);
6837 /* Put the separator after the NUL. */
6838 p[len + 1] = sep;
6839 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006840 viminfo_history[type][idx].hisnum = 0;
6841 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006842 viminfo_hisidx[type]++;
6843 }
6844 }
6845 }
6846 }
6847 }
6848}
6849
6850/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006851 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006852 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006853 static void
6854concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856 int idx;
6857 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006858
6859 idx = hisidx[type] + viminfo_hisidx[type];
6860 if (idx >= hislen)
6861 idx -= hislen;
6862 else if (idx < 0)
6863 idx = hislen - 1;
6864 if (viminfo_add_at_front)
6865 hisidx[type] = idx;
6866 else
6867 {
6868 if (hisidx[type] == -1)
6869 hisidx[type] = hislen - 1;
6870 do
6871 {
6872 if (history[type][idx].hisstr != NULL
6873 || history[type][idx].viminfo)
6874 break;
6875 if (++idx == hislen)
6876 idx = 0;
6877 } while (idx != hisidx[type]);
6878 if (idx != hisidx[type] && --idx < 0)
6879 idx = hislen - 1;
6880 }
6881 for (i = 0; i < viminfo_hisidx[type]; i++)
6882 {
6883 vim_free(history[type][idx].hisstr);
6884 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6885 history[type][idx].viminfo = TRUE;
6886 history[type][idx].time_set = viminfo_history[type][i].time_set;
6887 if (--idx < 0)
6888 idx = hislen - 1;
6889 }
6890 idx += 1;
6891 idx %= hislen;
6892 for (i = 0; i < viminfo_hisidx[type]; i++)
6893 {
6894 history[type][idx++].hisnum = ++hisnum[type];
6895 idx %= hislen;
6896 }
6897}
6898
6899#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6900 static int
6901#ifdef __BORLANDC__
6902_RTLENTRYF
6903#endif
6904sort_hist(const void *s1, const void *s2)
6905{
6906 histentry_T *p1 = *(histentry_T **)s1;
6907 histentry_T *p2 = *(histentry_T **)s2;
6908
6909 if (p1->time_set < p2->time_set) return -1;
6910 if (p1->time_set > p2->time_set) return 1;
6911 return 0;
6912}
6913#endif
6914
6915/*
6916 * Merge history lines from viminfo and lines typed in this Vim based on the
6917 * timestamp;
6918 */
6919 static void
6920merge_history(int type)
6921{
6922 int max_len;
6923 histentry_T **tot_hist;
6924 histentry_T *new_hist;
6925 int i;
6926 int len;
6927
6928 /* Make one long list with all entries. */
6929 max_len = hislen + viminfo_hisidx[type];
6930 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6931 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6932 if (tot_hist == NULL || new_hist == NULL)
6933 {
6934 vim_free(tot_hist);
6935 vim_free(new_hist);
6936 return;
6937 }
6938 for (i = 0; i < viminfo_hisidx[type]; i++)
6939 tot_hist[i] = &viminfo_history[type][i];
6940 len = i;
6941 for (i = 0; i < hislen; i++)
6942 if (history[type][i].hisstr != NULL)
6943 tot_hist[len++] = &history[type][i];
6944
6945 /* Sort the list on timestamp. */
6946 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6947
6948 /* Keep the newest ones. */
6949 for (i = 0; i < hislen; i++)
6950 {
6951 if (i < len)
6952 {
6953 new_hist[i] = *tot_hist[i];
6954 tot_hist[i]->hisstr = NULL;
6955 if (new_hist[i].hisnum == 0)
6956 new_hist[i].hisnum = ++hisnum[type];
6957 }
6958 else
6959 clear_hist_entry(&new_hist[i]);
6960 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006961 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006962
6963 /* Free what is not kept. */
6964 for (i = 0; i < viminfo_hisidx[type]; i++)
6965 vim_free(viminfo_history[type][i].hisstr);
6966 for (i = 0; i < hislen; i++)
6967 vim_free(history[type][i].hisstr);
6968 vim_free(history[type]);
6969 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006970 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006971}
6972
6973/*
6974 * Finish reading history lines from viminfo. Not used when writing viminfo.
6975 */
6976 void
6977finish_viminfo_history(vir_T *virp)
6978{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006980 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 for (type = 0; type < HIST_COUNT; ++type)
6983 {
6984 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006985 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006986
6987 if (merge)
6988 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006990 concat_history(type);
6991
Bram Moolenaard23a8232018-02-10 18:45:26 +01006992 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006993 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
6995}
6996
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006997/*
6998 * Write history to viminfo file in "fp".
6999 * When "merge" is TRUE merge history lines with a previously read viminfo
7000 * file, data is in viminfo_history[].
7001 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007004write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 int i;
7007 int type;
7008 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007009 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 init_history();
7012 if (hislen == 0)
7013 return;
7014 for (type = 0; type < HIST_COUNT; ++type)
7015 {
7016 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7017 if (num_saved == 0)
7018 continue;
7019 if (num_saved < 0) /* Use default */
7020 num_saved = hislen;
7021 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7022 type == HIST_CMD ? _("Command Line") :
7023 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007024 type == HIST_EXPR ? _("Expression") :
7025 type == HIST_INPUT ? _("Input Line") :
7026 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 if (num_saved > hislen)
7028 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007029
7030 /*
7031 * Merge typed and viminfo history:
7032 * round 1: history of typed commands.
7033 * round 2: history from recently read viminfo.
7034 */
7035 for (round = 1; round <= 2; ++round)
7036 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007037 if (round == 1)
7038 /* start at newest entry, somewhere in the list */
7039 i = hisidx[type];
7040 else if (viminfo_hisidx[type] > 0)
7041 /* start at newest entry, first in the list */
7042 i = 0;
7043 else
7044 /* empty list */
7045 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007046 if (i >= 0)
7047 while (num_saved > 0
7048 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007050 char_u *p;
7051 time_t timestamp;
7052 int c = NUL;
7053
7054 if (round == 1)
7055 {
7056 p = history[type][i].hisstr;
7057 timestamp = history[type][i].time_set;
7058 }
7059 else
7060 {
7061 p = viminfo_history[type] == NULL ? NULL
7062 : viminfo_history[type][i].hisstr;
7063 timestamp = viminfo_history[type] == NULL ? 0
7064 : viminfo_history[type][i].time_set;
7065 }
7066
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007067 if (p != NULL && (round == 2
7068 || !merge
7069 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007071 --num_saved;
7072 fputc(hist_type2char(type, TRUE), fp);
7073 /* For the search history: put the separator in the
7074 * second column; use a space if there isn't one. */
7075 if (type == HIST_SEARCH)
7076 {
7077 c = p[STRLEN(p) + 1];
7078 putc(c == NUL ? ' ' : c, fp);
7079 }
7080 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007081
7082 {
7083 char cbuf[NUMBUFLEN];
7084
7085 /* New style history with a bar line. Format:
7086 * |{bartype},{histtype},{timestamp},{separator},"text" */
7087 if (c == NUL)
7088 cbuf[0] = NUL;
7089 else
7090 sprintf(cbuf, "%d", c);
7091 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7092 type, (long)timestamp, cbuf);
7093 barline_writestring(fp, p, LSIZE - 20);
7094 putc('\n', fp);
7095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007097 if (round == 1)
7098 {
7099 /* Decrement index, loop around and stop when back at
7100 * the start. */
7101 if (--i < 0)
7102 i = hislen - 1;
7103 if (i == hisidx[type])
7104 break;
7105 }
7106 else
7107 {
7108 /* Increment index. Stop at the end in the while. */
7109 ++i;
7110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007112 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007113 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007114 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007115 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007116 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007117 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119}
7120#endif /* FEAT_VIMINFO */
7121
7122#if defined(FEAT_FKMAP) || defined(PROTO)
7123/*
7124 * Write a character at the current cursor+offset position.
7125 * It is directly written into the command buffer block.
7126 */
7127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007128cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7131 {
7132 EMSG(_("E198: cmd_pchar beyond the command length"));
7133 return;
7134 }
7135 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7136 ccline.cmdbuff[ccline.cmdlen] = NUL;
7137}
7138
7139 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007140cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
7142 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7143 {
7144 /* EMSG(_("cmd_gchar beyond the command length")); */
7145 return NUL;
7146 }
7147 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7148}
7149#endif
7150
7151#if defined(FEAT_CMDWIN) || defined(PROTO)
7152/*
7153 * Open a window on the current command line and history. Allow editing in
7154 * the window. Returns when the window is closed.
7155 * Returns:
7156 * CR if the command is to be executed
7157 * Ctrl_C if it is to be abandoned
7158 * K_IGNORE if editing continues
7159 */
7160 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007161open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007164 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007166 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 win_T *wp;
7168 int i;
7169 linenr_T lnum;
7170 int histtype;
7171 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 int save_restart_edit = restart_edit;
7173 int save_State = State;
7174 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007175#ifdef FEAT_RIGHTLEFT
7176 int save_cmdmsg_rl = cmdmsg_rl;
7177#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007178#ifdef FEAT_FOLDING
7179 int save_KeyTyped;
7180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
7182 /* Can't do this recursively. Can't do it when typing a password. */
7183 if (cmdwin_type != 0
7184# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7185 || cmdline_star > 0
7186# endif
7187 )
7188 {
7189 beep_flush();
7190 return K_IGNORE;
7191 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007192 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
7194 /* Save current window sizes. */
7195 win_size_save(&winsizes);
7196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007198 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007199
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007200 /* don't use a new tab page */
7201 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007202 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007203
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 /* Create a window for the command-line buffer. */
7205 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7206 {
7207 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007208 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 return K_IGNORE;
7210 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007211 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007214 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007215 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007218#ifdef FEAT_FOLDING
7219 curwin->w_p_fen = FALSE;
7220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007222 curwin->w_p_rl = cmdmsg_rl;
7223 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007225 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007228 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007229 /* But don't allow switching to another buffer. */
7230 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
Bram Moolenaar46152342005-09-07 21:18:43 +00007232 /* Showing the prompt may have set need_wait_return, reset it. */
7233 need_wait_return = FALSE;
7234
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007235 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7237 {
7238 if (p_wc == TAB)
7239 {
7240 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7241 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7242 }
7243 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7244 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007245 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007247 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7248 * sets 'textwidth' to 78). */
7249 curbuf->b_p_tw = 0;
7250
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 /* Fill the buffer with the history. */
7252 init_history();
7253 if (hislen > 0)
7254 {
7255 i = hisidx[histtype];
7256 if (i >= 0)
7257 {
7258 lnum = 0;
7259 do
7260 {
7261 if (++i == hislen)
7262 i = 0;
7263 if (history[histtype][i].hisstr != NULL)
7264 ml_append(lnum++, history[histtype][i].hisstr,
7265 (colnr_T)0, FALSE);
7266 }
7267 while (i != hisidx[histtype]);
7268 }
7269 }
7270
7271 /* Replace the empty last line with the current command-line and put the
7272 * cursor there. */
7273 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7274 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7275 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007276 changed_line_abv_curs();
7277 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007278 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279
7280 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007281 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 /* No Ex mode here! */
7284 exmode_active = 0;
7285
7286 State = NORMAL;
7287# ifdef FEAT_MOUSE
7288 setmouse();
7289# endif
7290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007292 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007293 if (restart_edit != 0) /* autocmd with ":startinsert" */
7294 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
7296 i = RedrawingDisabled;
7297 RedrawingDisabled = 0;
7298
7299 /*
7300 * Call the main loop until <CR> or CTRL-C is typed.
7301 */
7302 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007303 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
7305 RedrawingDisabled = i;
7306
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007307# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007308 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007309# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007310
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007312 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007313
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007314# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007315 /* Restore KeyTyped in case it is modified by autocommands */
7316 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317# endif
7318
Bram Moolenaarccc18222007-05-10 18:25:20 +00007319 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007320 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 cmdwin_type = 0;
7322
7323 exmode_active = save_exmode;
7324
Bram Moolenaarf9821062008-06-20 16:31:07 +00007325 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007327 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 {
7329 cmdwin_result = Ctrl_C;
7330 EMSG(_("E199: Active window or buffer deleted"));
7331 }
7332 else
7333 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007334# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 /* autocmds may abort script processing */
7336 if (aborting() && cmdwin_result != K_IGNORE)
7337 cmdwin_result = Ctrl_C;
7338# endif
7339 /* Set the new command line from the cmdline buffer. */
7340 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007341 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007343 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7344
7345 if (histtype == HIST_CMD)
7346 {
7347 /* Execute the command directly. */
7348 ccline.cmdbuff = vim_strsave((char_u *)p);
7349 cmdwin_result = CAR;
7350 }
7351 else
7352 {
7353 /* First need to cancel what we were doing. */
7354 ccline.cmdbuff = NULL;
7355 stuffcharReadbuff(':');
7356 stuffReadbuff((char_u *)p);
7357 stuffcharReadbuff(CAR);
7358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 }
7360 else if (cmdwin_result == K_XF2) /* :qa typed */
7361 {
7362 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7363 cmdwin_result = CAR;
7364 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007365 else if (cmdwin_result == Ctrl_C)
7366 {
7367 /* :q or :close, don't execute any command
7368 * and don't modify the cmd window. */
7369 ccline.cmdbuff = NULL;
7370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 else
7372 ccline.cmdbuff = vim_strsave(ml_get_curline());
7373 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007374 {
7375 ccline.cmdbuff = vim_strsave((char_u *)"");
7376 ccline.cmdlen = 0;
7377 ccline.cmdbufflen = 1;
7378 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 else
7382 {
7383 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7384 ccline.cmdbufflen = ccline.cmdlen + 1;
7385 ccline.cmdpos = curwin->w_cursor.col;
7386 if (ccline.cmdpos > ccline.cmdlen)
7387 ccline.cmdpos = ccline.cmdlen;
7388 if (cmdwin_result == K_IGNORE)
7389 {
7390 set_cmdspos_cursor();
7391 redrawcmd();
7392 }
7393 }
7394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007396 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007397# ifdef FEAT_CONCEAL
7398 /* Avoid command-line window first character being concealed. */
7399 curwin->w_p_cole = 0;
7400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007402 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 win_goto(old_curwin);
7404 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007405
7406 /* win_close() may have already wiped the buffer when 'bh' is
7407 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007408 if (bufref_valid(&bufref))
7409 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
7411 /* Restore window sizes. */
7412 win_size_restore(&winsizes);
7413
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007414 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416
7417 ga_clear(&winsizes);
7418 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007419# ifdef FEAT_RIGHTLEFT
7420 cmdmsg_rl = save_cmdmsg_rl;
7421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 State = save_State;
7424# ifdef FEAT_MOUSE
7425 setmouse();
7426# endif
7427
7428 return cmdwin_result;
7429}
7430#endif /* FEAT_CMDWIN */
7431
7432/*
7433 * Used for commands that either take a simple command string argument, or:
7434 * cmd << endmarker
7435 * {script}
7436 * endmarker
7437 * Returns a pointer to allocated memory with {script} or NULL.
7438 */
7439 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007440script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 char_u *theline;
7443 char *end_pattern = NULL;
7444 char dot[] = ".";
7445 garray_T ga;
7446
7447 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7448 return NULL;
7449
7450 ga_init2(&ga, 1, 0x400);
7451
7452 if (cmd[2] != NUL)
7453 end_pattern = (char *)skipwhite(cmd + 2);
7454 else
7455 end_pattern = dot;
7456
7457 for (;;)
7458 {
7459 theline = eap->getline(
7460#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007461 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462#endif
7463 NUL, eap->cookie, 0);
7464
7465 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007466 {
7467 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
7471 ga_concat(&ga, theline);
7472 ga_append(&ga, '\n');
7473 vim_free(theline);
7474 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007475 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
7477 return (char_u *)ga.ga_data;
7478}