blob: acccd27321ae74a714d97bf6c1c95b7732244923 [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 Moolenaar4e303c82018-11-24 14:27:44 +01001381 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 }
1384 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001385 got_int = FALSE; /* don't abandon the command line */
1386 did_emsg = FALSE;
1387 emsg_on_display = FALSE;
1388 redrawcmd();
1389 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391#endif
1392 else
1393 {
1394 if (c == Ctrl_G && p_im && restart_edit == 0)
1395 restart_edit = 'a';
1396 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1397 in history */
1398 goto returncmd; /* back to Normal mode */
1399 }
1400 }
1401
1402#ifdef FEAT_CMDWIN
1403 if (c == cedit_key || c == K_CMDWIN)
1404 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001405 if (ex_normal_busy == 0 && got_int == FALSE)
1406 {
1407 /*
1408 * Open a window to edit the command line (and history).
1409 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001410 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001411 some_key_typed = TRUE;
1412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414# ifdef FEAT_DIGRAPHS
1415 else
1416# endif
1417#endif
1418#ifdef FEAT_DIGRAPHS
1419 c = do_digraph(c);
1420#endif
1421
1422 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1423 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1424 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001425 /* In Ex mode a backslash escapes a newline. */
1426 if (exmode_active
1427 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001428 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001429 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001430 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001432 if (c == K_KENTER)
1433 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001435 else
1436 {
1437 gotesc = FALSE; /* Might have typed ESC previously, don't
1438 truncate the cmdline now. */
1439 if (ccheck_abbr(c + ABBR_OFF))
1440 goto cmdline_changed;
1441 if (!cmd_silent)
1442 {
1443 windgoto(msg_row, 0);
1444 out_flush();
1445 }
1446 break;
1447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449
1450 /*
1451 * Completion for 'wildchar' or 'wildcharm' key.
1452 * - hitting <ESC> twice means: abandon command line.
1453 * - wildcard expansion is only done when the 'wildchar' key is really
1454 * typed, not when it comes from a macro
1455 */
1456 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1457 {
1458 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1459 {
1460 /* if 'wildmode' contains "list" may still need to list */
1461 if (xpc.xp_numfiles > 1
1462 && !did_wild_list
1463 && (wim_flags[wim_index] & WIM_LIST))
1464 {
1465 (void)showmatches(&xpc, FALSE);
1466 redrawcmd();
1467 did_wild_list = TRUE;
1468 }
1469 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001470 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1471 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001473 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1474 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else
1476 res = OK; /* don't insert 'wildchar' now */
1477 }
1478 else /* typed p_wc first time */
1479 {
1480 wim_index = 0;
1481 j = ccline.cmdpos;
1482 /* if 'wildmode' first contains "longest", get longest
1483 * common part */
1484 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001485 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1486 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001488 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1489 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 /* if interrupted while completing, behave like it failed */
1492 if (got_int)
1493 {
1494 (void)vpeekc(); /* remove <C-C> from input stream */
1495 got_int = FALSE; /* don't abandon the command line */
1496 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1497#ifdef FEAT_WILDMENU
1498 xpc.xp_context = EXPAND_NOTHING;
1499#endif
1500 goto cmdline_changed;
1501 }
1502
1503 /* when more than one match, and 'wildmode' first contains
1504 * "list", or no change and 'wildmode' contains "longest,list",
1505 * list all matches */
1506 if (res == OK && xpc.xp_numfiles > 1)
1507 {
1508 /* a "longest" that didn't do anything is skipped (but not
1509 * "list:longest") */
1510 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1511 wim_index = 1;
1512 if ((wim_flags[wim_index] & WIM_LIST)
1513#ifdef FEAT_WILDMENU
1514 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1515#endif
1516 )
1517 {
1518 if (!(wim_flags[0] & WIM_LONGEST))
1519 {
1520#ifdef FEAT_WILDMENU
1521 int p_wmnu_save = p_wmnu;
1522 p_wmnu = 0;
1523#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001524 /* remove match */
1525 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526#ifdef FEAT_WILDMENU
1527 p_wmnu = p_wmnu_save;
1528#endif
1529 }
1530#ifdef FEAT_WILDMENU
1531 (void)showmatches(&xpc, p_wmnu
1532 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1533#else
1534 (void)showmatches(&xpc, FALSE);
1535#endif
1536 redrawcmd();
1537 did_wild_list = TRUE;
1538 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001539 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1540 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001542 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1543 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001546 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548#ifdef FEAT_WILDMENU
1549 else if (xpc.xp_numfiles == -1)
1550 xpc.xp_context = EXPAND_NOTHING;
1551#endif
1552 }
1553 if (wim_index < 3)
1554 ++wim_index;
1555 if (c == ESC)
1556 gotesc = TRUE;
1557 if (res == OK)
1558 goto cmdline_changed;
1559 }
1560
1561 gotesc = FALSE;
1562
1563 /* <S-Tab> goes to last match, in a clumsy way */
1564 if (c == K_S_TAB && KeyTyped)
1565 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001566 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1567 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1568 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 goto cmdline_changed;
1570 }
1571
1572 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1573 c = NL;
1574
1575 do_abbr = TRUE; /* default: check for abbreviation */
1576
1577 /*
1578 * Big switch for a typed command line character.
1579 */
1580 switch (c)
1581 {
1582 case K_BS:
1583 case Ctrl_H:
1584 case K_DEL:
1585 case K_KDEL:
1586 case Ctrl_W:
1587#ifdef FEAT_FKMAP
1588 if (cmd_fkmap && c == K_BS)
1589 c = K_DEL;
1590#endif
1591 if (c == K_KDEL)
1592 c = K_DEL;
1593
1594 /*
1595 * delete current character is the same as backspace on next
1596 * character, except at end of line
1597 */
1598 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1599 ++ccline.cmdpos;
1600#ifdef FEAT_MBYTE
1601 if (has_mbyte && c == K_DEL)
1602 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1603 ccline.cmdbuff + ccline.cmdpos);
1604#endif
1605 if (ccline.cmdpos > 0)
1606 {
1607 char_u *p;
1608
1609 j = ccline.cmdpos;
1610 p = ccline.cmdbuff + j;
1611#ifdef FEAT_MBYTE
1612 if (has_mbyte)
1613 {
1614 p = mb_prevptr(ccline.cmdbuff, p);
1615 if (c == Ctrl_W)
1616 {
1617 while (p > ccline.cmdbuff && vim_isspace(*p))
1618 p = mb_prevptr(ccline.cmdbuff, p);
1619 i = mb_get_class(p);
1620 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1621 p = mb_prevptr(ccline.cmdbuff, p);
1622 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001623 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 }
1625 }
1626 else
1627#endif
1628 if (c == Ctrl_W)
1629 {
1630 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1631 --p;
1632 i = vim_iswordc(p[-1]);
1633 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1634 && vim_iswordc(p[-1]) == i)
1635 --p;
1636 }
1637 else
1638 --p;
1639 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1640 ccline.cmdlen -= j - ccline.cmdpos;
1641 i = ccline.cmdpos;
1642 while (i < ccline.cmdlen)
1643 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1644
1645 /* Truncate at the end, required for multi-byte chars. */
1646 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001647#ifdef FEAT_SEARCH_EXTRA
1648 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001649 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001650 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001651 /* save view settings, so that the screen
1652 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001653 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001654 }
1655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 redrawcmd();
1657 }
1658 else if (ccline.cmdlen == 0 && c != Ctrl_W
1659 && ccline.cmdprompt == NULL && indent == 0)
1660 {
1661 /* In ex and debug mode it doesn't make sense to return. */
1662 if (exmode_active
1663#ifdef FEAT_EVAL
1664 || ccline.cmdfirstc == '>'
1665#endif
1666 )
1667 goto cmdline_not_changed;
1668
Bram Moolenaard23a8232018-02-10 18:45:26 +01001669 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (!cmd_silent)
1671 {
1672#ifdef FEAT_RIGHTLEFT
1673 if (cmdmsg_rl)
1674 msg_col = Columns;
1675 else
1676#endif
1677 msg_col = 0;
1678 msg_putchar(' '); /* delete ':' */
1679 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001680#ifdef FEAT_SEARCH_EXTRA
1681 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001682 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 redraw_cmdline = TRUE;
1685 goto returncmd; /* back to cmd mode */
1686 }
1687 goto cmdline_changed;
1688
1689 case K_INS:
1690 case K_KINS:
1691#ifdef FEAT_FKMAP
1692 /* if Farsi mode set, we are in reverse insert mode -
1693 Do not change the mode */
1694 if (cmd_fkmap)
1695 beep_flush();
1696 else
1697#endif
1698 ccline.overstrike = !ccline.overstrike;
1699#ifdef CURSOR_SHAPE
1700 ui_cursor_shape(); /* may show different cursor shape */
1701#endif
1702 goto cmdline_not_changed;
1703
1704 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001705 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 /* ":lmap" mappings exists, toggle use of mappings. */
1708 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 im_set_active(FALSE); /* Disable input method */
1711#endif
1712 if (b_im_ptr != NULL)
1713 {
1714 if (State & LANGMAP)
1715 *b_im_ptr = B_IMODE_LMAP;
1716 else
1717 *b_im_ptr = B_IMODE_NONE;
1718 }
1719 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 else
1722 {
1723 /* There are no ":lmap" mappings, toggle IM. When
1724 * 'imdisable' is set don't try getting the status, it's
1725 * always off. */
1726 if ((p_imdisable && b_im_ptr != NULL)
1727 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1728 {
1729 im_set_active(FALSE); /* Disable input method */
1730 if (b_im_ptr != NULL)
1731 *b_im_ptr = B_IMODE_NONE;
1732 }
1733 else
1734 {
1735 im_set_active(TRUE); /* Enable input method */
1736 if (b_im_ptr != NULL)
1737 *b_im_ptr = B_IMODE_IM;
1738 }
1739 }
1740#endif
1741 if (b_im_ptr != NULL)
1742 {
1743 if (b_im_ptr == &curbuf->b_p_iminsert)
1744 set_iminsert_global();
1745 else
1746 set_imsearch_global();
1747 }
1748#ifdef CURSOR_SHAPE
1749 ui_cursor_shape(); /* may show different cursor shape */
1750#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001751#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 /* Show/unshow value of 'keymap' in status lines later. */
1753 status_redraw_curbuf();
1754#endif
1755 goto cmdline_not_changed;
1756
1757/* case '@': only in very old vi */
1758 case Ctrl_U:
1759 /* delete all characters left of the cursor */
1760 j = ccline.cmdpos;
1761 ccline.cmdlen -= j;
1762 i = ccline.cmdpos = 0;
1763 while (i < ccline.cmdlen)
1764 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1765 /* Truncate at the end, required for multi-byte chars. */
1766 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001767#ifdef FEAT_SEARCH_EXTRA
1768 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001769 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 redrawcmd();
1772 goto cmdline_changed;
1773
1774#ifdef FEAT_CLIPBOARD
1775 case Ctrl_Y:
1776 /* Copy the modeless selection, if there is one. */
1777 if (clip_star.state != SELECT_CLEARED)
1778 {
1779 if (clip_star.state == SELECT_DONE)
1780 clip_copy_modeless_selection(TRUE);
1781 goto cmdline_not_changed;
1782 }
1783 break;
1784#endif
1785
1786 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1787 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001788 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001789 * ":normal" runs out of characters. */
1790 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001791 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 goto cmdline_not_changed;
1793
1794 gotesc = TRUE; /* will free ccline.cmdbuff after
1795 putting it in history */
1796 goto returncmd; /* back to cmd mode */
1797
1798 case Ctrl_R: /* insert register */
1799#ifdef USE_ON_FLY_SCROLL
1800 dont_scroll = TRUE; /* disallow scrolling here */
1801#endif
1802 putcmdline('"', TRUE);
1803 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001804 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (i == Ctrl_O)
1806 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1807 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001808 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001809 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 --no_mapping;
1811#ifdef FEAT_EVAL
1812 /*
1813 * Insert the result of an expression.
1814 * Need to save the current command line, to be able to enter
1815 * a new one...
1816 */
1817 new_cmdpos = -1;
1818 if (c == '=')
1819 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001820 if (ccline.cmdfirstc == '=' // can't do this recursively
1821 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
1823 beep_flush();
1824 c = ESC;
1825 }
1826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829#endif
1830 if (c != ESC) /* use ESC to cancel inserting register */
1831 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001832 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001833
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001834#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001835 /* When there was a serious error abort getting the
1836 * command line. */
1837 if (aborting())
1838 {
1839 gotesc = TRUE; /* will free ccline.cmdbuff after
1840 putting it in history */
1841 goto returncmd; /* back to cmd mode */
1842 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 KeyTyped = FALSE; /* Don't do p_wc completion. */
1845#ifdef FEAT_EVAL
1846 if (new_cmdpos >= 0)
1847 {
1848 /* set_cmdline_pos() was used */
1849 if (new_cmdpos > ccline.cmdlen)
1850 ccline.cmdpos = ccline.cmdlen;
1851 else
1852 ccline.cmdpos = new_cmdpos;
1853 }
1854#endif
1855 }
1856 redrawcmd();
1857 goto cmdline_changed;
1858
1859 case Ctrl_D:
1860 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1861 break; /* Use ^D as normal char instead */
1862
1863 redrawcmd();
1864 continue; /* don't do incremental search now */
1865
1866 case K_RIGHT:
1867 case K_S_RIGHT:
1868 case K_C_RIGHT:
1869 do
1870 {
1871 if (ccline.cmdpos >= ccline.cmdlen)
1872 break;
1873 i = cmdline_charsize(ccline.cmdpos);
1874 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1875 break;
1876 ccline.cmdspos += i;
1877#ifdef FEAT_MBYTE
1878 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001879 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 + ccline.cmdpos);
1881 else
1882#endif
1883 ++ccline.cmdpos;
1884 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001885 while ((c == K_S_RIGHT || c == K_C_RIGHT
1886 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1888#ifdef FEAT_MBYTE
1889 if (has_mbyte)
1890 set_cmdspos_cursor();
1891#endif
1892 goto cmdline_not_changed;
1893
1894 case K_LEFT:
1895 case K_S_LEFT:
1896 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001897 if (ccline.cmdpos == 0)
1898 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 do
1900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 --ccline.cmdpos;
1902#ifdef FEAT_MBYTE
1903 if (has_mbyte) /* move to first byte of char */
1904 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1905 ccline.cmdbuff + ccline.cmdpos);
1906#endif
1907 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1908 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001909 while (ccline.cmdpos > 0
1910 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001911 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1913#ifdef FEAT_MBYTE
1914 if (has_mbyte)
1915 set_cmdspos_cursor();
1916#endif
1917 goto cmdline_not_changed;
1918
1919 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001920 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001921 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
Bram Moolenaar4770d092006-01-12 23:22:24 +00001923#ifdef FEAT_GUI_W32
1924 /* On Win32 ignore <M-F4>, we get it when closing the window was
1925 * cancelled. */
1926 case K_F4:
1927 if (mod_mask == MOD_MASK_ALT)
1928 {
1929 redrawcmd(); /* somehow the cmdline is cleared */
1930 goto cmdline_not_changed;
1931 }
1932 break;
1933#endif
1934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935#ifdef FEAT_MOUSE
1936 case K_MIDDLEDRAG:
1937 case K_MIDDLERELEASE:
1938 goto cmdline_not_changed; /* Ignore mouse */
1939
1940 case K_MIDDLEMOUSE:
1941# ifdef FEAT_GUI
1942 /* When GUI is active, also paste when 'mouse' is empty */
1943 if (!gui.in_use)
1944# endif
1945 if (!mouse_has(MOUSE_COMMAND))
1946 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001947# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001949 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001951# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001952 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 redrawcmd();
1954 goto cmdline_changed;
1955
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001956# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001958 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 redrawcmd();
1960 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963 case K_LEFTDRAG:
1964 case K_LEFTRELEASE:
1965 case K_RIGHTDRAG:
1966 case K_RIGHTRELEASE:
1967 /* Ignore drag and release events when the button-down wasn't
1968 * seen before. */
1969 if (ignore_drag_release)
1970 goto cmdline_not_changed;
1971 /* FALLTHROUGH */
1972 case K_LEFTMOUSE:
1973 case K_RIGHTMOUSE:
1974 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1975 ignore_drag_release = TRUE;
1976 else
1977 ignore_drag_release = FALSE;
1978# ifdef FEAT_GUI
1979 /* When GUI is active, also move when 'mouse' is empty */
1980 if (!gui.in_use)
1981# endif
1982 if (!mouse_has(MOUSE_COMMAND))
1983 goto cmdline_not_changed; /* Ignore mouse */
1984# ifdef FEAT_CLIPBOARD
1985 if (mouse_row < cmdline_row && clip_star.available)
1986 {
1987 int button, is_click, is_drag;
1988
1989 /*
1990 * Handle modeless selection.
1991 */
1992 button = get_mouse_button(KEY2TERMCAP1(c),
1993 &is_click, &is_drag);
1994 if (mouse_model_popup() && button == MOUSE_LEFT
1995 && (mod_mask & MOD_MASK_SHIFT))
1996 {
1997 /* Translate shift-left to right button. */
1998 button = MOUSE_RIGHT;
1999 mod_mask &= ~MOD_MASK_SHIFT;
2000 }
2001 clip_modeless(button, is_click, is_drag);
2002 goto cmdline_not_changed;
2003 }
2004# endif
2005
2006 set_cmdspos();
2007 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2008 ++ccline.cmdpos)
2009 {
2010 i = cmdline_charsize(ccline.cmdpos);
2011 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2012 && mouse_col < ccline.cmdspos % Columns + i)
2013 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002014# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (has_mbyte)
2016 {
2017 /* Count ">" for double-wide char that doesn't fit. */
2018 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002019 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 + ccline.cmdpos) - 1;
2021 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 ccline.cmdspos += i;
2024 }
2025 goto cmdline_not_changed;
2026
2027 /* Mouse scroll wheel: ignored here */
2028 case K_MOUSEDOWN:
2029 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002030 case K_MOUSELEFT:
2031 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 /* Alternate buttons ignored here */
2033 case K_X1MOUSE:
2034 case K_X1DRAG:
2035 case K_X1RELEASE:
2036 case K_X2MOUSE:
2037 case K_X2DRAG:
2038 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002039 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 goto cmdline_not_changed;
2041
2042#endif /* FEAT_MOUSE */
2043
2044#ifdef FEAT_GUI
2045 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2046 case K_LEFTRELEASE_NM:
2047 goto cmdline_not_changed;
2048
2049 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002050 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
2052 gui_do_scroll();
2053 redrawcmd();
2054 }
2055 goto cmdline_not_changed;
2056
2057 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002058 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002060 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 redrawcmd();
2062 }
2063 goto cmdline_not_changed;
2064#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002065#ifdef FEAT_GUI_TABLINE
2066 case K_TABLINE:
2067 case K_TABMENU:
2068 /* Don't want to change any tabs here. Make sure the same tab
2069 * is still selected. */
2070 if (gui_use_tabline())
2071 gui_mch_set_curtab(tabpage_index(curtab));
2072 goto cmdline_not_changed;
2073#endif
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 case K_SELECT: /* end of Select mode mapping - ignore */
2076 goto cmdline_not_changed;
2077
2078 case Ctrl_B: /* begin of command line */
2079 case K_HOME:
2080 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 case K_S_HOME:
2082 case K_C_HOME:
2083 ccline.cmdpos = 0;
2084 set_cmdspos();
2085 goto cmdline_not_changed;
2086
2087 case Ctrl_E: /* end of command line */
2088 case K_END:
2089 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 case K_S_END:
2091 case K_C_END:
2092 ccline.cmdpos = ccline.cmdlen;
2093 set_cmdspos_cursor();
2094 goto cmdline_not_changed;
2095
2096 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002097 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 break;
2099 goto cmdline_changed;
2100
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002101 case Ctrl_L:
2102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002103 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002104 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002105#endif
2106
2107 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002108 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 break;
2110 goto cmdline_changed;
2111
2112 case Ctrl_N: /* next match */
2113 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002114 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002116 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2117 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002119 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002122 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 case K_UP:
2124 case K_DOWN:
2125 case K_S_UP:
2126 case K_S_DOWN:
2127 case K_PAGEUP:
2128 case K_KPAGEUP:
2129 case K_PAGEDOWN:
2130 case K_KPAGEDOWN:
2131 if (hislen == 0 || firstc == NUL) /* no history */
2132 goto cmdline_not_changed;
2133
2134 i = hiscnt;
2135
2136 /* save current command string so it can be restored later */
2137 if (lookfor == NULL)
2138 {
2139 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2140 goto cmdline_not_changed;
2141 lookfor[ccline.cmdpos] = NUL;
2142 }
2143
2144 j = (int)STRLEN(lookfor);
2145 for (;;)
2146 {
2147 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002148 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002149 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 if (hiscnt == hislen) /* first time */
2152 hiscnt = hisidx[histype];
2153 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2154 hiscnt = hislen - 1;
2155 else if (hiscnt != hisidx[histype] + 1)
2156 --hiscnt;
2157 else /* at top of list */
2158 {
2159 hiscnt = i;
2160 break;
2161 }
2162 }
2163 else /* one step forwards */
2164 {
2165 /* on last entry, clear the line */
2166 if (hiscnt == hisidx[histype])
2167 {
2168 hiscnt = hislen;
2169 break;
2170 }
2171
2172 /* not on a history line, nothing to do */
2173 if (hiscnt == hislen)
2174 break;
2175 if (hiscnt == hislen - 1) /* wrap around */
2176 hiscnt = 0;
2177 else
2178 ++hiscnt;
2179 }
2180 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2181 {
2182 hiscnt = i;
2183 break;
2184 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002185 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002186 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 || STRNCMP(history[histype][hiscnt].hisstr,
2188 lookfor, (size_t)j) == 0)
2189 break;
2190 }
2191
2192 if (hiscnt != i) /* jumped to other entry */
2193 {
2194 char_u *p;
2195 int len;
2196 int old_firstc;
2197
Bram Moolenaar438d1762018-09-30 17:11:48 +02002198 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002199 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (hiscnt == hislen)
2201 p = lookfor; /* back to the old one */
2202 else
2203 p = history[histype][hiscnt].hisstr;
2204
2205 if (histype == HIST_SEARCH
2206 && p != lookfor
2207 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2208 {
2209 /* Correct for the separator character used when
2210 * adding the history entry vs the one used now.
2211 * First loop: count length.
2212 * Second loop: copy the characters. */
2213 for (i = 0; i <= 1; ++i)
2214 {
2215 len = 0;
2216 for (j = 0; p[j] != NUL; ++j)
2217 {
2218 /* Replace old sep with new sep, unless it is
2219 * escaped. */
2220 if (p[j] == old_firstc
2221 && (j == 0 || p[j - 1] != '\\'))
2222 {
2223 if (i > 0)
2224 ccline.cmdbuff[len] = firstc;
2225 }
2226 else
2227 {
2228 /* Escape new sep, unless it is already
2229 * escaped. */
2230 if (p[j] == firstc
2231 && (j == 0 || p[j - 1] != '\\'))
2232 {
2233 if (i > 0)
2234 ccline.cmdbuff[len] = '\\';
2235 ++len;
2236 }
2237 if (i > 0)
2238 ccline.cmdbuff[len] = p[j];
2239 }
2240 ++len;
2241 }
2242 if (i == 0)
2243 {
2244 alloc_cmdbuff(len);
2245 if (ccline.cmdbuff == NULL)
2246 goto returncmd;
2247 }
2248 }
2249 ccline.cmdbuff[len] = NUL;
2250 }
2251 else
2252 {
2253 alloc_cmdbuff((int)STRLEN(p));
2254 if (ccline.cmdbuff == NULL)
2255 goto returncmd;
2256 STRCPY(ccline.cmdbuff, p);
2257 }
2258
2259 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2260 redrawcmd();
2261 goto cmdline_changed;
2262 }
2263 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002264#endif
2265 goto cmdline_not_changed;
2266
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002267#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002268 case Ctrl_G: /* next match */
2269 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002270 if (may_adjust_incsearch_highlighting(
2271 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002272 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#endif
2275
2276 case Ctrl_V:
2277 case Ctrl_Q:
2278#ifdef FEAT_MOUSE
2279 ignore_drag_release = TRUE;
2280#endif
2281 putcmdline('^', TRUE);
2282 c = get_literal(); /* get next (two) character(s) */
2283 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002284 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#ifdef FEAT_MBYTE
2286 /* may need to remove ^ when composing char was typed */
2287 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2288 {
2289 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2290 msg_putchar(' ');
2291 cursorcmd();
2292 }
2293#endif
2294 break;
2295
2296#ifdef FEAT_DIGRAPHS
2297 case Ctrl_K:
2298#ifdef FEAT_MOUSE
2299 ignore_drag_release = TRUE;
2300#endif
2301 putcmdline('?', TRUE);
2302#ifdef USE_ON_FLY_SCROLL
2303 dont_scroll = TRUE; /* disallow scrolling here */
2304#endif
2305 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002306 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (c != NUL)
2308 break;
2309
2310 redrawcmd();
2311 goto cmdline_not_changed;
2312#endif /* FEAT_DIGRAPHS */
2313
2314#ifdef FEAT_RIGHTLEFT
2315 case Ctrl__: /* CTRL-_: switch language mode */
2316 if (!p_ari)
2317 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002318# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (p_altkeymap)
2320 {
2321 cmd_fkmap = !cmd_fkmap;
2322 if (cmd_fkmap) /* in Farsi always in Insert mode */
2323 ccline.overstrike = FALSE;
2324 }
2325 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 cmd_hkmap = !cmd_hkmap;
2328 goto cmdline_not_changed;
2329#endif
2330
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002331 case K_PS:
2332 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2333 goto cmdline_changed;
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 default:
2336#ifdef UNIX
2337 if (c == intr_char)
2338 {
2339 gotesc = TRUE; /* will free ccline.cmdbuff after
2340 putting it in history */
2341 goto returncmd; /* back to Normal mode */
2342 }
2343#endif
2344 /*
2345 * Normal character with no special meaning. Just set mod_mask
2346 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2347 * the string <S-Space>. This should only happen after ^V.
2348 */
2349 if (!IS_SPECIAL(c))
2350 mod_mask = 0x0;
2351 break;
2352 }
2353 /*
2354 * End of switch on command line character.
2355 * We come here if we have a normal character.
2356 */
2357
Bram Moolenaarede3e632013-06-23 16:16:19 +02002358 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#ifdef FEAT_MBYTE
2360 /* Add ABBR_OFF for characters above 0x100, this is
2361 * what check_abbr() expects. */
2362 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2363#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002364 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 goto cmdline_changed;
2366
2367 /*
2368 * put the character in the command line
2369 */
2370 if (IS_SPECIAL(c) || mod_mask != 0)
2371 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2372 else
2373 {
2374#ifdef FEAT_MBYTE
2375 if (has_mbyte)
2376 {
2377 j = (*mb_char2bytes)(c, IObuff);
2378 IObuff[j] = NUL; /* exclude composing chars */
2379 put_on_cmdline(IObuff, j, TRUE);
2380 }
2381 else
2382#endif
2383 {
2384 IObuff[0] = c;
2385 put_on_cmdline(IObuff, 1, TRUE);
2386 }
2387 }
2388 goto cmdline_changed;
2389
2390/*
2391 * This part implements incremental searches for "/" and "?"
2392 * Jump to cmdline_not_changed when a character has been read but the command
2393 * line did not change. Then we only search and redraw if something changed in
2394 * the past.
2395 * Jump to cmdline_changed when the command line did change.
2396 * (Sorry for the goto's, I know it is ugly).
2397 */
2398cmdline_not_changed:
2399#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002400 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 continue;
2402#endif
2403
2404cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002405 /* Trigger CmdlineChanged autocommands. */
2406 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002409 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#endif
2411
2412#ifdef FEAT_RIGHTLEFT
2413 if (cmdmsg_rl
2414# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002415 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416# endif
2417 )
2418 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002419 * right-left typing. Not efficient, but it works.
2420 * Do it only when there are no characters left to read
2421 * to avoid useless intermediate redraws. */
2422 if (vpeekc() == NUL)
2423 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#endif
2425 }
2426
2427returncmd:
2428
2429#ifdef FEAT_RIGHTLEFT
2430 cmdmsg_rl = FALSE;
2431#endif
2432
2433#ifdef FEAT_FKMAP
2434 cmd_fkmap = 0;
2435#endif
2436
2437 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002438 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002441 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442#endif
2443
2444 if (ccline.cmdbuff != NULL)
2445 {
2446 /*
2447 * Put line in history buffer (":" and "=" only when it was typed).
2448 */
2449#ifdef FEAT_CMDHIST
2450 if (ccline.cmdlen && firstc != NUL
2451 && (some_key_typed || histype == HIST_SEARCH))
2452 {
2453 add_to_history(histype, ccline.cmdbuff, TRUE,
2454 histype == HIST_SEARCH ? firstc : NUL);
2455 if (firstc == ':')
2456 {
2457 vim_free(new_last_cmdline);
2458 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2459 }
2460 }
2461#endif
2462
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002463 if (gotesc)
2464 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
2467 /*
2468 * If the screen was shifted up, redraw the whole screen (later).
2469 * If the line is too long, clear it, so ruler and shown command do
2470 * not get printed in the middle of it.
2471 */
2472 msg_check();
2473 msg_scroll = save_msg_scroll;
2474 redir_off = FALSE;
2475
2476 /* When the command line was typed, no need for a wait-return prompt. */
2477 if (some_key_typed)
2478 need_wait_return = FALSE;
2479
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002480 /* Trigger CmdlineLeave autocommands. */
2481 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002484#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2486 im_save_status(b_im_ptr);
2487 im_set_active(FALSE);
2488#endif
2489#ifdef FEAT_MOUSE
2490 setmouse();
2491#endif
2492#ifdef CURSOR_SHAPE
2493 ui_cursor_shape(); /* may show different cursor shape */
2494#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002495 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
Bram Moolenaar438d1762018-09-30 17:11:48 +02002497theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002498 {
2499 char_u *p = ccline.cmdbuff;
2500
Bram Moolenaar438d1762018-09-30 17:11:48 +02002501 if (did_save_ccline)
2502 restore_cmdline(&save_ccline);
2503 else
2504 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002505 return p;
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507}
2508
2509#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2510/*
2511 * Get a command line with a prompt.
2512 * This is prepared to be called recursively from getcmdline() (e.g. by
2513 * f_input() when evaluating an expression from CTRL-R =).
2514 * Returns the command line in allocated memory, or NULL.
2515 */
2516 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002517getcmdline_prompt(
2518 int firstc,
2519 char_u *prompt, /* command line prompt */
2520 int attr, /* attributes for prompt */
2521 int xp_context, /* type of expansion */
2522 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 char_u *s;
2525 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002526 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002528 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar438d1762018-09-30 17:11:48 +02002530 if (ccline.cmdbuff != NULL)
2531 {
2532 // Save the values of the current cmdline and restore them below.
2533 save_cmdline(&save_ccline);
2534 did_save_ccline = TRUE;
2535 }
2536
2537 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 ccline.cmdprompt = prompt;
2539 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002540# ifdef FEAT_EVAL
2541 ccline.xp_context = xp_context;
2542 ccline.xp_arg = xp_arg;
2543 ccline.input_fn = (firstc == '@');
2544# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002545 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002546 s = getcmdline_int(firstc, 1L, 0, FALSE);
2547
2548 if (did_save_ccline)
2549 restore_cmdline(&save_ccline);
2550
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002551 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002552 /* Restore msg_col, the prompt from input() may have changed it.
2553 * But only if called recursively and the commandline is therefore being
2554 * restored to an old one; if not, the input() prompt stays on the screen,
2555 * so we need its modified msg_col left intact. */
2556 if (ccline.cmdbuff != NULL)
2557 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 return s;
2560}
2561#endif
2562
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002563/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002564 * Return TRUE when the text must not be changed and we can't switch to
2565 * another window or buffer. Used when editing the command line, evaluating
2566 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002567 */
2568 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002569text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002570{
2571#ifdef FEAT_CMDWIN
2572 if (cmdwin_type != 0)
2573 return TRUE;
2574#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002575 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002576}
2577
2578/*
2579 * Give an error message for a command that isn't allowed while the cmdline
2580 * window is open or editing the cmdline in another way.
2581 */
2582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002583text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002584{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002585 EMSG(_(get_text_locked_msg()));
2586}
2587
2588 char_u *
2589get_text_locked_msg(void)
2590{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002591#ifdef FEAT_CMDWIN
2592 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002593 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002594#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002595 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002596}
2597
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002598/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002599 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2600 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002601 */
2602 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002603curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002604{
2605 if (curbuf_lock > 0)
2606 {
2607 EMSG(_("E788: Not allowed to edit another buffer now"));
2608 return TRUE;
2609 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002610 return allbuf_locked();
2611}
2612
2613/*
2614 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2615 * message.
2616 */
2617 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002618allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002619{
2620 if (allbuf_lock > 0)
2621 {
2622 EMSG(_("E811: Not allowed to change buffer information now"));
2623 return TRUE;
2624 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002625 return FALSE;
2626}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002629cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2632 if (cmdline_star > 0) /* showing '*', always 1 position */
2633 return 1;
2634#endif
2635 return ptr2cells(ccline.cmdbuff + idx);
2636}
2637
2638/*
2639 * Compute the offset of the cursor on the command line for the prompt and
2640 * indent.
2641 */
2642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002643set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002645 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 ccline.cmdspos = 1 + ccline.cmdindent;
2647 else
2648 ccline.cmdspos = 0 + ccline.cmdindent;
2649}
2650
2651/*
2652 * Compute the screen position for the cursor on the command line.
2653 */
2654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002655set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 int i, m, c;
2658
2659 set_cmdspos();
2660 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002663 if (m < 0) /* overflow, Columns or Rows at weird value */
2664 m = MAXCOL;
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 else
2667 m = MAXCOL;
2668 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2669 {
2670 c = cmdline_charsize(i);
2671#ifdef FEAT_MBYTE
2672 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2673 if (has_mbyte)
2674 correct_cmdspos(i, c);
2675#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002676 /* If the cmdline doesn't fit, show cursor on last visible char.
2677 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if ((ccline.cmdspos += c) >= m)
2679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 ccline.cmdspos -= c;
2681 break;
2682 }
2683#ifdef FEAT_MBYTE
2684 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002685 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#endif
2687 }
2688}
2689
2690#ifdef FEAT_MBYTE
2691/*
2692 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2693 * character that doesn't fit, so that a ">" must be displayed.
2694 */
2695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002696correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002698 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2700 && ccline.cmdspos % Columns + cells > Columns)
2701 ccline.cmdspos++;
2702}
2703#endif
2704
2705/*
2706 * Get an Ex command line for the ":" command.
2707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002709getexline(
2710 int c, /* normally ':', NUL for ":append" */
2711 void *cookie UNUSED,
2712 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 /* When executing a register, remove ':' that's in front of each line. */
2715 if (exec_from_reg && vpeekc() == ':')
2716 (void)vgetc();
2717 return getcmdline(c, 1L, indent);
2718}
2719
2720/*
2721 * Get an Ex command line for Ex mode.
2722 * In Ex mode we only use the OS supplied line editing features and no
2723 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002727getexmodeline(
2728 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002730 void *cookie UNUSED,
2731 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 garray_T line_ga;
2734 char_u *pend;
2735 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002736 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 int escaped = FALSE; /* CTRL-V typed */
2738 int vcol = 0;
2739 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002740 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002741 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 /* Switch cursor on now. This avoids that it happens after the "\n", which
2744 * confuses the system function that computes tabstops. */
2745 cursor_on();
2746
2747 /* always start in column 0; write a newline if necessary */
2748 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002751 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002753 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 if (p_prompt)
2755 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 while (indent-- > 0)
2757 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760
2761 ga_init2(&line_ga, 1, 30);
2762
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002763 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002765 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002766 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002767 while (indent >= 8)
2768 {
2769 ga_append(&line_ga, TAB);
2770 msg_puts((char_u *)" ");
2771 indent -= 8;
2772 }
2773 while (indent-- > 0)
2774 {
2775 ga_append(&line_ga, ' ');
2776 msg_putchar(' ');
2777 }
2778 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 ++no_mapping;
2780 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 /*
2783 * Get the line, one character at a time.
2784 */
2785 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002786 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002788 long sw;
2789 char_u *s;
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (ga_grow(&line_ga, 40) == FAIL)
2792 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaarba748c82017-02-26 14:00:07 +01002794 /*
2795 * Get one character at a time.
2796 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002797 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002798
2799 /* Check for a ":normal" command and no more characters left. */
2800 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2801 c1 = '\n';
2802 else
2803 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806 * Handle line editing.
2807 * Previously this was left to the system, putting the terminal in
2808 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002812 msg_putchar('\n');
2813 break;
2814 }
2815
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002816 if (c1 == K_PS)
2817 {
2818 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2819 goto redraw;
2820 }
2821
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 if (!escaped)
2823 {
2824 /* CR typed means "enter", which is NL */
2825 if (c1 == '\r')
2826 c1 = '\n';
2827
2828 if (c1 == BS || c1 == K_BS
2829 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 if (line_ga.ga_len > 0)
2832 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002833#ifdef FEAT_MBYTE
2834 if (has_mbyte)
2835 {
2836 p = (char_u *)line_ga.ga_data;
2837 p[line_ga.ga_len] = NUL;
2838 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2839 line_ga.ga_len -= len;
2840 }
2841 else
2842#endif
2843 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844 goto redraw;
2845 }
2846 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 msg_col = startcol;
2852 msg_clr_eos();
2853 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002854 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 }
2856
2857 if (c1 == Ctrl_T)
2858 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002859 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 p = (char_u *)line_ga.ga_data;
2861 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002862 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002863 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002865 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002867 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002868 p = (char_u *)line_ga.ga_data;
2869 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2871 *s = ' ';
2872 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874redraw:
2875 /* redraw the line */
2876 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002878 p = (char_u *)line_ga.ga_data;
2879 p[line_ga.ga_len] = NUL;
2880 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002882 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002884 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886 msg_putchar(' ');
2887 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002888 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002892 len = MB_PTR2LEN(p);
2893 msg_outtrans_len(p, len);
2894 vcol += ptr2cells(p);
2895 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002899 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900 continue;
2901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002903 if (c1 == Ctrl_D)
2904 {
2905 /* Delete one shiftwidth. */
2906 p = (char_u *)line_ga.ga_data;
2907 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909 if (prev_char == '^')
2910 ex_keep_indent = TRUE;
2911 indent = 0;
2912 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914 else
2915 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002916 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002917 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002918 if (indent > 0)
2919 {
2920 --indent;
2921 indent -= indent % get_sw_value(curbuf);
2922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002924 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002925 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002926 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002927 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2928 --line_ga.ga_len;
2929 }
2930 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002932
2933 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2934 {
2935 escaped = TRUE;
2936 continue;
2937 }
2938
2939 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2940 if (IS_SPECIAL(c1))
2941 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002943
2944 if (IS_SPECIAL(c1))
2945 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002946#ifdef FEAT_MBYTE
2947 if (has_mbyte)
2948 len = (*mb_char2bytes)(c1,
2949 (char_u *)line_ga.ga_data + line_ga.ga_len);
2950 else
2951#endif
2952 {
2953 len = 1;
2954 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2955 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002956 if (c1 == '\n')
2957 msg_putchar('\n');
2958 else if (c1 == TAB)
2959 {
2960 /* Don't use chartabsize(), 'ts' can be different */
2961 do
2962 {
2963 msg_putchar(' ');
2964 } while (++vcol % 8);
2965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002968 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002969 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002970 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002972 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002973 escaped = FALSE;
2974
2975 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002976 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002977
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002978 /* We are done when a NL is entered, but not when it comes after an
2979 * odd number of backslashes, that results in a NUL. */
2980 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002982 int bcount = 0;
2983
2984 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2985 ++bcount;
2986
2987 if (bcount > 0)
2988 {
2989 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2990 * "\NL", etc. */
2991 line_ga.ga_len -= (bcount + 1) / 2;
2992 pend -= (bcount + 1) / 2;
2993 pend[-1] = '\n';
2994 }
2995
2996 if ((bcount & 1) == 0)
2997 {
2998 --line_ga.ga_len;
2999 --pend;
3000 *pend = NUL;
3001 break;
3002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 }
3005
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003006 --no_mapping;
3007 --allow_keys;
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 /* make following messages go to the next line */
3010 msg_didout = FALSE;
3011 msg_col = 0;
3012 if (msg_row < Rows - 1)
3013 ++msg_row;
3014 emsg_on_display = FALSE; /* don't want ui_delay() */
3015
3016 if (got_int)
3017 ga_clear(&line_ga);
3018
3019 return (char_u *)line_ga.ga_data;
3020}
3021
Bram Moolenaare344bea2005-09-01 20:46:49 +00003022# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3023 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024/*
3025 * Return TRUE if ccline.overstrike is on.
3026 */
3027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003028cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 return ccline.overstrike;
3031}
3032
3033/*
3034 * Return TRUE if the cursor is at the end of the cmdline.
3035 */
3036 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003037cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 return (ccline.cmdpos >= ccline.cmdlen);
3040}
3041#endif
3042
Bram Moolenaar9372a112005-12-06 19:59:18 +00003043#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * Return the virtual column number at the current cursor position.
3046 * This is used by the IM code to obtain the start of the preedit string.
3047 */
3048 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3052 return MAXCOL;
3053
3054# ifdef FEAT_MBYTE
3055 if (has_mbyte)
3056 {
3057 colnr_T col;
3058 int i = 0;
3059
3060 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003061 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 return col;
3064 }
3065 else
3066# endif
3067 return ccline.cmdpos;
3068}
3069#endif
3070
3071#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3072/*
3073 * If part of the command line is an IM preedit string, redraw it with
3074 * IM feedback attributes. The cursor position is restored after drawing.
3075 */
3076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003077redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 if ((State & CMDLINE)
3080 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003081 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 && !p_imdisable
3083 && im_is_preediting())
3084 {
3085 int cmdpos = 0;
3086 int cmdspos;
3087 int old_row;
3088 int old_col;
3089 colnr_T col;
3090
3091 old_row = msg_row;
3092 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003093 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095# ifdef FEAT_MBYTE
3096 if (has_mbyte)
3097 {
3098 for (col = 0; col < preedit_start_col
3099 && cmdpos < ccline.cmdlen; ++col)
3100 {
3101 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003102 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104 }
3105 else
3106# endif
3107 {
3108 cmdspos += preedit_start_col;
3109 cmdpos += preedit_start_col;
3110 }
3111
3112 msg_row = cmdline_row + (cmdspos / (int)Columns);
3113 msg_col = cmdspos % (int)Columns;
3114 if (msg_row >= Rows)
3115 msg_row = Rows - 1;
3116
3117 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3118 {
3119 int char_len;
3120 int char_attr;
3121
3122 char_attr = im_get_feedback_attr(col);
3123 if (char_attr < 0)
3124 break; /* end of preedit string */
3125
3126# ifdef FEAT_MBYTE
3127 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003128 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 else
3130# endif
3131 char_len = 1;
3132
3133 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3134 cmdpos += char_len;
3135 }
3136
3137 msg_row = old_row;
3138 msg_col = old_col;
3139 }
3140}
3141#endif /* FEAT_XIM && FEAT_GUI_GTK */
3142
3143/*
3144 * Allocate a new command line buffer.
3145 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
3147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150 /*
3151 * give some extra space to avoid having to allocate all the time
3152 */
3153 if (len < 80)
3154 len = 100;
3155 else
3156 len += 20;
3157
3158 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3159 ccline.cmdbufflen = len;
3160}
3161
3162/*
3163 * Re-allocate the command line to length len + something extra.
3164 * return FAIL for failure, OK otherwise
3165 */
3166 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003167realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 char_u *p;
3170
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003171 if (len < ccline.cmdbufflen)
3172 return OK; /* no need to resize */
3173
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 p = ccline.cmdbuff;
3175 alloc_cmdbuff(len); /* will get some more */
3176 if (ccline.cmdbuff == NULL) /* out of memory */
3177 {
3178 ccline.cmdbuff = p; /* keep the old one */
3179 return FAIL;
3180 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003181 /* There isn't always a NUL after the command, but it may need to be
3182 * there, thus copy up to the NUL and add a NUL. */
3183 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3184 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003186
3187 if (ccline.xpc != NULL
3188 && ccline.xpc->xp_pattern != NULL
3189 && ccline.xpc->xp_context != EXPAND_NOTHING
3190 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3191 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003192 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003193
3194 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3195 * to point into the newly allocated memory. */
3196 if (i >= 0 && i <= ccline.cmdlen)
3197 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3198 }
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return OK;
3201}
3202
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203#if defined(FEAT_ARABIC) || defined(PROTO)
3204static char_u *arshape_buf = NULL;
3205
3206# if defined(EXITFREE) || defined(PROTO)
3207 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003208free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003209{
3210 vim_free(arshape_buf);
3211}
3212# endif
3213#endif
3214
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215/*
3216 * Draw part of the cmdline at the current cursor position. But draw stars
3217 * when cmdline_star is TRUE.
3218 */
3219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003220draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3223 int i;
3224
3225 if (cmdline_star > 0)
3226 for (i = 0; i < len; ++i)
3227 {
3228 msg_putchar('*');
3229# ifdef FEAT_MBYTE
3230 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003231 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232# endif
3233 }
3234 else
3235#endif
3236#ifdef FEAT_ARABIC
3237 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 static int buflen = 0;
3240 char_u *p;
3241 int j;
3242 int newlen = 0;
3243 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003244 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 int prev_c = 0;
3246 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003247 int u8c;
3248 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 /*
3252 * Do arabic shaping into a temporary buffer. This is very
3253 * inefficient!
3254 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003255 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
3257 /* Re-allocate the buffer. We keep it around to avoid a lot of
3258 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003260 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 arshape_buf = alloc(buflen);
3262 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 return; /* out of memory */
3264 }
3265
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003266 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3267 {
3268 /* Prepend a space to draw the leading composing char on. */
3269 arshape_buf[0] = ' ';
3270 newlen = 1;
3271 }
3272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 for (j = start; j < start + len; j += mb_l)
3274 {
3275 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003276 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003277 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (ARABIC_CHAR(u8c))
3279 {
3280 /* Do Arabic shaping. */
3281 if (cmdmsg_rl)
3282 {
3283 /* displaying from right to left */
3284 pc = prev_c;
3285 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003286 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (j + mb_l >= start + len)
3288 nc = NUL;
3289 else
3290 nc = utf_ptr2char(p + mb_l);
3291 }
3292 else
3293 {
3294 /* displaying from left to right */
3295 if (j + mb_l >= start + len)
3296 pc = NUL;
3297 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003298 {
3299 int pcc[MAX_MCO];
3300
3301 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003303 pc1 = pcc[0];
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 nc = prev_c;
3306 }
3307 prev_c = u8c;
3308
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003309 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003311 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003312 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003314 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3315 if (u8cc[1] != 0)
3316 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003317 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319 }
3320 else
3321 {
3322 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 newlen += mb_l;
3325 }
3326 }
3327
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003328 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330 else
3331#endif
3332 msg_outtrans_len(ccline.cmdbuff + start, len);
3333}
3334
3335/*
3336 * Put a character on the command line. Shifts the following text to the
3337 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3338 * "c" must be printable (fit in one display cell)!
3339 */
3340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003341putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 if (cmd_silent)
3344 return;
3345 msg_no_more = TRUE;
3346 msg_putchar(c);
3347 if (shift)
3348 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3349 msg_no_more = FALSE;
3350 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003351 extra_char = c;
3352 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353}
3354
3355/*
3356 * Undo a putcmdline(c, FALSE).
3357 */
3358 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003359unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 if (cmd_silent)
3362 return;
3363 msg_no_more = TRUE;
3364 if (ccline.cmdlen == ccline.cmdpos)
3365 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003366#ifdef FEAT_MBYTE
3367 else if (has_mbyte)
3368 draw_cmdline(ccline.cmdpos,
3369 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
3372 draw_cmdline(ccline.cmdpos, 1);
3373 msg_no_more = FALSE;
3374 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003375 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376}
3377
3378/*
3379 * Put the given string, of the given length, onto the command line.
3380 * If len is -1, then STRLEN() is used to calculate the length.
3381 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3382 * part will be redrawn, otherwise it will not. If this function is called
3383 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3384 * called afterwards.
3385 */
3386 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003387put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 int retval;
3390 int i;
3391 int m;
3392 int c;
3393
3394 if (len < 0)
3395 len = (int)STRLEN(str);
3396
3397 /* Check if ccline.cmdbuff needs to be longer */
3398 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003399 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 else
3401 retval = OK;
3402 if (retval == OK)
3403 {
3404 if (!ccline.overstrike)
3405 {
3406 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3407 ccline.cmdbuff + ccline.cmdpos,
3408 (size_t)(ccline.cmdlen - ccline.cmdpos));
3409 ccline.cmdlen += len;
3410 }
3411 else
3412 {
3413#ifdef FEAT_MBYTE
3414 if (has_mbyte)
3415 {
3416 /* Count nr of characters in the new string. */
3417 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003418 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 ++m;
3420 /* Count nr of bytes in cmdline that are overwritten by these
3421 * characters. */
3422 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003423 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 --m;
3425 if (i < ccline.cmdlen)
3426 {
3427 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3428 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3429 ccline.cmdlen += ccline.cmdpos + len - i;
3430 }
3431 else
3432 ccline.cmdlen = ccline.cmdpos + len;
3433 }
3434 else
3435#endif
3436 if (ccline.cmdpos + len > ccline.cmdlen)
3437 ccline.cmdlen = ccline.cmdpos + len;
3438 }
3439 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3440 ccline.cmdbuff[ccline.cmdlen] = NUL;
3441
3442#ifdef FEAT_MBYTE
3443 if (enc_utf8)
3444 {
3445 /* When the inserted text starts with a composing character,
3446 * backup to the character before it. There could be two of them.
3447 */
3448 i = 0;
3449 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3450 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3451 {
3452 i = (*mb_head_off)(ccline.cmdbuff,
3453 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3454 ccline.cmdpos -= i;
3455 len += i;
3456 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3457 }
3458# ifdef FEAT_ARABIC
3459 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3460 {
3461 /* Check the previous character for Arabic combining pair. */
3462 i = (*mb_head_off)(ccline.cmdbuff,
3463 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3464 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3465 + ccline.cmdpos - i), c))
3466 {
3467 ccline.cmdpos -= i;
3468 len += i;
3469 }
3470 else
3471 i = 0;
3472 }
3473# endif
3474 if (i != 0)
3475 {
3476 /* Also backup the cursor position. */
3477 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3478 ccline.cmdspos -= i;
3479 msg_col -= i;
3480 if (msg_col < 0)
3481 {
3482 msg_col += Columns;
3483 --msg_row;
3484 }
3485 }
3486 }
3487#endif
3488
3489 if (redraw && !cmd_silent)
3490 {
3491 msg_no_more = TRUE;
3492 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003493 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3495 /* Avoid clearing the rest of the line too often. */
3496 if (cmdline_row != i || ccline.overstrike)
3497 msg_clr_eos();
3498 msg_no_more = FALSE;
3499 }
3500#ifdef FEAT_FKMAP
3501 /*
3502 * If we are in Farsi command mode, the character input must be in
3503 * Insert mode. So do not advance the cmdpos.
3504 */
3505 if (!cmd_fkmap)
3506#endif
3507 {
3508 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003511 if (m < 0) /* overflow, Columns or Rows at weird value */
3512 m = MAXCOL;
3513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 else
3515 m = MAXCOL;
3516 for (i = 0; i < len; ++i)
3517 {
3518 c = cmdline_charsize(ccline.cmdpos);
3519#ifdef FEAT_MBYTE
3520 /* count ">" for a double-wide char that doesn't fit. */
3521 if (has_mbyte)
3522 correct_cmdspos(ccline.cmdpos, c);
3523#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003524 /* Stop cursor at the end of the screen, but do increment the
3525 * insert position, so that entering a very long command
3526 * works, even though you can't see it. */
3527 if (ccline.cmdspos + c < m)
3528 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529#ifdef FEAT_MBYTE
3530 if (has_mbyte)
3531 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003532 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (c > len - i - 1)
3534 c = len - i - 1;
3535 ccline.cmdpos += c;
3536 i += c;
3537 }
3538#endif
3539 ++ccline.cmdpos;
3540 }
3541 }
3542 }
3543 if (redraw)
3544 msg_check();
3545 return retval;
3546}
3547
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003548static struct cmdline_info prev_ccline;
3549static int prev_ccline_used = FALSE;
3550
3551/*
3552 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3553 * and overwrite it. But get_cmdline_str() may need it, thus make it
3554 * available globally in prev_ccline.
3555 */
3556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003557save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003558{
3559 if (!prev_ccline_used)
3560 {
3561 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3562 prev_ccline_used = TRUE;
3563 }
3564 *ccp = prev_ccline;
3565 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003566 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003567}
3568
3569/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003570 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003571 */
3572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003573restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003574{
3575 ccline = prev_ccline;
3576 prev_ccline = *ccp;
3577}
3578
Bram Moolenaar8299df92004-07-10 09:47:34 +00003579/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003580 * Paste a yank register into the command line.
3581 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003582 * insert_reg() can't be used here, because special characters from the
3583 * register contents will be interpreted as commands.
3584 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003585 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003586 */
3587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003588cmdline_paste(
3589 int regname,
3590 int literally, /* Insert text literally instead of "as typed" */
3591 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003592{
3593 long i;
3594 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003595 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597
3598 /* check for valid regname; also accept special characters for CTRL-R in
3599 * the command line */
3600 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003601 && regname != Ctrl_A && regname != Ctrl_L
3602 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603 return FAIL;
3604
3605 /* A register containing CTRL-R can cause an endless loop. Allow using
3606 * CTRL-C to break the loop. */
3607 line_breakcheck();
3608 if (got_int)
3609 return FAIL;
3610
3611#ifdef FEAT_CLIPBOARD
3612 regname = may_get_selection(regname);
3613#endif
3614
Bram Moolenaar438d1762018-09-30 17:11:48 +02003615 // Need to set "textlock" to avoid nasty things like going to another
3616 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003617 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003618 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003619 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003620
3621 if (i)
3622 {
3623 /* Got the value of a special register in "arg". */
3624 if (arg == NULL)
3625 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003626
3627 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3628 * part of the word. */
3629 p = arg;
3630 if (p_is && regname == Ctrl_W)
3631 {
3632 char_u *w;
3633 int len;
3634
3635 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003636 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003637 {
3638#ifdef FEAT_MBYTE
3639 if (has_mbyte)
3640 {
3641 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3642 if (!vim_iswordc(mb_ptr2char(w - len)))
3643 break;
3644 w -= len;
3645 }
3646 else
3647#endif
3648 {
3649 if (!vim_iswordc(w[-1]))
3650 break;
3651 --w;
3652 }
3653 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003654 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003655 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3656 p += len;
3657 }
3658
3659 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003660 if (allocated)
3661 vim_free(arg);
3662 return OK;
3663 }
3664
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003665 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003666}
3667
3668/*
3669 * Put a string on the command line.
3670 * When "literally" is TRUE, insert literally.
3671 * When "literally" is FALSE, insert as typed, but don't leave the command
3672 * line.
3673 */
3674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003675cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003676{
3677 int c, cv;
3678
3679 if (literally)
3680 put_on_cmdline(s, -1, TRUE);
3681 else
3682 while (*s != NUL)
3683 {
3684 cv = *s;
3685 if (cv == Ctrl_V && s[1])
3686 ++s;
3687#ifdef FEAT_MBYTE
3688 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003689 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003690 else
3691#endif
3692 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003693 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3694 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003695#ifdef UNIX
3696 || c == intr_char
3697#endif
3698 || (c == Ctrl_BSL && *s == Ctrl_N))
3699 stuffcharReadbuff(Ctrl_V);
3700 stuffcharReadbuff(c);
3701 }
3702}
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#ifdef FEAT_WILDMENU
3705/*
3706 * Delete characters on the command line, from "from" to the current
3707 * position.
3708 */
3709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003710cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3713 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3714 ccline.cmdlen -= ccline.cmdpos - from;
3715 ccline.cmdpos = from;
3716}
3717#endif
3718
3719/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003720 * This function is called when the screen size changes and with incremental
3721 * search and in other situations where the command line may have been
3722 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 */
3724 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003725redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003727 redrawcmdline_ex(TRUE);
3728}
3729
3730 void
3731redrawcmdline_ex(int do_compute_cmdrow)
3732{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (cmd_silent)
3734 return;
3735 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003736 if (do_compute_cmdrow)
3737 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 redrawcmd();
3739 cursorcmd();
3740}
3741
3742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003743redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 int i;
3746
3747 if (cmd_silent)
3748 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003749 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 msg_putchar(ccline.cmdfirstc);
3751 if (ccline.cmdprompt != NULL)
3752 {
3753 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3754 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3755 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003756 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 --ccline.cmdindent;
3758 }
3759 else
3760 for (i = ccline.cmdindent; i > 0; --i)
3761 msg_putchar(' ');
3762}
3763
3764/*
3765 * Redraw what is currently on the command line.
3766 */
3767 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003768redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 if (cmd_silent)
3771 return;
3772
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003773 /* when 'incsearch' is set there may be no command line while redrawing */
3774 if (ccline.cmdbuff == NULL)
3775 {
3776 windgoto(cmdline_row, 0);
3777 msg_clr_eos();
3778 return;
3779 }
3780
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 msg_start();
3782 redrawcmdprompt();
3783
3784 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3785 msg_no_more = TRUE;
3786 draw_cmdline(0, ccline.cmdlen);
3787 msg_clr_eos();
3788 msg_no_more = FALSE;
3789
3790 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003791 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003792 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /*
3795 * An emsg() before may have set msg_scroll. This is used in normal mode,
3796 * in cmdline mode we can reset them now.
3797 */
3798 msg_scroll = FALSE; /* next message overwrites cmdline */
3799
3800 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3801 * in cmdline mode */
3802 skip_redraw = FALSE;
3803}
3804
3805 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003806compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003808 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 cmdline_row = Rows - 1;
3810 else
3811 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003812 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813}
3814
3815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003816cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
3818 if (cmd_silent)
3819 return;
3820
3821#ifdef FEAT_RIGHTLEFT
3822 if (cmdmsg_rl)
3823 {
3824 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3825 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3826 if (msg_row <= 0)
3827 msg_row = Rows - 1;
3828 }
3829 else
3830#endif
3831 {
3832 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3833 msg_col = ccline.cmdspos % (int)Columns;
3834 if (msg_row >= Rows)
3835 msg_row = Rows - 1;
3836 }
3837
3838 windgoto(msg_row, msg_col);
3839#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003840 if (p_imst == IM_ON_THE_SPOT)
3841 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#endif
3843#ifdef MCH_CURSOR_SHAPE
3844 mch_update_cursor();
3845#endif
3846}
3847
3848 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003849gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851 msg_start();
3852#ifdef FEAT_RIGHTLEFT
3853 if (cmdmsg_rl)
3854 msg_col = Columns - 1;
3855 else
3856#endif
3857 msg_col = 0; /* always start in column 0 */
3858 if (clr) /* clear the bottom line(s) */
3859 msg_clr_eos(); /* will reset clear_cmdline */
3860 windgoto(cmdline_row, 0);
3861}
3862
3863/*
3864 * Check the word in front of the cursor for an abbreviation.
3865 * Called when the non-id character "c" has been entered.
3866 * When an abbreviation is recognized it is removed from the text with
3867 * backspaces and the replacement string is inserted, followed by "c".
3868 */
3869 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003870ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003872 int spos = 0;
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3875 return FALSE;
3876
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003877 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3878 * Actually accepts any mark. */
3879 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3880 spos++;
3881 if (ccline.cmdlen - spos > 5
3882 && ccline.cmdbuff[spos] == '\''
3883 && ccline.cmdbuff[spos + 2] == ','
3884 && ccline.cmdbuff[spos + 3] == '\'')
3885 spos += 5;
3886 else
3887 /* check abbreviation from the beginning of the commandline */
3888 spos = 0;
3889
3890 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891}
3892
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003893#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3894 static int
3895#ifdef __BORLANDC__
3896_RTLENTRYF
3897#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003898sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003899{
3900 char_u *p1 = *(char_u **)s1;
3901 char_u *p2 = *(char_u **)s2;
3902
3903 if (*p1 != '<' && *p2 == '<') return -1;
3904 if (*p1 == '<' && *p2 != '<') return 1;
3905 return STRCMP(p1, p2);
3906}
3907#endif
3908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909/*
3910 * Return FAIL if this is not an appropriate context in which to do
3911 * completion of anything, return OK if it is (even if there are no matches).
3912 * For the caller, this means that the character is just passed through like a
3913 * normal character (instead of being expanded). This allows :s/^I^D etc.
3914 */
3915 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003916nextwild(
3917 expand_T *xp,
3918 int type,
3919 int options, /* extra options for ExpandOne() */
3920 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
3922 int i, j;
3923 char_u *p1;
3924 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 int difflen;
3926 int v;
3927
3928 if (xp->xp_numfiles == -1)
3929 {
3930 set_expand_context(xp);
3931 cmd_showtail = expand_showtail(xp);
3932 }
3933
3934 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3935 {
3936 beep_flush();
3937 return OK; /* Something illegal on command line */
3938 }
3939 if (xp->xp_context == EXPAND_NOTHING)
3940 {
3941 /* Caller can use the character as a normal char instead */
3942 return FAIL;
3943 }
3944
3945 MSG_PUTS("..."); /* show that we are busy */
3946 out_flush();
3947
3948 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003949 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
3951 if (type == WILD_NEXT || type == WILD_PREV)
3952 {
3953 /*
3954 * Get next/previous match for a previous expanded pattern.
3955 */
3956 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3957 }
3958 else
3959 {
3960 /*
3961 * Translate string into pattern and expand it.
3962 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003963 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3964 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 p2 = NULL;
3966 else
3967 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003968 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003969 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3970 if (escape)
3971 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003972
3973 if (p_wic)
3974 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003975 p2 = ExpandOne(xp, p1,
3976 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003977 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003979 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (p2 != NULL && type == WILD_LONGEST)
3981 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003982 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (ccline.cmdbuff[i + j] == '*'
3984 || ccline.cmdbuff[i + j] == '?')
3985 break;
3986 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003987 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 }
3990 }
3991
3992 if (p2 != NULL && !got_int)
3993 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003994 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003995 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003997 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 xp->xp_pattern = ccline.cmdbuff + i;
3999 }
4000 else
4001 v = OK;
4002 if (v == OK)
4003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4005 &ccline.cmdbuff[ccline.cmdpos],
4006 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4007 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 ccline.cmdlen += difflen;
4009 ccline.cmdpos += difflen;
4010 }
4011 }
4012 vim_free(p2);
4013
4014 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004015 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 /* When expanding a ":map" command and no matches are found, assume that
4018 * the key is supposed to be inserted literally */
4019 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4020 return FAIL;
4021
4022 if (xp->xp_numfiles <= 0 && p2 == NULL)
4023 beep_flush();
4024 else if (xp->xp_numfiles == 1)
4025 /* free expanded pattern */
4026 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4027
4028 return OK;
4029}
4030
4031/*
4032 * Do wildcard expansion on the string 'str'.
4033 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004034 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * Return NULL for failure.
4036 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004037 * "orig" is the originally expanded string, copied to allocated memory. It
4038 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4039 * WILD_PREV "orig" should be NULL.
4040 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004041 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4042 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 *
4044 * mode = WILD_FREE: just free previously expanded matches
4045 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4046 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4047 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4048 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4049 * mode = WILD_ALL: return all matches concatenated
4050 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004051 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 *
4053 * options = WILD_LIST_NOTFOUND: list entries without a match
4054 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4055 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4056 * options = WILD_NO_BEEP: Don't beep for multiple matches
4057 * options = WILD_ADD_SLASH: add a slash after directory names
4058 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4059 * options = WILD_SILENT: don't print warning messages
4060 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004061 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 *
4063 * The variables xp->xp_context and xp->xp_backslash must have been set!
4064 */
4065 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004066ExpandOne(
4067 expand_T *xp,
4068 char_u *str,
4069 char_u *orig, /* allocated copy of original of expanded string */
4070 int options,
4071 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
4073 char_u *ss = NULL;
4074 static int findex;
4075 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004076 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int i;
4078 long_u len;
4079 int non_suf_match; /* number without matching suffix */
4080
4081 /*
4082 * first handle the case of using an old match
4083 */
4084 if (mode == WILD_NEXT || mode == WILD_PREV)
4085 {
4086 if (xp->xp_numfiles > 0)
4087 {
4088 if (mode == WILD_PREV)
4089 {
4090 if (findex == -1)
4091 findex = xp->xp_numfiles;
4092 --findex;
4093 }
4094 else /* mode == WILD_NEXT */
4095 ++findex;
4096
4097 /*
4098 * When wrapping around, return the original string, set findex to
4099 * -1.
4100 */
4101 if (findex < 0)
4102 {
4103 if (orig_save == NULL)
4104 findex = xp->xp_numfiles - 1;
4105 else
4106 findex = -1;
4107 }
4108 if (findex >= xp->xp_numfiles)
4109 {
4110 if (orig_save == NULL)
4111 findex = 0;
4112 else
4113 findex = -1;
4114 }
4115#ifdef FEAT_WILDMENU
4116 if (p_wmnu)
4117 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4118 findex, cmd_showtail);
4119#endif
4120 if (findex == -1)
4121 return vim_strsave(orig_save);
4122 return vim_strsave(xp->xp_files[findex]);
4123 }
4124 else
4125 return NULL;
4126 }
4127
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004128 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4130 {
4131 FreeWild(xp->xp_numfiles, xp->xp_files);
4132 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004133 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 findex = 0;
4136
4137 if (mode == WILD_FREE) /* only release file name */
4138 return NULL;
4139
4140 if (xp->xp_numfiles == -1)
4141 {
4142 vim_free(orig_save);
4143 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004144 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 /*
4147 * Do the expansion.
4148 */
4149 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4150 options) == FAIL)
4151 {
4152#ifdef FNAME_ILLEGAL
4153 /* Illegal file name has been silently skipped. But when there
4154 * are wildcards, the real problem is that there was no match,
4155 * causing the pattern to be added, which has illegal characters.
4156 */
4157 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4158 EMSG2(_(e_nomatch2), str);
4159#endif
4160 }
4161 else if (xp->xp_numfiles == 0)
4162 {
4163 if (!(options & WILD_SILENT))
4164 EMSG2(_(e_nomatch2), str);
4165 }
4166 else
4167 {
4168 /* Escape the matches for use on the command line. */
4169 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4170
4171 /*
4172 * Check for matching suffixes in file names.
4173 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004174 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4175 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 if (xp->xp_numfiles)
4178 non_suf_match = xp->xp_numfiles;
4179 else
4180 non_suf_match = 1;
4181 if ((xp->xp_context == EXPAND_FILES
4182 || xp->xp_context == EXPAND_DIRECTORIES)
4183 && xp->xp_numfiles > 1)
4184 {
4185 /*
4186 * More than one match; check suffix.
4187 * The files will have been sorted on matching suffix in
4188 * expand_wildcards, only need to check the first two.
4189 */
4190 non_suf_match = 0;
4191 for (i = 0; i < 2; ++i)
4192 if (match_suffix(xp->xp_files[i]))
4193 ++non_suf_match;
4194 }
4195 if (non_suf_match != 1)
4196 {
4197 /* Can we ever get here unless it's while expanding
4198 * interactively? If not, we can get rid of this all
4199 * together. Don't really want to wait for this message
4200 * (and possibly have to hit return to continue!).
4201 */
4202 if (!(options & WILD_SILENT))
4203 EMSG(_(e_toomany));
4204 else if (!(options & WILD_NO_BEEP))
4205 beep_flush();
4206 }
4207 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4208 ss = vim_strsave(xp->xp_files[0]);
4209 }
4210 }
4211 }
4212
4213 /* Find longest common part */
4214 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4215 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004216 int mb_len = 1;
4217 int c0, ci;
4218
4219 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004221#ifdef FEAT_MBYTE
4222 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004224 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4225 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4226 }
4227 else
4228#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004229 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004230 for (i = 1; i < xp->xp_numfiles; ++i)
4231 {
4232#ifdef FEAT_MBYTE
4233 if (has_mbyte)
4234 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4235 else
4236#endif
4237 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004238 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004240 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004241 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004243 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 break;
4245 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004246 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 break;
4248 }
4249 if (i < xp->xp_numfiles)
4250 {
4251 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004252 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 break;
4254 }
4255 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 ss = alloc((unsigned)len + 1);
4258 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004259 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 findex = -1; /* next p_wc gets first one */
4261 }
4262
4263 /* Concatenate all matching names */
4264 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4265 {
4266 len = 0;
4267 for (i = 0; i < xp->xp_numfiles; ++i)
4268 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4269 ss = lalloc(len, TRUE);
4270 if (ss != NULL)
4271 {
4272 *ss = NUL;
4273 for (i = 0; i < xp->xp_numfiles; ++i)
4274 {
4275 STRCAT(ss, xp->xp_files[i]);
4276 if (i != xp->xp_numfiles - 1)
4277 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4278 }
4279 }
4280 }
4281
4282 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4283 ExpandCleanup(xp);
4284
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004285 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004286 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004287 vim_free(orig);
4288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return ss;
4290}
4291
4292/*
4293 * Prepare an expand structure for use.
4294 */
4295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004296ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004298 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004299 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004301#ifndef BACKSLASH_IN_FILENAME
4302 xp->xp_shell = FALSE;
4303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 xp->xp_numfiles = -1;
4305 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004306#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4307 xp->xp_arg = NULL;
4308#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004309 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310}
4311
4312/*
4313 * Cleanup an expand structure after use.
4314 */
4315 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004316ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
4318 if (xp->xp_numfiles >= 0)
4319 {
4320 FreeWild(xp->xp_numfiles, xp->xp_files);
4321 xp->xp_numfiles = -1;
4322 }
4323}
4324
4325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004326ExpandEscape(
4327 expand_T *xp,
4328 char_u *str,
4329 int numfiles,
4330 char_u **files,
4331 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
4333 int i;
4334 char_u *p;
4335
4336 /*
4337 * May change home directory back to "~"
4338 */
4339 if (options & WILD_HOME_REPLACE)
4340 tilde_replace(str, numfiles, files);
4341
4342 if (options & WILD_ESCAPE)
4343 {
4344 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004345 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004346 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 || xp->xp_context == EXPAND_BUFFERS
4348 || xp->xp_context == EXPAND_DIRECTORIES)
4349 {
4350 /*
4351 * Insert a backslash into a file name before a space, \, %, #
4352 * and wildmatch characters, except '~'.
4353 */
4354 for (i = 0; i < numfiles; ++i)
4355 {
4356 /* for ":set path=" we need to escape spaces twice */
4357 if (xp->xp_backslash == XP_BS_THREE)
4358 {
4359 p = vim_strsave_escaped(files[i], (char_u *)" ");
4360 if (p != NULL)
4361 {
4362 vim_free(files[i]);
4363 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004364#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 p = vim_strsave_escaped(files[i], (char_u *)" ");
4366 if (p != NULL)
4367 {
4368 vim_free(files[i]);
4369 files[i] = p;
4370 }
4371#endif
4372 }
4373 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004374#ifdef BACKSLASH_IN_FILENAME
4375 p = vim_strsave_fnameescape(files[i], FALSE);
4376#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004377 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (p != NULL)
4380 {
4381 vim_free(files[i]);
4382 files[i] = p;
4383 }
4384
4385 /* If 'str' starts with "\~", replace "~" at start of
4386 * files[i] with "\~". */
4387 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004388 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004391
4392 /* If the first file starts with a '+' escape it. Otherwise it
4393 * could be seen as "+cmd". */
4394 if (*files[0] == '+')
4395 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 else if (xp->xp_context == EXPAND_TAGS)
4398 {
4399 /*
4400 * Insert a backslash before characters in a tag name that
4401 * would terminate the ":tag" command.
4402 */
4403 for (i = 0; i < numfiles; ++i)
4404 {
4405 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4406 if (p != NULL)
4407 {
4408 vim_free(files[i]);
4409 files[i] = p;
4410 }
4411 }
4412 }
4413 }
4414}
4415
4416/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004417 * Escape special characters in "fname" for when used as a file name argument
4418 * after a Vim command, or, when "shell" is non-zero, a shell command.
4419 * Returns the result in allocated memory.
4420 */
4421 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004422vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004423{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004424 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004425#ifdef BACKSLASH_IN_FILENAME
4426 char_u buf[20];
4427 int j = 0;
4428
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004429 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004430 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004431 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004432 buf[j++] = *p;
4433 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004434 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004435#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004436 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4437 if (shell && csh_like_shell() && p != NULL)
4438 {
4439 char_u *s;
4440
4441 /* For csh and similar shells need to put two backslashes before '!'.
4442 * One is taken by Vim, one by the shell. */
4443 s = vim_strsave_escaped(p, (char_u *)"!");
4444 vim_free(p);
4445 p = s;
4446 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004447#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004448
4449 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4450 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004451 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004452 escape_fname(&p);
4453
4454 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004455}
4456
4457/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004458 * Put a backslash before the file name in "pp", which is in allocated memory.
4459 */
4460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004461escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004462{
4463 char_u *p;
4464
4465 p = alloc((unsigned)(STRLEN(*pp) + 2));
4466 if (p != NULL)
4467 {
4468 p[0] = '\\';
4469 STRCPY(p + 1, *pp);
4470 vim_free(*pp);
4471 *pp = p;
4472 }
4473}
4474
4475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 * For each file name in files[num_files]:
4477 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4478 */
4479 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004480tilde_replace(
4481 char_u *orig_pat,
4482 int num_files,
4483 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
4485 int i;
4486 char_u *p;
4487
4488 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4489 {
4490 for (i = 0; i < num_files; ++i)
4491 {
4492 p = home_replace_save(NULL, files[i]);
4493 if (p != NULL)
4494 {
4495 vim_free(files[i]);
4496 files[i] = p;
4497 }
4498 }
4499 }
4500}
4501
4502/*
4503 * Show all matches for completion on the command line.
4504 * Returns EXPAND_NOTHING when the character that triggered expansion should
4505 * be inserted like a normal character.
4506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004508showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4511 int num_files;
4512 char_u **files_found;
4513 int i, j, k;
4514 int maxlen;
4515 int lines;
4516 int columns;
4517 char_u *p;
4518 int lastlen;
4519 int attr;
4520 int showtail;
4521
4522 if (xp->xp_numfiles == -1)
4523 {
4524 set_expand_context(xp);
4525 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4526 &num_files, &files_found);
4527 showtail = expand_showtail(xp);
4528 if (i != EXPAND_OK)
4529 return i;
4530
4531 }
4532 else
4533 {
4534 num_files = xp->xp_numfiles;
4535 files_found = xp->xp_files;
4536 showtail = cmd_showtail;
4537 }
4538
4539#ifdef FEAT_WILDMENU
4540 if (!wildmenu)
4541 {
4542#endif
4543 msg_didany = FALSE; /* lines_left will be set */
4544 msg_start(); /* prepare for paging */
4545 msg_putchar('\n');
4546 out_flush();
4547 cmdline_row = msg_row;
4548 msg_didany = FALSE; /* lines_left will be set again */
4549 msg_start(); /* prepare for paging */
4550#ifdef FEAT_WILDMENU
4551 }
4552#endif
4553
4554 if (got_int)
4555 got_int = FALSE; /* only int. the completion, not the cmd line */
4556#ifdef FEAT_WILDMENU
4557 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004558 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559#endif
4560 else
4561 {
4562 /* find the length of the longest file name */
4563 maxlen = 0;
4564 for (i = 0; i < num_files; ++i)
4565 {
4566 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004567 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 || xp->xp_context == EXPAND_BUFFERS))
4569 {
4570 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4571 j = vim_strsize(NameBuff);
4572 }
4573 else
4574 j = vim_strsize(L_SHOWFILE(i));
4575 if (j > maxlen)
4576 maxlen = j;
4577 }
4578
4579 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4580 lines = num_files;
4581 else
4582 {
4583 /* compute the number of columns and lines for the listing */
4584 maxlen += 2; /* two spaces between file names */
4585 columns = ((int)Columns + 2) / maxlen;
4586 if (columns < 1)
4587 columns = 1;
4588 lines = (num_files + columns - 1) / columns;
4589 }
4590
Bram Moolenaar8820b482017-03-16 17:23:31 +01004591 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4594 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004595 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 msg_clr_eos();
4597 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004598 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600
4601 /* list the files line by line */
4602 for (i = 0; i < lines; ++i)
4603 {
4604 lastlen = 999;
4605 for (k = i; k < num_files; k += lines)
4606 {
4607 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4608 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004609 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 p = files_found[k] + STRLEN(files_found[k]) + 1;
4611 msg_advance(maxlen + 1);
4612 msg_puts(p);
4613 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004614 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 break;
4616 }
4617 for (j = maxlen - lastlen; --j >= 0; )
4618 msg_putchar(' ');
4619 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004620 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 || xp->xp_context == EXPAND_BUFFERS)
4622 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004623 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004624 if (xp->xp_numfiles != -1)
4625 {
4626 char_u *halved_slash;
4627 char_u *exp_path;
4628
4629 /* Expansion was done before and special characters
4630 * were escaped, need to halve backslashes. Also
4631 * $HOME has been replaced with ~/. */
4632 exp_path = expand_env_save_opt(files_found[k], TRUE);
4633 halved_slash = backslash_halve_save(
4634 exp_path != NULL ? exp_path : files_found[k]);
4635 j = mch_isdir(halved_slash != NULL ? halved_slash
4636 : files_found[k]);
4637 vim_free(exp_path);
4638 vim_free(halved_slash);
4639 }
4640 else
4641 /* Expansion was done here, file names are literal. */
4642 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (showtail)
4644 p = L_SHOWFILE(k);
4645 else
4646 {
4647 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4648 TRUE);
4649 p = NameBuff;
4650 }
4651 }
4652 else
4653 {
4654 j = FALSE;
4655 p = L_SHOWFILE(k);
4656 }
4657 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4658 }
4659 if (msg_col > 0) /* when not wrapped around */
4660 {
4661 msg_clr_eos();
4662 msg_putchar('\n');
4663 }
4664 out_flush(); /* show one line at a time */
4665 if (got_int)
4666 {
4667 got_int = FALSE;
4668 break;
4669 }
4670 }
4671
4672 /*
4673 * we redraw the command below the lines that we have just listed
4674 * This is a bit tricky, but it saves a lot of screen updating.
4675 */
4676 cmdline_row = msg_row; /* will put it back later */
4677 }
4678
4679 if (xp->xp_numfiles == -1)
4680 FreeWild(num_files, files_found);
4681
4682 return EXPAND_OK;
4683}
4684
4685/*
4686 * Private gettail for showmatches() (and win_redr_status_matches()):
4687 * Find tail of file name path, but ignore trailing "/".
4688 */
4689 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004690sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 char_u *p;
4693 char_u *t = s;
4694 int had_sep = FALSE;
4695
4696 for (p = s; *p != NUL; )
4697 {
4698 if (vim_ispathsep(*p)
4699#ifdef BACKSLASH_IN_FILENAME
4700 && !rem_backslash(p)
4701#endif
4702 )
4703 had_sep = TRUE;
4704 else if (had_sep)
4705 {
4706 t = p;
4707 had_sep = FALSE;
4708 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004709 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
4711 return t;
4712}
4713
4714/*
4715 * Return TRUE if we only need to show the tail of completion matches.
4716 * When not completing file names or there is a wildcard in the path FALSE is
4717 * returned.
4718 */
4719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004720expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
4722 char_u *s;
4723 char_u *end;
4724
4725 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004726 if (xp->xp_context != EXPAND_FILES
4727 && xp->xp_context != EXPAND_SHELLCMD
4728 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FALSE;
4730
4731 end = gettail(xp->xp_pattern);
4732 if (end == xp->xp_pattern) /* there is no path separator */
4733 return FALSE;
4734
4735 for (s = xp->xp_pattern; s < end; s++)
4736 {
4737 /* Skip escaped wildcards. Only when the backslash is not a path
4738 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4739 if (rem_backslash(s))
4740 ++s;
4741 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4742 return FALSE;
4743 }
4744 return TRUE;
4745}
4746
4747/*
4748 * Prepare a string for expansion.
4749 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004750 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 * When expanding other names: The string will be used with regcomp(). Copy
4752 * the name into allocated memory and prepend "^".
4753 */
4754 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004755addstar(
4756 char_u *fname,
4757 int len,
4758 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 char_u *retval;
4761 int i, j;
4762 int new_len;
4763 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004764 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004766 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004767 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004768 && context != EXPAND_SHELLCMD
4769 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 /*
4772 * Matching will be done internally (on something other than files).
4773 * So we convert the file-matching-type wildcards into our kind for
4774 * use with vim_regcomp(). First work out how long it will be:
4775 */
4776
4777 /* For help tags the translation is done in find_help_tags().
4778 * For a tag pattern starting with "/" no translation is needed. */
4779 if (context == EXPAND_HELP
4780 || context == EXPAND_COLORS
4781 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004782 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004783 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004784 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004785 || ((context == EXPAND_TAGS_LISTFILES
4786 || context == EXPAND_TAGS)
4787 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 retval = vim_strnsave(fname, len);
4789 else
4790 {
4791 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4792 for (i = 0; i < len; i++)
4793 {
4794 if (fname[i] == '*' || fname[i] == '~')
4795 new_len++; /* '*' needs to be replaced by ".*"
4796 '~' needs to be replaced by "\~" */
4797
4798 /* Buffer names are like file names. "." should be literal */
4799 if (context == EXPAND_BUFFERS && fname[i] == '.')
4800 new_len++; /* "." becomes "\." */
4801
4802 /* Custom expansion takes care of special things, match
4803 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004804 if ((context == EXPAND_USER_DEFINED
4805 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 new_len++; /* '\' becomes "\\" */
4807 }
4808 retval = alloc(new_len);
4809 if (retval != NULL)
4810 {
4811 retval[0] = '^';
4812 j = 1;
4813 for (i = 0; i < len; i++, j++)
4814 {
4815 /* Skip backslash. But why? At least keep it for custom
4816 * expansion. */
4817 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004818 && context != EXPAND_USER_LIST
4819 && fname[i] == '\\'
4820 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 break;
4822
4823 switch (fname[i])
4824 {
4825 case '*': retval[j++] = '.';
4826 break;
4827 case '~': retval[j++] = '\\';
4828 break;
4829 case '?': retval[j] = '.';
4830 continue;
4831 case '.': if (context == EXPAND_BUFFERS)
4832 retval[j++] = '\\';
4833 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004834 case '\\': if (context == EXPAND_USER_DEFINED
4835 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 retval[j++] = '\\';
4837 break;
4838 }
4839 retval[j] = fname[i];
4840 }
4841 retval[j] = NUL;
4842 }
4843 }
4844 }
4845 else
4846 {
4847 retval = alloc(len + 4);
4848 if (retval != NULL)
4849 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004850 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
4852 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004853 * Don't add a star to *, ~, ~user, $var or `cmd`.
4854 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 * ~ would be at the start of the file name, but not the tail.
4856 * $ could be anywhere in the tail.
4857 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004858 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 */
4860 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004861 ends_in_star = (len > 0 && retval[len - 1] == '*');
4862#ifndef BACKSLASH_IN_FILENAME
4863 for (i = len - 2; i >= 0; --i)
4864 {
4865 if (retval[i] != '\\')
4866 break;
4867 ends_in_star = !ends_in_star;
4868 }
4869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004871 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 && vim_strchr(tail, '$') == NULL
4873 && vim_strchr(retval, '`') == NULL)
4874 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004875 else if (len > 0 && retval[len - 1] == '$')
4876 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 retval[len] = NUL;
4878 }
4879 }
4880 return retval;
4881}
4882
4883/*
4884 * Must parse the command line so far to work out what context we are in.
4885 * Completion can then be done based on that context.
4886 * This routine sets the variables:
4887 * xp->xp_pattern The start of the pattern to be expanded within
4888 * the command line (ends at the cursor).
4889 * xp->xp_context The type of thing to expand. Will be one of:
4890 *
4891 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4892 * the command line, like an unknown command. Caller
4893 * should beep.
4894 * EXPAND_NOTHING Unrecognised context for completion, use char like
4895 * a normal char, rather than for completion. eg
4896 * :s/^I/
4897 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4898 * it.
4899 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4900 * EXPAND_FILES After command with XFILE set, or after setting
4901 * with P_EXPAND set. eg :e ^I, :w>>^I
4902 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4903 * when we know only directories are of interest. eg
4904 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004905 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4907 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4908 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4909 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4910 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4911 * EXPAND_EVENTS Complete event names
4912 * EXPAND_SYNTAX Complete :syntax command arguments
4913 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4914 * EXPAND_AUGROUP Complete autocommand group names
4915 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4916 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4917 * eg :unmap a^I , :cunab x^I
4918 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4919 * eg :call sub^I
4920 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4921 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4922 * names in expressions, eg :while s^I
4923 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004924 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
4926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004927set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004929 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (ccline.cmdfirstc != ':'
4931#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004932 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004933 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#endif
4935 )
4936 {
4937 xp->xp_context = EXPAND_NOTHING;
4938 return;
4939 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004940 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941}
4942
4943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004944set_cmd_context(
4945 expand_T *xp,
4946 char_u *str, /* start of command line */
4947 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004948 int col, /* position of cursor */
4949 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 int old_char = NUL;
4952 char_u *nextcomm;
4953
4954 /*
4955 * Avoid a UMR warning from Purify, only save the character if it has been
4956 * written before.
4957 */
4958 if (col < len)
4959 old_char = str[col];
4960 str[col] = NUL;
4961 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004962
4963#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004964 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004965 {
4966# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004967 /* pass CMD_SIZE because there is no real command */
4968 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004969# endif
4970 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004971 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004972 {
4973 xp->xp_context = ccline.xp_context;
4974 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004975# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004976 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004977# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004978 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004979 else
4980#endif
4981 while (nextcomm != NULL)
4982 nextcomm = set_one_cmd_context(xp, nextcomm);
4983
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004984 /* Store the string here so that call_user_expand_func() can get to them
4985 * easily. */
4986 xp->xp_line = str;
4987 xp->xp_col = col;
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 str[col] = old_char;
4990}
4991
4992/*
4993 * Expand the command line "str" from context "xp".
4994 * "xp" must have been set by set_cmd_context().
4995 * xp->xp_pattern points into "str", to where the text that is to be expanded
4996 * starts.
4997 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4998 * cursor.
4999 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
5000 * key that triggered expansion literally.
5001 * Returns EXPAND_OK otherwise.
5002 */
5003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005004expand_cmdline(
5005 expand_T *xp,
5006 char_u *str, /* start of command line */
5007 int col, /* position of cursor */
5008 int *matchcount, /* return: nr of matches */
5009 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
5011 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005012 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5015 {
5016 beep_flush();
5017 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5018 }
5019 if (xp->xp_context == EXPAND_NOTHING)
5020 {
5021 /* Caller can use the character as a normal char instead */
5022 return EXPAND_NOTHING;
5023 }
5024
5025 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005026 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5027 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 if (file_str == NULL)
5029 return EXPAND_UNSUCCESSFUL;
5030
Bram Moolenaar94950a92010-12-02 16:01:29 +01005031 if (p_wic)
5032 options += WILD_ICASE;
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005035 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 *matchcount = 0;
5038 *matches = NULL;
5039 }
5040 vim_free(file_str);
5041
5042 return EXPAND_OK;
5043}
5044
5045#ifdef FEAT_MULTI_LANG
5046/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005047 * Cleanup matches for help tags:
5048 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5049 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005052cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
5054 int i, j;
5055 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005056 char_u buf[4];
5057 char_u *p = buf;
5058
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005059 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005060 {
5061 *p++ = '@';
5062 *p++ = p_hlg[0];
5063 *p++ = p_hlg[1];
5064 }
5065 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 for (i = 0; i < num_file; ++i)
5068 {
5069 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005070 if (len <= 0)
5071 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005072 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
5074 /* Sorting on priority means the same item in another language may
5075 * be anywhere. Search all items for a match up to the "@en". */
5076 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005077 if (j != i && (int)STRLEN(file[j]) == len + 3
5078 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005081 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 file[i][len] = NUL;
5083 }
5084 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005085
5086 if (*buf != NUL)
5087 for (i = 0; i < num_file; ++i)
5088 {
5089 len = (int)STRLEN(file[i]) - 3;
5090 if (len <= 0)
5091 continue;
5092 if (STRCMP(file[i] + len, buf) == 0)
5093 {
5094 /* remove the default language */
5095 file[i][len] = NUL;
5096 }
5097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098}
5099#endif
5100
5101/*
5102 * Do the expansion based on xp->xp_context and "pat".
5103 */
5104 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005105ExpandFromContext(
5106 expand_T *xp,
5107 char_u *pat,
5108 int *num_file,
5109 char_u ***file,
5110 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111{
5112#ifdef FEAT_CMDL_COMPL
5113 regmatch_T regmatch;
5114#endif
5115 int ret;
5116 int flags;
5117
5118 flags = EW_DIR; /* include directories */
5119 if (options & WILD_LIST_NOTFOUND)
5120 flags |= EW_NOTFOUND;
5121 if (options & WILD_ADD_SLASH)
5122 flags |= EW_ADDSLASH;
5123 if (options & WILD_KEEP_ALL)
5124 flags |= EW_KEEPALL;
5125 if (options & WILD_SILENT)
5126 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005127 if (options & WILD_ALLLINKS)
5128 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005130 if (xp->xp_context == EXPAND_FILES
5131 || xp->xp_context == EXPAND_DIRECTORIES
5132 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 /*
5135 * Expand file or directory names.
5136 */
5137 int free_pat = FALSE;
5138 int i;
5139
5140 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5141 * space */
5142 if (xp->xp_backslash != XP_BS_NONE)
5143 {
5144 free_pat = TRUE;
5145 pat = vim_strsave(pat);
5146 for (i = 0; pat[i]; ++i)
5147 if (pat[i] == '\\')
5148 {
5149 if (xp->xp_backslash == XP_BS_THREE
5150 && pat[i + 1] == '\\'
5151 && pat[i + 2] == '\\'
5152 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005153 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 if (xp->xp_backslash == XP_BS_ONE
5155 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005156 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 }
5159
5160 if (xp->xp_context == EXPAND_FILES)
5161 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005162 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5163 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 else
5165 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005166 if (options & WILD_ICASE)
5167 flags |= EW_ICASE;
5168
Bram Moolenaard7834d32009-12-02 16:14:36 +00005169 /* Expand wildcards, supporting %:h and the like. */
5170 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (free_pat)
5172 vim_free(pat);
5173 return ret;
5174 }
5175
5176 *file = (char_u **)"";
5177 *num_file = 0;
5178 if (xp->xp_context == EXPAND_HELP)
5179 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005180 /* With an empty argument we would get all the help tags, which is
5181 * very slow. Get matches for "help" instead. */
5182 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5183 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
5185#ifdef FEAT_MULTI_LANG
5186 cleanup_help_tags(*num_file, *file);
5187#endif
5188 return OK;
5189 }
5190 return FAIL;
5191 }
5192
5193#ifndef FEAT_CMDL_COMPL
5194 return FAIL;
5195#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005196 if (xp->xp_context == EXPAND_SHELLCMD)
5197 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (xp->xp_context == EXPAND_OLD_SETTING)
5199 return ExpandOldSetting(num_file, file);
5200 if (xp->xp_context == EXPAND_BUFFERS)
5201 return ExpandBufnames(pat, num_file, file, options);
5202 if (xp->xp_context == EXPAND_TAGS
5203 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5204 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5205 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005206 {
5207 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005208 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5209 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005212 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005213 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005214 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005215 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005216 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005217 {
5218 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005219 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005220 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005221 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005222 {
5223 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005224 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005225 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005226# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5227 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005228 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005229# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005230 if (xp->xp_context == EXPAND_PACKADD)
5231 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
5233 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5234 if (regmatch.regprog == NULL)
5235 return FAIL;
5236
5237 /* set ignore-case according to p_ic, p_scs and pat */
5238 regmatch.rm_ic = ignorecase(pat);
5239
5240 if (xp->xp_context == EXPAND_SETTINGS
5241 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5242 ret = ExpandSettings(xp, &regmatch, num_file, file);
5243 else if (xp->xp_context == EXPAND_MAPPINGS)
5244 ret = ExpandMappings(&regmatch, num_file, file);
5245# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5246 else if (xp->xp_context == EXPAND_USER_DEFINED)
5247 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5248# endif
5249 else
5250 {
5251 static struct expgen
5252 {
5253 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005254 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005256 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 } tab[] =
5258 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005259 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5260 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005261 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005262 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005263#ifdef FEAT_CMDHIST
5264 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005267 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005268 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005269 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5270 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5271 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272#endif
5273#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005274 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5275 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5276 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5277 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278#endif
5279#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005280 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5281 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#endif
5283#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005284 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005286#ifdef FEAT_PROFILE
5287 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5288#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005289 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005290 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5291 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005292#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005293 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005294#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005295#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005296 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005297#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005298#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005299 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5302 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005303 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5304 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005306 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005307 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005308 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 };
5310 int i;
5311
5312 /*
5313 * Find a context in the table and call the ExpandGeneric() with the
5314 * right function to do the expansion.
5315 */
5316 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005317 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (xp->xp_context == tab[i].context)
5319 {
5320 if (tab[i].ic)
5321 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005322 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005323 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 break;
5325 }
5326 }
5327
Bram Moolenaar473de612013-06-08 18:19:48 +02005328 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 return ret;
5331#endif /* FEAT_CMDL_COMPL */
5332}
5333
5334#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5335/*
5336 * Expand a list of names.
5337 *
5338 * Generic function for command line completion. It calls a function to
5339 * obtain strings, one by one. The strings are matched against a regexp
5340 * program. Matching strings are copied into an array, which is returned.
5341 *
5342 * Returns OK when no problems encountered, FAIL for error (out of memory).
5343 */
5344 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ExpandGeneric(
5346 expand_T *xp,
5347 regmatch_T *regmatch,
5348 int *num_file,
5349 char_u ***file,
5350 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 int i;
5355 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005356 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 char_u *str;
5358
5359 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005360 * round == 0: count the number of matching names
5361 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005363 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
5365 for (i = 0; ; ++i)
5366 {
5367 str = (*func)(xp, i);
5368 if (str == NULL) /* end of list */
5369 break;
5370 if (*str == NUL) /* skip empty strings */
5371 continue;
5372
5373 if (vim_regexec(regmatch, str, (colnr_T)0))
5374 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005375 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005377 if (escaped)
5378 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5379 else
5380 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 (*file)[count] = str;
5382#ifdef FEAT_MENU
5383 if (func == get_menu_names && str != NULL)
5384 {
5385 /* test for separator added by get_menu_names() */
5386 str += STRLEN(str) - 1;
5387 if (*str == '\001')
5388 *str = '.';
5389 }
5390#endif
5391 }
5392 ++count;
5393 }
5394 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005395 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
5397 if (count == 0)
5398 return OK;
5399 *num_file = count;
5400 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5401 if (*file == NULL)
5402 {
5403 *file = (char_u **)"";
5404 return FAIL;
5405 }
5406 count = 0;
5407 }
5408 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005409
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005410 /* Sort the results. Keep menu's in the specified order. */
5411 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005412 {
5413 if (xp->xp_context == EXPAND_EXPRESSION
5414 || xp->xp_context == EXPAND_FUNCTIONS
5415 || xp->xp_context == EXPAND_USER_FUNC)
5416 /* <SNR> functions should be sorted to the end. */
5417 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5418 sort_func_compare);
5419 else
5420 sort_strings(*file, *num_file);
5421 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005422
Bram Moolenaar4f688582007-07-24 12:34:30 +00005423#ifdef FEAT_CMDL_COMPL
5424 /* Reset the variables used for special highlight names expansion, so that
5425 * they don't show up when getting normal highlight names by ID. */
5426 reset_expand_highlight();
5427#endif
5428
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 return OK;
5430}
5431
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005432/*
5433 * Complete a shell command.
5434 * Returns FAIL or OK;
5435 */
5436 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005437expand_shellcmd(
5438 char_u *filepat, /* pattern to match with command names */
5439 int *num_file, /* return: number of matches */
5440 char_u ***file, /* return: array with matches */
5441 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005442{
5443 char_u *pat;
5444 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005445 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005446 int mustfree = FALSE;
5447 garray_T ga;
5448 char_u *buf = alloc(MAXPATHL);
5449 size_t l;
5450 char_u *s, *e;
5451 int flags = flagsarg;
5452 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005453 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005454 hashtab_T found_ht;
5455 hashitem_T *hi;
5456 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005457
5458 if (buf == NULL)
5459 return FAIL;
5460
5461 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5462 * space */
5463 pat = vim_strsave(filepat);
5464 for (i = 0; pat[i]; ++i)
5465 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005466 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005467
Bram Moolenaarb5971142015-03-21 17:32:19 +01005468 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005469
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005470 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5471 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005472 path = (char_u *)".";
5473 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005474 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005475 /* For an absolute name we don't use $PATH. */
5476 if (!mch_isFullName(pat))
5477 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005478 if (path == NULL)
5479 path = (char_u *)"";
5480 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005481
5482 /*
5483 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005484 * collect them in "ga". When "." is not in $PATH also expand for the
5485 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005486 */
5487 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005488 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005489 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005490 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005491#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005492 e = vim_strchr(s, ';');
5493#else
5494 e = vim_strchr(s, ':');
5495#endif
5496 if (e == NULL)
5497 e = s + STRLEN(s);
5498
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005499 if (*s == NUL)
5500 {
5501 if (did_curdir)
5502 break;
5503 // Find directories in the current directory, path is empty.
5504 did_curdir = TRUE;
5505 flags |= EW_DIR;
5506 }
5507 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5508 {
5509 did_curdir = TRUE;
5510 flags |= EW_DIR;
5511 }
5512 else
5513 // Do not match directories inside a $PATH item.
5514 flags &= ~EW_DIR;
5515
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005516 l = e - s;
5517 if (l > MAXPATHL - 5)
5518 break;
5519 vim_strncpy(buf, s, l);
5520 add_pathsep(buf);
5521 l = STRLEN(buf);
5522 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5523
5524 /* Expand matches in one directory of $PATH. */
5525 ret = expand_wildcards(1, &buf, num_file, file, flags);
5526 if (ret == OK)
5527 {
5528 if (ga_grow(&ga, *num_file) == FAIL)
5529 FreeWild(*num_file, *file);
5530 else
5531 {
5532 for (i = 0; i < *num_file; ++i)
5533 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005534 char_u *name = (*file)[i];
5535
5536 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005537 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005538 // Check if this name was already found.
5539 hash = hash_hash(name + l);
5540 hi = hash_lookup(&found_ht, name + l, hash);
5541 if (HASHITEM_EMPTY(hi))
5542 {
5543 // Remove the path that was prepended.
5544 STRMOVE(name, name + l);
5545 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5546 hash_add_item(&found_ht, hi, name, hash);
5547 name = NULL;
5548 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005549 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005550 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005551 }
5552 vim_free(*file);
5553 }
5554 }
5555 if (*e != NUL)
5556 ++e;
5557 }
5558 *file = ga.ga_data;
5559 *num_file = ga.ga_len;
5560
5561 vim_free(buf);
5562 vim_free(pat);
5563 if (mustfree)
5564 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005565 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005566 return OK;
5567}
5568
5569
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5571/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005572 * Call "user_expand_func()" to invoke a user defined Vim script function and
5573 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005575 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005577 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578 expand_T *xp,
5579 int *num_file,
5580 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005582 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005583 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005584 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005585 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005586 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
Bram Moolenaarb7515462013-06-29 12:58:33 +02005588 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005589 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 *num_file = 0;
5591 *file = NULL;
5592
Bram Moolenaarb7515462013-06-29 12:58:33 +02005593 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005594 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005595 keep = ccline.cmdbuff[ccline.cmdlen];
5596 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005597 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005598
Bram Moolenaarffa96842018-06-12 22:05:14 +02005599 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5600
5601 args[0].v_type = VAR_STRING;
5602 args[0].vval.v_string = pat;
5603 args[1].v_type = VAR_STRING;
5604 args[1].vval.v_string = xp->xp_line;
5605 args[2].v_type = VAR_NUMBER;
5606 args[2].vval.v_number = xp->xp_col;
5607 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005609 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005610
Bram Moolenaarded27a12018-08-01 19:06:03 +02005611 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005612
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005613 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005614 if (ccline.cmdbuff != NULL)
5615 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005616
Bram Moolenaarffa96842018-06-12 22:05:14 +02005617 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005618 return ret;
5619}
5620
5621/*
5622 * Expand names with a function defined by the user.
5623 */
5624 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ExpandUserDefined(
5626 expand_T *xp,
5627 regmatch_T *regmatch,
5628 int *num_file,
5629 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005630{
5631 char_u *retstr;
5632 char_u *s;
5633 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005634 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005635 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005636 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005637
5638 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5639 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 return FAIL;
5641
5642 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005643 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
5645 e = vim_strchr(s, '\n');
5646 if (e == NULL)
5647 e = s + STRLEN(s);
5648 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005649 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005651 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5652 *e = keep;
5653
5654 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005656 if (ga_grow(&ga, 1) == FAIL)
5657 break;
5658 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5659 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (*e != NUL)
5663 ++e;
5664 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005665 vim_free(retstr);
5666 *file = ga.ga_data;
5667 *num_file = ga.ga_len;
5668 return OK;
5669}
5670
5671/*
5672 * Expand names with a list returned by a function defined by the user.
5673 */
5674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675ExpandUserList(
5676 expand_T *xp,
5677 int *num_file,
5678 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005679{
5680 list_T *retlist;
5681 listitem_T *li;
5682 garray_T ga;
5683
5684 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5685 if (retlist == NULL)
5686 return FAIL;
5687
5688 ga_init2(&ga, (int)sizeof(char *), 3);
5689 /* Loop over the items in the list. */
5690 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5691 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005692 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5693 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005694
5695 if (ga_grow(&ga, 1) == FAIL)
5696 break;
5697
5698 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005699 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005700 ++ga.ga_len;
5701 }
5702 list_unref(retlist);
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 *file = ga.ga_data;
5705 *num_file = ga.ga_len;
5706 return OK;
5707}
5708#endif
5709
5710/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005711 * Expand color scheme, compiler or filetype names.
5712 * Search from 'runtimepath':
5713 * 'runtimepath'/{dirnames}/{pat}.vim
5714 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5715 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5716 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5717 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005718 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 */
5720 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005721ExpandRTDir(
5722 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005723 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005724 int *num_file,
5725 char_u ***file,
5726 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 char_u *s;
5729 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005730 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005732 int i;
5733 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 *num_file = 0;
5736 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005737 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005738 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005740 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005742 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5743 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005745 ga_clear_strings(&ga);
5746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005748 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005749 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005750 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005752
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005753 if (flags & DIP_START) {
5754 for (i = 0; dirnames[i] != NULL; ++i)
5755 {
5756 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5757 if (s == NULL)
5758 {
5759 ga_clear_strings(&ga);
5760 return FAIL;
5761 }
5762 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5763 globpath(p_pp, s, &ga, 0);
5764 vim_free(s);
5765 }
5766 }
5767
5768 if (flags & DIP_OPT) {
5769 for (i = 0; dirnames[i] != NULL; ++i)
5770 {
5771 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5772 if (s == NULL)
5773 {
5774 ga_clear_strings(&ga);
5775 return FAIL;
5776 }
5777 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5778 globpath(p_pp, s, &ga, 0);
5779 vim_free(s);
5780 }
5781 }
5782
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005783 for (i = 0; i < ga.ga_len; ++i)
5784 {
5785 match = ((char_u **)ga.ga_data)[i];
5786 s = match;
5787 e = s + STRLEN(s);
5788 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5789 {
5790 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005791 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005792 if (s < match || vim_ispathsep(*s))
5793 break;
5794 ++s;
5795 *e = NUL;
5796 mch_memmove(match, s, e - s + 1);
5797 }
5798 }
5799
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005800 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005801 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005802
5803 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005804 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005805 remove_duplicates(&ga);
5806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 *file = ga.ga_data;
5808 *num_file = ga.ga_len;
5809 return OK;
5810}
5811
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005812/*
5813 * Expand loadplugin names:
5814 * 'packpath'/pack/ * /opt/{pat}
5815 */
5816 static int
5817ExpandPackAddDir(
5818 char_u *pat,
5819 int *num_file,
5820 char_u ***file)
5821{
5822 char_u *s;
5823 char_u *e;
5824 char_u *match;
5825 garray_T ga;
5826 int i;
5827 int pat_len;
5828
5829 *num_file = 0;
5830 *file = NULL;
5831 pat_len = (int)STRLEN(pat);
5832 ga_init2(&ga, (int)sizeof(char *), 10);
5833
5834 s = alloc((unsigned)(pat_len + 26));
5835 if (s == NULL)
5836 {
5837 ga_clear_strings(&ga);
5838 return FAIL;
5839 }
5840 sprintf((char *)s, "pack/*/opt/%s*", pat);
5841 globpath(p_pp, s, &ga, 0);
5842 vim_free(s);
5843
5844 for (i = 0; i < ga.ga_len; ++i)
5845 {
5846 match = ((char_u **)ga.ga_data)[i];
5847 s = gettail(match);
5848 e = s + STRLEN(s);
5849 mch_memmove(match, s, e - s + 1);
5850 }
5851
5852 if (ga.ga_len == 0)
5853 return FAIL;
5854
5855 /* Sort and remove duplicates which can happen when specifying multiple
5856 * directories in dirnames. */
5857 remove_duplicates(&ga);
5858
5859 *file = ga.ga_data;
5860 *num_file = ga.ga_len;
5861 return OK;
5862}
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#endif
5865
5866#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5867/*
5868 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005869 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005871 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005872globpath(
5873 char_u *path,
5874 char_u *file,
5875 garray_T *ga,
5876 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 expand_T xpc;
5879 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 int num_p;
5882 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
5884 buf = alloc(MAXPATHL);
5885 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005886 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005888 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 /* Loop over all entries in {path}. */
5892 while (*path != NUL)
5893 {
5894 /* Copy one item of the path to buf[] and concatenate the file name. */
5895 copy_option_part(&path, buf, MAXPATHL, ",");
5896 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5897 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005898# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005899 /* Using the platform's path separator (\) makes vim incorrectly
5900 * treat it as an escape character, use '/' instead. */
5901 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5902 STRCAT(buf, "/");
5903# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005907 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5908 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005910 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005912 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 for (i = 0; i < num_p; ++i)
5915 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005916 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005917 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005918 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 FreeWild(num_p, p);
5923 }
5924 }
5925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928}
5929
5930#endif
5931
5932#if defined(FEAT_CMDHIST) || defined(PROTO)
5933
5934/*********************************
5935 * Command line history stuff *
5936 *********************************/
5937
5938/*
5939 * Translate a history character to the associated type number.
5940 */
5941 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005942hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 if (c == ':')
5945 return HIST_CMD;
5946 if (c == '=')
5947 return HIST_EXPR;
5948 if (c == '@')
5949 return HIST_INPUT;
5950 if (c == '>')
5951 return HIST_DEBUG;
5952 return HIST_SEARCH; /* must be '?' or '/' */
5953}
5954
5955/*
5956 * Table of history names.
5957 * These names are used in :history and various hist...() functions.
5958 * It is sufficient to give the significant prefix of a history name.
5959 */
5960
5961static char *(history_names[]) =
5962{
5963 "cmd",
5964 "search",
5965 "expr",
5966 "input",
5967 "debug",
5968 NULL
5969};
5970
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005971#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5972/*
5973 * Function given to ExpandGeneric() to obtain the possible first
5974 * arguments of the ":history command.
5975 */
5976 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005977get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005978{
5979 static char_u compl[2] = { NUL, NUL };
5980 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005981 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005982 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5983
5984 if (idx < short_names_count)
5985 {
5986 compl[0] = (char_u)short_names[idx];
5987 return compl;
5988 }
5989 if (idx < short_names_count + history_name_count)
5990 return (char_u *)history_names[idx - short_names_count];
5991 if (idx == short_names_count + history_name_count)
5992 return (char_u *)"all";
5993 return NULL;
5994}
5995#endif
5996
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997/*
5998 * init_history() - Initialize the command line history.
5999 * Also used to re-allocate the history when the size changes.
6000 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006001 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006002init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 int newlen; /* new length of history table */
6005 histentry_T *temp;
6006 int i;
6007 int j;
6008 int type;
6009
6010 /*
6011 * If size of history table changed, reallocate it
6012 */
6013 newlen = (int)p_hi;
6014 if (newlen != hislen) /* history length changed */
6015 {
6016 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6017 {
6018 if (newlen)
6019 {
6020 temp = (histentry_T *)lalloc(
6021 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6022 if (temp == NULL) /* out of memory! */
6023 {
6024 if (type == 0) /* first one: just keep the old length */
6025 {
6026 newlen = hislen;
6027 break;
6028 }
6029 /* Already changed one table, now we can only have zero
6030 * length for all tables. */
6031 newlen = 0;
6032 type = -1;
6033 continue;
6034 }
6035 }
6036 else
6037 temp = NULL;
6038 if (newlen == 0 || temp != NULL)
6039 {
6040 if (hisidx[type] < 0) /* there are no entries yet */
6041 {
6042 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006043 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045 else if (newlen > hislen) /* array becomes bigger */
6046 {
6047 for (i = 0; i <= hisidx[type]; ++i)
6048 temp[i] = history[type][i];
6049 j = i;
6050 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006051 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 for ( ; j < hislen; ++i, ++j)
6053 temp[i] = history[type][j];
6054 }
6055 else /* array becomes smaller or 0 */
6056 {
6057 j = hisidx[type];
6058 for (i = newlen - 1; ; --i)
6059 {
6060 if (i >= 0) /* copy newest entries */
6061 temp[i] = history[type][j];
6062 else /* remove older entries */
6063 vim_free(history[type][j].hisstr);
6064 if (--j < 0)
6065 j = hislen - 1;
6066 if (j == hisidx[type])
6067 break;
6068 }
6069 hisidx[type] = newlen - 1;
6070 }
6071 vim_free(history[type]);
6072 history[type] = temp;
6073 }
6074 }
6075 hislen = newlen;
6076 }
6077}
6078
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006080clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006081{
6082 hisptr->hisnum = 0;
6083 hisptr->viminfo = FALSE;
6084 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006085 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006086}
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088/*
6089 * Check if command line 'str' is already in history.
6090 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6091 */
6092 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006093in_history(
6094 int type,
6095 char_u *str,
6096 int move_to_front, /* Move the entry to the front if it exists */
6097 int sep,
6098 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 int i;
6101 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006102 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104 if (hisidx[type] < 0)
6105 return FALSE;
6106 i = hisidx[type];
6107 do
6108 {
6109 if (history[type][i].hisstr == NULL)
6110 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006111
6112 /* For search history, check that the separator character matches as
6113 * well. */
6114 p = history[type][i].hisstr;
6115 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006116 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006117 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
6119 if (!move_to_front)
6120 return TRUE;
6121 last_i = i;
6122 break;
6123 }
6124 if (--i < 0)
6125 i = hislen - 1;
6126 } while (i != hisidx[type]);
6127
6128 if (last_i >= 0)
6129 {
6130 str = history[type][i].hisstr;
6131 while (i != hisidx[type])
6132 {
6133 if (++i >= hislen)
6134 i = 0;
6135 history[type][last_i] = history[type][i];
6136 last_i = i;
6137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006139 history[type][i].viminfo = FALSE;
6140 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006141 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 return TRUE;
6143 }
6144 return FALSE;
6145}
6146
6147/*
6148 * Convert history name (from table above) to its HIST_ equivalent.
6149 * When "name" is empty, return "cmd" history.
6150 * Returns -1 for unknown history name.
6151 */
6152 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006153get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 int i;
6156 int len = (int)STRLEN(name);
6157
6158 /* No argument: use current history. */
6159 if (len == 0)
6160 return hist_char2type(ccline.cmdfirstc);
6161
6162 for (i = 0; history_names[i] != NULL; ++i)
6163 if (STRNICMP(name, history_names[i], len) == 0)
6164 return i;
6165
6166 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6167 return hist_char2type(name[0]);
6168
6169 return -1;
6170}
6171
6172static int last_maptick = -1; /* last seen maptick */
6173
6174/*
6175 * Add the given string to the given history. If the string is already in the
6176 * history then it is moved to the front. "histype" may be one of he HIST_
6177 * values.
6178 */
6179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180add_to_history(
6181 int histype,
6182 char_u *new_entry,
6183 int in_map, /* consider maptick when inside a mapping */
6184 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
6186 histentry_T *hisptr;
6187 int len;
6188
6189 if (hislen == 0) /* no history */
6190 return;
6191
Bram Moolenaara939e432013-11-09 05:30:26 +01006192 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6193 return;
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 /*
6196 * Searches inside the same mapping overwrite each other, so that only
6197 * the last line is kept. Be careful not to remove a line that was moved
6198 * down, only lines that were added.
6199 */
6200 if (histype == HIST_SEARCH && in_map)
6201 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006202 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
6204 /* Current line is from the same mapping, remove it */
6205 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6206 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006207 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 --hisnum[histype];
6209 if (--hisidx[HIST_SEARCH] < 0)
6210 hisidx[HIST_SEARCH] = hislen - 1;
6211 }
6212 last_maptick = -1;
6213 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006214 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
6216 if (++hisidx[histype] == hislen)
6217 hisidx[histype] = 0;
6218 hisptr = &history[histype][hisidx[histype]];
6219 vim_free(hisptr->hisstr);
6220
6221 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006222 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6224 if (hisptr->hisstr != NULL)
6225 hisptr->hisstr[len + 1] = sep;
6226
6227 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006228 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006229 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 if (histype == HIST_SEARCH && in_map)
6231 last_maptick = maptick;
6232 }
6233}
6234
6235#if defined(FEAT_EVAL) || defined(PROTO)
6236
6237/*
6238 * Get identifier of newest history entry.
6239 * "histype" may be one of the HIST_ values.
6240 */
6241 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006242get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6245 || hisidx[histype] < 0)
6246 return -1;
6247
6248 return history[histype][hisidx[histype]].hisnum;
6249}
6250
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 * Calculate history index from a number:
6253 * num > 0: seen as identifying number of a history entry
6254 * num < 0: relative position in history wrt newest entry
6255 * "histype" may be one of the HIST_ values.
6256 */
6257 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006258calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 int i;
6261 histentry_T *hist;
6262 int wrapped = FALSE;
6263
6264 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6265 || (i = hisidx[histype]) < 0 || num == 0)
6266 return -1;
6267
6268 hist = history[histype];
6269 if (num > 0)
6270 {
6271 while (hist[i].hisnum > num)
6272 if (--i < 0)
6273 {
6274 if (wrapped)
6275 break;
6276 i += hislen;
6277 wrapped = TRUE;
6278 }
6279 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6280 return i;
6281 }
6282 else if (-num <= hislen)
6283 {
6284 i += num + 1;
6285 if (i < 0)
6286 i += hislen;
6287 if (hist[i].hisstr != NULL)
6288 return i;
6289 }
6290 return -1;
6291}
6292
6293/*
6294 * Get a history entry by its index.
6295 * "histype" may be one of the HIST_ values.
6296 */
6297 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006298get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 idx = calc_hist_idx(histype, idx);
6301 if (idx >= 0)
6302 return history[histype][idx].hisstr;
6303 else
6304 return (char_u *)"";
6305}
6306
6307/*
6308 * Clear all entries of a history.
6309 * "histype" may be one of the HIST_ values.
6310 */
6311 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006312clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 int i;
6315 histentry_T *hisptr;
6316
6317 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6318 {
6319 hisptr = history[histype];
6320 for (i = hislen; i--;)
6321 {
6322 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006323 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006324 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 }
6326 hisidx[histype] = -1; /* mark history as cleared */
6327 hisnum[histype] = 0; /* reset identifier counter */
6328 return OK;
6329 }
6330 return FAIL;
6331}
6332
6333/*
6334 * Remove all entries matching {str} from a history.
6335 * "histype" may be one of the HIST_ values.
6336 */
6337 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006338del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 regmatch_T regmatch;
6341 histentry_T *hisptr;
6342 int idx;
6343 int i;
6344 int last;
6345 int found = FALSE;
6346
6347 regmatch.regprog = NULL;
6348 regmatch.rm_ic = FALSE; /* always match case */
6349 if (hislen != 0
6350 && histype >= 0
6351 && histype < HIST_COUNT
6352 && *str != NUL
6353 && (idx = hisidx[histype]) >= 0
6354 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6355 != NULL)
6356 {
6357 i = last = idx;
6358 do
6359 {
6360 hisptr = &history[histype][i];
6361 if (hisptr->hisstr == NULL)
6362 break;
6363 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6364 {
6365 found = TRUE;
6366 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006367 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369 else
6370 {
6371 if (i != last)
6372 {
6373 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006374 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376 if (--last < 0)
6377 last += hislen;
6378 }
6379 if (--i < 0)
6380 i += hislen;
6381 } while (i != idx);
6382 if (history[histype][idx].hisstr == NULL)
6383 hisidx[histype] = -1;
6384 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006385 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 return found;
6387}
6388
6389/*
6390 * Remove an indexed entry from a history.
6391 * "histype" may be one of the HIST_ values.
6392 */
6393 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006394del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
6396 int i, j;
6397
6398 i = calc_hist_idx(histype, idx);
6399 if (i < 0)
6400 return FALSE;
6401 idx = hisidx[histype];
6402 vim_free(history[histype][i].hisstr);
6403
6404 /* When deleting the last added search string in a mapping, reset
6405 * last_maptick, so that the last added search string isn't deleted again.
6406 */
6407 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6408 last_maptick = -1;
6409
6410 while (i != idx)
6411 {
6412 j = (i + 1) % hislen;
6413 history[histype][i] = history[histype][j];
6414 i = j;
6415 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006416 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (--i < 0)
6418 i += hislen;
6419 hisidx[histype] = i;
6420 return TRUE;
6421}
6422
6423#endif /* FEAT_EVAL */
6424
6425#if defined(FEAT_CRYPT) || defined(PROTO)
6426/*
6427 * Very specific function to remove the value in ":set key=val" from the
6428 * history.
6429 */
6430 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006431remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 char_u *p;
6434 int i;
6435
6436 i = hisidx[HIST_CMD];
6437 if (i < 0)
6438 return;
6439 p = history[HIST_CMD][i].hisstr;
6440 if (p != NULL)
6441 for ( ; *p; ++p)
6442 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6443 {
6444 p = vim_strchr(p + 3, '=');
6445 if (p == NULL)
6446 break;
6447 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006448 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (p[i] == '\\' && p[i + 1])
6450 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006451 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 --p;
6453 }
6454}
6455#endif
6456
6457#endif /* FEAT_CMDHIST */
6458
Bram Moolenaar064154c2016-03-19 22:50:43 +01006459#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006460/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006461 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006462 * ccline and put the previous value in prev_ccline.
6463 */
6464 static struct cmdline_info *
6465get_ccline_ptr(void)
6466{
6467 if ((State & CMDLINE) == 0)
6468 return NULL;
6469 if (ccline.cmdbuff != NULL)
6470 return &ccline;
6471 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6472 return &prev_ccline;
6473 return NULL;
6474}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006475#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006476
Bram Moolenaar064154c2016-03-19 22:50:43 +01006477#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006478/*
6479 * Get the current command line in allocated memory.
6480 * Only works when the command line is being edited.
6481 * Returns NULL when something is wrong.
6482 */
6483 char_u *
6484get_cmdline_str(void)
6485{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006486 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006487
Bram Moolenaaree91c332018-09-25 22:27:35 +02006488 if (cmdline_star > 0)
6489 return NULL;
6490 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006491 if (p == NULL)
6492 return NULL;
6493 return vim_strnsave(p->cmdbuff, p->cmdlen);
6494}
6495
6496/*
6497 * Get the current command line position, counted in bytes.
6498 * Zero is the first position.
6499 * Only works when the command line is being edited.
6500 * Returns -1 when something is wrong.
6501 */
6502 int
6503get_cmdline_pos(void)
6504{
6505 struct cmdline_info *p = get_ccline_ptr();
6506
6507 if (p == NULL)
6508 return -1;
6509 return p->cmdpos;
6510}
6511
6512/*
6513 * Set the command line byte position to "pos". Zero is the first position.
6514 * Only works when the command line is being edited.
6515 * Returns 1 when failed, 0 when OK.
6516 */
6517 int
6518set_cmdline_pos(
6519 int pos)
6520{
6521 struct cmdline_info *p = get_ccline_ptr();
6522
6523 if (p == NULL)
6524 return 1;
6525
6526 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6527 * changed the command line. */
6528 if (pos < 0)
6529 new_cmdpos = 0;
6530 else
6531 new_cmdpos = pos;
6532 return 0;
6533}
6534#endif
6535
6536#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6537/*
6538 * Get the current command-line type.
6539 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6540 * Only works when the command line is being edited.
6541 * Returns NUL when something is wrong.
6542 */
6543 int
6544get_cmdline_type(void)
6545{
6546 struct cmdline_info *p = get_ccline_ptr();
6547
6548 if (p == NULL)
6549 return NUL;
6550 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006551 return
6552# ifdef FEAT_EVAL
6553 (p->input_fn) ? '@' :
6554# endif
6555 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006556 return p->cmdfirstc;
6557}
6558#endif
6559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6561/*
6562 * Get indices "num1,num2" that specify a range within a list (not a range of
6563 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6564 * Returns OK if parsed successfully, otherwise FAIL.
6565 */
6566 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006567get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
6569 int len;
6570 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006571 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 *str = skipwhite(*str);
6574 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6575 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006576 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 *str += len;
6578 *num1 = (int)num;
6579 first = TRUE;
6580 }
6581 *str = skipwhite(*str);
6582 if (**str == ',') /* parse "to" part of range */
6583 {
6584 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006585 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (len > 0)
6587 {
6588 *num2 = (int)num;
6589 *str = skipwhite(*str + len);
6590 }
6591 else if (!first) /* no number given at all */
6592 return FAIL;
6593 }
6594 else if (first) /* only one number given */
6595 *num2 = *num1;
6596 return OK;
6597}
6598#endif
6599
6600#if defined(FEAT_CMDHIST) || defined(PROTO)
6601/*
6602 * :history command - print a history
6603 */
6604 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006605ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 histentry_T *hist;
6608 int histype1 = HIST_CMD;
6609 int histype2 = HIST_CMD;
6610 int hisidx1 = 1;
6611 int hisidx2 = -1;
6612 int idx;
6613 int i, j, k;
6614 char_u *end;
6615 char_u *arg = eap->arg;
6616
6617 if (hislen == 0)
6618 {
6619 MSG(_("'history' option is zero"));
6620 return;
6621 }
6622
6623 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6624 {
6625 end = arg;
6626 while (ASCII_ISALPHA(*end)
6627 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6628 end++;
6629 i = *end;
6630 *end = NUL;
6631 histype1 = get_histtype(arg);
6632 if (histype1 == -1)
6633 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006634 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
6636 histype1 = 0;
6637 histype2 = HIST_COUNT-1;
6638 }
6639 else
6640 {
6641 *end = i;
6642 EMSG(_(e_trailing));
6643 return;
6644 }
6645 }
6646 else
6647 histype2 = histype1;
6648 *end = i;
6649 }
6650 else
6651 end = arg;
6652 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6653 {
6654 EMSG(_(e_trailing));
6655 return;
6656 }
6657
6658 for (; !got_int && histype1 <= histype2; ++histype1)
6659 {
6660 STRCPY(IObuff, "\n # ");
6661 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6662 MSG_PUTS_TITLE(IObuff);
6663 idx = hisidx[histype1];
6664 hist = history[histype1];
6665 j = hisidx1;
6666 k = hisidx2;
6667 if (j < 0)
6668 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6669 if (k < 0)
6670 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6671 if (idx >= 0 && j <= k)
6672 for (i = idx + 1; !got_int; ++i)
6673 {
6674 if (i == hislen)
6675 i = 0;
6676 if (hist[i].hisstr != NULL
6677 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6678 {
6679 msg_putchar('\n');
6680 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6681 hist[i].hisnum);
6682 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6683 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006684 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 else
6686 STRCAT(IObuff, hist[i].hisstr);
6687 msg_outtrans(IObuff);
6688 out_flush();
6689 }
6690 if (i == idx)
6691 break;
6692 }
6693 }
6694}
6695#endif
6696
6697#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006698/*
6699 * Buffers for history read from a viminfo file. Only valid while reading.
6700 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006701static histentry_T *viminfo_history[HIST_COUNT] =
6702 {NULL, NULL, NULL, NULL, NULL};
6703static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6704static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705static int viminfo_add_at_front = FALSE;
6706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707/*
6708 * Translate a history type number to the associated character.
6709 */
6710 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006711hist_type2char(
6712 int type,
6713 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 if (type == HIST_CMD)
6716 return ':';
6717 if (type == HIST_SEARCH)
6718 {
6719 if (use_question)
6720 return '?';
6721 else
6722 return '/';
6723 }
6724 if (type == HIST_EXPR)
6725 return '=';
6726 return '@';
6727}
6728
6729/*
6730 * Prepare for reading the history from the viminfo file.
6731 * This allocates history arrays to store the read history lines.
6732 */
6733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006734prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 int i;
6737 int num;
6738 int type;
6739 int len;
6740
6741 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006742 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (asklen > hislen)
6744 asklen = hislen;
6745
6746 for (type = 0; type < HIST_COUNT; ++type)
6747 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006748 /* Count the number of empty spaces in the history list. Entries read
6749 * from viminfo previously are also considered empty. If there are
6750 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006752 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 num++;
6754 len = asklen;
6755 if (num > len)
6756 len = num;
6757 if (len <= 0)
6758 viminfo_history[type] = NULL;
6759 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006760 viminfo_history[type] = (histentry_T *)lalloc(
6761 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (viminfo_history[type] == NULL)
6763 len = 0;
6764 viminfo_hislen[type] = len;
6765 viminfo_hisidx[type] = 0;
6766 }
6767}
6768
6769/*
6770 * Accept a line from the viminfo, store it in the history array when it's
6771 * new.
6772 */
6773 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006774read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
6776 int type;
6777 long_u len;
6778 char_u *val;
6779 char_u *p;
6780
6781 type = hist_char2type(virp->vir_line[0]);
6782 if (viminfo_hisidx[type] < viminfo_hislen[type])
6783 {
6784 val = viminfo_readstring(virp, 1, TRUE);
6785 if (val != NULL && *val != NUL)
6786 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006787 int sep = (*val == ' ' ? NUL : *val);
6788
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006790 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 /* Need to re-allocate to append the separator byte. */
6793 len = STRLEN(val);
6794 p = lalloc(len + 2, TRUE);
6795 if (p != NULL)
6796 {
6797 if (type == HIST_SEARCH)
6798 {
6799 /* Search entry: Move the separator from the first
6800 * column to after the NUL. */
6801 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006802 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804 else
6805 {
6806 /* Not a search entry: No separator in the viminfo
6807 * file, add a NUL separator. */
6808 mch_memmove(p, val, (size_t)len + 1);
6809 p[len + 1] = NUL;
6810 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006811 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6812 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006813 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6814 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006815 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817 }
6818 }
6819 vim_free(val);
6820 }
6821 return viminfo_readline(virp);
6822}
6823
Bram Moolenaar07219f92013-04-14 23:19:36 +02006824/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006825 * Accept a new style history line from the viminfo, store it in the history
6826 * array when it's new.
6827 */
6828 void
6829handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006830 garray_T *values,
6831 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006832{
6833 int type;
6834 long_u len;
6835 char_u *val;
6836 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006837 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006838
6839 /* Check the format:
6840 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006841 if (values->ga_len < 4
6842 || vp[0].bv_type != BVAL_NR
6843 || vp[1].bv_type != BVAL_NR
6844 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6845 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006846 return;
6847
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006848 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006849 if (type >= HIST_COUNT)
6850 return;
6851 if (viminfo_hisidx[type] < viminfo_hislen[type])
6852 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006853 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006854 if (val != NULL && *val != NUL)
6855 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006856 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6857 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006858 int idx;
6859 int overwrite = FALSE;
6860
6861 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6862 {
6863 /* If lines were written by an older Vim we need to avoid
6864 * getting duplicates. See if the entry already exists. */
6865 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6866 {
6867 p = viminfo_history[type][idx].hisstr;
6868 if (STRCMP(val, p) == 0
6869 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6870 {
6871 overwrite = TRUE;
6872 break;
6873 }
6874 }
6875
6876 if (!overwrite)
6877 {
6878 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006879 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006880 p = lalloc(len + 2, TRUE);
6881 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006882 else
6883 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006884 if (p != NULL)
6885 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006886 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006887 if (!overwrite)
6888 {
6889 mch_memmove(p, val, (size_t)len + 1);
6890 /* Put the separator after the NUL. */
6891 p[len + 1] = sep;
6892 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006893 viminfo_history[type][idx].hisnum = 0;
6894 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006895 viminfo_hisidx[type]++;
6896 }
6897 }
6898 }
6899 }
6900 }
6901}
6902
6903/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006904 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006905 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006906 static void
6907concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908{
6909 int idx;
6910 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006911
6912 idx = hisidx[type] + viminfo_hisidx[type];
6913 if (idx >= hislen)
6914 idx -= hislen;
6915 else if (idx < 0)
6916 idx = hislen - 1;
6917 if (viminfo_add_at_front)
6918 hisidx[type] = idx;
6919 else
6920 {
6921 if (hisidx[type] == -1)
6922 hisidx[type] = hislen - 1;
6923 do
6924 {
6925 if (history[type][idx].hisstr != NULL
6926 || history[type][idx].viminfo)
6927 break;
6928 if (++idx == hislen)
6929 idx = 0;
6930 } while (idx != hisidx[type]);
6931 if (idx != hisidx[type] && --idx < 0)
6932 idx = hislen - 1;
6933 }
6934 for (i = 0; i < viminfo_hisidx[type]; i++)
6935 {
6936 vim_free(history[type][idx].hisstr);
6937 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6938 history[type][idx].viminfo = TRUE;
6939 history[type][idx].time_set = viminfo_history[type][i].time_set;
6940 if (--idx < 0)
6941 idx = hislen - 1;
6942 }
6943 idx += 1;
6944 idx %= hislen;
6945 for (i = 0; i < viminfo_hisidx[type]; i++)
6946 {
6947 history[type][idx++].hisnum = ++hisnum[type];
6948 idx %= hislen;
6949 }
6950}
6951
6952#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6953 static int
6954#ifdef __BORLANDC__
6955_RTLENTRYF
6956#endif
6957sort_hist(const void *s1, const void *s2)
6958{
6959 histentry_T *p1 = *(histentry_T **)s1;
6960 histentry_T *p2 = *(histentry_T **)s2;
6961
6962 if (p1->time_set < p2->time_set) return -1;
6963 if (p1->time_set > p2->time_set) return 1;
6964 return 0;
6965}
6966#endif
6967
6968/*
6969 * Merge history lines from viminfo and lines typed in this Vim based on the
6970 * timestamp;
6971 */
6972 static void
6973merge_history(int type)
6974{
6975 int max_len;
6976 histentry_T **tot_hist;
6977 histentry_T *new_hist;
6978 int i;
6979 int len;
6980
6981 /* Make one long list with all entries. */
6982 max_len = hislen + viminfo_hisidx[type];
6983 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6984 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6985 if (tot_hist == NULL || new_hist == NULL)
6986 {
6987 vim_free(tot_hist);
6988 vim_free(new_hist);
6989 return;
6990 }
6991 for (i = 0; i < viminfo_hisidx[type]; i++)
6992 tot_hist[i] = &viminfo_history[type][i];
6993 len = i;
6994 for (i = 0; i < hislen; i++)
6995 if (history[type][i].hisstr != NULL)
6996 tot_hist[len++] = &history[type][i];
6997
6998 /* Sort the list on timestamp. */
6999 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
7000
7001 /* Keep the newest ones. */
7002 for (i = 0; i < hislen; i++)
7003 {
7004 if (i < len)
7005 {
7006 new_hist[i] = *tot_hist[i];
7007 tot_hist[i]->hisstr = NULL;
7008 if (new_hist[i].hisnum == 0)
7009 new_hist[i].hisnum = ++hisnum[type];
7010 }
7011 else
7012 clear_hist_entry(&new_hist[i]);
7013 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007014 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007015
7016 /* Free what is not kept. */
7017 for (i = 0; i < viminfo_hisidx[type]; i++)
7018 vim_free(viminfo_history[type][i].hisstr);
7019 for (i = 0; i < hislen; i++)
7020 vim_free(history[type][i].hisstr);
7021 vim_free(history[type]);
7022 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007023 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007024}
7025
7026/*
7027 * Finish reading history lines from viminfo. Not used when writing viminfo.
7028 */
7029 void
7030finish_viminfo_history(vir_T *virp)
7031{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007033 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 for (type = 0; type < HIST_COUNT; ++type)
7036 {
7037 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007038 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007039
7040 if (merge)
7041 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007043 concat_history(type);
7044
Bram Moolenaard23a8232018-02-10 18:45:26 +01007045 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007046 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
7048}
7049
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007050/*
7051 * Write history to viminfo file in "fp".
7052 * When "merge" is TRUE merge history lines with a previously read viminfo
7053 * file, data is in viminfo_history[].
7054 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007057write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 int i;
7060 int type;
7061 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007062 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064 init_history();
7065 if (hislen == 0)
7066 return;
7067 for (type = 0; type < HIST_COUNT; ++type)
7068 {
7069 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7070 if (num_saved == 0)
7071 continue;
7072 if (num_saved < 0) /* Use default */
7073 num_saved = hislen;
7074 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7075 type == HIST_CMD ? _("Command Line") :
7076 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007077 type == HIST_EXPR ? _("Expression") :
7078 type == HIST_INPUT ? _("Input Line") :
7079 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (num_saved > hislen)
7081 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007082
7083 /*
7084 * Merge typed and viminfo history:
7085 * round 1: history of typed commands.
7086 * round 2: history from recently read viminfo.
7087 */
7088 for (round = 1; round <= 2; ++round)
7089 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007090 if (round == 1)
7091 /* start at newest entry, somewhere in the list */
7092 i = hisidx[type];
7093 else if (viminfo_hisidx[type] > 0)
7094 /* start at newest entry, first in the list */
7095 i = 0;
7096 else
7097 /* empty list */
7098 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007099 if (i >= 0)
7100 while (num_saved > 0
7101 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007103 char_u *p;
7104 time_t timestamp;
7105 int c = NUL;
7106
7107 if (round == 1)
7108 {
7109 p = history[type][i].hisstr;
7110 timestamp = history[type][i].time_set;
7111 }
7112 else
7113 {
7114 p = viminfo_history[type] == NULL ? NULL
7115 : viminfo_history[type][i].hisstr;
7116 timestamp = viminfo_history[type] == NULL ? 0
7117 : viminfo_history[type][i].time_set;
7118 }
7119
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007120 if (p != NULL && (round == 2
7121 || !merge
7122 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007124 --num_saved;
7125 fputc(hist_type2char(type, TRUE), fp);
7126 /* For the search history: put the separator in the
7127 * second column; use a space if there isn't one. */
7128 if (type == HIST_SEARCH)
7129 {
7130 c = p[STRLEN(p) + 1];
7131 putc(c == NUL ? ' ' : c, fp);
7132 }
7133 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007134
7135 {
7136 char cbuf[NUMBUFLEN];
7137
7138 /* New style history with a bar line. Format:
7139 * |{bartype},{histtype},{timestamp},{separator},"text" */
7140 if (c == NUL)
7141 cbuf[0] = NUL;
7142 else
7143 sprintf(cbuf, "%d", c);
7144 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7145 type, (long)timestamp, cbuf);
7146 barline_writestring(fp, p, LSIZE - 20);
7147 putc('\n', fp);
7148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007150 if (round == 1)
7151 {
7152 /* Decrement index, loop around and stop when back at
7153 * the start. */
7154 if (--i < 0)
7155 i = hislen - 1;
7156 if (i == hisidx[type])
7157 break;
7158 }
7159 else
7160 {
7161 /* Increment index. Stop at the end in the while. */
7162 ++i;
7163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007165 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007166 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007167 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007168 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007169 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007170 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 }
7172}
7173#endif /* FEAT_VIMINFO */
7174
7175#if defined(FEAT_FKMAP) || defined(PROTO)
7176/*
7177 * Write a character at the current cursor+offset position.
7178 * It is directly written into the command buffer block.
7179 */
7180 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007181cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182{
7183 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7184 {
7185 EMSG(_("E198: cmd_pchar beyond the command length"));
7186 return;
7187 }
7188 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7189 ccline.cmdbuff[ccline.cmdlen] = NUL;
7190}
7191
7192 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7196 {
7197 /* EMSG(_("cmd_gchar beyond the command length")); */
7198 return NUL;
7199 }
7200 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7201}
7202#endif
7203
7204#if defined(FEAT_CMDWIN) || defined(PROTO)
7205/*
7206 * Open a window on the current command line and history. Allow editing in
7207 * the window. Returns when the window is closed.
7208 * Returns:
7209 * CR if the command is to be executed
7210 * Ctrl_C if it is to be abandoned
7211 * K_IGNORE if editing continues
7212 */
7213 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007214open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007216 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007218 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 win_T *wp;
7220 int i;
7221 linenr_T lnum;
7222 int histtype;
7223 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 int save_restart_edit = restart_edit;
7225 int save_State = State;
7226 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007227#ifdef FEAT_RIGHTLEFT
7228 int save_cmdmsg_rl = cmdmsg_rl;
7229#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007230#ifdef FEAT_FOLDING
7231 int save_KeyTyped;
7232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
7234 /* Can't do this recursively. Can't do it when typing a password. */
7235 if (cmdwin_type != 0
7236# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7237 || cmdline_star > 0
7238# endif
7239 )
7240 {
7241 beep_flush();
7242 return K_IGNORE;
7243 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007244 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
7246 /* Save current window sizes. */
7247 win_size_save(&winsizes);
7248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007250 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007251
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007252 /* don't use a new tab page */
7253 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007254 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 /* Create a window for the command-line buffer. */
7257 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7258 {
7259 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007260 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 return K_IGNORE;
7262 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007263 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007266 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007267 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007270#ifdef FEAT_FOLDING
7271 curwin->w_p_fen = FALSE;
7272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007274 curwin->w_p_rl = cmdmsg_rl;
7275 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007277 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007280 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007281 /* But don't allow switching to another buffer. */
7282 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
Bram Moolenaar46152342005-09-07 21:18:43 +00007284 /* Showing the prompt may have set need_wait_return, reset it. */
7285 need_wait_return = FALSE;
7286
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007287 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7289 {
7290 if (p_wc == TAB)
7291 {
7292 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7293 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7294 }
7295 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7296 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007297 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007299 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7300 * sets 'textwidth' to 78). */
7301 curbuf->b_p_tw = 0;
7302
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 /* Fill the buffer with the history. */
7304 init_history();
7305 if (hislen > 0)
7306 {
7307 i = hisidx[histtype];
7308 if (i >= 0)
7309 {
7310 lnum = 0;
7311 do
7312 {
7313 if (++i == hislen)
7314 i = 0;
7315 if (history[histtype][i].hisstr != NULL)
7316 ml_append(lnum++, history[histtype][i].hisstr,
7317 (colnr_T)0, FALSE);
7318 }
7319 while (i != hisidx[histtype]);
7320 }
7321 }
7322
7323 /* Replace the empty last line with the current command-line and put the
7324 * cursor there. */
7325 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7326 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7327 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007328 changed_line_abv_curs();
7329 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007330 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 /* No Ex mode here! */
7333 exmode_active = 0;
7334
7335 State = NORMAL;
7336# ifdef FEAT_MOUSE
7337 setmouse();
7338# endif
7339
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007341 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007342 if (restart_edit != 0) /* autocmd with ":startinsert" */
7343 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345 i = RedrawingDisabled;
7346 RedrawingDisabled = 0;
7347
7348 /*
7349 * Call the main loop until <CR> or CTRL-C is typed.
7350 */
7351 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007352 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354 RedrawingDisabled = i;
7355
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007356# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007357 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007358# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007359
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007361 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007362
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007363# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007364 /* Restore KeyTyped in case it is modified by autocommands */
7365 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366# endif
7367
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 exmode_active = save_exmode;
7370
Bram Moolenaarf9821062008-06-20 16:31:07 +00007371 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007373 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 {
7375 cmdwin_result = Ctrl_C;
7376 EMSG(_("E199: Active window or buffer deleted"));
7377 }
7378 else
7379 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007380# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 /* autocmds may abort script processing */
7382 if (aborting() && cmdwin_result != K_IGNORE)
7383 cmdwin_result = Ctrl_C;
7384# endif
7385 /* Set the new command line from the cmdline buffer. */
7386 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007387 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007389 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7390
7391 if (histtype == HIST_CMD)
7392 {
7393 /* Execute the command directly. */
7394 ccline.cmdbuff = vim_strsave((char_u *)p);
7395 cmdwin_result = CAR;
7396 }
7397 else
7398 {
7399 /* First need to cancel what we were doing. */
7400 ccline.cmdbuff = NULL;
7401 stuffcharReadbuff(':');
7402 stuffReadbuff((char_u *)p);
7403 stuffcharReadbuff(CAR);
7404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 else if (cmdwin_result == K_XF2) /* :qa typed */
7407 {
7408 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7409 cmdwin_result = CAR;
7410 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007411 else if (cmdwin_result == Ctrl_C)
7412 {
7413 /* :q or :close, don't execute any command
7414 * and don't modify the cmd window. */
7415 ccline.cmdbuff = NULL;
7416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 else
7418 ccline.cmdbuff = vim_strsave(ml_get_curline());
7419 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007420 {
7421 ccline.cmdbuff = vim_strsave((char_u *)"");
7422 ccline.cmdlen = 0;
7423 ccline.cmdbufflen = 1;
7424 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
7428 {
7429 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7430 ccline.cmdbufflen = ccline.cmdlen + 1;
7431 ccline.cmdpos = curwin->w_cursor.col;
7432 if (ccline.cmdpos > ccline.cmdlen)
7433 ccline.cmdpos = ccline.cmdlen;
7434 if (cmdwin_result == K_IGNORE)
7435 {
7436 set_cmdspos_cursor();
7437 redrawcmd();
7438 }
7439 }
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007442 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007443# ifdef FEAT_CONCEAL
7444 /* Avoid command-line window first character being concealed. */
7445 curwin->w_p_cole = 0;
7446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007448 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 win_goto(old_curwin);
7450 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007451
7452 /* win_close() may have already wiped the buffer when 'bh' is
7453 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007454 if (bufref_valid(&bufref))
7455 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457 /* Restore window sizes. */
7458 win_size_restore(&winsizes);
7459
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007460 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462
7463 ga_clear(&winsizes);
7464 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007465# ifdef FEAT_RIGHTLEFT
7466 cmdmsg_rl = save_cmdmsg_rl;
7467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
7469 State = save_State;
7470# ifdef FEAT_MOUSE
7471 setmouse();
7472# endif
7473
7474 return cmdwin_result;
7475}
7476#endif /* FEAT_CMDWIN */
7477
7478/*
7479 * Used for commands that either take a simple command string argument, or:
7480 * cmd << endmarker
7481 * {script}
7482 * endmarker
7483 * Returns a pointer to allocated memory with {script} or NULL.
7484 */
7485 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488 char_u *theline;
7489 char *end_pattern = NULL;
7490 char dot[] = ".";
7491 garray_T ga;
7492
7493 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7494 return NULL;
7495
7496 ga_init2(&ga, 1, 0x400);
7497
7498 if (cmd[2] != NUL)
7499 end_pattern = (char *)skipwhite(cmd + 2);
7500 else
7501 end_pattern = dot;
7502
7503 for (;;)
7504 {
7505 theline = eap->getline(
7506#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007507 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#endif
7509 NUL, eap->cookie, 0);
7510
7511 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007512 {
7513 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
7517 ga_concat(&ga, theline);
7518 ga_append(&ga, '\n');
7519 vim_free(theline);
7520 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007521 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
7523 return (char_u *)ga.ga_data;
7524}