blob: 31e646dd353eaaccc700a4b6f9e932f0fecca7d0 [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
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020/*
21 * Variables shared between getcmdline(), redrawcmdline() and others.
22 * These need to be saved when using CTRL-R |, that's why they are in a
23 * structure.
24 */
25struct cmdline_info
26{
27 char_u *cmdbuff; /* pointer to command line buffer */
28 int cmdbufflen; /* length of cmdbuff */
29 int cmdlen; /* number of chars in command line */
30 int cmdpos; /* current cursor position */
31 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020032 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 int cmdindent; /* number of spaces before cmdline */
34 char_u *cmdprompt; /* message in front of cmdline */
35 int cmdattr; /* attributes for prompt */
36 int overstrike; /* Typing mode on the command line. Shared by
37 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000038 expand_T *xpc; /* struct being used for expansion, xp_pattern
39 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040 int xp_context; /* type of expansion */
41# ifdef FEAT_EVAL
42 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000043 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
Bram Moolenaar438d1762018-09-30 17:11:48 +020047// The current cmdline_info. It is initialized in getcmdline() and after that
48// used by other functions. When invoking getcmdline() recursively it needs
49// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000050static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar438d1762018-09-30 17:11:48 +020052static int cmd_showtail; /* Only show path tail in lists ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef FEAT_EVAL
55static int new_cmdpos; /* position set by set_cmdline_pos() */
56#endif
57
Bram Moolenaara92522f2017-07-15 15:21:38 +020058static int extra_char = NUL; /* extra character to display when redrawing
59 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020060static int extra_char_shift;
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_CMDHIST
63typedef struct hist_entry
64{
65 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020066 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020068 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069} histentry_T;
70
71static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
72static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
73static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
74 /* identifying (unique) number of newest history entry */
75static int hislen = 0; /* actual length of history tables */
76
Bram Moolenaard25c16e2016-01-29 22:13:30 +010077static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
80#ifdef FEAT_RIGHTLEFT
81static int cmd_hkmap = 0; /* Hebrew mapping during command line */
82#endif
83
84#ifdef FEAT_FKMAP
85static int cmd_fkmap = 0; /* Farsi mapping during command line */
86#endif
87
Bram Moolenaar438d1762018-09-30 17:11:48 +020088static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static int cmdline_charsize(int idx);
90static void set_cmdspos(void);
91static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void alloc_cmdbuff(int len);
96static int realloc_cmdbuff(int len);
97static void draw_cmdline(int start, int len);
98static void save_cmdline(struct cmdline_info *ccp);
99static void restore_cmdline(struct cmdline_info *ccp);
100static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100104static void redrawcmdprompt(void);
105static void cursorcmd(void);
106static int ccheck_abbr(int);
107static int nextwild(expand_T *xp, int type, int options, int escape);
108static void escape_fname(char_u **pp);
109static int showmatches(expand_T *xp, int wildmenu);
110static void set_expand_context(expand_T *xp);
111static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
112static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100114static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100115static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100116static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200117# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100121static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
122static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123# endif
124#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200125#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200130static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
134static int
135#ifdef __BORLANDC__
136_RTLENTRYF
137#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100138sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200139#endif
140
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200141
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200142 static void
143trigger_cmd_autocmd(int typechar, int evt)
144{
145 char_u typestr[2];
146
147 typestr[0] = typechar;
148 typestr[1] = NUL;
149 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
150}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200153 * Abandon the command line.
154 */
155 static void
156abandon_cmdline(void)
157{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100158 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200159 if (msg_scrolled == 0)
160 compute_cmdrow();
161 MSG("");
162 redraw_cmdline = TRUE;
163}
164
Bram Moolenaaree219b02017-12-17 14:55:01 +0100165#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200166/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100167 * Guess that the pattern matches everything. Only finds specific cases, such
168 * as a trailing \|, which can happen while typing a pattern.
169 */
170 static int
171empty_pattern(char_u *p)
172{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100173 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100174
175 /* remove trailing \v and the like */
176 while (n >= 2 && p[n - 2] == '\\'
177 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
178 n -= 2;
179 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
180}
181
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200182// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200183typedef struct {
184 colnr_T vs_curswant;
185 colnr_T vs_leftcol;
186 linenr_T vs_topline;
187# ifdef FEAT_DIFF
188 int vs_topfill;
189# endif
190 linenr_T vs_botline;
191 linenr_T vs_empty_rows;
192} viewstate_T;
193
194 static void
195save_viewstate(viewstate_T *vs)
196{
197 vs->vs_curswant = curwin->w_curswant;
198 vs->vs_leftcol = curwin->w_leftcol;
199 vs->vs_topline = curwin->w_topline;
200# ifdef FEAT_DIFF
201 vs->vs_topfill = curwin->w_topfill;
202# endif
203 vs->vs_botline = curwin->w_botline;
204 vs->vs_empty_rows = curwin->w_empty_rows;
205}
206
207 static void
208restore_viewstate(viewstate_T *vs)
209{
210 curwin->w_curswant = vs->vs_curswant;
211 curwin->w_leftcol = vs->vs_leftcol;
212 curwin->w_topline = vs->vs_topline;
213# ifdef FEAT_DIFF
214 curwin->w_topfill = vs->vs_topfill;
215# endif
216 curwin->w_botline = vs->vs_botline;
217 curwin->w_empty_rows = vs->vs_empty_rows;
218}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200219
220// Struct to store the state of 'incsearch' highlighting.
221typedef struct {
222 pos_T search_start; // where 'incsearch' starts searching
223 pos_T save_cursor;
224 viewstate_T init_viewstate;
225 viewstate_T old_viewstate;
226 pos_T match_start;
227 pos_T match_end;
228 int did_incsearch;
229 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200230 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200231} incsearch_state_T;
232
233 static void
234init_incsearch_state(incsearch_state_T *is_state)
235{
236 is_state->match_start = curwin->w_cursor;
237 is_state->did_incsearch = FALSE;
238 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200239 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200240 CLEAR_POS(&is_state->match_end);
241 is_state->save_cursor = curwin->w_cursor; // may be restored later
242 is_state->search_start = curwin->w_cursor;
243 save_viewstate(&is_state->init_viewstate);
244 save_viewstate(&is_state->old_viewstate);
245}
246
247/*
248 * First move cursor to end of match, then to the start. This
249 * moves the whole match onto the screen when 'nowrap' is set.
250 */
251 static void
252set_search_match(pos_T *t)
253{
254 t->lnum += search_match_lines;
255 t->col = search_match_endcol;
256 if (t->lnum > curbuf->b_ml.ml_line_count)
257 {
258 t->lnum = curbuf->b_ml.ml_line_count;
259 coladvance((colnr_T)MAXCOL);
260 }
261}
262
263/*
264 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200265 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200266 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200267 */
268 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200269do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
270 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200271{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200272 char_u *cmd;
273 cmdmod_T save_cmdmod = cmdmod;
274 char_u *p;
275 int delim_optional = FALSE;
276 int delim;
277 char_u *end;
278 char_u *dummy;
279 exarg_T ea;
280 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200281 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200282
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200283 *skiplen = 0;
284 *patlen = ccline.cmdlen;
285
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200286 if (!p_is || cmd_silent)
287 return FALSE;
288
289 // by default search all lines
290 search_first_line = 0;
291 search_last_line = MAXLNUM;
292
293 if (firstc == '/' || firstc == '?')
294 return TRUE;
295 if (firstc != ':')
296 return FALSE;
297
298 vim_memset(&ea, 0, sizeof(ea));
299 ea.line1 = 1;
300 ea.line2 = 1;
301 ea.cmd = ccline.cmdbuff;
302 ea.addr_type = ADDR_LINES;
303
304 parse_command_modifiers(&ea, &dummy, TRUE);
305 cmdmod = save_cmdmod;
306
307 cmd = skip_range(ea.cmd, NULL);
308 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
309 return FALSE;
310
311 // Skip over "substitute" to find the pattern separator.
312 for (p = cmd; ASCII_ISALPHA(*p); ++p)
313 ;
314 if (*skipwhite(p) == NUL)
315 return FALSE;
316
317 if (STRNCMP(cmd, "substitute", p - cmd) == 0
318 || STRNCMP(cmd, "smagic", p - cmd) == 0
319 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
320 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200321 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200322 if (*cmd == 's' && cmd[1] == 'm')
323 p_magic = TRUE;
324 else if (*cmd == 's' && cmd[1] == 'n')
325 p_magic = FALSE;
326 }
327 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
328 {
329 // skip over flags
330 while (ASCII_ISALPHA(*(p = skipwhite(p))))
331 ++p;
332 if (*p == NUL)
333 return FALSE;
334 }
335 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
336 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
337 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
338 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
339 || STRNCMP(cmd, "global", p - cmd) == 0)
340 {
341 // skip over "!"
342 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200343 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200344 p++;
345 if (*skipwhite(p) == NUL)
346 return FALSE;
347 }
348 if (*cmd != 'g')
349 delim_optional = TRUE;
350 }
351 else
352 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200353
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200354 p = skipwhite(p);
355 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
356 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200357
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200358 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200359
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200360 if (end == p && !use_last_pat)
361 return FALSE;
362
363 // Don't do 'hlsearch' highlighting if the pattern matches everything.
364 if (!use_last_pat)
365 {
366 char c = *end;
367 int empty;
368
369 *end = NUL;
370 empty = empty_pattern(p);
371 *end = c;
372 if (empty)
373 return FALSE;
374 }
375
376 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200377 *skiplen = (int)(p - ccline.cmdbuff);
378 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200379
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200380 // parse the address range
381 save_cursor = curwin->w_cursor;
382 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200383 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200384 if (ea.addr_count > 0)
385 {
386 // Allow for reverse match.
387 if (ea.line2 < ea.line1)
388 {
389 search_first_line = ea.line2;
390 search_last_line = ea.line1;
391 }
392 else
393 {
394 search_first_line = ea.line1;
395 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200396 }
397 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200398 else if (cmd[0] == 's' && cmd[1] != 'o')
399 {
400 // :s defaults to the current line
401 search_first_line = curwin->w_cursor.lnum;
402 search_last_line = curwin->w_cursor.lnum;
403 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200405 curwin->w_cursor = save_cursor;
406 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407}
408
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200409 static void
410finish_incsearch_highlighting(
411 int gotesc,
412 incsearch_state_T *is_state,
413 int call_update_screen)
414{
415 if (is_state->did_incsearch)
416 {
417 is_state->did_incsearch = FALSE;
418 if (gotesc)
419 curwin->w_cursor = is_state->save_cursor;
420 else
421 {
422 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
423 {
424 // put the '" mark at the original position
425 curwin->w_cursor = is_state->save_cursor;
426 setpcmark();
427 }
428 curwin->w_cursor = is_state->search_start;
429 }
430 restore_viewstate(&is_state->old_viewstate);
431 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200432
433 // by default search all lines
434 search_first_line = 0;
435 search_last_line = MAXLNUM;
436
437 p_magic = is_state->magic_save;
438
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200439 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200440 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200441 if (call_update_screen)
442 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200443 }
444}
445
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200446/*
447 * Do 'incsearch' highlighting if desired.
448 */
449 static void
450may_do_incsearch_highlighting(
451 int firstc,
452 long count,
453 incsearch_state_T *is_state)
454{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200455 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200456 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200458#ifdef FEAT_RELTIME
459 proftime_T tm;
460#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200461 int next_char;
462 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200463
Bram Moolenaar198cb662018-09-06 21:44:17 +0200464 // Parsing range may already set the last search pattern.
465 save_last_search_pattern();
466
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200467 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200468 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200469 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200470 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200471 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200472 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473
474 // If there is a character waiting, search and redraw later.
475 if (char_avail())
476 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200477 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200478 is_state->incsearch_postponed = TRUE;
479 return;
480 }
481 is_state->incsearch_postponed = FALSE;
482
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200483 if (search_first_line == 0)
484 // start at the original cursor position
485 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100486 else if (search_first_line > curbuf->b_ml.ml_line_count)
487 {
488 // start after the last line
489 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
490 curwin->w_cursor.col = MAXCOL;
491 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200492 else
493 {
494 // start at the first line in the range
495 curwin->w_cursor.lnum = search_first_line;
496 curwin->w_cursor.col = 0;
497 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200498
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200499 // Use the previous pattern for ":s//".
500 next_char = ccline.cmdbuff[skiplen + patlen];
501 use_last_pat = patlen == 0 && skiplen > 0
502 && ccline.cmdbuff[skiplen - 1] == next_char;
503
504 // If there is no pattern, don't do anything.
505 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200506 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200507 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200508 set_no_hlsearch(TRUE); // turn off previous highlight
509 redraw_all_later(SOME_VALID);
510 }
511 else
512 {
513 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
514
515 cursor_off(); // so the user knows we're busy
516 out_flush();
517 ++emsg_off; // so it doesn't beep if bad expr
518#ifdef FEAT_RELTIME
519 // Set the time limit to half a second.
520 profile_setlimit(500L, &tm);
521#endif
522 if (!p_hls)
523 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200524 if (search_first_line != 0)
525 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200526 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200527 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200528 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529#ifdef FEAT_RELTIME
530 &tm, NULL
531#else
532 NULL, NULL
533#endif
534 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200535 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200536 --emsg_off;
537
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200538 if (curwin->w_cursor.lnum < search_first_line
539 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200540 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200541 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200542 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200543 curwin->w_cursor = is_state->search_start;
544 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200545
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200546 // if interrupted while searching, behave like it failed
547 if (got_int)
548 {
549 (void)vpeekc(); // remove <C-C> from input stream
550 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200551 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200552 }
553 else if (char_avail())
554 // cancelled searching because a char was typed
555 is_state->incsearch_postponed = TRUE;
556 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200557 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200558 highlight_match = TRUE; // highlight position
559 else
560 highlight_match = FALSE; // remove highlight
561
562 // First restore the old curwin values, so the screen is positioned in the
563 // same way as the actual search command.
564 restore_viewstate(&is_state->old_viewstate);
565 changed_cline_bef_curs();
566 update_topline();
567
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200568 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200569 {
570 pos_T save_pos = curwin->w_cursor;
571
572 is_state->match_start = curwin->w_cursor;
573 set_search_match(&curwin->w_cursor);
574 validate_cursor();
575 end_pos = curwin->w_cursor;
576 is_state->match_end = end_pos;
577 curwin->w_cursor = save_pos;
578 }
579 else
580 end_pos = curwin->w_cursor; // shutup gcc 4
581
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200582 // Disable 'hlsearch' highlighting if the pattern matches everything.
583 // Avoids a flash when typing "foo\|".
584 if (!use_last_pat)
585 {
586 next_char = ccline.cmdbuff[skiplen + patlen];
587 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200588 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
589 {
590 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200591 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200592 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200593 ccline.cmdbuff[skiplen + patlen] = next_char;
594 }
595
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 validate_cursor();
597 // May redraw the status line to show the cursor position.
598 if (p_ru && curwin->w_status_height > 0)
599 curwin->w_redr_status = TRUE;
600
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200601 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 restore_last_search_pattern();
603
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200604 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
605 // end of the pattern, e.g. for ":s/pat/".
606 if (ccline.cmdbuff[skiplen + patlen] != NUL)
607 curwin->w_cursor = is_state->search_start;
608 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200609 curwin->w_cursor = end_pos;
610
611 msg_starthere();
612 redrawcmdline();
613 is_state->did_incsearch = TRUE;
614}
615
616/*
617 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
618 * or previous match.
619 * Returns FAIL when jumping to cmdline_not_changed;
620 */
621 static int
622may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200623 int firstc,
624 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200625 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200626 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200627{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629 pos_T t;
630 char_u *pat;
631 int search_flags = SEARCH_NOOF;
632 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200633 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200634
Bram Moolenaar198cb662018-09-06 21:44:17 +0200635 // Parsing range may already set the last search pattern.
636 save_last_search_pattern();
637
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200638 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200639 {
640 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200641 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200642 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200643 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200644 {
645 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200646 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200647 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200648
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200649 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200650 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200652 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200653 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200654 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200655 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200656 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200657
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200658 cursor_off();
659 out_flush();
660 if (c == Ctrl_G)
661 {
662 t = is_state->match_end;
663 if (LT_POS(is_state->match_start, is_state->match_end))
664 // Start searching at the end of the match not at the beginning of
665 // the next column.
666 (void)decl(&t);
667 search_flags += SEARCH_COL;
668 }
669 else
670 t = is_state->match_start;
671 if (!p_hls)
672 search_flags += SEARCH_KEEP;
673 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200674 save = pat[patlen];
675 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200676 i = searchit(curwin, curbuf, &t,
677 c == Ctrl_G ? FORWARD : BACKWARD,
678 pat, count, search_flags,
679 RE_SEARCH, 0, NULL, NULL);
680 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200681 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200682 if (i)
683 {
684 is_state->search_start = is_state->match_start;
685 is_state->match_end = t;
686 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200687 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200688 {
689 // Move just before the current match, so that when nv_search
690 // finishes the cursor will be put back on the match.
691 is_state->search_start = t;
692 (void)decl(&is_state->search_start);
693 }
694 else if (c == Ctrl_G && firstc == '?')
695 {
696 // Move just after the current match, so that when nv_search
697 // finishes the cursor will be put back on the match.
698 is_state->search_start = t;
699 (void)incl(&is_state->search_start);
700 }
701 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
702 {
703 // wrap around
704 is_state->search_start = t;
705 if (firstc == '?')
706 (void)incl(&is_state->search_start);
707 else
708 (void)decl(&is_state->search_start);
709 }
710
711 set_search_match(&is_state->match_end);
712 curwin->w_cursor = is_state->match_start;
713 changed_cline_bef_curs();
714 update_topline();
715 validate_cursor();
716 highlight_match = TRUE;
717 save_viewstate(&is_state->old_viewstate);
718 update_screen(NOT_VALID);
719 redrawcmdline();
720 }
721 else
722 vim_beep(BO_ERROR);
723 restore_last_search_pattern();
724 return FAIL;
725}
726
727/*
728 * When CTRL-L typed: add character from the match to the pattern.
729 * May set "*c" to the added character.
730 * Return OK when jumping to cmdline_not_changed.
731 */
732 static int
733may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
734{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200735 int skiplen, patlen;
736
Bram Moolenaar198cb662018-09-06 21:44:17 +0200737 // Parsing range may already set the last search pattern.
738 save_last_search_pattern();
739
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200740 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200741 {
742 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200743 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200744 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200745
746 // Add a character from under the cursor for 'incsearch'.
747 if (is_state->did_incsearch)
748 {
749 curwin->w_cursor = is_state->match_end;
750 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
751 {
752 *c = gchar_cursor();
753
754 // If 'ignorecase' and 'smartcase' are set and the
755 // command line has no uppercase characters, convert
756 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200757 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200758 *c = MB_TOLOWER(*c);
759 if (*c != NUL)
760 {
761 if (*c == firstc || vim_strchr((char_u *)(
762 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
763 {
764 // put a backslash before special characters
765 stuffcharReadbuff(*c);
766 *c = '\\';
767 }
768 return FAIL;
769 }
770 }
771 }
772 return OK;
773}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200774#endif
775
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200776 void
777cmdline_init(void)
778{
779 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
780}
781
Bram Moolenaar66216052017-12-16 16:33:44 +0100782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * getcmdline() - accept a command line starting with firstc.
784 *
785 * firstc == ':' get ":" command line.
786 * firstc == '/' or '?' get search pattern
787 * firstc == '=' get expression
788 * firstc == '@' get text for input() function
789 * firstc == '>' get text for debug mode
790 * firstc == NUL get text for :insert command
791 * firstc == -1 like NUL, and break on CTRL-C
792 *
793 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
794 * command line.
795 *
796 * Careful: getcmdline() can be called recursively!
797 *
798 * Return pointer to allocated string if there is a commandline, NULL
799 * otherwise.
800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100802getcmdline(
803 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200804 long count, // only used for incremental search
805 int indent) // indent for inside conditionals
806{
807 return getcmdline_int(firstc, count, indent, TRUE);
808}
809
810 static char_u *
811getcmdline_int(
812 int firstc,
813 long count UNUSED, // only used for incremental search
814 int indent, // indent for inside conditionals
815 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 int c;
818 int i;
819 int j;
820 int gotesc = FALSE; /* TRUE when <ESC> just typed */
821 int do_abbr; /* when TRUE check for abbr. */
822#ifdef FEAT_CMDHIST
823 char_u *lookfor = NULL; /* string to match */
824 int hiscnt; /* current history line in use */
825 int histype; /* history type to be used */
826#endif
827#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200828 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829#endif
830 int did_wild_list = FALSE; /* did wild_list() recently */
831 int wim_index = 0; /* index in wim_flags[] */
832 int res;
833 int save_msg_scroll = msg_scroll;
834 int save_State = State; /* remember State when called */
835 int some_key_typed = FALSE; /* one of the keys was typed */
836#ifdef FEAT_MOUSE
837 /* mouse drag and release events are ignored, unless they are
838 * preceded with a mouse down event */
839 int ignore_drag_release = TRUE;
840#endif
841#ifdef FEAT_EVAL
842 int break_ctrl_c = FALSE;
843#endif
844 expand_T xpc;
845 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000846 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200847 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200848 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar438d1762018-09-30 17:11:48 +0200850 if (ccline.cmdbuff != NULL)
851 {
852 // Being called recursively. Since ccline is global, we need to save
853 // the current buffer and restore it when returning.
854 save_cmdline(&save_ccline);
855 did_save_ccline = TRUE;
856 }
857 if (init_ccline)
858 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#ifdef FEAT_EVAL
861 if (firstc == -1)
862 {
863 firstc = NUL;
864 break_ctrl_c = TRUE;
865 }
866#endif
867#ifdef FEAT_RIGHTLEFT
868 /* start without Hebrew mapping for a command line */
869 if (firstc == ':' || firstc == '=' || firstc == '>')
870 cmd_hkmap = 0;
871#endif
872
873 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200876 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#endif
878
879 /*
880 * set some variables for redrawcmd()
881 */
882 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000883 ccline.cmdindent = (firstc > 0 ? indent : 0);
884
885 /* alloc initial ccline.cmdbuff */
886 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200888 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 ccline.cmdlen = ccline.cmdpos = 0;
890 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100891 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000893 /* autoindent for :insert and :append */
894 if (firstc <= 0)
895 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200896 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000897 ccline.cmdbuff[indent] = NUL;
898 ccline.cmdpos = indent;
899 ccline.cmdspos = indent;
900 ccline.cmdlen = indent;
901 }
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000904 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
906#ifdef FEAT_RIGHTLEFT
907 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
908 && (firstc == '/' || firstc == '?'))
909 cmdmsg_rl = TRUE;
910 else
911 cmdmsg_rl = FALSE;
912#endif
913
914 redir_off = TRUE; /* don't redirect the typed command */
915 if (!cmd_silent)
916 {
917 i = msg_scrolled;
918 msg_scrolled = 0; /* avoid wait_return message */
919 gotocmdline(TRUE);
920 msg_scrolled += i;
921 redrawcmdprompt(); /* draw prompt or indent */
922 set_cmdspos();
923 }
924 xpc.xp_context = EXPAND_NOTHING;
925 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000926#ifndef BACKSLASH_IN_FILENAME
927 xpc.xp_shell = FALSE;
928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000930#if defined(FEAT_EVAL)
931 if (ccline.input_fn)
932 {
933 xpc.xp_context = ccline.xp_context;
934 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000935# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000936 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000937# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000938 }
939#endif
940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 /*
942 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
943 * doing ":@0" when register 0 doesn't contain a CR.
944 */
945 msg_scroll = FALSE;
946
947 State = CMDLINE;
948
949 if (firstc == '/' || firstc == '?' || firstc == '@')
950 {
951 /* Use ":lmap" mappings for search pattern and input(). */
952 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
953 b_im_ptr = &curbuf->b_p_iminsert;
954 else
955 b_im_ptr = &curbuf->b_p_imsearch;
956 if (*b_im_ptr == B_IMODE_LMAP)
957 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100958#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 im_set_active(*b_im_ptr == B_IMODE_IM);
960#endif
961 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100962#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 else if (p_imcmdline)
964 im_set_active(TRUE);
965#endif
966
967#ifdef FEAT_MOUSE
968 setmouse();
969#endif
970#ifdef CURSOR_SHAPE
971 ui_cursor_shape(); /* may show different cursor shape */
972#endif
973
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000974 /* When inside an autocommand for writing "exiting" may be set and
975 * terminal mode set to cooked. Need to set raw mode here then. */
976 settmode(TMODE_RAW);
977
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200978 /* Trigger CmdlineEnter autocommands. */
979 cmdline_type = firstc == NUL ? '-' : firstc;
980 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#ifdef FEAT_CMDHIST
983 init_history();
984 hiscnt = hislen; /* set hiscnt to impossible history value */
985 histype = hist_char2type(firstc);
986#endif
987
988#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000989 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
991
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200992 /* If something above caused an error, reset the flags, we do want to type
993 * and execute commands. Display may be messed up a bit. */
994 if (did_emsg)
995 redrawcmd();
996 did_emsg = FALSE;
997 got_int = FALSE;
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * Collect the command string, handling editing keys.
1001 */
1002 for (;;)
1003 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001004 redir_off = TRUE; /* Don't redirect the typed command.
1005 Repeated, because a ":redir" inside
1006 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#ifdef USE_ON_FLY_SCROLL
1008 dont_scroll = FALSE; /* allow scrolling here */
1009#endif
1010 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1011
Bram Moolenaar72532d32018-04-07 19:09:09 +02001012 did_emsg = FALSE; /* There can't really be a reason why an error
1013 that occurs while typing a command should
1014 cause the command not to be executed. */
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001017
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001018 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1019 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001020 do
1021 {
1022 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001023 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (KeyTyped)
1026 {
1027 some_key_typed = TRUE;
1028#ifdef FEAT_RIGHTLEFT
1029 if (cmd_hkmap)
1030 c = hkmap(c);
1031# ifdef FEAT_FKMAP
1032 if (cmd_fkmap)
1033 c = cmdl_fkmap(c);
1034# endif
1035 if (cmdmsg_rl && !KeyStuffed)
1036 {
1037 /* Invert horizontal movements and operations. Only when
1038 * typed by the user directly, not when the result of a
1039 * mapping. */
1040 switch (c)
1041 {
1042 case K_RIGHT: c = K_LEFT; break;
1043 case K_S_RIGHT: c = K_S_LEFT; break;
1044 case K_C_RIGHT: c = K_C_LEFT; break;
1045 case K_LEFT: c = K_RIGHT; break;
1046 case K_S_LEFT: c = K_S_RIGHT; break;
1047 case K_C_LEFT: c = K_C_RIGHT; break;
1048 }
1049 }
1050#endif
1051 }
1052
1053 /*
1054 * Ignore got_int when CTRL-C was typed here.
1055 * Don't ignore it in :global, we really need to break then, e.g., for
1056 * ":g/pat/normal /pat" (without the <CR>).
1057 * Don't ignore it for the input() function.
1058 */
1059 if ((c == Ctrl_C
1060#ifdef UNIX
1061 || c == intr_char
1062#endif
1063 )
1064#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1065 && firstc != '@'
1066#endif
1067#ifdef FEAT_EVAL
1068 && !break_ctrl_c
1069#endif
1070 && !global_busy)
1071 got_int = FALSE;
1072
1073#ifdef FEAT_CMDHIST
1074 /* free old command line when finished moving around in the history
1075 * list */
1076 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001077 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001078 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 && c != K_PAGEDOWN && c != K_PAGEUP
1080 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001081 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001083 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#endif
1085
1086 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001087 * When there are matching completions to select <S-Tab> works like
1088 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001090 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 c = Ctrl_P;
1092
1093#ifdef FEAT_WILDMENU
1094 /* Special translations for 'wildmenu' */
1095 if (did_wild_list && p_wmnu)
1096 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001097 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001099 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 c = Ctrl_N;
1101 }
1102 /* Hitting CR after "emenu Name.": complete submenu */
1103 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1104 && ccline.cmdpos > 1
1105 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1106 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1107 && (c == '\n' || c == '\r' || c == K_KENTER))
1108 c = K_DOWN;
1109#endif
1110
1111 /* free expanded names when finished walking through matches */
1112 if (xpc.xp_numfiles != -1
1113 && !(c == p_wc && KeyTyped) && c != p_wcm
1114 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1115 && c != Ctrl_L)
1116 {
1117 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1118 did_wild_list = FALSE;
1119#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001120 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#endif
1122 xpc.xp_context = EXPAND_NOTHING;
1123 wim_index = 0;
1124#ifdef FEAT_WILDMENU
1125 if (p_wmnu && wild_menu_showing != 0)
1126 {
1127 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001128 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001129
1130 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001131 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
1133 if (wild_menu_showing == WM_SCROLLED)
1134 {
1135 /* Entered command line, move it up */
1136 cmdline_row--;
1137 redrawcmd();
1138 }
1139 else if (save_p_ls != -1)
1140 {
1141 /* restore 'laststatus' and 'winminheight' */
1142 p_ls = save_p_ls;
1143 p_wmh = save_p_wmh;
1144 last_status(FALSE);
1145 update_screen(VALID); /* redraw the screen NOW */
1146 redrawcmd();
1147 save_p_ls = -1;
1148 }
1149 else
1150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 redraw_statuslines();
1153 }
1154 KeyTyped = skt;
1155 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001156 if (ccline.input_fn)
1157 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159#endif
1160 }
1161
1162#ifdef FEAT_WILDMENU
1163 /* Special translations for 'wildmenu' */
1164 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1165 {
1166 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001167 if (c == K_DOWN && ccline.cmdpos > 0
1168 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001170 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 /* Hitting <Up>: Remove one submenu name in front of the
1173 * cursor */
1174 int found = FALSE;
1175
1176 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1177 i = 0;
1178 while (--j > 0)
1179 {
1180 /* check for start of menu name */
1181 if (ccline.cmdbuff[j] == ' '
1182 && ccline.cmdbuff[j - 1] != '\\')
1183 {
1184 i = j + 1;
1185 break;
1186 }
1187 /* check for start of submenu name */
1188 if (ccline.cmdbuff[j] == '.'
1189 && ccline.cmdbuff[j - 1] != '\\')
1190 {
1191 if (found)
1192 {
1193 i = j + 1;
1194 break;
1195 }
1196 else
1197 found = TRUE;
1198 }
1199 }
1200 if (i > 0)
1201 cmdline_del(i);
1202 c = p_wc;
1203 xpc.xp_context = EXPAND_NOTHING;
1204 }
1205 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001206 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001207 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001208 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
1210 char_u upseg[5];
1211
1212 upseg[0] = PATHSEP;
1213 upseg[1] = '.';
1214 upseg[2] = '.';
1215 upseg[3] = PATHSEP;
1216 upseg[4] = NUL;
1217
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001218 if (c == K_DOWN
1219 && ccline.cmdpos > 0
1220 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1221 && (ccline.cmdpos < 3
1222 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1224 {
1225 /* go down a directory */
1226 c = p_wc;
1227 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001228 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
1230 /* If in a direct ancestor, strip off one ../ to go down */
1231 int found = FALSE;
1232
1233 j = ccline.cmdpos;
1234 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1235 while (--j > i)
1236 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001237#ifdef FEAT_MBYTE
1238 if (has_mbyte)
1239 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (vim_ispathsep(ccline.cmdbuff[j]))
1242 {
1243 found = TRUE;
1244 break;
1245 }
1246 }
1247 if (found
1248 && ccline.cmdbuff[j - 1] == '.'
1249 && ccline.cmdbuff[j - 2] == '.'
1250 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1251 {
1252 cmdline_del(j - 2);
1253 c = p_wc;
1254 }
1255 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001256 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 /* go up a directory */
1259 int found = FALSE;
1260
1261 j = ccline.cmdpos - 1;
1262 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1263 while (--j > i)
1264 {
1265#ifdef FEAT_MBYTE
1266 if (has_mbyte)
1267 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1268#endif
1269 if (vim_ispathsep(ccline.cmdbuff[j])
1270#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001271 && vim_strchr((char_u *)" *?[{`$%#",
1272 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#endif
1274 )
1275 {
1276 if (found)
1277 {
1278 i = j + 1;
1279 break;
1280 }
1281 else
1282 found = TRUE;
1283 }
1284 }
1285
1286 if (!found)
1287 j = i;
1288 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1289 j += 4;
1290 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1291 && j == i)
1292 j += 3;
1293 else
1294 j = 0;
1295 if (j > 0)
1296 {
1297 /* TODO this is only for DOS/UNIX systems - need to put in
1298 * machine-specific stuff here and in upseg init */
1299 cmdline_del(j);
1300 put_on_cmdline(upseg + 1, 3, FALSE);
1301 }
1302 else if (ccline.cmdpos > i)
1303 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001304
1305 /* Now complete in the new directory. Set KeyTyped in case the
1306 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001308 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
1312#endif /* FEAT_WILDMENU */
1313
1314 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1315 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1316 if (c == Ctrl_BSL)
1317 {
1318 ++no_mapping;
1319 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001320 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 --no_mapping;
1322 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001323 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1324 * is in a mapping. */
1325 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001326 || (ccline.cmdfirstc == '=' && KeyTyped)
1327#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001328 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001329#endif
1330 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
1332 vungetc(c);
1333 c = Ctrl_BSL;
1334 }
1335#ifdef FEAT_EVAL
1336 else if (c == 'e')
1337 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001338 char_u *p = NULL;
1339 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 /*
1342 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001343 * Need to save and restore the current command line, to be
1344 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 */
1346 if (ccline.cmdpos == ccline.cmdlen)
1347 new_cmdpos = 99999; /* keep it at the end */
1348 else
1349 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (c == '=')
1353 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001354 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001355 * to avoid nasty things like going to another buffer when
1356 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001357 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001359 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001360
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001361 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001363 len = (int)STRLEN(p);
1364 if (realloc_cmdbuff(len + 1) == OK)
1365 {
1366 ccline.cmdlen = len;
1367 STRCPY(ccline.cmdbuff, p);
1368 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001370 /* Restore the cursor or use the position set with
1371 * set_cmdline_pos(). */
1372 if (new_cmdpos > ccline.cmdlen)
1373 ccline.cmdpos = ccline.cmdlen;
1374 else
1375 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001377 KeyTyped = FALSE; /* Don't do p_wc completion. */
1378 redrawcmd();
1379 goto cmdline_changed;
1380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 }
1383 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001384 got_int = FALSE; /* don't abandon the command line */
1385 did_emsg = FALSE;
1386 emsg_on_display = FALSE;
1387 redrawcmd();
1388 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390#endif
1391 else
1392 {
1393 if (c == Ctrl_G && p_im && restart_edit == 0)
1394 restart_edit = 'a';
1395 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1396 in history */
1397 goto returncmd; /* back to Normal mode */
1398 }
1399 }
1400
1401#ifdef FEAT_CMDWIN
1402 if (c == cedit_key || c == K_CMDWIN)
1403 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001404 if (ex_normal_busy == 0 && got_int == FALSE)
1405 {
1406 /*
1407 * Open a window to edit the command line (and history).
1408 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001409 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001410 some_key_typed = TRUE;
1411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 }
1413# ifdef FEAT_DIGRAPHS
1414 else
1415# endif
1416#endif
1417#ifdef FEAT_DIGRAPHS
1418 c = do_digraph(c);
1419#endif
1420
1421 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1422 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1423 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001424 /* In Ex mode a backslash escapes a newline. */
1425 if (exmode_active
1426 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001427 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001428 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001429 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001431 if (c == K_KENTER)
1432 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001434 else
1435 {
1436 gotesc = FALSE; /* Might have typed ESC previously, don't
1437 truncate the cmdline now. */
1438 if (ccheck_abbr(c + ABBR_OFF))
1439 goto cmdline_changed;
1440 if (!cmd_silent)
1441 {
1442 windgoto(msg_row, 0);
1443 out_flush();
1444 }
1445 break;
1446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 /*
1450 * Completion for 'wildchar' or 'wildcharm' key.
1451 * - hitting <ESC> twice means: abandon command line.
1452 * - wildcard expansion is only done when the 'wildchar' key is really
1453 * typed, not when it comes from a macro
1454 */
1455 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1456 {
1457 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1458 {
1459 /* if 'wildmode' contains "list" may still need to list */
1460 if (xpc.xp_numfiles > 1
1461 && !did_wild_list
1462 && (wim_flags[wim_index] & WIM_LIST))
1463 {
1464 (void)showmatches(&xpc, FALSE);
1465 redrawcmd();
1466 did_wild_list = TRUE;
1467 }
1468 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001469 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1470 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001472 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1473 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 else
1475 res = OK; /* don't insert 'wildchar' now */
1476 }
1477 else /* typed p_wc first time */
1478 {
1479 wim_index = 0;
1480 j = ccline.cmdpos;
1481 /* if 'wildmode' first contains "longest", get longest
1482 * common part */
1483 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001484 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1485 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001487 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1488 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
1490 /* if interrupted while completing, behave like it failed */
1491 if (got_int)
1492 {
1493 (void)vpeekc(); /* remove <C-C> from input stream */
1494 got_int = FALSE; /* don't abandon the command line */
1495 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1496#ifdef FEAT_WILDMENU
1497 xpc.xp_context = EXPAND_NOTHING;
1498#endif
1499 goto cmdline_changed;
1500 }
1501
1502 /* when more than one match, and 'wildmode' first contains
1503 * "list", or no change and 'wildmode' contains "longest,list",
1504 * list all matches */
1505 if (res == OK && xpc.xp_numfiles > 1)
1506 {
1507 /* a "longest" that didn't do anything is skipped (but not
1508 * "list:longest") */
1509 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1510 wim_index = 1;
1511 if ((wim_flags[wim_index] & WIM_LIST)
1512#ifdef FEAT_WILDMENU
1513 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1514#endif
1515 )
1516 {
1517 if (!(wim_flags[0] & WIM_LONGEST))
1518 {
1519#ifdef FEAT_WILDMENU
1520 int p_wmnu_save = p_wmnu;
1521 p_wmnu = 0;
1522#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001523 /* remove match */
1524 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#ifdef FEAT_WILDMENU
1526 p_wmnu = p_wmnu_save;
1527#endif
1528 }
1529#ifdef FEAT_WILDMENU
1530 (void)showmatches(&xpc, p_wmnu
1531 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1532#else
1533 (void)showmatches(&xpc, FALSE);
1534#endif
1535 redrawcmd();
1536 did_wild_list = TRUE;
1537 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001538 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1539 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001541 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1542 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001545 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547#ifdef FEAT_WILDMENU
1548 else if (xpc.xp_numfiles == -1)
1549 xpc.xp_context = EXPAND_NOTHING;
1550#endif
1551 }
1552 if (wim_index < 3)
1553 ++wim_index;
1554 if (c == ESC)
1555 gotesc = TRUE;
1556 if (res == OK)
1557 goto cmdline_changed;
1558 }
1559
1560 gotesc = FALSE;
1561
1562 /* <S-Tab> goes to last match, in a clumsy way */
1563 if (c == K_S_TAB && KeyTyped)
1564 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001565 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1566 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1567 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 goto cmdline_changed;
1569 }
1570
1571 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1572 c = NL;
1573
1574 do_abbr = TRUE; /* default: check for abbreviation */
1575
1576 /*
1577 * Big switch for a typed command line character.
1578 */
1579 switch (c)
1580 {
1581 case K_BS:
1582 case Ctrl_H:
1583 case K_DEL:
1584 case K_KDEL:
1585 case Ctrl_W:
1586#ifdef FEAT_FKMAP
1587 if (cmd_fkmap && c == K_BS)
1588 c = K_DEL;
1589#endif
1590 if (c == K_KDEL)
1591 c = K_DEL;
1592
1593 /*
1594 * delete current character is the same as backspace on next
1595 * character, except at end of line
1596 */
1597 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1598 ++ccline.cmdpos;
1599#ifdef FEAT_MBYTE
1600 if (has_mbyte && c == K_DEL)
1601 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1602 ccline.cmdbuff + ccline.cmdpos);
1603#endif
1604 if (ccline.cmdpos > 0)
1605 {
1606 char_u *p;
1607
1608 j = ccline.cmdpos;
1609 p = ccline.cmdbuff + j;
1610#ifdef FEAT_MBYTE
1611 if (has_mbyte)
1612 {
1613 p = mb_prevptr(ccline.cmdbuff, p);
1614 if (c == Ctrl_W)
1615 {
1616 while (p > ccline.cmdbuff && vim_isspace(*p))
1617 p = mb_prevptr(ccline.cmdbuff, p);
1618 i = mb_get_class(p);
1619 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1620 p = mb_prevptr(ccline.cmdbuff, p);
1621 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001622 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
1624 }
1625 else
1626#endif
1627 if (c == Ctrl_W)
1628 {
1629 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1630 --p;
1631 i = vim_iswordc(p[-1]);
1632 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1633 && vim_iswordc(p[-1]) == i)
1634 --p;
1635 }
1636 else
1637 --p;
1638 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1639 ccline.cmdlen -= j - ccline.cmdpos;
1640 i = ccline.cmdpos;
1641 while (i < ccline.cmdlen)
1642 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1643
1644 /* Truncate at the end, required for multi-byte chars. */
1645 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001646#ifdef FEAT_SEARCH_EXTRA
1647 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001648 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001649 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001650 /* save view settings, so that the screen
1651 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001652 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001653 }
1654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 redrawcmd();
1656 }
1657 else if (ccline.cmdlen == 0 && c != Ctrl_W
1658 && ccline.cmdprompt == NULL && indent == 0)
1659 {
1660 /* In ex and debug mode it doesn't make sense to return. */
1661 if (exmode_active
1662#ifdef FEAT_EVAL
1663 || ccline.cmdfirstc == '>'
1664#endif
1665 )
1666 goto cmdline_not_changed;
1667
Bram Moolenaard23a8232018-02-10 18:45:26 +01001668 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (!cmd_silent)
1670 {
1671#ifdef FEAT_RIGHTLEFT
1672 if (cmdmsg_rl)
1673 msg_col = Columns;
1674 else
1675#endif
1676 msg_col = 0;
1677 msg_putchar(' '); /* delete ':' */
1678 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001679#ifdef FEAT_SEARCH_EXTRA
1680 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001681 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 redraw_cmdline = TRUE;
1684 goto returncmd; /* back to cmd mode */
1685 }
1686 goto cmdline_changed;
1687
1688 case K_INS:
1689 case K_KINS:
1690#ifdef FEAT_FKMAP
1691 /* if Farsi mode set, we are in reverse insert mode -
1692 Do not change the mode */
1693 if (cmd_fkmap)
1694 beep_flush();
1695 else
1696#endif
1697 ccline.overstrike = !ccline.overstrike;
1698#ifdef CURSOR_SHAPE
1699 ui_cursor_shape(); /* may show different cursor shape */
1700#endif
1701 goto cmdline_not_changed;
1702
1703 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001704 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 {
1706 /* ":lmap" mappings exists, toggle use of mappings. */
1707 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001708#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 im_set_active(FALSE); /* Disable input method */
1710#endif
1711 if (b_im_ptr != NULL)
1712 {
1713 if (State & LANGMAP)
1714 *b_im_ptr = B_IMODE_LMAP;
1715 else
1716 *b_im_ptr = B_IMODE_NONE;
1717 }
1718 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001719#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 else
1721 {
1722 /* There are no ":lmap" mappings, toggle IM. When
1723 * 'imdisable' is set don't try getting the status, it's
1724 * always off. */
1725 if ((p_imdisable && b_im_ptr != NULL)
1726 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1727 {
1728 im_set_active(FALSE); /* Disable input method */
1729 if (b_im_ptr != NULL)
1730 *b_im_ptr = B_IMODE_NONE;
1731 }
1732 else
1733 {
1734 im_set_active(TRUE); /* Enable input method */
1735 if (b_im_ptr != NULL)
1736 *b_im_ptr = B_IMODE_IM;
1737 }
1738 }
1739#endif
1740 if (b_im_ptr != NULL)
1741 {
1742 if (b_im_ptr == &curbuf->b_p_iminsert)
1743 set_iminsert_global();
1744 else
1745 set_imsearch_global();
1746 }
1747#ifdef CURSOR_SHAPE
1748 ui_cursor_shape(); /* may show different cursor shape */
1749#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001750#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 /* Show/unshow value of 'keymap' in status lines later. */
1752 status_redraw_curbuf();
1753#endif
1754 goto cmdline_not_changed;
1755
1756/* case '@': only in very old vi */
1757 case Ctrl_U:
1758 /* delete all characters left of the cursor */
1759 j = ccline.cmdpos;
1760 ccline.cmdlen -= j;
1761 i = ccline.cmdpos = 0;
1762 while (i < ccline.cmdlen)
1763 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1764 /* Truncate at the end, required for multi-byte chars. */
1765 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001766#ifdef FEAT_SEARCH_EXTRA
1767 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001768 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 redrawcmd();
1771 goto cmdline_changed;
1772
1773#ifdef FEAT_CLIPBOARD
1774 case Ctrl_Y:
1775 /* Copy the modeless selection, if there is one. */
1776 if (clip_star.state != SELECT_CLEARED)
1777 {
1778 if (clip_star.state == SELECT_DONE)
1779 clip_copy_modeless_selection(TRUE);
1780 goto cmdline_not_changed;
1781 }
1782 break;
1783#endif
1784
1785 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1786 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001787 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001788 * ":normal" runs out of characters. */
1789 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001790 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 goto cmdline_not_changed;
1792
1793 gotesc = TRUE; /* will free ccline.cmdbuff after
1794 putting it in history */
1795 goto returncmd; /* back to cmd mode */
1796
1797 case Ctrl_R: /* insert register */
1798#ifdef USE_ON_FLY_SCROLL
1799 dont_scroll = TRUE; /* disallow scrolling here */
1800#endif
1801 putcmdline('"', TRUE);
1802 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001803 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (i == Ctrl_O)
1805 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1806 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001807 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001808 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 --no_mapping;
1810#ifdef FEAT_EVAL
1811 /*
1812 * Insert the result of an expression.
1813 * Need to save the current command line, to be able to enter
1814 * a new one...
1815 */
1816 new_cmdpos = -1;
1817 if (c == '=')
1818 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001819 if (ccline.cmdfirstc == '=' // can't do this recursively
1820 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
1822 beep_flush();
1823 c = ESC;
1824 }
1825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828#endif
1829 if (c != ESC) /* use ESC to cancel inserting register */
1830 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001831 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001832
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001833#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001834 /* When there was a serious error abort getting the
1835 * command line. */
1836 if (aborting())
1837 {
1838 gotesc = TRUE; /* will free ccline.cmdbuff after
1839 putting it in history */
1840 goto returncmd; /* back to cmd mode */
1841 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 KeyTyped = FALSE; /* Don't do p_wc completion. */
1844#ifdef FEAT_EVAL
1845 if (new_cmdpos >= 0)
1846 {
1847 /* set_cmdline_pos() was used */
1848 if (new_cmdpos > ccline.cmdlen)
1849 ccline.cmdpos = ccline.cmdlen;
1850 else
1851 ccline.cmdpos = new_cmdpos;
1852 }
1853#endif
1854 }
1855 redrawcmd();
1856 goto cmdline_changed;
1857
1858 case Ctrl_D:
1859 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1860 break; /* Use ^D as normal char instead */
1861
1862 redrawcmd();
1863 continue; /* don't do incremental search now */
1864
1865 case K_RIGHT:
1866 case K_S_RIGHT:
1867 case K_C_RIGHT:
1868 do
1869 {
1870 if (ccline.cmdpos >= ccline.cmdlen)
1871 break;
1872 i = cmdline_charsize(ccline.cmdpos);
1873 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1874 break;
1875 ccline.cmdspos += i;
1876#ifdef FEAT_MBYTE
1877 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001878 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 + ccline.cmdpos);
1880 else
1881#endif
1882 ++ccline.cmdpos;
1883 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001884 while ((c == K_S_RIGHT || c == K_C_RIGHT
1885 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1887#ifdef FEAT_MBYTE
1888 if (has_mbyte)
1889 set_cmdspos_cursor();
1890#endif
1891 goto cmdline_not_changed;
1892
1893 case K_LEFT:
1894 case K_S_LEFT:
1895 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001896 if (ccline.cmdpos == 0)
1897 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 do
1899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 --ccline.cmdpos;
1901#ifdef FEAT_MBYTE
1902 if (has_mbyte) /* move to first byte of char */
1903 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1904 ccline.cmdbuff + ccline.cmdpos);
1905#endif
1906 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1907 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001908 while (ccline.cmdpos > 0
1909 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001910 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1912#ifdef FEAT_MBYTE
1913 if (has_mbyte)
1914 set_cmdspos_cursor();
1915#endif
1916 goto cmdline_not_changed;
1917
1918 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001919 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001920 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
Bram Moolenaar4770d092006-01-12 23:22:24 +00001922#ifdef FEAT_GUI_W32
1923 /* On Win32 ignore <M-F4>, we get it when closing the window was
1924 * cancelled. */
1925 case K_F4:
1926 if (mod_mask == MOD_MASK_ALT)
1927 {
1928 redrawcmd(); /* somehow the cmdline is cleared */
1929 goto cmdline_not_changed;
1930 }
1931 break;
1932#endif
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934#ifdef FEAT_MOUSE
1935 case K_MIDDLEDRAG:
1936 case K_MIDDLERELEASE:
1937 goto cmdline_not_changed; /* Ignore mouse */
1938
1939 case K_MIDDLEMOUSE:
1940# ifdef FEAT_GUI
1941 /* When GUI is active, also paste when 'mouse' is empty */
1942 if (!gui.in_use)
1943# endif
1944 if (!mouse_has(MOUSE_COMMAND))
1945 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001946# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001948 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001950# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001951 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 redrawcmd();
1953 goto cmdline_changed;
1954
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001955# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001957 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 redrawcmd();
1959 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001960# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
1962 case K_LEFTDRAG:
1963 case K_LEFTRELEASE:
1964 case K_RIGHTDRAG:
1965 case K_RIGHTRELEASE:
1966 /* Ignore drag and release events when the button-down wasn't
1967 * seen before. */
1968 if (ignore_drag_release)
1969 goto cmdline_not_changed;
1970 /* FALLTHROUGH */
1971 case K_LEFTMOUSE:
1972 case K_RIGHTMOUSE:
1973 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1974 ignore_drag_release = TRUE;
1975 else
1976 ignore_drag_release = FALSE;
1977# ifdef FEAT_GUI
1978 /* When GUI is active, also move when 'mouse' is empty */
1979 if (!gui.in_use)
1980# endif
1981 if (!mouse_has(MOUSE_COMMAND))
1982 goto cmdline_not_changed; /* Ignore mouse */
1983# ifdef FEAT_CLIPBOARD
1984 if (mouse_row < cmdline_row && clip_star.available)
1985 {
1986 int button, is_click, is_drag;
1987
1988 /*
1989 * Handle modeless selection.
1990 */
1991 button = get_mouse_button(KEY2TERMCAP1(c),
1992 &is_click, &is_drag);
1993 if (mouse_model_popup() && button == MOUSE_LEFT
1994 && (mod_mask & MOD_MASK_SHIFT))
1995 {
1996 /* Translate shift-left to right button. */
1997 button = MOUSE_RIGHT;
1998 mod_mask &= ~MOD_MASK_SHIFT;
1999 }
2000 clip_modeless(button, is_click, is_drag);
2001 goto cmdline_not_changed;
2002 }
2003# endif
2004
2005 set_cmdspos();
2006 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2007 ++ccline.cmdpos)
2008 {
2009 i = cmdline_charsize(ccline.cmdpos);
2010 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2011 && mouse_col < ccline.cmdspos % Columns + i)
2012 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002013# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (has_mbyte)
2015 {
2016 /* Count ">" for double-wide char that doesn't fit. */
2017 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002018 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 + ccline.cmdpos) - 1;
2020 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 ccline.cmdspos += i;
2023 }
2024 goto cmdline_not_changed;
2025
2026 /* Mouse scroll wheel: ignored here */
2027 case K_MOUSEDOWN:
2028 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002029 case K_MOUSELEFT:
2030 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 /* Alternate buttons ignored here */
2032 case K_X1MOUSE:
2033 case K_X1DRAG:
2034 case K_X1RELEASE:
2035 case K_X2MOUSE:
2036 case K_X2DRAG:
2037 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002038 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 goto cmdline_not_changed;
2040
2041#endif /* FEAT_MOUSE */
2042
2043#ifdef FEAT_GUI
2044 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2045 case K_LEFTRELEASE_NM:
2046 goto cmdline_not_changed;
2047
2048 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002049 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
2051 gui_do_scroll();
2052 redrawcmd();
2053 }
2054 goto cmdline_not_changed;
2055
2056 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002057 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002059 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 redrawcmd();
2061 }
2062 goto cmdline_not_changed;
2063#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002064#ifdef FEAT_GUI_TABLINE
2065 case K_TABLINE:
2066 case K_TABMENU:
2067 /* Don't want to change any tabs here. Make sure the same tab
2068 * is still selected. */
2069 if (gui_use_tabline())
2070 gui_mch_set_curtab(tabpage_index(curtab));
2071 goto cmdline_not_changed;
2072#endif
2073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 case K_SELECT: /* end of Select mode mapping - ignore */
2075 goto cmdline_not_changed;
2076
2077 case Ctrl_B: /* begin of command line */
2078 case K_HOME:
2079 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 case K_S_HOME:
2081 case K_C_HOME:
2082 ccline.cmdpos = 0;
2083 set_cmdspos();
2084 goto cmdline_not_changed;
2085
2086 case Ctrl_E: /* end of command line */
2087 case K_END:
2088 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 case K_S_END:
2090 case K_C_END:
2091 ccline.cmdpos = ccline.cmdlen;
2092 set_cmdspos_cursor();
2093 goto cmdline_not_changed;
2094
2095 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002096 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 break;
2098 goto cmdline_changed;
2099
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002100 case Ctrl_L:
2101#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002102 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002103 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002104#endif
2105
2106 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002107 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 break;
2109 goto cmdline_changed;
2110
2111 case Ctrl_N: /* next match */
2112 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002113 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002115 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2116 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002118 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002121 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 case K_UP:
2123 case K_DOWN:
2124 case K_S_UP:
2125 case K_S_DOWN:
2126 case K_PAGEUP:
2127 case K_KPAGEUP:
2128 case K_PAGEDOWN:
2129 case K_KPAGEDOWN:
2130 if (hislen == 0 || firstc == NUL) /* no history */
2131 goto cmdline_not_changed;
2132
2133 i = hiscnt;
2134
2135 /* save current command string so it can be restored later */
2136 if (lookfor == NULL)
2137 {
2138 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2139 goto cmdline_not_changed;
2140 lookfor[ccline.cmdpos] = NUL;
2141 }
2142
2143 j = (int)STRLEN(lookfor);
2144 for (;;)
2145 {
2146 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002147 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002148 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 if (hiscnt == hislen) /* first time */
2151 hiscnt = hisidx[histype];
2152 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2153 hiscnt = hislen - 1;
2154 else if (hiscnt != hisidx[histype] + 1)
2155 --hiscnt;
2156 else /* at top of list */
2157 {
2158 hiscnt = i;
2159 break;
2160 }
2161 }
2162 else /* one step forwards */
2163 {
2164 /* on last entry, clear the line */
2165 if (hiscnt == hisidx[histype])
2166 {
2167 hiscnt = hislen;
2168 break;
2169 }
2170
2171 /* not on a history line, nothing to do */
2172 if (hiscnt == hislen)
2173 break;
2174 if (hiscnt == hislen - 1) /* wrap around */
2175 hiscnt = 0;
2176 else
2177 ++hiscnt;
2178 }
2179 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2180 {
2181 hiscnt = i;
2182 break;
2183 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002184 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002185 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 || STRNCMP(history[histype][hiscnt].hisstr,
2187 lookfor, (size_t)j) == 0)
2188 break;
2189 }
2190
2191 if (hiscnt != i) /* jumped to other entry */
2192 {
2193 char_u *p;
2194 int len;
2195 int old_firstc;
2196
Bram Moolenaar438d1762018-09-30 17:11:48 +02002197 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002198 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (hiscnt == hislen)
2200 p = lookfor; /* back to the old one */
2201 else
2202 p = history[histype][hiscnt].hisstr;
2203
2204 if (histype == HIST_SEARCH
2205 && p != lookfor
2206 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2207 {
2208 /* Correct for the separator character used when
2209 * adding the history entry vs the one used now.
2210 * First loop: count length.
2211 * Second loop: copy the characters. */
2212 for (i = 0; i <= 1; ++i)
2213 {
2214 len = 0;
2215 for (j = 0; p[j] != NUL; ++j)
2216 {
2217 /* Replace old sep with new sep, unless it is
2218 * escaped. */
2219 if (p[j] == old_firstc
2220 && (j == 0 || p[j - 1] != '\\'))
2221 {
2222 if (i > 0)
2223 ccline.cmdbuff[len] = firstc;
2224 }
2225 else
2226 {
2227 /* Escape new sep, unless it is already
2228 * escaped. */
2229 if (p[j] == firstc
2230 && (j == 0 || p[j - 1] != '\\'))
2231 {
2232 if (i > 0)
2233 ccline.cmdbuff[len] = '\\';
2234 ++len;
2235 }
2236 if (i > 0)
2237 ccline.cmdbuff[len] = p[j];
2238 }
2239 ++len;
2240 }
2241 if (i == 0)
2242 {
2243 alloc_cmdbuff(len);
2244 if (ccline.cmdbuff == NULL)
2245 goto returncmd;
2246 }
2247 }
2248 ccline.cmdbuff[len] = NUL;
2249 }
2250 else
2251 {
2252 alloc_cmdbuff((int)STRLEN(p));
2253 if (ccline.cmdbuff == NULL)
2254 goto returncmd;
2255 STRCPY(ccline.cmdbuff, p);
2256 }
2257
2258 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2259 redrawcmd();
2260 goto cmdline_changed;
2261 }
2262 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002263#endif
2264 goto cmdline_not_changed;
2265
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002266#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002267 case Ctrl_G: /* next match */
2268 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002269 if (may_adjust_incsearch_highlighting(
2270 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002271 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#endif
2274
2275 case Ctrl_V:
2276 case Ctrl_Q:
2277#ifdef FEAT_MOUSE
2278 ignore_drag_release = TRUE;
2279#endif
2280 putcmdline('^', TRUE);
2281 c = get_literal(); /* get next (two) character(s) */
2282 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002283 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#ifdef FEAT_MBYTE
2285 /* may need to remove ^ when composing char was typed */
2286 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2287 {
2288 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2289 msg_putchar(' ');
2290 cursorcmd();
2291 }
2292#endif
2293 break;
2294
2295#ifdef FEAT_DIGRAPHS
2296 case Ctrl_K:
2297#ifdef FEAT_MOUSE
2298 ignore_drag_release = TRUE;
2299#endif
2300 putcmdline('?', TRUE);
2301#ifdef USE_ON_FLY_SCROLL
2302 dont_scroll = TRUE; /* disallow scrolling here */
2303#endif
2304 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002305 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (c != NUL)
2307 break;
2308
2309 redrawcmd();
2310 goto cmdline_not_changed;
2311#endif /* FEAT_DIGRAPHS */
2312
2313#ifdef FEAT_RIGHTLEFT
2314 case Ctrl__: /* CTRL-_: switch language mode */
2315 if (!p_ari)
2316 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002317# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (p_altkeymap)
2319 {
2320 cmd_fkmap = !cmd_fkmap;
2321 if (cmd_fkmap) /* in Farsi always in Insert mode */
2322 ccline.overstrike = FALSE;
2323 }
2324 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 cmd_hkmap = !cmd_hkmap;
2327 goto cmdline_not_changed;
2328#endif
2329
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002330 case K_PS:
2331 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2332 goto cmdline_changed;
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 default:
2335#ifdef UNIX
2336 if (c == intr_char)
2337 {
2338 gotesc = TRUE; /* will free ccline.cmdbuff after
2339 putting it in history */
2340 goto returncmd; /* back to Normal mode */
2341 }
2342#endif
2343 /*
2344 * Normal character with no special meaning. Just set mod_mask
2345 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2346 * the string <S-Space>. This should only happen after ^V.
2347 */
2348 if (!IS_SPECIAL(c))
2349 mod_mask = 0x0;
2350 break;
2351 }
2352 /*
2353 * End of switch on command line character.
2354 * We come here if we have a normal character.
2355 */
2356
Bram Moolenaarede3e632013-06-23 16:16:19 +02002357 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358#ifdef FEAT_MBYTE
2359 /* Add ABBR_OFF for characters above 0x100, this is
2360 * what check_abbr() expects. */
2361 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2362#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002363 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 goto cmdline_changed;
2365
2366 /*
2367 * put the character in the command line
2368 */
2369 if (IS_SPECIAL(c) || mod_mask != 0)
2370 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2371 else
2372 {
2373#ifdef FEAT_MBYTE
2374 if (has_mbyte)
2375 {
2376 j = (*mb_char2bytes)(c, IObuff);
2377 IObuff[j] = NUL; /* exclude composing chars */
2378 put_on_cmdline(IObuff, j, TRUE);
2379 }
2380 else
2381#endif
2382 {
2383 IObuff[0] = c;
2384 put_on_cmdline(IObuff, 1, TRUE);
2385 }
2386 }
2387 goto cmdline_changed;
2388
2389/*
2390 * This part implements incremental searches for "/" and "?"
2391 * Jump to cmdline_not_changed when a character has been read but the command
2392 * line did not change. Then we only search and redraw if something changed in
2393 * the past.
2394 * Jump to cmdline_changed when the command line did change.
2395 * (Sorry for the goto's, I know it is ugly).
2396 */
2397cmdline_not_changed:
2398#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002399 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 continue;
2401#endif
2402
2403cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002404 /* Trigger CmdlineChanged autocommands. */
2405 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002408 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#endif
2410
2411#ifdef FEAT_RIGHTLEFT
2412 if (cmdmsg_rl
2413# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002414 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415# endif
2416 )
2417 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002418 * right-left typing. Not efficient, but it works.
2419 * Do it only when there are no characters left to read
2420 * to avoid useless intermediate redraws. */
2421 if (vpeekc() == NUL)
2422 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#endif
2424 }
2425
2426returncmd:
2427
2428#ifdef FEAT_RIGHTLEFT
2429 cmdmsg_rl = FALSE;
2430#endif
2431
2432#ifdef FEAT_FKMAP
2433 cmd_fkmap = 0;
2434#endif
2435
2436 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002437 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002440 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441#endif
2442
2443 if (ccline.cmdbuff != NULL)
2444 {
2445 /*
2446 * Put line in history buffer (":" and "=" only when it was typed).
2447 */
2448#ifdef FEAT_CMDHIST
2449 if (ccline.cmdlen && firstc != NUL
2450 && (some_key_typed || histype == HIST_SEARCH))
2451 {
2452 add_to_history(histype, ccline.cmdbuff, TRUE,
2453 histype == HIST_SEARCH ? firstc : NUL);
2454 if (firstc == ':')
2455 {
2456 vim_free(new_last_cmdline);
2457 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2458 }
2459 }
2460#endif
2461
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002462 if (gotesc)
2463 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465
2466 /*
2467 * If the screen was shifted up, redraw the whole screen (later).
2468 * If the line is too long, clear it, so ruler and shown command do
2469 * not get printed in the middle of it.
2470 */
2471 msg_check();
2472 msg_scroll = save_msg_scroll;
2473 redir_off = FALSE;
2474
2475 /* When the command line was typed, no need for a wait-return prompt. */
2476 if (some_key_typed)
2477 need_wait_return = FALSE;
2478
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002479 /* Trigger CmdlineLeave autocommands. */
2480 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002483#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2485 im_save_status(b_im_ptr);
2486 im_set_active(FALSE);
2487#endif
2488#ifdef FEAT_MOUSE
2489 setmouse();
2490#endif
2491#ifdef CURSOR_SHAPE
2492 ui_cursor_shape(); /* may show different cursor shape */
2493#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002494 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar438d1762018-09-30 17:11:48 +02002496theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002497 {
2498 char_u *p = ccline.cmdbuff;
2499
Bram Moolenaar438d1762018-09-30 17:11:48 +02002500 if (did_save_ccline)
2501 restore_cmdline(&save_ccline);
2502 else
2503 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002504 return p;
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506}
2507
2508#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2509/*
2510 * Get a command line with a prompt.
2511 * This is prepared to be called recursively from getcmdline() (e.g. by
2512 * f_input() when evaluating an expression from CTRL-R =).
2513 * Returns the command line in allocated memory, or NULL.
2514 */
2515 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002516getcmdline_prompt(
2517 int firstc,
2518 char_u *prompt, /* command line prompt */
2519 int attr, /* attributes for prompt */
2520 int xp_context, /* type of expansion */
2521 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 char_u *s;
2524 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002525 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002527 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar438d1762018-09-30 17:11:48 +02002529 if (ccline.cmdbuff != NULL)
2530 {
2531 // Save the values of the current cmdline and restore them below.
2532 save_cmdline(&save_ccline);
2533 did_save_ccline = TRUE;
2534 }
2535
2536 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 ccline.cmdprompt = prompt;
2538 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002539# ifdef FEAT_EVAL
2540 ccline.xp_context = xp_context;
2541 ccline.xp_arg = xp_arg;
2542 ccline.input_fn = (firstc == '@');
2543# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002544 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002545 s = getcmdline_int(firstc, 1L, 0, FALSE);
2546
2547 if (did_save_ccline)
2548 restore_cmdline(&save_ccline);
2549
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002550 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002551 /* Restore msg_col, the prompt from input() may have changed it.
2552 * But only if called recursively and the commandline is therefore being
2553 * restored to an old one; if not, the input() prompt stays on the screen,
2554 * so we need its modified msg_col left intact. */
2555 if (ccline.cmdbuff != NULL)
2556 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 return s;
2559}
2560#endif
2561
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002562/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002563 * Return TRUE when the text must not be changed and we can't switch to
2564 * another window or buffer. Used when editing the command line, evaluating
2565 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002566 */
2567 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002568text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002569{
2570#ifdef FEAT_CMDWIN
2571 if (cmdwin_type != 0)
2572 return TRUE;
2573#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002574 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002575}
2576
2577/*
2578 * Give an error message for a command that isn't allowed while the cmdline
2579 * window is open or editing the cmdline in another way.
2580 */
2581 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002582text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002583{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002584 EMSG(_(get_text_locked_msg()));
2585}
2586
2587 char_u *
2588get_text_locked_msg(void)
2589{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002590#ifdef FEAT_CMDWIN
2591 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002592 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002593#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002594 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002595}
2596
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002597/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002598 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2599 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002600 */
2601 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002603{
2604 if (curbuf_lock > 0)
2605 {
2606 EMSG(_("E788: Not allowed to edit another buffer now"));
2607 return TRUE;
2608 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002609 return allbuf_locked();
2610}
2611
2612/*
2613 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2614 * message.
2615 */
2616 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002617allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002618{
2619 if (allbuf_lock > 0)
2620 {
2621 EMSG(_("E811: Not allowed to change buffer information now"));
2622 return TRUE;
2623 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002624 return FALSE;
2625}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002628cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2631 if (cmdline_star > 0) /* showing '*', always 1 position */
2632 return 1;
2633#endif
2634 return ptr2cells(ccline.cmdbuff + idx);
2635}
2636
2637/*
2638 * Compute the offset of the cursor on the command line for the prompt and
2639 * indent.
2640 */
2641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002642set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002644 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 ccline.cmdspos = 1 + ccline.cmdindent;
2646 else
2647 ccline.cmdspos = 0 + ccline.cmdindent;
2648}
2649
2650/*
2651 * Compute the screen position for the cursor on the command line.
2652 */
2653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002654set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 int i, m, c;
2657
2658 set_cmdspos();
2659 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002662 if (m < 0) /* overflow, Columns or Rows at weird value */
2663 m = MAXCOL;
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 else
2666 m = MAXCOL;
2667 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2668 {
2669 c = cmdline_charsize(i);
2670#ifdef FEAT_MBYTE
2671 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2672 if (has_mbyte)
2673 correct_cmdspos(i, c);
2674#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002675 /* If the cmdline doesn't fit, show cursor on last visible char.
2676 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if ((ccline.cmdspos += c) >= m)
2678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 ccline.cmdspos -= c;
2680 break;
2681 }
2682#ifdef FEAT_MBYTE
2683 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002684 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#endif
2686 }
2687}
2688
2689#ifdef FEAT_MBYTE
2690/*
2691 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2692 * character that doesn't fit, so that a ">" must be displayed.
2693 */
2694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002695correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002697 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2699 && ccline.cmdspos % Columns + cells > Columns)
2700 ccline.cmdspos++;
2701}
2702#endif
2703
2704/*
2705 * Get an Ex command line for the ":" command.
2706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002708getexline(
2709 int c, /* normally ':', NUL for ":append" */
2710 void *cookie UNUSED,
2711 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 /* When executing a register, remove ':' that's in front of each line. */
2714 if (exec_from_reg && vpeekc() == ':')
2715 (void)vgetc();
2716 return getcmdline(c, 1L, indent);
2717}
2718
2719/*
2720 * Get an Ex command line for Ex mode.
2721 * In Ex mode we only use the OS supplied line editing features and no
2722 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002723 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002726getexmodeline(
2727 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002729 void *cookie UNUSED,
2730 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732 garray_T line_ga;
2733 char_u *pend;
2734 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002735 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002736 int escaped = FALSE; /* CTRL-V typed */
2737 int vcol = 0;
2738 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002739 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002740 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 /* Switch cursor on now. This avoids that it happens after the "\n", which
2743 * confuses the system function that computes tabstops. */
2744 cursor_on();
2745
2746 /* always start in column 0; write a newline if necessary */
2747 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002752 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 if (p_prompt)
2754 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 while (indent-- > 0)
2756 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759
2760 ga_init2(&line_ga, 1, 30);
2761
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002762 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002764 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002765 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002766 while (indent >= 8)
2767 {
2768 ga_append(&line_ga, TAB);
2769 msg_puts((char_u *)" ");
2770 indent -= 8;
2771 }
2772 while (indent-- > 0)
2773 {
2774 ga_append(&line_ga, ' ');
2775 msg_putchar(' ');
2776 }
2777 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 ++no_mapping;
2779 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 /*
2782 * Get the line, one character at a time.
2783 */
2784 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002785 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002787 long sw;
2788 char_u *s;
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (ga_grow(&line_ga, 40) == FAIL)
2791 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaarba748c82017-02-26 14:00:07 +01002793 /*
2794 * Get one character at a time.
2795 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002796 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002797
2798 /* Check for a ":normal" command and no more characters left. */
2799 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2800 c1 = '\n';
2801 else
2802 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
2804 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 * Handle line editing.
2806 * Previously this was left to the system, putting the terminal in
2807 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 msg_putchar('\n');
2812 break;
2813 }
2814
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002815 if (c1 == K_PS)
2816 {
2817 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2818 goto redraw;
2819 }
2820
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002821 if (!escaped)
2822 {
2823 /* CR typed means "enter", which is NL */
2824 if (c1 == '\r')
2825 c1 = '\n';
2826
2827 if (c1 == BS || c1 == K_BS
2828 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830 if (line_ga.ga_len > 0)
2831 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002832#ifdef FEAT_MBYTE
2833 if (has_mbyte)
2834 {
2835 p = (char_u *)line_ga.ga_data;
2836 p[line_ga.ga_len] = NUL;
2837 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2838 line_ga.ga_len -= len;
2839 }
2840 else
2841#endif
2842 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 goto redraw;
2844 }
2845 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 msg_col = startcol;
2851 msg_clr_eos();
2852 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002853 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 }
2855
2856 if (c1 == Ctrl_T)
2857 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002858 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 p = (char_u *)line_ga.ga_data;
2860 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002861 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002862 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002864 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002866 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002867 p = (char_u *)line_ga.ga_data;
2868 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2870 *s = ' ';
2871 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873redraw:
2874 /* redraw the line */
2875 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002877 p = (char_u *)line_ga.ga_data;
2878 p[line_ga.ga_len] = NUL;
2879 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002881 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002885 msg_putchar(' ');
2886 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002887 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002891 len = MB_PTR2LEN(p);
2892 msg_outtrans_len(p, len);
2893 vcol += ptr2cells(p);
2894 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002897 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002898 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002899 continue;
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902 if (c1 == Ctrl_D)
2903 {
2904 /* Delete one shiftwidth. */
2905 p = (char_u *)line_ga.ga_data;
2906 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002908 if (prev_char == '^')
2909 ex_keep_indent = TRUE;
2910 indent = 0;
2911 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 else
2914 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002916 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002917 if (indent > 0)
2918 {
2919 --indent;
2920 indent -= indent % get_sw_value(curbuf);
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002923 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002924 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002925 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002926 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2927 --line_ga.ga_len;
2928 }
2929 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002931
2932 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2933 {
2934 escaped = TRUE;
2935 continue;
2936 }
2937
2938 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2939 if (IS_SPECIAL(c1))
2940 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002942
2943 if (IS_SPECIAL(c1))
2944 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002945#ifdef FEAT_MBYTE
2946 if (has_mbyte)
2947 len = (*mb_char2bytes)(c1,
2948 (char_u *)line_ga.ga_data + line_ga.ga_len);
2949 else
2950#endif
2951 {
2952 len = 1;
2953 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2954 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002955 if (c1 == '\n')
2956 msg_putchar('\n');
2957 else if (c1 == TAB)
2958 {
2959 /* Don't use chartabsize(), 'ts' can be different */
2960 do
2961 {
2962 msg_putchar(' ');
2963 } while (++vcol % 8);
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002967 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002968 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002969 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002971 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002972 escaped = FALSE;
2973
2974 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002975 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002976
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002977 /* We are done when a NL is entered, but not when it comes after an
2978 * odd number of backslashes, that results in a NUL. */
2979 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002981 int bcount = 0;
2982
2983 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2984 ++bcount;
2985
2986 if (bcount > 0)
2987 {
2988 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2989 * "\NL", etc. */
2990 line_ga.ga_len -= (bcount + 1) / 2;
2991 pend -= (bcount + 1) / 2;
2992 pend[-1] = '\n';
2993 }
2994
2995 if ((bcount & 1) == 0)
2996 {
2997 --line_ga.ga_len;
2998 --pend;
2999 *pend = NUL;
3000 break;
3001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003 }
3004
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003005 --no_mapping;
3006 --allow_keys;
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 /* make following messages go to the next line */
3009 msg_didout = FALSE;
3010 msg_col = 0;
3011 if (msg_row < Rows - 1)
3012 ++msg_row;
3013 emsg_on_display = FALSE; /* don't want ui_delay() */
3014
3015 if (got_int)
3016 ga_clear(&line_ga);
3017
3018 return (char_u *)line_ga.ga_data;
3019}
3020
Bram Moolenaare344bea2005-09-01 20:46:49 +00003021# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3022 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023/*
3024 * Return TRUE if ccline.overstrike is on.
3025 */
3026 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003027cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 return ccline.overstrike;
3030}
3031
3032/*
3033 * Return TRUE if the cursor is at the end of the cmdline.
3034 */
3035 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003036cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 return (ccline.cmdpos >= ccline.cmdlen);
3039}
3040#endif
3041
Bram Moolenaar9372a112005-12-06 19:59:18 +00003042#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
3044 * Return the virtual column number at the current cursor position.
3045 * This is used by the IM code to obtain the start of the preedit string.
3046 */
3047 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003048cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3051 return MAXCOL;
3052
3053# ifdef FEAT_MBYTE
3054 if (has_mbyte)
3055 {
3056 colnr_T col;
3057 int i = 0;
3058
3059 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003060 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 return col;
3063 }
3064 else
3065# endif
3066 return ccline.cmdpos;
3067}
3068#endif
3069
3070#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3071/*
3072 * If part of the command line is an IM preedit string, redraw it with
3073 * IM feedback attributes. The cursor position is restored after drawing.
3074 */
3075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003076redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 if ((State & CMDLINE)
3079 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003080 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 && !p_imdisable
3082 && im_is_preediting())
3083 {
3084 int cmdpos = 0;
3085 int cmdspos;
3086 int old_row;
3087 int old_col;
3088 colnr_T col;
3089
3090 old_row = msg_row;
3091 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003092 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094# ifdef FEAT_MBYTE
3095 if (has_mbyte)
3096 {
3097 for (col = 0; col < preedit_start_col
3098 && cmdpos < ccline.cmdlen; ++col)
3099 {
3100 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003101 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 }
3103 }
3104 else
3105# endif
3106 {
3107 cmdspos += preedit_start_col;
3108 cmdpos += preedit_start_col;
3109 }
3110
3111 msg_row = cmdline_row + (cmdspos / (int)Columns);
3112 msg_col = cmdspos % (int)Columns;
3113 if (msg_row >= Rows)
3114 msg_row = Rows - 1;
3115
3116 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3117 {
3118 int char_len;
3119 int char_attr;
3120
3121 char_attr = im_get_feedback_attr(col);
3122 if (char_attr < 0)
3123 break; /* end of preedit string */
3124
3125# ifdef FEAT_MBYTE
3126 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003127 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 else
3129# endif
3130 char_len = 1;
3131
3132 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3133 cmdpos += char_len;
3134 }
3135
3136 msg_row = old_row;
3137 msg_col = old_col;
3138 }
3139}
3140#endif /* FEAT_XIM && FEAT_GUI_GTK */
3141
3142/*
3143 * Allocate a new command line buffer.
3144 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
3146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 /*
3150 * give some extra space to avoid having to allocate all the time
3151 */
3152 if (len < 80)
3153 len = 100;
3154 else
3155 len += 20;
3156
3157 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3158 ccline.cmdbufflen = len;
3159}
3160
3161/*
3162 * Re-allocate the command line to length len + something extra.
3163 * return FAIL for failure, OK otherwise
3164 */
3165 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003166realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
3168 char_u *p;
3169
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003170 if (len < ccline.cmdbufflen)
3171 return OK; /* no need to resize */
3172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 p = ccline.cmdbuff;
3174 alloc_cmdbuff(len); /* will get some more */
3175 if (ccline.cmdbuff == NULL) /* out of memory */
3176 {
3177 ccline.cmdbuff = p; /* keep the old one */
3178 return FAIL;
3179 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003180 /* There isn't always a NUL after the command, but it may need to be
3181 * there, thus copy up to the NUL and add a NUL. */
3182 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3183 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003185
3186 if (ccline.xpc != NULL
3187 && ccline.xpc->xp_pattern != NULL
3188 && ccline.xpc->xp_context != EXPAND_NOTHING
3189 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3190 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003191 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003192
3193 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3194 * to point into the newly allocated memory. */
3195 if (i >= 0 && i <= ccline.cmdlen)
3196 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3197 }
3198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 return OK;
3200}
3201
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202#if defined(FEAT_ARABIC) || defined(PROTO)
3203static char_u *arshape_buf = NULL;
3204
3205# if defined(EXITFREE) || defined(PROTO)
3206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003207free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003208{
3209 vim_free(arshape_buf);
3210}
3211# endif
3212#endif
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214/*
3215 * Draw part of the cmdline at the current cursor position. But draw stars
3216 * when cmdline_star is TRUE.
3217 */
3218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003219draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3222 int i;
3223
3224 if (cmdline_star > 0)
3225 for (i = 0; i < len; ++i)
3226 {
3227 msg_putchar('*');
3228# ifdef FEAT_MBYTE
3229 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003230 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231# endif
3232 }
3233 else
3234#endif
3235#ifdef FEAT_ARABIC
3236 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 static int buflen = 0;
3239 char_u *p;
3240 int j;
3241 int newlen = 0;
3242 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003243 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 int prev_c = 0;
3245 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003246 int u8c;
3247 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 /*
3251 * Do arabic shaping into a temporary buffer. This is very
3252 * inefficient!
3253 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003254 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 /* Re-allocate the buffer. We keep it around to avoid a lot of
3257 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003259 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 arshape_buf = alloc(buflen);
3261 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 return; /* out of memory */
3263 }
3264
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003265 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3266 {
3267 /* Prepend a space to draw the leading composing char on. */
3268 arshape_buf[0] = ' ';
3269 newlen = 1;
3270 }
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 for (j = start; j < start + len; j += mb_l)
3273 {
3274 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003275 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003276 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (ARABIC_CHAR(u8c))
3278 {
3279 /* Do Arabic shaping. */
3280 if (cmdmsg_rl)
3281 {
3282 /* displaying from right to left */
3283 pc = prev_c;
3284 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003285 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (j + mb_l >= start + len)
3287 nc = NUL;
3288 else
3289 nc = utf_ptr2char(p + mb_l);
3290 }
3291 else
3292 {
3293 /* displaying from left to right */
3294 if (j + mb_l >= start + len)
3295 pc = NUL;
3296 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003297 {
3298 int pcc[MAX_MCO];
3299
3300 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003302 pc1 = pcc[0];
3303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 nc = prev_c;
3305 }
3306 prev_c = u8c;
3307
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003308 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003311 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003313 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3314 if (u8cc[1] != 0)
3315 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318 }
3319 else
3320 {
3321 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 newlen += mb_l;
3324 }
3325 }
3326
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
3329 else
3330#endif
3331 msg_outtrans_len(ccline.cmdbuff + start, len);
3332}
3333
3334/*
3335 * Put a character on the command line. Shifts the following text to the
3336 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3337 * "c" must be printable (fit in one display cell)!
3338 */
3339 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003340putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 if (cmd_silent)
3343 return;
3344 msg_no_more = TRUE;
3345 msg_putchar(c);
3346 if (shift)
3347 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3348 msg_no_more = FALSE;
3349 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003350 extra_char = c;
3351 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352}
3353
3354/*
3355 * Undo a putcmdline(c, FALSE).
3356 */
3357 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003358unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 if (cmd_silent)
3361 return;
3362 msg_no_more = TRUE;
3363 if (ccline.cmdlen == ccline.cmdpos)
3364 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003365#ifdef FEAT_MBYTE
3366 else if (has_mbyte)
3367 draw_cmdline(ccline.cmdpos,
3368 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
3371 draw_cmdline(ccline.cmdpos, 1);
3372 msg_no_more = FALSE;
3373 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003374 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375}
3376
3377/*
3378 * Put the given string, of the given length, onto the command line.
3379 * If len is -1, then STRLEN() is used to calculate the length.
3380 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3381 * part will be redrawn, otherwise it will not. If this function is called
3382 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3383 * called afterwards.
3384 */
3385 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003386put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 int retval;
3389 int i;
3390 int m;
3391 int c;
3392
3393 if (len < 0)
3394 len = (int)STRLEN(str);
3395
3396 /* Check if ccline.cmdbuff needs to be longer */
3397 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003398 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
3400 retval = OK;
3401 if (retval == OK)
3402 {
3403 if (!ccline.overstrike)
3404 {
3405 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3406 ccline.cmdbuff + ccline.cmdpos,
3407 (size_t)(ccline.cmdlen - ccline.cmdpos));
3408 ccline.cmdlen += len;
3409 }
3410 else
3411 {
3412#ifdef FEAT_MBYTE
3413 if (has_mbyte)
3414 {
3415 /* Count nr of characters in the new string. */
3416 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003417 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 ++m;
3419 /* Count nr of bytes in cmdline that are overwritten by these
3420 * characters. */
3421 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003422 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 --m;
3424 if (i < ccline.cmdlen)
3425 {
3426 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3427 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3428 ccline.cmdlen += ccline.cmdpos + len - i;
3429 }
3430 else
3431 ccline.cmdlen = ccline.cmdpos + len;
3432 }
3433 else
3434#endif
3435 if (ccline.cmdpos + len > ccline.cmdlen)
3436 ccline.cmdlen = ccline.cmdpos + len;
3437 }
3438 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3439 ccline.cmdbuff[ccline.cmdlen] = NUL;
3440
3441#ifdef FEAT_MBYTE
3442 if (enc_utf8)
3443 {
3444 /* When the inserted text starts with a composing character,
3445 * backup to the character before it. There could be two of them.
3446 */
3447 i = 0;
3448 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3449 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3450 {
3451 i = (*mb_head_off)(ccline.cmdbuff,
3452 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3453 ccline.cmdpos -= i;
3454 len += i;
3455 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3456 }
3457# ifdef FEAT_ARABIC
3458 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3459 {
3460 /* Check the previous character for Arabic combining pair. */
3461 i = (*mb_head_off)(ccline.cmdbuff,
3462 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3463 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3464 + ccline.cmdpos - i), c))
3465 {
3466 ccline.cmdpos -= i;
3467 len += i;
3468 }
3469 else
3470 i = 0;
3471 }
3472# endif
3473 if (i != 0)
3474 {
3475 /* Also backup the cursor position. */
3476 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3477 ccline.cmdspos -= i;
3478 msg_col -= i;
3479 if (msg_col < 0)
3480 {
3481 msg_col += Columns;
3482 --msg_row;
3483 }
3484 }
3485 }
3486#endif
3487
3488 if (redraw && !cmd_silent)
3489 {
3490 msg_no_more = TRUE;
3491 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003492 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3494 /* Avoid clearing the rest of the line too often. */
3495 if (cmdline_row != i || ccline.overstrike)
3496 msg_clr_eos();
3497 msg_no_more = FALSE;
3498 }
3499#ifdef FEAT_FKMAP
3500 /*
3501 * If we are in Farsi command mode, the character input must be in
3502 * Insert mode. So do not advance the cmdpos.
3503 */
3504 if (!cmd_fkmap)
3505#endif
3506 {
3507 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003510 if (m < 0) /* overflow, Columns or Rows at weird value */
3511 m = MAXCOL;
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 else
3514 m = MAXCOL;
3515 for (i = 0; i < len; ++i)
3516 {
3517 c = cmdline_charsize(ccline.cmdpos);
3518#ifdef FEAT_MBYTE
3519 /* count ">" for a double-wide char that doesn't fit. */
3520 if (has_mbyte)
3521 correct_cmdspos(ccline.cmdpos, c);
3522#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003523 /* Stop cursor at the end of the screen, but do increment the
3524 * insert position, so that entering a very long command
3525 * works, even though you can't see it. */
3526 if (ccline.cmdspos + c < m)
3527 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528#ifdef FEAT_MBYTE
3529 if (has_mbyte)
3530 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003531 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (c > len - i - 1)
3533 c = len - i - 1;
3534 ccline.cmdpos += c;
3535 i += c;
3536 }
3537#endif
3538 ++ccline.cmdpos;
3539 }
3540 }
3541 }
3542 if (redraw)
3543 msg_check();
3544 return retval;
3545}
3546
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003547static struct cmdline_info prev_ccline;
3548static int prev_ccline_used = FALSE;
3549
3550/*
3551 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3552 * and overwrite it. But get_cmdline_str() may need it, thus make it
3553 * available globally in prev_ccline.
3554 */
3555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003556save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003557{
3558 if (!prev_ccline_used)
3559 {
3560 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3561 prev_ccline_used = TRUE;
3562 }
3563 *ccp = prev_ccline;
3564 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003565 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003566}
3567
3568/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003569 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003570 */
3571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003572restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003573{
3574 ccline = prev_ccline;
3575 prev_ccline = *ccp;
3576}
3577
Bram Moolenaar8299df92004-07-10 09:47:34 +00003578/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003579 * Paste a yank register into the command line.
3580 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581 * insert_reg() can't be used here, because special characters from the
3582 * register contents will be interpreted as commands.
3583 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003584 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003585 */
3586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003587cmdline_paste(
3588 int regname,
3589 int literally, /* Insert text literally instead of "as typed" */
3590 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003591{
3592 long i;
3593 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003594 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003595 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596
3597 /* check for valid regname; also accept special characters for CTRL-R in
3598 * the command line */
3599 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003600 && regname != Ctrl_A && regname != Ctrl_L
3601 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003602 return FAIL;
3603
3604 /* A register containing CTRL-R can cause an endless loop. Allow using
3605 * CTRL-C to break the loop. */
3606 line_breakcheck();
3607 if (got_int)
3608 return FAIL;
3609
3610#ifdef FEAT_CLIPBOARD
3611 regname = may_get_selection(regname);
3612#endif
3613
Bram Moolenaar438d1762018-09-30 17:11:48 +02003614 // Need to set "textlock" to avoid nasty things like going to another
3615 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003616 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003617 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003618 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003619
3620 if (i)
3621 {
3622 /* Got the value of a special register in "arg". */
3623 if (arg == NULL)
3624 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003625
3626 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3627 * part of the word. */
3628 p = arg;
3629 if (p_is && regname == Ctrl_W)
3630 {
3631 char_u *w;
3632 int len;
3633
3634 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003635 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003636 {
3637#ifdef FEAT_MBYTE
3638 if (has_mbyte)
3639 {
3640 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3641 if (!vim_iswordc(mb_ptr2char(w - len)))
3642 break;
3643 w -= len;
3644 }
3645 else
3646#endif
3647 {
3648 if (!vim_iswordc(w[-1]))
3649 break;
3650 --w;
3651 }
3652 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003653 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003654 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3655 p += len;
3656 }
3657
3658 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003659 if (allocated)
3660 vim_free(arg);
3661 return OK;
3662 }
3663
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003664 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003665}
3666
3667/*
3668 * Put a string on the command line.
3669 * When "literally" is TRUE, insert literally.
3670 * When "literally" is FALSE, insert as typed, but don't leave the command
3671 * line.
3672 */
3673 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003674cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003675{
3676 int c, cv;
3677
3678 if (literally)
3679 put_on_cmdline(s, -1, TRUE);
3680 else
3681 while (*s != NUL)
3682 {
3683 cv = *s;
3684 if (cv == Ctrl_V && s[1])
3685 ++s;
3686#ifdef FEAT_MBYTE
3687 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003688 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003689 else
3690#endif
3691 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003692 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3693 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003694#ifdef UNIX
3695 || c == intr_char
3696#endif
3697 || (c == Ctrl_BSL && *s == Ctrl_N))
3698 stuffcharReadbuff(Ctrl_V);
3699 stuffcharReadbuff(c);
3700 }
3701}
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#ifdef FEAT_WILDMENU
3704/*
3705 * Delete characters on the command line, from "from" to the current
3706 * position.
3707 */
3708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003709cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3712 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3713 ccline.cmdlen -= ccline.cmdpos - from;
3714 ccline.cmdpos = from;
3715}
3716#endif
3717
3718/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003719 * This function is called when the screen size changes and with incremental
3720 * search and in other situations where the command line may have been
3721 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 */
3723 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003724redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003726 redrawcmdline_ex(TRUE);
3727}
3728
3729 void
3730redrawcmdline_ex(int do_compute_cmdrow)
3731{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (cmd_silent)
3733 return;
3734 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003735 if (do_compute_cmdrow)
3736 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 redrawcmd();
3738 cursorcmd();
3739}
3740
3741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003742redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 int i;
3745
3746 if (cmd_silent)
3747 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003748 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 msg_putchar(ccline.cmdfirstc);
3750 if (ccline.cmdprompt != NULL)
3751 {
3752 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3753 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3754 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003755 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 --ccline.cmdindent;
3757 }
3758 else
3759 for (i = ccline.cmdindent; i > 0; --i)
3760 msg_putchar(' ');
3761}
3762
3763/*
3764 * Redraw what is currently on the command line.
3765 */
3766 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003767redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
3769 if (cmd_silent)
3770 return;
3771
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003772 /* when 'incsearch' is set there may be no command line while redrawing */
3773 if (ccline.cmdbuff == NULL)
3774 {
3775 windgoto(cmdline_row, 0);
3776 msg_clr_eos();
3777 return;
3778 }
3779
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 msg_start();
3781 redrawcmdprompt();
3782
3783 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3784 msg_no_more = TRUE;
3785 draw_cmdline(0, ccline.cmdlen);
3786 msg_clr_eos();
3787 msg_no_more = FALSE;
3788
3789 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003790 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003791 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 /*
3794 * An emsg() before may have set msg_scroll. This is used in normal mode,
3795 * in cmdline mode we can reset them now.
3796 */
3797 msg_scroll = FALSE; /* next message overwrites cmdline */
3798
3799 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3800 * in cmdline mode */
3801 skip_redraw = FALSE;
3802}
3803
3804 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003805compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003807 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 cmdline_row = Rows - 1;
3809 else
3810 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003811 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812}
3813
3814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003815cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 if (cmd_silent)
3818 return;
3819
3820#ifdef FEAT_RIGHTLEFT
3821 if (cmdmsg_rl)
3822 {
3823 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3824 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3825 if (msg_row <= 0)
3826 msg_row = Rows - 1;
3827 }
3828 else
3829#endif
3830 {
3831 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3832 msg_col = ccline.cmdspos % (int)Columns;
3833 if (msg_row >= Rows)
3834 msg_row = Rows - 1;
3835 }
3836
3837 windgoto(msg_row, msg_col);
3838#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003839 if (p_imst == IM_ON_THE_SPOT)
3840 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841#endif
3842#ifdef MCH_CURSOR_SHAPE
3843 mch_update_cursor();
3844#endif
3845}
3846
3847 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003848gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849{
3850 msg_start();
3851#ifdef FEAT_RIGHTLEFT
3852 if (cmdmsg_rl)
3853 msg_col = Columns - 1;
3854 else
3855#endif
3856 msg_col = 0; /* always start in column 0 */
3857 if (clr) /* clear the bottom line(s) */
3858 msg_clr_eos(); /* will reset clear_cmdline */
3859 windgoto(cmdline_row, 0);
3860}
3861
3862/*
3863 * Check the word in front of the cursor for an abbreviation.
3864 * Called when the non-id character "c" has been entered.
3865 * When an abbreviation is recognized it is removed from the text with
3866 * backspaces and the replacement string is inserted, followed by "c".
3867 */
3868 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003869ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003871 int spos = 0;
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3874 return FALSE;
3875
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003876 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3877 * Actually accepts any mark. */
3878 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3879 spos++;
3880 if (ccline.cmdlen - spos > 5
3881 && ccline.cmdbuff[spos] == '\''
3882 && ccline.cmdbuff[spos + 2] == ','
3883 && ccline.cmdbuff[spos + 3] == '\'')
3884 spos += 5;
3885 else
3886 /* check abbreviation from the beginning of the commandline */
3887 spos = 0;
3888
3889 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890}
3891
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003892#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3893 static int
3894#ifdef __BORLANDC__
3895_RTLENTRYF
3896#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003897sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003898{
3899 char_u *p1 = *(char_u **)s1;
3900 char_u *p2 = *(char_u **)s2;
3901
3902 if (*p1 != '<' && *p2 == '<') return -1;
3903 if (*p1 == '<' && *p2 != '<') return 1;
3904 return STRCMP(p1, p2);
3905}
3906#endif
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908/*
3909 * Return FAIL if this is not an appropriate context in which to do
3910 * completion of anything, return OK if it is (even if there are no matches).
3911 * For the caller, this means that the character is just passed through like a
3912 * normal character (instead of being expanded). This allows :s/^I^D etc.
3913 */
3914 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003915nextwild(
3916 expand_T *xp,
3917 int type,
3918 int options, /* extra options for ExpandOne() */
3919 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
3921 int i, j;
3922 char_u *p1;
3923 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 int difflen;
3925 int v;
3926
3927 if (xp->xp_numfiles == -1)
3928 {
3929 set_expand_context(xp);
3930 cmd_showtail = expand_showtail(xp);
3931 }
3932
3933 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3934 {
3935 beep_flush();
3936 return OK; /* Something illegal on command line */
3937 }
3938 if (xp->xp_context == EXPAND_NOTHING)
3939 {
3940 /* Caller can use the character as a normal char instead */
3941 return FAIL;
3942 }
3943
3944 MSG_PUTS("..."); /* show that we are busy */
3945 out_flush();
3946
3947 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003948 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 if (type == WILD_NEXT || type == WILD_PREV)
3951 {
3952 /*
3953 * Get next/previous match for a previous expanded pattern.
3954 */
3955 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3956 }
3957 else
3958 {
3959 /*
3960 * Translate string into pattern and expand it.
3961 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003962 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3963 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 p2 = NULL;
3965 else
3966 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003967 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003968 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3969 if (escape)
3970 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003971
3972 if (p_wic)
3973 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003974 p2 = ExpandOne(xp, p1,
3975 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003976 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003978 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (p2 != NULL && type == WILD_LONGEST)
3980 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003981 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (ccline.cmdbuff[i + j] == '*'
3983 || ccline.cmdbuff[i + j] == '?')
3984 break;
3985 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003986 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
3988 }
3989 }
3990
3991 if (p2 != NULL && !got_int)
3992 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003993 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003994 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003996 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 xp->xp_pattern = ccline.cmdbuff + i;
3998 }
3999 else
4000 v = OK;
4001 if (v == OK)
4002 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004003 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4004 &ccline.cmdbuff[ccline.cmdpos],
4005 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4006 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 ccline.cmdlen += difflen;
4008 ccline.cmdpos += difflen;
4009 }
4010 }
4011 vim_free(p2);
4012
4013 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004014 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 /* When expanding a ":map" command and no matches are found, assume that
4017 * the key is supposed to be inserted literally */
4018 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4019 return FAIL;
4020
4021 if (xp->xp_numfiles <= 0 && p2 == NULL)
4022 beep_flush();
4023 else if (xp->xp_numfiles == 1)
4024 /* free expanded pattern */
4025 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4026
4027 return OK;
4028}
4029
4030/*
4031 * Do wildcard expansion on the string 'str'.
4032 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004033 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * Return NULL for failure.
4035 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004036 * "orig" is the originally expanded string, copied to allocated memory. It
4037 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4038 * WILD_PREV "orig" should be NULL.
4039 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004040 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4041 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 *
4043 * mode = WILD_FREE: just free previously expanded matches
4044 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4045 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4046 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4047 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4048 * mode = WILD_ALL: return all matches concatenated
4049 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004050 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 *
4052 * options = WILD_LIST_NOTFOUND: list entries without a match
4053 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4054 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4055 * options = WILD_NO_BEEP: Don't beep for multiple matches
4056 * options = WILD_ADD_SLASH: add a slash after directory names
4057 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4058 * options = WILD_SILENT: don't print warning messages
4059 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004060 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 *
4062 * The variables xp->xp_context and xp->xp_backslash must have been set!
4063 */
4064 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004065ExpandOne(
4066 expand_T *xp,
4067 char_u *str,
4068 char_u *orig, /* allocated copy of original of expanded string */
4069 int options,
4070 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
4072 char_u *ss = NULL;
4073 static int findex;
4074 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004075 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 int i;
4077 long_u len;
4078 int non_suf_match; /* number without matching suffix */
4079
4080 /*
4081 * first handle the case of using an old match
4082 */
4083 if (mode == WILD_NEXT || mode == WILD_PREV)
4084 {
4085 if (xp->xp_numfiles > 0)
4086 {
4087 if (mode == WILD_PREV)
4088 {
4089 if (findex == -1)
4090 findex = xp->xp_numfiles;
4091 --findex;
4092 }
4093 else /* mode == WILD_NEXT */
4094 ++findex;
4095
4096 /*
4097 * When wrapping around, return the original string, set findex to
4098 * -1.
4099 */
4100 if (findex < 0)
4101 {
4102 if (orig_save == NULL)
4103 findex = xp->xp_numfiles - 1;
4104 else
4105 findex = -1;
4106 }
4107 if (findex >= xp->xp_numfiles)
4108 {
4109 if (orig_save == NULL)
4110 findex = 0;
4111 else
4112 findex = -1;
4113 }
4114#ifdef FEAT_WILDMENU
4115 if (p_wmnu)
4116 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4117 findex, cmd_showtail);
4118#endif
4119 if (findex == -1)
4120 return vim_strsave(orig_save);
4121 return vim_strsave(xp->xp_files[findex]);
4122 }
4123 else
4124 return NULL;
4125 }
4126
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004127 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4129 {
4130 FreeWild(xp->xp_numfiles, xp->xp_files);
4131 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004132 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134 findex = 0;
4135
4136 if (mode == WILD_FREE) /* only release file name */
4137 return NULL;
4138
4139 if (xp->xp_numfiles == -1)
4140 {
4141 vim_free(orig_save);
4142 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004143 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
4146 * Do the expansion.
4147 */
4148 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4149 options) == FAIL)
4150 {
4151#ifdef FNAME_ILLEGAL
4152 /* Illegal file name has been silently skipped. But when there
4153 * are wildcards, the real problem is that there was no match,
4154 * causing the pattern to be added, which has illegal characters.
4155 */
4156 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4157 EMSG2(_(e_nomatch2), str);
4158#endif
4159 }
4160 else if (xp->xp_numfiles == 0)
4161 {
4162 if (!(options & WILD_SILENT))
4163 EMSG2(_(e_nomatch2), str);
4164 }
4165 else
4166 {
4167 /* Escape the matches for use on the command line. */
4168 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4169
4170 /*
4171 * Check for matching suffixes in file names.
4172 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004173 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4174 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176 if (xp->xp_numfiles)
4177 non_suf_match = xp->xp_numfiles;
4178 else
4179 non_suf_match = 1;
4180 if ((xp->xp_context == EXPAND_FILES
4181 || xp->xp_context == EXPAND_DIRECTORIES)
4182 && xp->xp_numfiles > 1)
4183 {
4184 /*
4185 * More than one match; check suffix.
4186 * The files will have been sorted on matching suffix in
4187 * expand_wildcards, only need to check the first two.
4188 */
4189 non_suf_match = 0;
4190 for (i = 0; i < 2; ++i)
4191 if (match_suffix(xp->xp_files[i]))
4192 ++non_suf_match;
4193 }
4194 if (non_suf_match != 1)
4195 {
4196 /* Can we ever get here unless it's while expanding
4197 * interactively? If not, we can get rid of this all
4198 * together. Don't really want to wait for this message
4199 * (and possibly have to hit return to continue!).
4200 */
4201 if (!(options & WILD_SILENT))
4202 EMSG(_(e_toomany));
4203 else if (!(options & WILD_NO_BEEP))
4204 beep_flush();
4205 }
4206 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4207 ss = vim_strsave(xp->xp_files[0]);
4208 }
4209 }
4210 }
4211
4212 /* Find longest common part */
4213 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4214 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004215 int mb_len = 1;
4216 int c0, ci;
4217
4218 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004220#ifdef FEAT_MBYTE
4221 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004223 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4224 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4225 }
4226 else
4227#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004228 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004229 for (i = 1; i < xp->xp_numfiles; ++i)
4230 {
4231#ifdef FEAT_MBYTE
4232 if (has_mbyte)
4233 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4234 else
4235#endif
4236 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004237 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004239 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004240 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004242 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 break;
4244 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004245 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 break;
4247 }
4248 if (i < xp->xp_numfiles)
4249 {
4250 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004251 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 break;
4253 }
4254 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 ss = alloc((unsigned)len + 1);
4257 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004258 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 findex = -1; /* next p_wc gets first one */
4260 }
4261
4262 /* Concatenate all matching names */
4263 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4264 {
4265 len = 0;
4266 for (i = 0; i < xp->xp_numfiles; ++i)
4267 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4268 ss = lalloc(len, TRUE);
4269 if (ss != NULL)
4270 {
4271 *ss = NUL;
4272 for (i = 0; i < xp->xp_numfiles; ++i)
4273 {
4274 STRCAT(ss, xp->xp_files[i]);
4275 if (i != xp->xp_numfiles - 1)
4276 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4277 }
4278 }
4279 }
4280
4281 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4282 ExpandCleanup(xp);
4283
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004284 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004285 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004286 vim_free(orig);
4287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return ss;
4289}
4290
4291/*
4292 * Prepare an expand structure for use.
4293 */
4294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004295ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004297 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004298 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004300#ifndef BACKSLASH_IN_FILENAME
4301 xp->xp_shell = FALSE;
4302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 xp->xp_numfiles = -1;
4304 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004305#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4306 xp->xp_arg = NULL;
4307#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004308 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309}
4310
4311/*
4312 * Cleanup an expand structure after use.
4313 */
4314 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004315ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
4317 if (xp->xp_numfiles >= 0)
4318 {
4319 FreeWild(xp->xp_numfiles, xp->xp_files);
4320 xp->xp_numfiles = -1;
4321 }
4322}
4323
4324 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004325ExpandEscape(
4326 expand_T *xp,
4327 char_u *str,
4328 int numfiles,
4329 char_u **files,
4330 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331{
4332 int i;
4333 char_u *p;
4334
4335 /*
4336 * May change home directory back to "~"
4337 */
4338 if (options & WILD_HOME_REPLACE)
4339 tilde_replace(str, numfiles, files);
4340
4341 if (options & WILD_ESCAPE)
4342 {
4343 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004344 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004345 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 || xp->xp_context == EXPAND_BUFFERS
4347 || xp->xp_context == EXPAND_DIRECTORIES)
4348 {
4349 /*
4350 * Insert a backslash into a file name before a space, \, %, #
4351 * and wildmatch characters, except '~'.
4352 */
4353 for (i = 0; i < numfiles; ++i)
4354 {
4355 /* for ":set path=" we need to escape spaces twice */
4356 if (xp->xp_backslash == XP_BS_THREE)
4357 {
4358 p = vim_strsave_escaped(files[i], (char_u *)" ");
4359 if (p != NULL)
4360 {
4361 vim_free(files[i]);
4362 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004363#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 p = vim_strsave_escaped(files[i], (char_u *)" ");
4365 if (p != NULL)
4366 {
4367 vim_free(files[i]);
4368 files[i] = p;
4369 }
4370#endif
4371 }
4372 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004373#ifdef BACKSLASH_IN_FILENAME
4374 p = vim_strsave_fnameescape(files[i], FALSE);
4375#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004376 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (p != NULL)
4379 {
4380 vim_free(files[i]);
4381 files[i] = p;
4382 }
4383
4384 /* If 'str' starts with "\~", replace "~" at start of
4385 * files[i] with "\~". */
4386 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004387 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004390
4391 /* If the first file starts with a '+' escape it. Otherwise it
4392 * could be seen as "+cmd". */
4393 if (*files[0] == '+')
4394 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396 else if (xp->xp_context == EXPAND_TAGS)
4397 {
4398 /*
4399 * Insert a backslash before characters in a tag name that
4400 * would terminate the ":tag" command.
4401 */
4402 for (i = 0; i < numfiles; ++i)
4403 {
4404 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4405 if (p != NULL)
4406 {
4407 vim_free(files[i]);
4408 files[i] = p;
4409 }
4410 }
4411 }
4412 }
4413}
4414
4415/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004416 * Escape special characters in "fname" for when used as a file name argument
4417 * after a Vim command, or, when "shell" is non-zero, a shell command.
4418 * Returns the result in allocated memory.
4419 */
4420 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004421vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004422{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004423 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004424#ifdef BACKSLASH_IN_FILENAME
4425 char_u buf[20];
4426 int j = 0;
4427
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004428 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004429 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004430 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004431 buf[j++] = *p;
4432 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004433 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004434#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004435 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4436 if (shell && csh_like_shell() && p != NULL)
4437 {
4438 char_u *s;
4439
4440 /* For csh and similar shells need to put two backslashes before '!'.
4441 * One is taken by Vim, one by the shell. */
4442 s = vim_strsave_escaped(p, (char_u *)"!");
4443 vim_free(p);
4444 p = s;
4445 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004446#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004447
4448 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4449 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004450 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004451 escape_fname(&p);
4452
4453 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004454}
4455
4456/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004457 * Put a backslash before the file name in "pp", which is in allocated memory.
4458 */
4459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004460escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004461{
4462 char_u *p;
4463
4464 p = alloc((unsigned)(STRLEN(*pp) + 2));
4465 if (p != NULL)
4466 {
4467 p[0] = '\\';
4468 STRCPY(p + 1, *pp);
4469 vim_free(*pp);
4470 *pp = p;
4471 }
4472}
4473
4474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 * For each file name in files[num_files]:
4476 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4477 */
4478 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004479tilde_replace(
4480 char_u *orig_pat,
4481 int num_files,
4482 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483{
4484 int i;
4485 char_u *p;
4486
4487 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4488 {
4489 for (i = 0; i < num_files; ++i)
4490 {
4491 p = home_replace_save(NULL, files[i]);
4492 if (p != NULL)
4493 {
4494 vim_free(files[i]);
4495 files[i] = p;
4496 }
4497 }
4498 }
4499}
4500
4501/*
4502 * Show all matches for completion on the command line.
4503 * Returns EXPAND_NOTHING when the character that triggered expansion should
4504 * be inserted like a normal character.
4505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004507showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508{
4509#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4510 int num_files;
4511 char_u **files_found;
4512 int i, j, k;
4513 int maxlen;
4514 int lines;
4515 int columns;
4516 char_u *p;
4517 int lastlen;
4518 int attr;
4519 int showtail;
4520
4521 if (xp->xp_numfiles == -1)
4522 {
4523 set_expand_context(xp);
4524 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4525 &num_files, &files_found);
4526 showtail = expand_showtail(xp);
4527 if (i != EXPAND_OK)
4528 return i;
4529
4530 }
4531 else
4532 {
4533 num_files = xp->xp_numfiles;
4534 files_found = xp->xp_files;
4535 showtail = cmd_showtail;
4536 }
4537
4538#ifdef FEAT_WILDMENU
4539 if (!wildmenu)
4540 {
4541#endif
4542 msg_didany = FALSE; /* lines_left will be set */
4543 msg_start(); /* prepare for paging */
4544 msg_putchar('\n');
4545 out_flush();
4546 cmdline_row = msg_row;
4547 msg_didany = FALSE; /* lines_left will be set again */
4548 msg_start(); /* prepare for paging */
4549#ifdef FEAT_WILDMENU
4550 }
4551#endif
4552
4553 if (got_int)
4554 got_int = FALSE; /* only int. the completion, not the cmd line */
4555#ifdef FEAT_WILDMENU
4556 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004557 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#endif
4559 else
4560 {
4561 /* find the length of the longest file name */
4562 maxlen = 0;
4563 for (i = 0; i < num_files; ++i)
4564 {
4565 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004566 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 || xp->xp_context == EXPAND_BUFFERS))
4568 {
4569 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4570 j = vim_strsize(NameBuff);
4571 }
4572 else
4573 j = vim_strsize(L_SHOWFILE(i));
4574 if (j > maxlen)
4575 maxlen = j;
4576 }
4577
4578 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4579 lines = num_files;
4580 else
4581 {
4582 /* compute the number of columns and lines for the listing */
4583 maxlen += 2; /* two spaces between file names */
4584 columns = ((int)Columns + 2) / maxlen;
4585 if (columns < 1)
4586 columns = 1;
4587 lines = (num_files + columns - 1) / columns;
4588 }
4589
Bram Moolenaar8820b482017-03-16 17:23:31 +01004590 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4593 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004594 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 msg_clr_eos();
4596 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004597 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599
4600 /* list the files line by line */
4601 for (i = 0; i < lines; ++i)
4602 {
4603 lastlen = 999;
4604 for (k = i; k < num_files; k += lines)
4605 {
4606 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4607 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004608 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 p = files_found[k] + STRLEN(files_found[k]) + 1;
4610 msg_advance(maxlen + 1);
4611 msg_puts(p);
4612 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004613 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 break;
4615 }
4616 for (j = maxlen - lastlen; --j >= 0; )
4617 msg_putchar(' ');
4618 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004619 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 || xp->xp_context == EXPAND_BUFFERS)
4621 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004622 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004623 if (xp->xp_numfiles != -1)
4624 {
4625 char_u *halved_slash;
4626 char_u *exp_path;
4627
4628 /* Expansion was done before and special characters
4629 * were escaped, need to halve backslashes. Also
4630 * $HOME has been replaced with ~/. */
4631 exp_path = expand_env_save_opt(files_found[k], TRUE);
4632 halved_slash = backslash_halve_save(
4633 exp_path != NULL ? exp_path : files_found[k]);
4634 j = mch_isdir(halved_slash != NULL ? halved_slash
4635 : files_found[k]);
4636 vim_free(exp_path);
4637 vim_free(halved_slash);
4638 }
4639 else
4640 /* Expansion was done here, file names are literal. */
4641 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (showtail)
4643 p = L_SHOWFILE(k);
4644 else
4645 {
4646 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4647 TRUE);
4648 p = NameBuff;
4649 }
4650 }
4651 else
4652 {
4653 j = FALSE;
4654 p = L_SHOWFILE(k);
4655 }
4656 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4657 }
4658 if (msg_col > 0) /* when not wrapped around */
4659 {
4660 msg_clr_eos();
4661 msg_putchar('\n');
4662 }
4663 out_flush(); /* show one line at a time */
4664 if (got_int)
4665 {
4666 got_int = FALSE;
4667 break;
4668 }
4669 }
4670
4671 /*
4672 * we redraw the command below the lines that we have just listed
4673 * This is a bit tricky, but it saves a lot of screen updating.
4674 */
4675 cmdline_row = msg_row; /* will put it back later */
4676 }
4677
4678 if (xp->xp_numfiles == -1)
4679 FreeWild(num_files, files_found);
4680
4681 return EXPAND_OK;
4682}
4683
4684/*
4685 * Private gettail for showmatches() (and win_redr_status_matches()):
4686 * Find tail of file name path, but ignore trailing "/".
4687 */
4688 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004689sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 char_u *p;
4692 char_u *t = s;
4693 int had_sep = FALSE;
4694
4695 for (p = s; *p != NUL; )
4696 {
4697 if (vim_ispathsep(*p)
4698#ifdef BACKSLASH_IN_FILENAME
4699 && !rem_backslash(p)
4700#endif
4701 )
4702 had_sep = TRUE;
4703 else if (had_sep)
4704 {
4705 t = p;
4706 had_sep = FALSE;
4707 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004708 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710 return t;
4711}
4712
4713/*
4714 * Return TRUE if we only need to show the tail of completion matches.
4715 * When not completing file names or there is a wildcard in the path FALSE is
4716 * returned.
4717 */
4718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 char_u *s;
4722 char_u *end;
4723
4724 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004725 if (xp->xp_context != EXPAND_FILES
4726 && xp->xp_context != EXPAND_SHELLCMD
4727 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return FALSE;
4729
4730 end = gettail(xp->xp_pattern);
4731 if (end == xp->xp_pattern) /* there is no path separator */
4732 return FALSE;
4733
4734 for (s = xp->xp_pattern; s < end; s++)
4735 {
4736 /* Skip escaped wildcards. Only when the backslash is not a path
4737 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4738 if (rem_backslash(s))
4739 ++s;
4740 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4741 return FALSE;
4742 }
4743 return TRUE;
4744}
4745
4746/*
4747 * Prepare a string for expansion.
4748 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004749 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 * When expanding other names: The string will be used with regcomp(). Copy
4751 * the name into allocated memory and prepend "^".
4752 */
4753 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754addstar(
4755 char_u *fname,
4756 int len,
4757 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 char_u *retval;
4760 int i, j;
4761 int new_len;
4762 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004763 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004765 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004766 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004767 && context != EXPAND_SHELLCMD
4768 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 /*
4771 * Matching will be done internally (on something other than files).
4772 * So we convert the file-matching-type wildcards into our kind for
4773 * use with vim_regcomp(). First work out how long it will be:
4774 */
4775
4776 /* For help tags the translation is done in find_help_tags().
4777 * For a tag pattern starting with "/" no translation is needed. */
4778 if (context == EXPAND_HELP
4779 || context == EXPAND_COLORS
4780 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004781 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004782 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004783 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004784 || ((context == EXPAND_TAGS_LISTFILES
4785 || context == EXPAND_TAGS)
4786 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 retval = vim_strnsave(fname, len);
4788 else
4789 {
4790 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4791 for (i = 0; i < len; i++)
4792 {
4793 if (fname[i] == '*' || fname[i] == '~')
4794 new_len++; /* '*' needs to be replaced by ".*"
4795 '~' needs to be replaced by "\~" */
4796
4797 /* Buffer names are like file names. "." should be literal */
4798 if (context == EXPAND_BUFFERS && fname[i] == '.')
4799 new_len++; /* "." becomes "\." */
4800
4801 /* Custom expansion takes care of special things, match
4802 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004803 if ((context == EXPAND_USER_DEFINED
4804 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 new_len++; /* '\' becomes "\\" */
4806 }
4807 retval = alloc(new_len);
4808 if (retval != NULL)
4809 {
4810 retval[0] = '^';
4811 j = 1;
4812 for (i = 0; i < len; i++, j++)
4813 {
4814 /* Skip backslash. But why? At least keep it for custom
4815 * expansion. */
4816 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004817 && context != EXPAND_USER_LIST
4818 && fname[i] == '\\'
4819 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 break;
4821
4822 switch (fname[i])
4823 {
4824 case '*': retval[j++] = '.';
4825 break;
4826 case '~': retval[j++] = '\\';
4827 break;
4828 case '?': retval[j] = '.';
4829 continue;
4830 case '.': if (context == EXPAND_BUFFERS)
4831 retval[j++] = '\\';
4832 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004833 case '\\': if (context == EXPAND_USER_DEFINED
4834 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 retval[j++] = '\\';
4836 break;
4837 }
4838 retval[j] = fname[i];
4839 }
4840 retval[j] = NUL;
4841 }
4842 }
4843 }
4844 else
4845 {
4846 retval = alloc(len + 4);
4847 if (retval != NULL)
4848 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004849 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004852 * Don't add a star to *, ~, ~user, $var or `cmd`.
4853 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 * ~ would be at the start of the file name, but not the tail.
4855 * $ could be anywhere in the tail.
4856 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004857 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 */
4859 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004860 ends_in_star = (len > 0 && retval[len - 1] == '*');
4861#ifndef BACKSLASH_IN_FILENAME
4862 for (i = len - 2; i >= 0; --i)
4863 {
4864 if (retval[i] != '\\')
4865 break;
4866 ends_in_star = !ends_in_star;
4867 }
4868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004870 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 && vim_strchr(tail, '$') == NULL
4872 && vim_strchr(retval, '`') == NULL)
4873 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004874 else if (len > 0 && retval[len - 1] == '$')
4875 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 retval[len] = NUL;
4877 }
4878 }
4879 return retval;
4880}
4881
4882/*
4883 * Must parse the command line so far to work out what context we are in.
4884 * Completion can then be done based on that context.
4885 * This routine sets the variables:
4886 * xp->xp_pattern The start of the pattern to be expanded within
4887 * the command line (ends at the cursor).
4888 * xp->xp_context The type of thing to expand. Will be one of:
4889 *
4890 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4891 * the command line, like an unknown command. Caller
4892 * should beep.
4893 * EXPAND_NOTHING Unrecognised context for completion, use char like
4894 * a normal char, rather than for completion. eg
4895 * :s/^I/
4896 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4897 * it.
4898 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4899 * EXPAND_FILES After command with XFILE set, or after setting
4900 * with P_EXPAND set. eg :e ^I, :w>>^I
4901 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4902 * when we know only directories are of interest. eg
4903 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004904 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4906 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4907 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4908 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4909 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4910 * EXPAND_EVENTS Complete event names
4911 * EXPAND_SYNTAX Complete :syntax command arguments
4912 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4913 * EXPAND_AUGROUP Complete autocommand group names
4914 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4915 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4916 * eg :unmap a^I , :cunab x^I
4917 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4918 * eg :call sub^I
4919 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4920 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4921 * names in expressions, eg :while s^I
4922 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004923 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 */
4925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004926set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004928 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (ccline.cmdfirstc != ':'
4930#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004931 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004932 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933#endif
4934 )
4935 {
4936 xp->xp_context = EXPAND_NOTHING;
4937 return;
4938 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004939 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940}
4941
4942 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004943set_cmd_context(
4944 expand_T *xp,
4945 char_u *str, /* start of command line */
4946 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004947 int col, /* position of cursor */
4948 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
4950 int old_char = NUL;
4951 char_u *nextcomm;
4952
4953 /*
4954 * Avoid a UMR warning from Purify, only save the character if it has been
4955 * written before.
4956 */
4957 if (col < len)
4958 old_char = str[col];
4959 str[col] = NUL;
4960 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004961
4962#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004963 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004964 {
4965# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004966 /* pass CMD_SIZE because there is no real command */
4967 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004968# endif
4969 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004970 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004971 {
4972 xp->xp_context = ccline.xp_context;
4973 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004974# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004975 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004976# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004977 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004978 else
4979#endif
4980 while (nextcomm != NULL)
4981 nextcomm = set_one_cmd_context(xp, nextcomm);
4982
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004983 /* Store the string here so that call_user_expand_func() can get to them
4984 * easily. */
4985 xp->xp_line = str;
4986 xp->xp_col = col;
4987
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 str[col] = old_char;
4989}
4990
4991/*
4992 * Expand the command line "str" from context "xp".
4993 * "xp" must have been set by set_cmd_context().
4994 * xp->xp_pattern points into "str", to where the text that is to be expanded
4995 * starts.
4996 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4997 * cursor.
4998 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4999 * key that triggered expansion literally.
5000 * Returns EXPAND_OK otherwise.
5001 */
5002 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005003expand_cmdline(
5004 expand_T *xp,
5005 char_u *str, /* start of command line */
5006 int col, /* position of cursor */
5007 int *matchcount, /* return: nr of matches */
5008 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
5010 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005011 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5014 {
5015 beep_flush();
5016 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5017 }
5018 if (xp->xp_context == EXPAND_NOTHING)
5019 {
5020 /* Caller can use the character as a normal char instead */
5021 return EXPAND_NOTHING;
5022 }
5023
5024 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005025 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5026 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (file_str == NULL)
5028 return EXPAND_UNSUCCESSFUL;
5029
Bram Moolenaar94950a92010-12-02 16:01:29 +01005030 if (p_wic)
5031 options += WILD_ICASE;
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005034 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
5036 *matchcount = 0;
5037 *matches = NULL;
5038 }
5039 vim_free(file_str);
5040
5041 return EXPAND_OK;
5042}
5043
5044#ifdef FEAT_MULTI_LANG
5045/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005046 * Cleanup matches for help tags:
5047 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5048 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005051cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
5053 int i, j;
5054 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005055 char_u buf[4];
5056 char_u *p = buf;
5057
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005058 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005059 {
5060 *p++ = '@';
5061 *p++ = p_hlg[0];
5062 *p++ = p_hlg[1];
5063 }
5064 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 for (i = 0; i < num_file; ++i)
5067 {
5068 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005069 if (len <= 0)
5070 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005071 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 /* Sorting on priority means the same item in another language may
5074 * be anywhere. Search all items for a match up to the "@en". */
5075 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005076 if (j != i && (int)STRLEN(file[j]) == len + 3
5077 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005080 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 file[i][len] = NUL;
5082 }
5083 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005084
5085 if (*buf != NUL)
5086 for (i = 0; i < num_file; ++i)
5087 {
5088 len = (int)STRLEN(file[i]) - 3;
5089 if (len <= 0)
5090 continue;
5091 if (STRCMP(file[i] + len, buf) == 0)
5092 {
5093 /* remove the default language */
5094 file[i][len] = NUL;
5095 }
5096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097}
5098#endif
5099
5100/*
5101 * Do the expansion based on xp->xp_context and "pat".
5102 */
5103 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005104ExpandFromContext(
5105 expand_T *xp,
5106 char_u *pat,
5107 int *num_file,
5108 char_u ***file,
5109 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
5111#ifdef FEAT_CMDL_COMPL
5112 regmatch_T regmatch;
5113#endif
5114 int ret;
5115 int flags;
5116
5117 flags = EW_DIR; /* include directories */
5118 if (options & WILD_LIST_NOTFOUND)
5119 flags |= EW_NOTFOUND;
5120 if (options & WILD_ADD_SLASH)
5121 flags |= EW_ADDSLASH;
5122 if (options & WILD_KEEP_ALL)
5123 flags |= EW_KEEPALL;
5124 if (options & WILD_SILENT)
5125 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005126 if (options & WILD_ALLLINKS)
5127 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005129 if (xp->xp_context == EXPAND_FILES
5130 || xp->xp_context == EXPAND_DIRECTORIES
5131 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 /*
5134 * Expand file or directory names.
5135 */
5136 int free_pat = FALSE;
5137 int i;
5138
5139 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5140 * space */
5141 if (xp->xp_backslash != XP_BS_NONE)
5142 {
5143 free_pat = TRUE;
5144 pat = vim_strsave(pat);
5145 for (i = 0; pat[i]; ++i)
5146 if (pat[i] == '\\')
5147 {
5148 if (xp->xp_backslash == XP_BS_THREE
5149 && pat[i + 1] == '\\'
5150 && pat[i + 2] == '\\'
5151 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005152 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (xp->xp_backslash == XP_BS_ONE
5154 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005155 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157 }
5158
5159 if (xp->xp_context == EXPAND_FILES)
5160 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005161 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5162 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 else
5164 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005165 if (options & WILD_ICASE)
5166 flags |= EW_ICASE;
5167
Bram Moolenaard7834d32009-12-02 16:14:36 +00005168 /* Expand wildcards, supporting %:h and the like. */
5169 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (free_pat)
5171 vim_free(pat);
5172 return ret;
5173 }
5174
5175 *file = (char_u **)"";
5176 *num_file = 0;
5177 if (xp->xp_context == EXPAND_HELP)
5178 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005179 /* With an empty argument we would get all the help tags, which is
5180 * very slow. Get matches for "help" instead. */
5181 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5182 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
5184#ifdef FEAT_MULTI_LANG
5185 cleanup_help_tags(*num_file, *file);
5186#endif
5187 return OK;
5188 }
5189 return FAIL;
5190 }
5191
5192#ifndef FEAT_CMDL_COMPL
5193 return FAIL;
5194#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005195 if (xp->xp_context == EXPAND_SHELLCMD)
5196 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (xp->xp_context == EXPAND_OLD_SETTING)
5198 return ExpandOldSetting(num_file, file);
5199 if (xp->xp_context == EXPAND_BUFFERS)
5200 return ExpandBufnames(pat, num_file, file, options);
5201 if (xp->xp_context == EXPAND_TAGS
5202 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5203 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5204 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005205 {
5206 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005207 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5208 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005211 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005212 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005213 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005214 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005215 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005216 {
5217 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005218 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005219 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005220 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005221 {
5222 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005223 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005224 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005225# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5226 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005227 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005228# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005229 if (xp->xp_context == EXPAND_PACKADD)
5230 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
5232 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5233 if (regmatch.regprog == NULL)
5234 return FAIL;
5235
5236 /* set ignore-case according to p_ic, p_scs and pat */
5237 regmatch.rm_ic = ignorecase(pat);
5238
5239 if (xp->xp_context == EXPAND_SETTINGS
5240 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5241 ret = ExpandSettings(xp, &regmatch, num_file, file);
5242 else if (xp->xp_context == EXPAND_MAPPINGS)
5243 ret = ExpandMappings(&regmatch, num_file, file);
5244# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5245 else if (xp->xp_context == EXPAND_USER_DEFINED)
5246 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5247# endif
5248 else
5249 {
5250 static struct expgen
5251 {
5252 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005253 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005255 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 } tab[] =
5257 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005258 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5259 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005260 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005261 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005262#ifdef FEAT_CMDHIST
5263 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005266 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005267 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005268 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5269 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5270 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#endif
5272#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005273 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5274 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5275 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5276 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#endif
5278#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005279 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5280 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281#endif
5282#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005283 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005285#ifdef FEAT_PROFILE
5286 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5287#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005288 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005289 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5290 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005291#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005292 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005293#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005294#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005295 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005296#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005297#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005298 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5301 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005302 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5303 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005305 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005306 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005307 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 };
5309 int i;
5310
5311 /*
5312 * Find a context in the table and call the ExpandGeneric() with the
5313 * right function to do the expansion.
5314 */
5315 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005316 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (xp->xp_context == tab[i].context)
5318 {
5319 if (tab[i].ic)
5320 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005321 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005322 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 break;
5324 }
5325 }
5326
Bram Moolenaar473de612013-06-08 18:19:48 +02005327 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 return ret;
5330#endif /* FEAT_CMDL_COMPL */
5331}
5332
5333#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5334/*
5335 * Expand a list of names.
5336 *
5337 * Generic function for command line completion. It calls a function to
5338 * obtain strings, one by one. The strings are matched against a regexp
5339 * program. Matching strings are copied into an array, which is returned.
5340 *
5341 * Returns OK when no problems encountered, FAIL for error (out of memory).
5342 */
5343 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005344ExpandGeneric(
5345 expand_T *xp,
5346 regmatch_T *regmatch,
5347 int *num_file,
5348 char_u ***file,
5349 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005351 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
5353 int i;
5354 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005355 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 char_u *str;
5357
5358 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005359 * round == 0: count the number of matching names
5360 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005362 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
5364 for (i = 0; ; ++i)
5365 {
5366 str = (*func)(xp, i);
5367 if (str == NULL) /* end of list */
5368 break;
5369 if (*str == NUL) /* skip empty strings */
5370 continue;
5371
5372 if (vim_regexec(regmatch, str, (colnr_T)0))
5373 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005374 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005376 if (escaped)
5377 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5378 else
5379 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 (*file)[count] = str;
5381#ifdef FEAT_MENU
5382 if (func == get_menu_names && str != NULL)
5383 {
5384 /* test for separator added by get_menu_names() */
5385 str += STRLEN(str) - 1;
5386 if (*str == '\001')
5387 *str = '.';
5388 }
5389#endif
5390 }
5391 ++count;
5392 }
5393 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005394 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 if (count == 0)
5397 return OK;
5398 *num_file = count;
5399 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5400 if (*file == NULL)
5401 {
5402 *file = (char_u **)"";
5403 return FAIL;
5404 }
5405 count = 0;
5406 }
5407 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005408
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005409 /* Sort the results. Keep menu's in the specified order. */
5410 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005411 {
5412 if (xp->xp_context == EXPAND_EXPRESSION
5413 || xp->xp_context == EXPAND_FUNCTIONS
5414 || xp->xp_context == EXPAND_USER_FUNC)
5415 /* <SNR> functions should be sorted to the end. */
5416 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5417 sort_func_compare);
5418 else
5419 sort_strings(*file, *num_file);
5420 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005421
Bram Moolenaar4f688582007-07-24 12:34:30 +00005422#ifdef FEAT_CMDL_COMPL
5423 /* Reset the variables used for special highlight names expansion, so that
5424 * they don't show up when getting normal highlight names by ID. */
5425 reset_expand_highlight();
5426#endif
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 return OK;
5429}
5430
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005431/*
5432 * Complete a shell command.
5433 * Returns FAIL or OK;
5434 */
5435 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005436expand_shellcmd(
5437 char_u *filepat, /* pattern to match with command names */
5438 int *num_file, /* return: number of matches */
5439 char_u ***file, /* return: array with matches */
5440 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005441{
5442 char_u *pat;
5443 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005444 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005445 int mustfree = FALSE;
5446 garray_T ga;
5447 char_u *buf = alloc(MAXPATHL);
5448 size_t l;
5449 char_u *s, *e;
5450 int flags = flagsarg;
5451 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005452 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005453 hashtab_T found_ht;
5454 hashitem_T *hi;
5455 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005456
5457 if (buf == NULL)
5458 return FAIL;
5459
5460 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5461 * space */
5462 pat = vim_strsave(filepat);
5463 for (i = 0; pat[i]; ++i)
5464 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005465 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005466
Bram Moolenaarb5971142015-03-21 17:32:19 +01005467 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005468
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005469 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5470 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005471 path = (char_u *)".";
5472 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005473 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005474 /* For an absolute name we don't use $PATH. */
5475 if (!mch_isFullName(pat))
5476 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005477 if (path == NULL)
5478 path = (char_u *)"";
5479 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005480
5481 /*
5482 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005483 * collect them in "ga". When "." is not in $PATH also expand for the
5484 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005485 */
5486 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005487 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005488 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005489 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005490#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005491 e = vim_strchr(s, ';');
5492#else
5493 e = vim_strchr(s, ':');
5494#endif
5495 if (e == NULL)
5496 e = s + STRLEN(s);
5497
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005498 if (*s == NUL)
5499 {
5500 if (did_curdir)
5501 break;
5502 // Find directories in the current directory, path is empty.
5503 did_curdir = TRUE;
5504 flags |= EW_DIR;
5505 }
5506 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5507 {
5508 did_curdir = TRUE;
5509 flags |= EW_DIR;
5510 }
5511 else
5512 // Do not match directories inside a $PATH item.
5513 flags &= ~EW_DIR;
5514
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005515 l = e - s;
5516 if (l > MAXPATHL - 5)
5517 break;
5518 vim_strncpy(buf, s, l);
5519 add_pathsep(buf);
5520 l = STRLEN(buf);
5521 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5522
5523 /* Expand matches in one directory of $PATH. */
5524 ret = expand_wildcards(1, &buf, num_file, file, flags);
5525 if (ret == OK)
5526 {
5527 if (ga_grow(&ga, *num_file) == FAIL)
5528 FreeWild(*num_file, *file);
5529 else
5530 {
5531 for (i = 0; i < *num_file; ++i)
5532 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005533 char_u *name = (*file)[i];
5534
5535 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005536 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005537 // Check if this name was already found.
5538 hash = hash_hash(name + l);
5539 hi = hash_lookup(&found_ht, name + l, hash);
5540 if (HASHITEM_EMPTY(hi))
5541 {
5542 // Remove the path that was prepended.
5543 STRMOVE(name, name + l);
5544 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5545 hash_add_item(&found_ht, hi, name, hash);
5546 name = NULL;
5547 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005548 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005549 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005550 }
5551 vim_free(*file);
5552 }
5553 }
5554 if (*e != NUL)
5555 ++e;
5556 }
5557 *file = ga.ga_data;
5558 *num_file = ga.ga_len;
5559
5560 vim_free(buf);
5561 vim_free(pat);
5562 if (mustfree)
5563 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005564 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005565 return OK;
5566}
5567
5568
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5570/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005571 * Call "user_expand_func()" to invoke a user defined Vim script function and
5572 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005574 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005575call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005576 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577 expand_T *xp,
5578 int *num_file,
5579 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005581 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005582 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005583 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005584 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005585 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaarb7515462013-06-29 12:58:33 +02005587 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005588 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 *num_file = 0;
5590 *file = NULL;
5591
Bram Moolenaarb7515462013-06-29 12:58:33 +02005592 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005593 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005594 keep = ccline.cmdbuff[ccline.cmdlen];
5595 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005596 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005597
Bram Moolenaarffa96842018-06-12 22:05:14 +02005598 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5599
5600 args[0].v_type = VAR_STRING;
5601 args[0].vval.v_string = pat;
5602 args[1].v_type = VAR_STRING;
5603 args[1].vval.v_string = xp->xp_line;
5604 args[2].v_type = VAR_NUMBER;
5605 args[2].vval.v_number = xp->xp_col;
5606 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005608 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005609
Bram Moolenaarded27a12018-08-01 19:06:03 +02005610 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005611
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005612 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005613 if (ccline.cmdbuff != NULL)
5614 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005615
Bram Moolenaarffa96842018-06-12 22:05:14 +02005616 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005617 return ret;
5618}
5619
5620/*
5621 * Expand names with a function defined by the user.
5622 */
5623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624ExpandUserDefined(
5625 expand_T *xp,
5626 regmatch_T *regmatch,
5627 int *num_file,
5628 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005629{
5630 char_u *retstr;
5631 char_u *s;
5632 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005633 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005634 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005635 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005636
5637 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5638 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 return FAIL;
5640
5641 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005642 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
5644 e = vim_strchr(s, '\n');
5645 if (e == NULL)
5646 e = s + STRLEN(s);
5647 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005648 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005650 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5651 *e = keep;
5652
5653 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005655 if (ga_grow(&ga, 1) == FAIL)
5656 break;
5657 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5658 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 if (*e != NUL)
5662 ++e;
5663 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005664 vim_free(retstr);
5665 *file = ga.ga_data;
5666 *num_file = ga.ga_len;
5667 return OK;
5668}
5669
5670/*
5671 * Expand names with a list returned by a function defined by the user.
5672 */
5673 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005674ExpandUserList(
5675 expand_T *xp,
5676 int *num_file,
5677 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005678{
5679 list_T *retlist;
5680 listitem_T *li;
5681 garray_T ga;
5682
5683 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5684 if (retlist == NULL)
5685 return FAIL;
5686
5687 ga_init2(&ga, (int)sizeof(char *), 3);
5688 /* Loop over the items in the list. */
5689 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5690 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005691 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5692 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005693
5694 if (ga_grow(&ga, 1) == FAIL)
5695 break;
5696
5697 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005698 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005699 ++ga.ga_len;
5700 }
5701 list_unref(retlist);
5702
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 *file = ga.ga_data;
5704 *num_file = ga.ga_len;
5705 return OK;
5706}
5707#endif
5708
5709/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005710 * Expand color scheme, compiler or filetype names.
5711 * Search from 'runtimepath':
5712 * 'runtimepath'/{dirnames}/{pat}.vim
5713 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5714 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5715 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5716 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005717 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 */
5719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005720ExpandRTDir(
5721 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005722 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005723 int *num_file,
5724 char_u ***file,
5725 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 char_u *s;
5728 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005729 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005731 int i;
5732 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 *num_file = 0;
5735 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005736 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005737 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005739 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005741 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5742 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005744 ga_clear_strings(&ga);
5745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005747 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005748 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005749 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005751
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005752 if (flags & DIP_START) {
5753 for (i = 0; dirnames[i] != NULL; ++i)
5754 {
5755 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5756 if (s == NULL)
5757 {
5758 ga_clear_strings(&ga);
5759 return FAIL;
5760 }
5761 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5762 globpath(p_pp, s, &ga, 0);
5763 vim_free(s);
5764 }
5765 }
5766
5767 if (flags & DIP_OPT) {
5768 for (i = 0; dirnames[i] != NULL; ++i)
5769 {
5770 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5771 if (s == NULL)
5772 {
5773 ga_clear_strings(&ga);
5774 return FAIL;
5775 }
5776 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5777 globpath(p_pp, s, &ga, 0);
5778 vim_free(s);
5779 }
5780 }
5781
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005782 for (i = 0; i < ga.ga_len; ++i)
5783 {
5784 match = ((char_u **)ga.ga_data)[i];
5785 s = match;
5786 e = s + STRLEN(s);
5787 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5788 {
5789 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005790 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005791 if (s < match || vim_ispathsep(*s))
5792 break;
5793 ++s;
5794 *e = NUL;
5795 mch_memmove(match, s, e - s + 1);
5796 }
5797 }
5798
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005799 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005800 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005801
5802 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005803 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005804 remove_duplicates(&ga);
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 *file = ga.ga_data;
5807 *num_file = ga.ga_len;
5808 return OK;
5809}
5810
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005811/*
5812 * Expand loadplugin names:
5813 * 'packpath'/pack/ * /opt/{pat}
5814 */
5815 static int
5816ExpandPackAddDir(
5817 char_u *pat,
5818 int *num_file,
5819 char_u ***file)
5820{
5821 char_u *s;
5822 char_u *e;
5823 char_u *match;
5824 garray_T ga;
5825 int i;
5826 int pat_len;
5827
5828 *num_file = 0;
5829 *file = NULL;
5830 pat_len = (int)STRLEN(pat);
5831 ga_init2(&ga, (int)sizeof(char *), 10);
5832
5833 s = alloc((unsigned)(pat_len + 26));
5834 if (s == NULL)
5835 {
5836 ga_clear_strings(&ga);
5837 return FAIL;
5838 }
5839 sprintf((char *)s, "pack/*/opt/%s*", pat);
5840 globpath(p_pp, s, &ga, 0);
5841 vim_free(s);
5842
5843 for (i = 0; i < ga.ga_len; ++i)
5844 {
5845 match = ((char_u **)ga.ga_data)[i];
5846 s = gettail(match);
5847 e = s + STRLEN(s);
5848 mch_memmove(match, s, e - s + 1);
5849 }
5850
5851 if (ga.ga_len == 0)
5852 return FAIL;
5853
5854 /* Sort and remove duplicates which can happen when specifying multiple
5855 * directories in dirnames. */
5856 remove_duplicates(&ga);
5857
5858 *file = ga.ga_data;
5859 *num_file = ga.ga_len;
5860 return OK;
5861}
5862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863#endif
5864
5865#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5866/*
5867 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005868 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005871globpath(
5872 char_u *path,
5873 char_u *file,
5874 garray_T *ga,
5875 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 expand_T xpc;
5878 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 int num_p;
5881 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 buf = alloc(MAXPATHL);
5884 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005885 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005887 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005889
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 /* Loop over all entries in {path}. */
5891 while (*path != NUL)
5892 {
5893 /* Copy one item of the path to buf[] and concatenate the file name. */
5894 copy_option_part(&path, buf, MAXPATHL, ",");
5895 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5896 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005897# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005898 /* Using the platform's path separator (\) makes vim incorrectly
5899 * treat it as an escape character, use '/' instead. */
5900 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5901 STRCAT(buf, "/");
5902# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005906 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5907 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005909 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005911 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 for (i = 0; i < num_p; ++i)
5914 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005915 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005916 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005917 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005920
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 FreeWild(num_p, p);
5922 }
5923 }
5924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
5926 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927}
5928
5929#endif
5930
5931#if defined(FEAT_CMDHIST) || defined(PROTO)
5932
5933/*********************************
5934 * Command line history stuff *
5935 *********************************/
5936
5937/*
5938 * Translate a history character to the associated type number.
5939 */
5940 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005941hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 if (c == ':')
5944 return HIST_CMD;
5945 if (c == '=')
5946 return HIST_EXPR;
5947 if (c == '@')
5948 return HIST_INPUT;
5949 if (c == '>')
5950 return HIST_DEBUG;
5951 return HIST_SEARCH; /* must be '?' or '/' */
5952}
5953
5954/*
5955 * Table of history names.
5956 * These names are used in :history and various hist...() functions.
5957 * It is sufficient to give the significant prefix of a history name.
5958 */
5959
5960static char *(history_names[]) =
5961{
5962 "cmd",
5963 "search",
5964 "expr",
5965 "input",
5966 "debug",
5967 NULL
5968};
5969
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005970#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5971/*
5972 * Function given to ExpandGeneric() to obtain the possible first
5973 * arguments of the ":history command.
5974 */
5975 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005976get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005977{
5978 static char_u compl[2] = { NUL, NUL };
5979 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005980 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005981 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5982
5983 if (idx < short_names_count)
5984 {
5985 compl[0] = (char_u)short_names[idx];
5986 return compl;
5987 }
5988 if (idx < short_names_count + history_name_count)
5989 return (char_u *)history_names[idx - short_names_count];
5990 if (idx == short_names_count + history_name_count)
5991 return (char_u *)"all";
5992 return NULL;
5993}
5994#endif
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996/*
5997 * init_history() - Initialize the command line history.
5998 * Also used to re-allocate the history when the size changes.
5999 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006000 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 int newlen; /* new length of history table */
6004 histentry_T *temp;
6005 int i;
6006 int j;
6007 int type;
6008
6009 /*
6010 * If size of history table changed, reallocate it
6011 */
6012 newlen = (int)p_hi;
6013 if (newlen != hislen) /* history length changed */
6014 {
6015 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6016 {
6017 if (newlen)
6018 {
6019 temp = (histentry_T *)lalloc(
6020 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6021 if (temp == NULL) /* out of memory! */
6022 {
6023 if (type == 0) /* first one: just keep the old length */
6024 {
6025 newlen = hislen;
6026 break;
6027 }
6028 /* Already changed one table, now we can only have zero
6029 * length for all tables. */
6030 newlen = 0;
6031 type = -1;
6032 continue;
6033 }
6034 }
6035 else
6036 temp = NULL;
6037 if (newlen == 0 || temp != NULL)
6038 {
6039 if (hisidx[type] < 0) /* there are no entries yet */
6040 {
6041 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006042 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 }
6044 else if (newlen > hislen) /* array becomes bigger */
6045 {
6046 for (i = 0; i <= hisidx[type]; ++i)
6047 temp[i] = history[type][i];
6048 j = i;
6049 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006050 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 for ( ; j < hislen; ++i, ++j)
6052 temp[i] = history[type][j];
6053 }
6054 else /* array becomes smaller or 0 */
6055 {
6056 j = hisidx[type];
6057 for (i = newlen - 1; ; --i)
6058 {
6059 if (i >= 0) /* copy newest entries */
6060 temp[i] = history[type][j];
6061 else /* remove older entries */
6062 vim_free(history[type][j].hisstr);
6063 if (--j < 0)
6064 j = hislen - 1;
6065 if (j == hisidx[type])
6066 break;
6067 }
6068 hisidx[type] = newlen - 1;
6069 }
6070 vim_free(history[type]);
6071 history[type] = temp;
6072 }
6073 }
6074 hislen = newlen;
6075 }
6076}
6077
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006079clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006080{
6081 hisptr->hisnum = 0;
6082 hisptr->viminfo = FALSE;
6083 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006084 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006085}
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087/*
6088 * Check if command line 'str' is already in history.
6089 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6090 */
6091 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006092in_history(
6093 int type,
6094 char_u *str,
6095 int move_to_front, /* Move the entry to the front if it exists */
6096 int sep,
6097 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
6099 int i;
6100 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006101 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
6103 if (hisidx[type] < 0)
6104 return FALSE;
6105 i = hisidx[type];
6106 do
6107 {
6108 if (history[type][i].hisstr == NULL)
6109 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006110
6111 /* For search history, check that the separator character matches as
6112 * well. */
6113 p = history[type][i].hisstr;
6114 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006115 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006116 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
6118 if (!move_to_front)
6119 return TRUE;
6120 last_i = i;
6121 break;
6122 }
6123 if (--i < 0)
6124 i = hislen - 1;
6125 } while (i != hisidx[type]);
6126
6127 if (last_i >= 0)
6128 {
6129 str = history[type][i].hisstr;
6130 while (i != hisidx[type])
6131 {
6132 if (++i >= hislen)
6133 i = 0;
6134 history[type][last_i] = history[type][i];
6135 last_i = i;
6136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006138 history[type][i].viminfo = FALSE;
6139 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006140 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 return TRUE;
6142 }
6143 return FALSE;
6144}
6145
6146/*
6147 * Convert history name (from table above) to its HIST_ equivalent.
6148 * When "name" is empty, return "cmd" history.
6149 * Returns -1 for unknown history name.
6150 */
6151 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006152get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 int i;
6155 int len = (int)STRLEN(name);
6156
6157 /* No argument: use current history. */
6158 if (len == 0)
6159 return hist_char2type(ccline.cmdfirstc);
6160
6161 for (i = 0; history_names[i] != NULL; ++i)
6162 if (STRNICMP(name, history_names[i], len) == 0)
6163 return i;
6164
6165 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6166 return hist_char2type(name[0]);
6167
6168 return -1;
6169}
6170
6171static int last_maptick = -1; /* last seen maptick */
6172
6173/*
6174 * Add the given string to the given history. If the string is already in the
6175 * history then it is moved to the front. "histype" may be one of he HIST_
6176 * values.
6177 */
6178 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006179add_to_history(
6180 int histype,
6181 char_u *new_entry,
6182 int in_map, /* consider maptick when inside a mapping */
6183 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 histentry_T *hisptr;
6186 int len;
6187
6188 if (hislen == 0) /* no history */
6189 return;
6190
Bram Moolenaara939e432013-11-09 05:30:26 +01006191 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6192 return;
6193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 /*
6195 * Searches inside the same mapping overwrite each other, so that only
6196 * the last line is kept. Be careful not to remove a line that was moved
6197 * down, only lines that were added.
6198 */
6199 if (histype == HIST_SEARCH && in_map)
6200 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006201 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
6203 /* Current line is from the same mapping, remove it */
6204 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6205 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006206 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 --hisnum[histype];
6208 if (--hisidx[HIST_SEARCH] < 0)
6209 hisidx[HIST_SEARCH] = hislen - 1;
6210 }
6211 last_maptick = -1;
6212 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006213 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 if (++hisidx[histype] == hislen)
6216 hisidx[histype] = 0;
6217 hisptr = &history[histype][hisidx[histype]];
6218 vim_free(hisptr->hisstr);
6219
6220 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006221 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6223 if (hisptr->hisstr != NULL)
6224 hisptr->hisstr[len + 1] = sep;
6225
6226 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006227 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006228 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 if (histype == HIST_SEARCH && in_map)
6230 last_maptick = maptick;
6231 }
6232}
6233
6234#if defined(FEAT_EVAL) || defined(PROTO)
6235
6236/*
6237 * Get identifier of newest history entry.
6238 * "histype" may be one of the HIST_ values.
6239 */
6240 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006241get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6244 || hisidx[histype] < 0)
6245 return -1;
6246
6247 return history[histype][hisidx[histype]].hisnum;
6248}
6249
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 * Calculate history index from a number:
6252 * num > 0: seen as identifying number of a history entry
6253 * num < 0: relative position in history wrt newest entry
6254 * "histype" may be one of the HIST_ values.
6255 */
6256 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006257calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 int i;
6260 histentry_T *hist;
6261 int wrapped = FALSE;
6262
6263 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6264 || (i = hisidx[histype]) < 0 || num == 0)
6265 return -1;
6266
6267 hist = history[histype];
6268 if (num > 0)
6269 {
6270 while (hist[i].hisnum > num)
6271 if (--i < 0)
6272 {
6273 if (wrapped)
6274 break;
6275 i += hislen;
6276 wrapped = TRUE;
6277 }
6278 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6279 return i;
6280 }
6281 else if (-num <= hislen)
6282 {
6283 i += num + 1;
6284 if (i < 0)
6285 i += hislen;
6286 if (hist[i].hisstr != NULL)
6287 return i;
6288 }
6289 return -1;
6290}
6291
6292/*
6293 * Get a history entry by its index.
6294 * "histype" may be one of the HIST_ values.
6295 */
6296 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006297get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 idx = calc_hist_idx(histype, idx);
6300 if (idx >= 0)
6301 return history[histype][idx].hisstr;
6302 else
6303 return (char_u *)"";
6304}
6305
6306/*
6307 * Clear all entries of a history.
6308 * "histype" may be one of the HIST_ values.
6309 */
6310 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006311clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 int i;
6314 histentry_T *hisptr;
6315
6316 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6317 {
6318 hisptr = history[histype];
6319 for (i = hislen; i--;)
6320 {
6321 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006322 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006323 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325 hisidx[histype] = -1; /* mark history as cleared */
6326 hisnum[histype] = 0; /* reset identifier counter */
6327 return OK;
6328 }
6329 return FAIL;
6330}
6331
6332/*
6333 * Remove all entries matching {str} from a history.
6334 * "histype" may be one of the HIST_ values.
6335 */
6336 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006337del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
6339 regmatch_T regmatch;
6340 histentry_T *hisptr;
6341 int idx;
6342 int i;
6343 int last;
6344 int found = FALSE;
6345
6346 regmatch.regprog = NULL;
6347 regmatch.rm_ic = FALSE; /* always match case */
6348 if (hislen != 0
6349 && histype >= 0
6350 && histype < HIST_COUNT
6351 && *str != NUL
6352 && (idx = hisidx[histype]) >= 0
6353 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6354 != NULL)
6355 {
6356 i = last = idx;
6357 do
6358 {
6359 hisptr = &history[histype][i];
6360 if (hisptr->hisstr == NULL)
6361 break;
6362 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6363 {
6364 found = TRUE;
6365 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006366 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 }
6368 else
6369 {
6370 if (i != last)
6371 {
6372 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006373 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 }
6375 if (--last < 0)
6376 last += hislen;
6377 }
6378 if (--i < 0)
6379 i += hislen;
6380 } while (i != idx);
6381 if (history[histype][idx].hisstr == NULL)
6382 hisidx[histype] = -1;
6383 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006384 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 return found;
6386}
6387
6388/*
6389 * Remove an indexed entry from a history.
6390 * "histype" may be one of the HIST_ values.
6391 */
6392 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006393del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 int i, j;
6396
6397 i = calc_hist_idx(histype, idx);
6398 if (i < 0)
6399 return FALSE;
6400 idx = hisidx[histype];
6401 vim_free(history[histype][i].hisstr);
6402
6403 /* When deleting the last added search string in a mapping, reset
6404 * last_maptick, so that the last added search string isn't deleted again.
6405 */
6406 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6407 last_maptick = -1;
6408
6409 while (i != idx)
6410 {
6411 j = (i + 1) % hislen;
6412 history[histype][i] = history[histype][j];
6413 i = j;
6414 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006415 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (--i < 0)
6417 i += hislen;
6418 hisidx[histype] = i;
6419 return TRUE;
6420}
6421
6422#endif /* FEAT_EVAL */
6423
6424#if defined(FEAT_CRYPT) || defined(PROTO)
6425/*
6426 * Very specific function to remove the value in ":set key=val" from the
6427 * history.
6428 */
6429 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006430remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 char_u *p;
6433 int i;
6434
6435 i = hisidx[HIST_CMD];
6436 if (i < 0)
6437 return;
6438 p = history[HIST_CMD][i].hisstr;
6439 if (p != NULL)
6440 for ( ; *p; ++p)
6441 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6442 {
6443 p = vim_strchr(p + 3, '=');
6444 if (p == NULL)
6445 break;
6446 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006447 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (p[i] == '\\' && p[i + 1])
6449 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006450 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 --p;
6452 }
6453}
6454#endif
6455
6456#endif /* FEAT_CMDHIST */
6457
Bram Moolenaar064154c2016-03-19 22:50:43 +01006458#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006459/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006460 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006461 * ccline and put the previous value in prev_ccline.
6462 */
6463 static struct cmdline_info *
6464get_ccline_ptr(void)
6465{
6466 if ((State & CMDLINE) == 0)
6467 return NULL;
6468 if (ccline.cmdbuff != NULL)
6469 return &ccline;
6470 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6471 return &prev_ccline;
6472 return NULL;
6473}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006474#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006475
Bram Moolenaar064154c2016-03-19 22:50:43 +01006476#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006477/*
6478 * Get the current command line in allocated memory.
6479 * Only works when the command line is being edited.
6480 * Returns NULL when something is wrong.
6481 */
6482 char_u *
6483get_cmdline_str(void)
6484{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006485 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006486
Bram Moolenaaree91c332018-09-25 22:27:35 +02006487 if (cmdline_star > 0)
6488 return NULL;
6489 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006490 if (p == NULL)
6491 return NULL;
6492 return vim_strnsave(p->cmdbuff, p->cmdlen);
6493}
6494
6495/*
6496 * Get the current command line position, counted in bytes.
6497 * Zero is the first position.
6498 * Only works when the command line is being edited.
6499 * Returns -1 when something is wrong.
6500 */
6501 int
6502get_cmdline_pos(void)
6503{
6504 struct cmdline_info *p = get_ccline_ptr();
6505
6506 if (p == NULL)
6507 return -1;
6508 return p->cmdpos;
6509}
6510
6511/*
6512 * Set the command line byte position to "pos". Zero is the first position.
6513 * Only works when the command line is being edited.
6514 * Returns 1 when failed, 0 when OK.
6515 */
6516 int
6517set_cmdline_pos(
6518 int pos)
6519{
6520 struct cmdline_info *p = get_ccline_ptr();
6521
6522 if (p == NULL)
6523 return 1;
6524
6525 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6526 * changed the command line. */
6527 if (pos < 0)
6528 new_cmdpos = 0;
6529 else
6530 new_cmdpos = pos;
6531 return 0;
6532}
6533#endif
6534
6535#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6536/*
6537 * Get the current command-line type.
6538 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6539 * Only works when the command line is being edited.
6540 * Returns NUL when something is wrong.
6541 */
6542 int
6543get_cmdline_type(void)
6544{
6545 struct cmdline_info *p = get_ccline_ptr();
6546
6547 if (p == NULL)
6548 return NUL;
6549 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006550 return
6551# ifdef FEAT_EVAL
6552 (p->input_fn) ? '@' :
6553# endif
6554 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006555 return p->cmdfirstc;
6556}
6557#endif
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6560/*
6561 * Get indices "num1,num2" that specify a range within a list (not a range of
6562 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6563 * Returns OK if parsed successfully, otherwise FAIL.
6564 */
6565 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006566get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567{
6568 int len;
6569 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006570 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 *str = skipwhite(*str);
6573 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6574 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006575 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 *str += len;
6577 *num1 = (int)num;
6578 first = TRUE;
6579 }
6580 *str = skipwhite(*str);
6581 if (**str == ',') /* parse "to" part of range */
6582 {
6583 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006584 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 if (len > 0)
6586 {
6587 *num2 = (int)num;
6588 *str = skipwhite(*str + len);
6589 }
6590 else if (!first) /* no number given at all */
6591 return FAIL;
6592 }
6593 else if (first) /* only one number given */
6594 *num2 = *num1;
6595 return OK;
6596}
6597#endif
6598
6599#if defined(FEAT_CMDHIST) || defined(PROTO)
6600/*
6601 * :history command - print a history
6602 */
6603 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006604ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 histentry_T *hist;
6607 int histype1 = HIST_CMD;
6608 int histype2 = HIST_CMD;
6609 int hisidx1 = 1;
6610 int hisidx2 = -1;
6611 int idx;
6612 int i, j, k;
6613 char_u *end;
6614 char_u *arg = eap->arg;
6615
6616 if (hislen == 0)
6617 {
6618 MSG(_("'history' option is zero"));
6619 return;
6620 }
6621
6622 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6623 {
6624 end = arg;
6625 while (ASCII_ISALPHA(*end)
6626 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6627 end++;
6628 i = *end;
6629 *end = NUL;
6630 histype1 = get_histtype(arg);
6631 if (histype1 == -1)
6632 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006633 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {
6635 histype1 = 0;
6636 histype2 = HIST_COUNT-1;
6637 }
6638 else
6639 {
6640 *end = i;
6641 EMSG(_(e_trailing));
6642 return;
6643 }
6644 }
6645 else
6646 histype2 = histype1;
6647 *end = i;
6648 }
6649 else
6650 end = arg;
6651 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6652 {
6653 EMSG(_(e_trailing));
6654 return;
6655 }
6656
6657 for (; !got_int && histype1 <= histype2; ++histype1)
6658 {
6659 STRCPY(IObuff, "\n # ");
6660 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6661 MSG_PUTS_TITLE(IObuff);
6662 idx = hisidx[histype1];
6663 hist = history[histype1];
6664 j = hisidx1;
6665 k = hisidx2;
6666 if (j < 0)
6667 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6668 if (k < 0)
6669 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6670 if (idx >= 0 && j <= k)
6671 for (i = idx + 1; !got_int; ++i)
6672 {
6673 if (i == hislen)
6674 i = 0;
6675 if (hist[i].hisstr != NULL
6676 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6677 {
6678 msg_putchar('\n');
6679 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6680 hist[i].hisnum);
6681 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6682 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006683 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 else
6685 STRCAT(IObuff, hist[i].hisstr);
6686 msg_outtrans(IObuff);
6687 out_flush();
6688 }
6689 if (i == idx)
6690 break;
6691 }
6692 }
6693}
6694#endif
6695
6696#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006697/*
6698 * Buffers for history read from a viminfo file. Only valid while reading.
6699 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006700static histentry_T *viminfo_history[HIST_COUNT] =
6701 {NULL, NULL, NULL, NULL, NULL};
6702static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6703static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704static int viminfo_add_at_front = FALSE;
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706/*
6707 * Translate a history type number to the associated character.
6708 */
6709 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006710hist_type2char(
6711 int type,
6712 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 if (type == HIST_CMD)
6715 return ':';
6716 if (type == HIST_SEARCH)
6717 {
6718 if (use_question)
6719 return '?';
6720 else
6721 return '/';
6722 }
6723 if (type == HIST_EXPR)
6724 return '=';
6725 return '@';
6726}
6727
6728/*
6729 * Prepare for reading the history from the viminfo file.
6730 * This allocates history arrays to store the read history lines.
6731 */
6732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 int i;
6736 int num;
6737 int type;
6738 int len;
6739
6740 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006741 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (asklen > hislen)
6743 asklen = hislen;
6744
6745 for (type = 0; type < HIST_COUNT; ++type)
6746 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006747 /* Count the number of empty spaces in the history list. Entries read
6748 * from viminfo previously are also considered empty. If there are
6749 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006751 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 num++;
6753 len = asklen;
6754 if (num > len)
6755 len = num;
6756 if (len <= 0)
6757 viminfo_history[type] = NULL;
6758 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006759 viminfo_history[type] = (histentry_T *)lalloc(
6760 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if (viminfo_history[type] == NULL)
6762 len = 0;
6763 viminfo_hislen[type] = len;
6764 viminfo_hisidx[type] = 0;
6765 }
6766}
6767
6768/*
6769 * Accept a line from the viminfo, store it in the history array when it's
6770 * new.
6771 */
6772 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006773read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 int type;
6776 long_u len;
6777 char_u *val;
6778 char_u *p;
6779
6780 type = hist_char2type(virp->vir_line[0]);
6781 if (viminfo_hisidx[type] < viminfo_hislen[type])
6782 {
6783 val = viminfo_readstring(virp, 1, TRUE);
6784 if (val != NULL && *val != NUL)
6785 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006786 int sep = (*val == ' ' ? NUL : *val);
6787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006789 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 /* Need to re-allocate to append the separator byte. */
6792 len = STRLEN(val);
6793 p = lalloc(len + 2, TRUE);
6794 if (p != NULL)
6795 {
6796 if (type == HIST_SEARCH)
6797 {
6798 /* Search entry: Move the separator from the first
6799 * column to after the NUL. */
6800 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006801 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803 else
6804 {
6805 /* Not a search entry: No separator in the viminfo
6806 * file, add a NUL separator. */
6807 mch_memmove(p, val, (size_t)len + 1);
6808 p[len + 1] = NUL;
6809 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006810 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6811 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006812 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6813 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006814 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816 }
6817 }
6818 vim_free(val);
6819 }
6820 return viminfo_readline(virp);
6821}
6822
Bram Moolenaar07219f92013-04-14 23:19:36 +02006823/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006824 * Accept a new style history line from the viminfo, store it in the history
6825 * array when it's new.
6826 */
6827 void
6828handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006829 garray_T *values,
6830 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006831{
6832 int type;
6833 long_u len;
6834 char_u *val;
6835 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006836 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006837
6838 /* Check the format:
6839 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006840 if (values->ga_len < 4
6841 || vp[0].bv_type != BVAL_NR
6842 || vp[1].bv_type != BVAL_NR
6843 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6844 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006845 return;
6846
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006847 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006848 if (type >= HIST_COUNT)
6849 return;
6850 if (viminfo_hisidx[type] < viminfo_hislen[type])
6851 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006852 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006853 if (val != NULL && *val != NUL)
6854 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006855 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6856 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006857 int idx;
6858 int overwrite = FALSE;
6859
6860 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6861 {
6862 /* If lines were written by an older Vim we need to avoid
6863 * getting duplicates. See if the entry already exists. */
6864 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6865 {
6866 p = viminfo_history[type][idx].hisstr;
6867 if (STRCMP(val, p) == 0
6868 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6869 {
6870 overwrite = TRUE;
6871 break;
6872 }
6873 }
6874
6875 if (!overwrite)
6876 {
6877 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006878 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006879 p = lalloc(len + 2, TRUE);
6880 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006881 else
6882 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006883 if (p != NULL)
6884 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006885 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006886 if (!overwrite)
6887 {
6888 mch_memmove(p, val, (size_t)len + 1);
6889 /* Put the separator after the NUL. */
6890 p[len + 1] = sep;
6891 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006892 viminfo_history[type][idx].hisnum = 0;
6893 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006894 viminfo_hisidx[type]++;
6895 }
6896 }
6897 }
6898 }
6899 }
6900}
6901
6902/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006903 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006904 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006905 static void
6906concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
6908 int idx;
6909 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006910
6911 idx = hisidx[type] + viminfo_hisidx[type];
6912 if (idx >= hislen)
6913 idx -= hislen;
6914 else if (idx < 0)
6915 idx = hislen - 1;
6916 if (viminfo_add_at_front)
6917 hisidx[type] = idx;
6918 else
6919 {
6920 if (hisidx[type] == -1)
6921 hisidx[type] = hislen - 1;
6922 do
6923 {
6924 if (history[type][idx].hisstr != NULL
6925 || history[type][idx].viminfo)
6926 break;
6927 if (++idx == hislen)
6928 idx = 0;
6929 } while (idx != hisidx[type]);
6930 if (idx != hisidx[type] && --idx < 0)
6931 idx = hislen - 1;
6932 }
6933 for (i = 0; i < viminfo_hisidx[type]; i++)
6934 {
6935 vim_free(history[type][idx].hisstr);
6936 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6937 history[type][idx].viminfo = TRUE;
6938 history[type][idx].time_set = viminfo_history[type][i].time_set;
6939 if (--idx < 0)
6940 idx = hislen - 1;
6941 }
6942 idx += 1;
6943 idx %= hislen;
6944 for (i = 0; i < viminfo_hisidx[type]; i++)
6945 {
6946 history[type][idx++].hisnum = ++hisnum[type];
6947 idx %= hislen;
6948 }
6949}
6950
6951#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6952 static int
6953#ifdef __BORLANDC__
6954_RTLENTRYF
6955#endif
6956sort_hist(const void *s1, const void *s2)
6957{
6958 histentry_T *p1 = *(histentry_T **)s1;
6959 histentry_T *p2 = *(histentry_T **)s2;
6960
6961 if (p1->time_set < p2->time_set) return -1;
6962 if (p1->time_set > p2->time_set) return 1;
6963 return 0;
6964}
6965#endif
6966
6967/*
6968 * Merge history lines from viminfo and lines typed in this Vim based on the
6969 * timestamp;
6970 */
6971 static void
6972merge_history(int type)
6973{
6974 int max_len;
6975 histentry_T **tot_hist;
6976 histentry_T *new_hist;
6977 int i;
6978 int len;
6979
6980 /* Make one long list with all entries. */
6981 max_len = hislen + viminfo_hisidx[type];
6982 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6983 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6984 if (tot_hist == NULL || new_hist == NULL)
6985 {
6986 vim_free(tot_hist);
6987 vim_free(new_hist);
6988 return;
6989 }
6990 for (i = 0; i < viminfo_hisidx[type]; i++)
6991 tot_hist[i] = &viminfo_history[type][i];
6992 len = i;
6993 for (i = 0; i < hislen; i++)
6994 if (history[type][i].hisstr != NULL)
6995 tot_hist[len++] = &history[type][i];
6996
6997 /* Sort the list on timestamp. */
6998 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6999
7000 /* Keep the newest ones. */
7001 for (i = 0; i < hislen; i++)
7002 {
7003 if (i < len)
7004 {
7005 new_hist[i] = *tot_hist[i];
7006 tot_hist[i]->hisstr = NULL;
7007 if (new_hist[i].hisnum == 0)
7008 new_hist[i].hisnum = ++hisnum[type];
7009 }
7010 else
7011 clear_hist_entry(&new_hist[i]);
7012 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007013 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007014
7015 /* Free what is not kept. */
7016 for (i = 0; i < viminfo_hisidx[type]; i++)
7017 vim_free(viminfo_history[type][i].hisstr);
7018 for (i = 0; i < hislen; i++)
7019 vim_free(history[type][i].hisstr);
7020 vim_free(history[type]);
7021 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007022 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007023}
7024
7025/*
7026 * Finish reading history lines from viminfo. Not used when writing viminfo.
7027 */
7028 void
7029finish_viminfo_history(vir_T *virp)
7030{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007032 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 for (type = 0; type < HIST_COUNT; ++type)
7035 {
7036 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007037 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007038
7039 if (merge)
7040 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007042 concat_history(type);
7043
Bram Moolenaard23a8232018-02-10 18:45:26 +01007044 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007045 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047}
7048
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007049/*
7050 * Write history to viminfo file in "fp".
7051 * When "merge" is TRUE merge history lines with a previously read viminfo
7052 * file, data is in viminfo_history[].
7053 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007056write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
7058 int i;
7059 int type;
7060 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007061 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
7063 init_history();
7064 if (hislen == 0)
7065 return;
7066 for (type = 0; type < HIST_COUNT; ++type)
7067 {
7068 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7069 if (num_saved == 0)
7070 continue;
7071 if (num_saved < 0) /* Use default */
7072 num_saved = hislen;
7073 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7074 type == HIST_CMD ? _("Command Line") :
7075 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007076 type == HIST_EXPR ? _("Expression") :
7077 type == HIST_INPUT ? _("Input Line") :
7078 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 if (num_saved > hislen)
7080 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007081
7082 /*
7083 * Merge typed and viminfo history:
7084 * round 1: history of typed commands.
7085 * round 2: history from recently read viminfo.
7086 */
7087 for (round = 1; round <= 2; ++round)
7088 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007089 if (round == 1)
7090 /* start at newest entry, somewhere in the list */
7091 i = hisidx[type];
7092 else if (viminfo_hisidx[type] > 0)
7093 /* start at newest entry, first in the list */
7094 i = 0;
7095 else
7096 /* empty list */
7097 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007098 if (i >= 0)
7099 while (num_saved > 0
7100 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007102 char_u *p;
7103 time_t timestamp;
7104 int c = NUL;
7105
7106 if (round == 1)
7107 {
7108 p = history[type][i].hisstr;
7109 timestamp = history[type][i].time_set;
7110 }
7111 else
7112 {
7113 p = viminfo_history[type] == NULL ? NULL
7114 : viminfo_history[type][i].hisstr;
7115 timestamp = viminfo_history[type] == NULL ? 0
7116 : viminfo_history[type][i].time_set;
7117 }
7118
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007119 if (p != NULL && (round == 2
7120 || !merge
7121 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007123 --num_saved;
7124 fputc(hist_type2char(type, TRUE), fp);
7125 /* For the search history: put the separator in the
7126 * second column; use a space if there isn't one. */
7127 if (type == HIST_SEARCH)
7128 {
7129 c = p[STRLEN(p) + 1];
7130 putc(c == NUL ? ' ' : c, fp);
7131 }
7132 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007133
7134 {
7135 char cbuf[NUMBUFLEN];
7136
7137 /* New style history with a bar line. Format:
7138 * |{bartype},{histtype},{timestamp},{separator},"text" */
7139 if (c == NUL)
7140 cbuf[0] = NUL;
7141 else
7142 sprintf(cbuf, "%d", c);
7143 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7144 type, (long)timestamp, cbuf);
7145 barline_writestring(fp, p, LSIZE - 20);
7146 putc('\n', fp);
7147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007149 if (round == 1)
7150 {
7151 /* Decrement index, loop around and stop when back at
7152 * the start. */
7153 if (--i < 0)
7154 i = hislen - 1;
7155 if (i == hisidx[type])
7156 break;
7157 }
7158 else
7159 {
7160 /* Increment index. Stop at the end in the while. */
7161 ++i;
7162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007164 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007165 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007166 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007167 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007168 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007169 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 }
7171}
7172#endif /* FEAT_VIMINFO */
7173
7174#if defined(FEAT_FKMAP) || defined(PROTO)
7175/*
7176 * Write a character at the current cursor+offset position.
7177 * It is directly written into the command buffer block.
7178 */
7179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007180cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7183 {
7184 EMSG(_("E198: cmd_pchar beyond the command length"));
7185 return;
7186 }
7187 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7188 ccline.cmdbuff[ccline.cmdlen] = NUL;
7189}
7190
7191 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007192cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7195 {
7196 /* EMSG(_("cmd_gchar beyond the command length")); */
7197 return NUL;
7198 }
7199 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7200}
7201#endif
7202
7203#if defined(FEAT_CMDWIN) || defined(PROTO)
7204/*
7205 * Open a window on the current command line and history. Allow editing in
7206 * the window. Returns when the window is closed.
7207 * Returns:
7208 * CR if the command is to be executed
7209 * Ctrl_C if it is to be abandoned
7210 * K_IGNORE if editing continues
7211 */
7212 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007213open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007215 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007217 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 win_T *wp;
7219 int i;
7220 linenr_T lnum;
7221 int histtype;
7222 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 int save_restart_edit = restart_edit;
7224 int save_State = State;
7225 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007226#ifdef FEAT_RIGHTLEFT
7227 int save_cmdmsg_rl = cmdmsg_rl;
7228#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007229#ifdef FEAT_FOLDING
7230 int save_KeyTyped;
7231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233 /* Can't do this recursively. Can't do it when typing a password. */
7234 if (cmdwin_type != 0
7235# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7236 || cmdline_star > 0
7237# endif
7238 )
7239 {
7240 beep_flush();
7241 return K_IGNORE;
7242 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007243 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
7245 /* Save current window sizes. */
7246 win_size_save(&winsizes);
7247
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007249 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007250
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007251 /* don't use a new tab page */
7252 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007253 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 /* Create a window for the command-line buffer. */
7256 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7257 {
7258 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007259 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 return K_IGNORE;
7261 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007262 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007265 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007266 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007269#ifdef FEAT_FOLDING
7270 curwin->w_p_fen = FALSE;
7271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007273 curwin->w_p_rl = cmdmsg_rl;
7274 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007276 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007279 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007280 /* But don't allow switching to another buffer. */
7281 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar46152342005-09-07 21:18:43 +00007283 /* Showing the prompt may have set need_wait_return, reset it. */
7284 need_wait_return = FALSE;
7285
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007286 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7288 {
7289 if (p_wc == TAB)
7290 {
7291 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7292 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7293 }
7294 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7295 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007296 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007298 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7299 * sets 'textwidth' to 78). */
7300 curbuf->b_p_tw = 0;
7301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 /* Fill the buffer with the history. */
7303 init_history();
7304 if (hislen > 0)
7305 {
7306 i = hisidx[histtype];
7307 if (i >= 0)
7308 {
7309 lnum = 0;
7310 do
7311 {
7312 if (++i == hislen)
7313 i = 0;
7314 if (history[histtype][i].hisstr != NULL)
7315 ml_append(lnum++, history[histtype][i].hisstr,
7316 (colnr_T)0, FALSE);
7317 }
7318 while (i != hisidx[histtype]);
7319 }
7320 }
7321
7322 /* Replace the empty last line with the current command-line and put the
7323 * cursor there. */
7324 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7325 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7326 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007327 changed_line_abv_curs();
7328 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007329 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 /* No Ex mode here! */
7332 exmode_active = 0;
7333
7334 State = NORMAL;
7335# ifdef FEAT_MOUSE
7336 setmouse();
7337# endif
7338
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007340 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007341 if (restart_edit != 0) /* autocmd with ":startinsert" */
7342 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
7344 i = RedrawingDisabled;
7345 RedrawingDisabled = 0;
7346
7347 /*
7348 * Call the main loop until <CR> or CTRL-C is typed.
7349 */
7350 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007351 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
7353 RedrawingDisabled = i;
7354
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007355# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007356 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007357# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007360 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007361
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007362# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007363 /* Restore KeyTyped in case it is modified by autocommands */
7364 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365# endif
7366
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 exmode_active = save_exmode;
7369
Bram Moolenaarf9821062008-06-20 16:31:07 +00007370 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007372 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 {
7374 cmdwin_result = Ctrl_C;
7375 EMSG(_("E199: Active window or buffer deleted"));
7376 }
7377 else
7378 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007379# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 /* autocmds may abort script processing */
7381 if (aborting() && cmdwin_result != K_IGNORE)
7382 cmdwin_result = Ctrl_C;
7383# endif
7384 /* Set the new command line from the cmdline buffer. */
7385 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007386 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007388 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7389
7390 if (histtype == HIST_CMD)
7391 {
7392 /* Execute the command directly. */
7393 ccline.cmdbuff = vim_strsave((char_u *)p);
7394 cmdwin_result = CAR;
7395 }
7396 else
7397 {
7398 /* First need to cancel what we were doing. */
7399 ccline.cmdbuff = NULL;
7400 stuffcharReadbuff(':');
7401 stuffReadbuff((char_u *)p);
7402 stuffcharReadbuff(CAR);
7403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
7405 else if (cmdwin_result == K_XF2) /* :qa typed */
7406 {
7407 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7408 cmdwin_result = CAR;
7409 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007410 else if (cmdwin_result == Ctrl_C)
7411 {
7412 /* :q or :close, don't execute any command
7413 * and don't modify the cmd window. */
7414 ccline.cmdbuff = NULL;
7415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 else
7417 ccline.cmdbuff = vim_strsave(ml_get_curline());
7418 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007419 {
7420 ccline.cmdbuff = vim_strsave((char_u *)"");
7421 ccline.cmdlen = 0;
7422 ccline.cmdbufflen = 1;
7423 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 else
7427 {
7428 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7429 ccline.cmdbufflen = ccline.cmdlen + 1;
7430 ccline.cmdpos = curwin->w_cursor.col;
7431 if (ccline.cmdpos > ccline.cmdlen)
7432 ccline.cmdpos = ccline.cmdlen;
7433 if (cmdwin_result == K_IGNORE)
7434 {
7435 set_cmdspos_cursor();
7436 redrawcmd();
7437 }
7438 }
7439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007441 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007442# ifdef FEAT_CONCEAL
7443 /* Avoid command-line window first character being concealed. */
7444 curwin->w_p_cole = 0;
7445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007447 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 win_goto(old_curwin);
7449 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007450
7451 /* win_close() may have already wiped the buffer when 'bh' is
7452 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007453 if (bufref_valid(&bufref))
7454 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
7456 /* Restore window sizes. */
7457 win_size_restore(&winsizes);
7458
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007459 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461
7462 ga_clear(&winsizes);
7463 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007464# ifdef FEAT_RIGHTLEFT
7465 cmdmsg_rl = save_cmdmsg_rl;
7466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468 State = save_State;
7469# ifdef FEAT_MOUSE
7470 setmouse();
7471# endif
7472
7473 return cmdwin_result;
7474}
7475#endif /* FEAT_CMDWIN */
7476
7477/*
7478 * Used for commands that either take a simple command string argument, or:
7479 * cmd << endmarker
7480 * {script}
7481 * endmarker
7482 * Returns a pointer to allocated memory with {script} or NULL.
7483 */
7484 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007485script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487 char_u *theline;
7488 char *end_pattern = NULL;
7489 char dot[] = ".";
7490 garray_T ga;
7491
7492 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7493 return NULL;
7494
7495 ga_init2(&ga, 1, 0x400);
7496
7497 if (cmd[2] != NUL)
7498 end_pattern = (char *)skipwhite(cmd + 2);
7499 else
7500 end_pattern = dot;
7501
7502 for (;;)
7503 {
7504 theline = eap->getline(
7505#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007506 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507#endif
7508 NUL, eap->cookie, 0);
7509
7510 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007511 {
7512 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
7516 ga_concat(&ga, theline);
7517 ga_append(&ga, '\n');
7518 vim_free(theline);
7519 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007520 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
7522 return (char_u *)ga.ga_data;
7523}