blob: a9d6bd4833c24d8cf2a188553ae33351da84da0d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200144#ifdef FEAT_SEARCH_EXTRA
145static void set_search_match(pos_T *t);
146#endif
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200148
149#ifdef FEAT_AUTOCMD
150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
159#endif
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200162 * Abandon the command line.
163 */
164 static void
165abandon_cmdline(void)
166{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100167 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200168 if (msg_scrolled == 0)
169 compute_cmdrow();
170 MSG("");
171 redraw_cmdline = TRUE;
172}
173
Bram Moolenaaree219b02017-12-17 14:55:01 +0100174#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200175/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100176 * Guess that the pattern matches everything. Only finds specific cases, such
177 * as a trailing \|, which can happen while typing a pattern.
178 */
179 static int
180empty_pattern(char_u *p)
181{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100182 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100183
184 /* remove trailing \v and the like */
185 while (n >= 2 && p[n - 2] == '\\'
186 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
187 n -= 2;
188 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
189}
Bram Moolenaaree219b02017-12-17 14:55:01 +0100190#endif
Bram Moolenaar66216052017-12-16 16:33:44 +0100191
192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 * getcmdline() - accept a command line starting with firstc.
194 *
195 * firstc == ':' get ":" command line.
196 * firstc == '/' or '?' get search pattern
197 * firstc == '=' get expression
198 * firstc == '@' get text for input() function
199 * firstc == '>' get text for debug mode
200 * firstc == NUL get text for :insert command
201 * firstc == -1 like NUL, and break on CTRL-C
202 *
203 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
204 * command line.
205 *
206 * Careful: getcmdline() can be called recursively!
207 *
208 * Return pointer to allocated string if there is a commandline, NULL
209 * otherwise.
210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100212getcmdline(
213 int firstc,
214 long count UNUSED, /* only used for incremental search */
215 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 int c;
218 int i;
219 int j;
220 int gotesc = FALSE; /* TRUE when <ESC> just typed */
221 int do_abbr; /* when TRUE check for abbr. */
222#ifdef FEAT_CMDHIST
223 char_u *lookfor = NULL; /* string to match */
224 int hiscnt; /* current history line in use */
225 int histype; /* history type to be used */
226#endif
227#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200228 pos_T search_start; /* where 'incsearch' starts searching */
229 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200231 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200233 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200235 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200236 pos_T match_start = curwin->w_cursor;
237 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# ifdef FEAT_DIFF
239 int old_topfill;
Bram Moolenaard0480092017-11-16 22:20:39 +0100240 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# endif
242 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200243 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 int did_incsearch = FALSE;
245 int incsearch_postponed = FALSE;
246#endif
247 int did_wild_list = FALSE; /* did wild_list() recently */
248 int wim_index = 0; /* index in wim_flags[] */
249 int res;
250 int save_msg_scroll = msg_scroll;
251 int save_State = State; /* remember State when called */
252 int some_key_typed = FALSE; /* one of the keys was typed */
253#ifdef FEAT_MOUSE
254 /* mouse drag and release events are ignored, unless they are
255 * preceded with a mouse down event */
256 int ignore_drag_release = TRUE;
257#endif
258#ifdef FEAT_EVAL
259 int break_ctrl_c = FALSE;
260#endif
261 expand_T xpc;
262 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100263#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000264 /* Everything that may work recursively should save and restore the
265 * current command line in save_ccline. That includes update_screen(), a
266 * custom status line may invoke ":normal". */
267 struct cmdline_info save_ccline;
268#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200269#ifdef FEAT_AUTOCMD
270 int cmdline_type;
271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#ifdef FEAT_EVAL
274 if (firstc == -1)
275 {
276 firstc = NUL;
277 break_ctrl_c = TRUE;
278 }
279#endif
280#ifdef FEAT_RIGHTLEFT
281 /* start without Hebrew mapping for a command line */
282 if (firstc == ':' || firstc == '=' || firstc == '>')
283 cmd_hkmap = 0;
284#endif
285
286 ccline.overstrike = FALSE; /* always start in insert mode */
287#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100288 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200289 save_cursor = curwin->w_cursor; /* may be restored later */
290 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 old_curswant = curwin->w_curswant;
292 old_leftcol = curwin->w_leftcol;
293 old_topline = curwin->w_topline;
294# ifdef FEAT_DIFF
295 old_topfill = curwin->w_topfill;
296# endif
297 old_botline = curwin->w_botline;
298#endif
299
300 /*
301 * set some variables for redrawcmd()
302 */
303 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000304 ccline.cmdindent = (firstc > 0 ? indent : 0);
305
306 /* alloc initial ccline.cmdbuff */
307 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 if (ccline.cmdbuff == NULL)
309 return NULL; /* out of memory */
310 ccline.cmdlen = ccline.cmdpos = 0;
311 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100312 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000314 /* autoindent for :insert and :append */
315 if (firstc <= 0)
316 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200317 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000318 ccline.cmdbuff[indent] = NUL;
319 ccline.cmdpos = indent;
320 ccline.cmdspos = indent;
321 ccline.cmdlen = indent;
322 }
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000325 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
327#ifdef FEAT_RIGHTLEFT
328 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
329 && (firstc == '/' || firstc == '?'))
330 cmdmsg_rl = TRUE;
331 else
332 cmdmsg_rl = FALSE;
333#endif
334
335 redir_off = TRUE; /* don't redirect the typed command */
336 if (!cmd_silent)
337 {
338 i = msg_scrolled;
339 msg_scrolled = 0; /* avoid wait_return message */
340 gotocmdline(TRUE);
341 msg_scrolled += i;
342 redrawcmdprompt(); /* draw prompt or indent */
343 set_cmdspos();
344 }
345 xpc.xp_context = EXPAND_NOTHING;
346 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000347#ifndef BACKSLASH_IN_FILENAME
348 xpc.xp_shell = FALSE;
349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000351#if defined(FEAT_EVAL)
352 if (ccline.input_fn)
353 {
354 xpc.xp_context = ccline.xp_context;
355 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000356# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000357 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000358# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000359 }
360#endif
361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 /*
363 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
364 * doing ":@0" when register 0 doesn't contain a CR.
365 */
366 msg_scroll = FALSE;
367
368 State = CMDLINE;
369
370 if (firstc == '/' || firstc == '?' || firstc == '@')
371 {
372 /* Use ":lmap" mappings for search pattern and input(). */
373 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
374 b_im_ptr = &curbuf->b_p_iminsert;
375 else
376 b_im_ptr = &curbuf->b_p_imsearch;
377 if (*b_im_ptr == B_IMODE_LMAP)
378 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100379#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 im_set_active(*b_im_ptr == B_IMODE_IM);
381#endif
382 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100383#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 else if (p_imcmdline)
385 im_set_active(TRUE);
386#endif
387
388#ifdef FEAT_MOUSE
389 setmouse();
390#endif
391#ifdef CURSOR_SHAPE
392 ui_cursor_shape(); /* may show different cursor shape */
393#endif
394
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000395 /* When inside an autocommand for writing "exiting" may be set and
396 * terminal mode set to cooked. Need to set raw mode here then. */
397 settmode(TMODE_RAW);
398
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200399#ifdef FEAT_AUTOCMD
400 /* Trigger CmdlineEnter autocommands. */
401 cmdline_type = firstc == NUL ? '-' : firstc;
402 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
403#endif
404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#ifdef FEAT_CMDHIST
406 init_history();
407 hiscnt = hislen; /* set hiscnt to impossible history value */
408 histype = hist_char2type(firstc);
409#endif
410
411#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000412 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413#endif
414
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200415 /* If something above caused an error, reset the flags, we do want to type
416 * and execute commands. Display may be messed up a bit. */
417 if (did_emsg)
418 redrawcmd();
419 did_emsg = FALSE;
420 got_int = FALSE;
421
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 /*
423 * Collect the command string, handling editing keys.
424 */
425 for (;;)
426 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000427 redir_off = TRUE; /* Don't redirect the typed command.
428 Repeated, because a ":redir" inside
429 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#ifdef USE_ON_FLY_SCROLL
431 dont_scroll = FALSE; /* allow scrolling here */
432#endif
433 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
434
435 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000436
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100437 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
438 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000439 do
440 {
441 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100442 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000443
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (KeyTyped)
445 {
446 some_key_typed = TRUE;
447#ifdef FEAT_RIGHTLEFT
448 if (cmd_hkmap)
449 c = hkmap(c);
450# ifdef FEAT_FKMAP
451 if (cmd_fkmap)
452 c = cmdl_fkmap(c);
453# endif
454 if (cmdmsg_rl && !KeyStuffed)
455 {
456 /* Invert horizontal movements and operations. Only when
457 * typed by the user directly, not when the result of a
458 * mapping. */
459 switch (c)
460 {
461 case K_RIGHT: c = K_LEFT; break;
462 case K_S_RIGHT: c = K_S_LEFT; break;
463 case K_C_RIGHT: c = K_C_LEFT; break;
464 case K_LEFT: c = K_RIGHT; break;
465 case K_S_LEFT: c = K_S_RIGHT; break;
466 case K_C_LEFT: c = K_C_RIGHT; break;
467 }
468 }
469#endif
470 }
471
472 /*
473 * Ignore got_int when CTRL-C was typed here.
474 * Don't ignore it in :global, we really need to break then, e.g., for
475 * ":g/pat/normal /pat" (without the <CR>).
476 * Don't ignore it for the input() function.
477 */
478 if ((c == Ctrl_C
479#ifdef UNIX
480 || c == intr_char
481#endif
482 )
483#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
484 && firstc != '@'
485#endif
486#ifdef FEAT_EVAL
487 && !break_ctrl_c
488#endif
489 && !global_busy)
490 got_int = FALSE;
491
492#ifdef FEAT_CMDHIST
493 /* free old command line when finished moving around in the history
494 * list */
495 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000496 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000497 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 && c != K_PAGEDOWN && c != K_PAGEUP
499 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000500 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100502 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#endif
504
505 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000506 * When there are matching completions to select <S-Tab> works like
507 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000509 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 c = Ctrl_P;
511
512#ifdef FEAT_WILDMENU
513 /* Special translations for 'wildmenu' */
514 if (did_wild_list && p_wmnu)
515 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000516 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000518 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 c = Ctrl_N;
520 }
521 /* Hitting CR after "emenu Name.": complete submenu */
522 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
523 && ccline.cmdpos > 1
524 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
525 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
526 && (c == '\n' || c == '\r' || c == K_KENTER))
527 c = K_DOWN;
528#endif
529
530 /* free expanded names when finished walking through matches */
531 if (xpc.xp_numfiles != -1
532 && !(c == p_wc && KeyTyped) && c != p_wcm
533 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
534 && c != Ctrl_L)
535 {
536 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
537 did_wild_list = FALSE;
538#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000539 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540#endif
541 xpc.xp_context = EXPAND_NOTHING;
542 wim_index = 0;
543#ifdef FEAT_WILDMENU
544 if (p_wmnu && wild_menu_showing != 0)
545 {
546 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000547 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000548
549 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 if (wild_menu_showing == WM_SCROLLED)
553 {
554 /* Entered command line, move it up */
555 cmdline_row--;
556 redrawcmd();
557 }
558 else if (save_p_ls != -1)
559 {
560 /* restore 'laststatus' and 'winminheight' */
561 p_ls = save_p_ls;
562 p_wmh = save_p_wmh;
563 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000564 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000566 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 redrawcmd();
568 save_p_ls = -1;
569 }
570 else
571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 redraw_statuslines();
574 }
575 KeyTyped = skt;
576 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000577 if (ccline.input_fn)
578 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580#endif
581 }
582
583#ifdef FEAT_WILDMENU
584 /* Special translations for 'wildmenu' */
585 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
586 {
587 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000588 if (c == K_DOWN && ccline.cmdpos > 0
589 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
593 /* Hitting <Up>: Remove one submenu name in front of the
594 * cursor */
595 int found = FALSE;
596
597 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
598 i = 0;
599 while (--j > 0)
600 {
601 /* check for start of menu name */
602 if (ccline.cmdbuff[j] == ' '
603 && ccline.cmdbuff[j - 1] != '\\')
604 {
605 i = j + 1;
606 break;
607 }
608 /* check for start of submenu name */
609 if (ccline.cmdbuff[j] == '.'
610 && ccline.cmdbuff[j - 1] != '\\')
611 {
612 if (found)
613 {
614 i = j + 1;
615 break;
616 }
617 else
618 found = TRUE;
619 }
620 }
621 if (i > 0)
622 cmdline_del(i);
623 c = p_wc;
624 xpc.xp_context = EXPAND_NOTHING;
625 }
626 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000627 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000628 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000629 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 {
631 char_u upseg[5];
632
633 upseg[0] = PATHSEP;
634 upseg[1] = '.';
635 upseg[2] = '.';
636 upseg[3] = PATHSEP;
637 upseg[4] = NUL;
638
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000639 if (c == K_DOWN
640 && ccline.cmdpos > 0
641 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
642 && (ccline.cmdpos < 3
643 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
645 {
646 /* go down a directory */
647 c = p_wc;
648 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000649 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
651 /* If in a direct ancestor, strip off one ../ to go down */
652 int found = FALSE;
653
654 j = ccline.cmdpos;
655 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
656 while (--j > i)
657 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000658#ifdef FEAT_MBYTE
659 if (has_mbyte)
660 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if (vim_ispathsep(ccline.cmdbuff[j]))
663 {
664 found = TRUE;
665 break;
666 }
667 }
668 if (found
669 && ccline.cmdbuff[j - 1] == '.'
670 && ccline.cmdbuff[j - 2] == '.'
671 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
672 {
673 cmdline_del(j - 2);
674 c = p_wc;
675 }
676 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000677 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 /* go up a directory */
680 int found = FALSE;
681
682 j = ccline.cmdpos - 1;
683 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
684 while (--j > i)
685 {
686#ifdef FEAT_MBYTE
687 if (has_mbyte)
688 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
689#endif
690 if (vim_ispathsep(ccline.cmdbuff[j])
691#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100692 && vim_strchr((char_u *)" *?[{`$%#",
693 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#endif
695 )
696 {
697 if (found)
698 {
699 i = j + 1;
700 break;
701 }
702 else
703 found = TRUE;
704 }
705 }
706
707 if (!found)
708 j = i;
709 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
710 j += 4;
711 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
712 && j == i)
713 j += 3;
714 else
715 j = 0;
716 if (j > 0)
717 {
718 /* TODO this is only for DOS/UNIX systems - need to put in
719 * machine-specific stuff here and in upseg init */
720 cmdline_del(j);
721 put_on_cmdline(upseg + 1, 3, FALSE);
722 }
723 else if (ccline.cmdpos > i)
724 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100725
726 /* Now complete in the new directory. Set KeyTyped in case the
727 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100729 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 }
731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733#endif /* FEAT_WILDMENU */
734
735 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
736 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
737 if (c == Ctrl_BSL)
738 {
739 ++no_mapping;
740 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000741 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 --no_mapping;
743 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200744 /* CTRL-\ e doesn't work when obtaining an expression, unless it
745 * is in a mapping. */
746 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
747 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
749 vungetc(c);
750 c = Ctrl_BSL;
751 }
752#ifdef FEAT_EVAL
753 else if (c == 'e')
754 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200755 char_u *p = NULL;
756 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 /*
759 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000760 * Need to save and restore the current command line, to be
761 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
763 if (ccline.cmdpos == ccline.cmdlen)
764 new_cmdpos = 99999; /* keep it at the end */
765 else
766 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000767
768 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000770 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (c == '=')
772 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000773 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000774 * to avoid nasty things like going to another buffer when
775 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000776 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000777 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000779 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000780 restore_cmdline(&save_ccline);
781
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200782 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200784 len = (int)STRLEN(p);
785 if (realloc_cmdbuff(len + 1) == OK)
786 {
787 ccline.cmdlen = len;
788 STRCPY(ccline.cmdbuff, p);
789 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200791 /* Restore the cursor or use the position set with
792 * set_cmdline_pos(). */
793 if (new_cmdpos > ccline.cmdlen)
794 ccline.cmdpos = ccline.cmdlen;
795 else
796 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200798 KeyTyped = FALSE; /* Don't do p_wc completion. */
799 redrawcmd();
800 goto cmdline_changed;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803 }
804 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100805 got_int = FALSE; /* don't abandon the command line */
806 did_emsg = FALSE;
807 emsg_on_display = FALSE;
808 redrawcmd();
809 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811#endif
812 else
813 {
814 if (c == Ctrl_G && p_im && restart_edit == 0)
815 restart_edit = 'a';
816 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
817 in history */
818 goto returncmd; /* back to Normal mode */
819 }
820 }
821
822#ifdef FEAT_CMDWIN
823 if (c == cedit_key || c == K_CMDWIN)
824 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200825 if (ex_normal_busy == 0 && got_int == FALSE)
826 {
827 /*
828 * Open a window to edit the command line (and history).
829 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200830 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200831 some_key_typed = TRUE;
832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834# ifdef FEAT_DIGRAPHS
835 else
836# endif
837#endif
838#ifdef FEAT_DIGRAPHS
839 c = do_digraph(c);
840#endif
841
842 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
843 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
844 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000845 /* In Ex mode a backslash escapes a newline. */
846 if (exmode_active
847 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000848 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000849 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000850 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000852 if (c == K_KENTER)
853 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000855 else
856 {
857 gotesc = FALSE; /* Might have typed ESC previously, don't
858 truncate the cmdline now. */
859 if (ccheck_abbr(c + ABBR_OFF))
860 goto cmdline_changed;
861 if (!cmd_silent)
862 {
863 windgoto(msg_row, 0);
864 out_flush();
865 }
866 break;
867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869
870 /*
871 * Completion for 'wildchar' or 'wildcharm' key.
872 * - hitting <ESC> twice means: abandon command line.
873 * - wildcard expansion is only done when the 'wildchar' key is really
874 * typed, not when it comes from a macro
875 */
876 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
877 {
878 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
879 {
880 /* if 'wildmode' contains "list" may still need to list */
881 if (xpc.xp_numfiles > 1
882 && !did_wild_list
883 && (wim_flags[wim_index] & WIM_LIST))
884 {
885 (void)showmatches(&xpc, FALSE);
886 redrawcmd();
887 did_wild_list = TRUE;
888 }
889 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100890 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
891 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100893 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
894 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 else
896 res = OK; /* don't insert 'wildchar' now */
897 }
898 else /* typed p_wc first time */
899 {
900 wim_index = 0;
901 j = ccline.cmdpos;
902 /* if 'wildmode' first contains "longest", get longest
903 * common part */
904 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100905 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
906 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100908 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
909 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
911 /* if interrupted while completing, behave like it failed */
912 if (got_int)
913 {
914 (void)vpeekc(); /* remove <C-C> from input stream */
915 got_int = FALSE; /* don't abandon the command line */
916 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
917#ifdef FEAT_WILDMENU
918 xpc.xp_context = EXPAND_NOTHING;
919#endif
920 goto cmdline_changed;
921 }
922
923 /* when more than one match, and 'wildmode' first contains
924 * "list", or no change and 'wildmode' contains "longest,list",
925 * list all matches */
926 if (res == OK && xpc.xp_numfiles > 1)
927 {
928 /* a "longest" that didn't do anything is skipped (but not
929 * "list:longest") */
930 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
931 wim_index = 1;
932 if ((wim_flags[wim_index] & WIM_LIST)
933#ifdef FEAT_WILDMENU
934 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
935#endif
936 )
937 {
938 if (!(wim_flags[0] & WIM_LONGEST))
939 {
940#ifdef FEAT_WILDMENU
941 int p_wmnu_save = p_wmnu;
942 p_wmnu = 0;
943#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100944 /* remove match */
945 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#ifdef FEAT_WILDMENU
947 p_wmnu = p_wmnu_save;
948#endif
949 }
950#ifdef FEAT_WILDMENU
951 (void)showmatches(&xpc, p_wmnu
952 && ((wim_flags[wim_index] & WIM_LIST) == 0));
953#else
954 (void)showmatches(&xpc, FALSE);
955#endif
956 redrawcmd();
957 did_wild_list = TRUE;
958 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100959 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
960 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100962 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
963 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200966 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968#ifdef FEAT_WILDMENU
969 else if (xpc.xp_numfiles == -1)
970 xpc.xp_context = EXPAND_NOTHING;
971#endif
972 }
973 if (wim_index < 3)
974 ++wim_index;
975 if (c == ESC)
976 gotesc = TRUE;
977 if (res == OK)
978 goto cmdline_changed;
979 }
980
981 gotesc = FALSE;
982
983 /* <S-Tab> goes to last match, in a clumsy way */
984 if (c == K_S_TAB && KeyTyped)
985 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100986 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
987 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
988 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 goto cmdline_changed;
990 }
991
992 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
993 c = NL;
994
995 do_abbr = TRUE; /* default: check for abbreviation */
996
997 /*
998 * Big switch for a typed command line character.
999 */
1000 switch (c)
1001 {
1002 case K_BS:
1003 case Ctrl_H:
1004 case K_DEL:
1005 case K_KDEL:
1006 case Ctrl_W:
1007#ifdef FEAT_FKMAP
1008 if (cmd_fkmap && c == K_BS)
1009 c = K_DEL;
1010#endif
1011 if (c == K_KDEL)
1012 c = K_DEL;
1013
1014 /*
1015 * delete current character is the same as backspace on next
1016 * character, except at end of line
1017 */
1018 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1019 ++ccline.cmdpos;
1020#ifdef FEAT_MBYTE
1021 if (has_mbyte && c == K_DEL)
1022 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1023 ccline.cmdbuff + ccline.cmdpos);
1024#endif
1025 if (ccline.cmdpos > 0)
1026 {
1027 char_u *p;
1028
1029 j = ccline.cmdpos;
1030 p = ccline.cmdbuff + j;
1031#ifdef FEAT_MBYTE
1032 if (has_mbyte)
1033 {
1034 p = mb_prevptr(ccline.cmdbuff, p);
1035 if (c == Ctrl_W)
1036 {
1037 while (p > ccline.cmdbuff && vim_isspace(*p))
1038 p = mb_prevptr(ccline.cmdbuff, p);
1039 i = mb_get_class(p);
1040 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1041 p = mb_prevptr(ccline.cmdbuff, p);
1042 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001043 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045 }
1046 else
1047#endif
1048 if (c == Ctrl_W)
1049 {
1050 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1051 --p;
1052 i = vim_iswordc(p[-1]);
1053 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1054 && vim_iswordc(p[-1]) == i)
1055 --p;
1056 }
1057 else
1058 --p;
1059 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1060 ccline.cmdlen -= j - ccline.cmdpos;
1061 i = ccline.cmdpos;
1062 while (i < ccline.cmdlen)
1063 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1064
1065 /* Truncate at the end, required for multi-byte chars. */
1066 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001067#ifdef FEAT_SEARCH_EXTRA
1068 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001069 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001070 search_start = save_cursor;
1071 /* save view settings, so that the screen
1072 * won't be restored at the wrong position */
1073 old_curswant = init_curswant;
1074 old_leftcol = init_leftcol;
1075 old_topline = init_topline;
1076# ifdef FEAT_DIFF
1077 old_topfill = init_topfill;
1078# endif
1079 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001080 }
1081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 redrawcmd();
1083 }
1084 else if (ccline.cmdlen == 0 && c != Ctrl_W
1085 && ccline.cmdprompt == NULL && indent == 0)
1086 {
1087 /* In ex and debug mode it doesn't make sense to return. */
1088 if (exmode_active
1089#ifdef FEAT_EVAL
1090 || ccline.cmdfirstc == '>'
1091#endif
1092 )
1093 goto cmdline_not_changed;
1094
Bram Moolenaard23a8232018-02-10 18:45:26 +01001095 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (!cmd_silent)
1097 {
1098#ifdef FEAT_RIGHTLEFT
1099 if (cmdmsg_rl)
1100 msg_col = Columns;
1101 else
1102#endif
1103 msg_col = 0;
1104 msg_putchar(' '); /* delete ':' */
1105 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001106#ifdef FEAT_SEARCH_EXTRA
1107 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001108 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 redraw_cmdline = TRUE;
1111 goto returncmd; /* back to cmd mode */
1112 }
1113 goto cmdline_changed;
1114
1115 case K_INS:
1116 case K_KINS:
1117#ifdef FEAT_FKMAP
1118 /* if Farsi mode set, we are in reverse insert mode -
1119 Do not change the mode */
1120 if (cmd_fkmap)
1121 beep_flush();
1122 else
1123#endif
1124 ccline.overstrike = !ccline.overstrike;
1125#ifdef CURSOR_SHAPE
1126 ui_cursor_shape(); /* may show different cursor shape */
1127#endif
1128 goto cmdline_not_changed;
1129
1130 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001131 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
1133 /* ":lmap" mappings exists, toggle use of mappings. */
1134 State ^= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01001135#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 im_set_active(FALSE); /* Disable input method */
1137#endif
1138 if (b_im_ptr != NULL)
1139 {
1140 if (State & LANGMAP)
1141 *b_im_ptr = B_IMODE_LMAP;
1142 else
1143 *b_im_ptr = B_IMODE_NONE;
1144 }
1145 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01001146#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 else
1148 {
1149 /* There are no ":lmap" mappings, toggle IM. When
1150 * 'imdisable' is set don't try getting the status, it's
1151 * always off. */
1152 if ((p_imdisable && b_im_ptr != NULL)
1153 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1154 {
1155 im_set_active(FALSE); /* Disable input method */
1156 if (b_im_ptr != NULL)
1157 *b_im_ptr = B_IMODE_NONE;
1158 }
1159 else
1160 {
1161 im_set_active(TRUE); /* Enable input method */
1162 if (b_im_ptr != NULL)
1163 *b_im_ptr = B_IMODE_IM;
1164 }
1165 }
1166#endif
1167 if (b_im_ptr != NULL)
1168 {
1169 if (b_im_ptr == &curbuf->b_p_iminsert)
1170 set_iminsert_global();
1171 else
1172 set_imsearch_global();
1173 }
1174#ifdef CURSOR_SHAPE
1175 ui_cursor_shape(); /* may show different cursor shape */
1176#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001177#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 /* Show/unshow value of 'keymap' in status lines later. */
1179 status_redraw_curbuf();
1180#endif
1181 goto cmdline_not_changed;
1182
1183/* case '@': only in very old vi */
1184 case Ctrl_U:
1185 /* delete all characters left of the cursor */
1186 j = ccline.cmdpos;
1187 ccline.cmdlen -= j;
1188 i = ccline.cmdpos = 0;
1189 while (i < ccline.cmdlen)
1190 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1191 /* Truncate at the end, required for multi-byte chars. */
1192 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001193#ifdef FEAT_SEARCH_EXTRA
1194 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001195 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 redrawcmd();
1198 goto cmdline_changed;
1199
1200#ifdef FEAT_CLIPBOARD
1201 case Ctrl_Y:
1202 /* Copy the modeless selection, if there is one. */
1203 if (clip_star.state != SELECT_CLEARED)
1204 {
1205 if (clip_star.state == SELECT_DONE)
1206 clip_copy_modeless_selection(TRUE);
1207 goto cmdline_not_changed;
1208 }
1209 break;
1210#endif
1211
1212 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1213 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001214 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001215 * ":normal" runs out of characters. */
1216 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001217 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 goto cmdline_not_changed;
1219
1220 gotesc = TRUE; /* will free ccline.cmdbuff after
1221 putting it in history */
1222 goto returncmd; /* back to cmd mode */
1223
1224 case Ctrl_R: /* insert register */
1225#ifdef USE_ON_FLY_SCROLL
1226 dont_scroll = TRUE; /* disallow scrolling here */
1227#endif
1228 putcmdline('"', TRUE);
1229 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001230 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (i == Ctrl_O)
1232 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1233 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001234 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001235 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 --no_mapping;
1237#ifdef FEAT_EVAL
1238 /*
1239 * Insert the result of an expression.
1240 * Need to save the current command line, to be able to enter
1241 * a new one...
1242 */
1243 new_cmdpos = -1;
1244 if (c == '=')
1245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1247 {
1248 beep_flush();
1249 c = ESC;
1250 }
1251 else
1252 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001253 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001255 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
1257 }
1258#endif
1259 if (c != ESC) /* use ESC to cancel inserting register */
1260 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001261 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001262
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001263#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001264 /* When there was a serious error abort getting the
1265 * command line. */
1266 if (aborting())
1267 {
1268 gotesc = TRUE; /* will free ccline.cmdbuff after
1269 putting it in history */
1270 goto returncmd; /* back to cmd mode */
1271 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 KeyTyped = FALSE; /* Don't do p_wc completion. */
1274#ifdef FEAT_EVAL
1275 if (new_cmdpos >= 0)
1276 {
1277 /* set_cmdline_pos() was used */
1278 if (new_cmdpos > ccline.cmdlen)
1279 ccline.cmdpos = ccline.cmdlen;
1280 else
1281 ccline.cmdpos = new_cmdpos;
1282 }
1283#endif
1284 }
1285 redrawcmd();
1286 goto cmdline_changed;
1287
1288 case Ctrl_D:
1289 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1290 break; /* Use ^D as normal char instead */
1291
1292 redrawcmd();
1293 continue; /* don't do incremental search now */
1294
1295 case K_RIGHT:
1296 case K_S_RIGHT:
1297 case K_C_RIGHT:
1298 do
1299 {
1300 if (ccline.cmdpos >= ccline.cmdlen)
1301 break;
1302 i = cmdline_charsize(ccline.cmdpos);
1303 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1304 break;
1305 ccline.cmdspos += i;
1306#ifdef FEAT_MBYTE
1307 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001308 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 + ccline.cmdpos);
1310 else
1311#endif
1312 ++ccline.cmdpos;
1313 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001314 while ((c == K_S_RIGHT || c == K_C_RIGHT
1315 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1317#ifdef FEAT_MBYTE
1318 if (has_mbyte)
1319 set_cmdspos_cursor();
1320#endif
1321 goto cmdline_not_changed;
1322
1323 case K_LEFT:
1324 case K_S_LEFT:
1325 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001326 if (ccline.cmdpos == 0)
1327 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 do
1329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 --ccline.cmdpos;
1331#ifdef FEAT_MBYTE
1332 if (has_mbyte) /* move to first byte of char */
1333 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1334 ccline.cmdbuff + ccline.cmdpos);
1335#endif
1336 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1337 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001338 while (ccline.cmdpos > 0
1339 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001340 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1342#ifdef FEAT_MBYTE
1343 if (has_mbyte)
1344 set_cmdspos_cursor();
1345#endif
1346 goto cmdline_not_changed;
1347
1348 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001349 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001350 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaar4770d092006-01-12 23:22:24 +00001352#ifdef FEAT_GUI_W32
1353 /* On Win32 ignore <M-F4>, we get it when closing the window was
1354 * cancelled. */
1355 case K_F4:
1356 if (mod_mask == MOD_MASK_ALT)
1357 {
1358 redrawcmd(); /* somehow the cmdline is cleared */
1359 goto cmdline_not_changed;
1360 }
1361 break;
1362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#ifdef FEAT_MOUSE
1365 case K_MIDDLEDRAG:
1366 case K_MIDDLERELEASE:
1367 goto cmdline_not_changed; /* Ignore mouse */
1368
1369 case K_MIDDLEMOUSE:
1370# ifdef FEAT_GUI
1371 /* When GUI is active, also paste when 'mouse' is empty */
1372 if (!gui.in_use)
1373# endif
1374 if (!mouse_has(MOUSE_COMMAND))
1375 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001376# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001378 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001380# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001381 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 redrawcmd();
1383 goto cmdline_changed;
1384
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001385# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001387 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 redrawcmd();
1389 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 case K_LEFTDRAG:
1393 case K_LEFTRELEASE:
1394 case K_RIGHTDRAG:
1395 case K_RIGHTRELEASE:
1396 /* Ignore drag and release events when the button-down wasn't
1397 * seen before. */
1398 if (ignore_drag_release)
1399 goto cmdline_not_changed;
1400 /* FALLTHROUGH */
1401 case K_LEFTMOUSE:
1402 case K_RIGHTMOUSE:
1403 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1404 ignore_drag_release = TRUE;
1405 else
1406 ignore_drag_release = FALSE;
1407# ifdef FEAT_GUI
1408 /* When GUI is active, also move when 'mouse' is empty */
1409 if (!gui.in_use)
1410# endif
1411 if (!mouse_has(MOUSE_COMMAND))
1412 goto cmdline_not_changed; /* Ignore mouse */
1413# ifdef FEAT_CLIPBOARD
1414 if (mouse_row < cmdline_row && clip_star.available)
1415 {
1416 int button, is_click, is_drag;
1417
1418 /*
1419 * Handle modeless selection.
1420 */
1421 button = get_mouse_button(KEY2TERMCAP1(c),
1422 &is_click, &is_drag);
1423 if (mouse_model_popup() && button == MOUSE_LEFT
1424 && (mod_mask & MOD_MASK_SHIFT))
1425 {
1426 /* Translate shift-left to right button. */
1427 button = MOUSE_RIGHT;
1428 mod_mask &= ~MOD_MASK_SHIFT;
1429 }
1430 clip_modeless(button, is_click, is_drag);
1431 goto cmdline_not_changed;
1432 }
1433# endif
1434
1435 set_cmdspos();
1436 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1437 ++ccline.cmdpos)
1438 {
1439 i = cmdline_charsize(ccline.cmdpos);
1440 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1441 && mouse_col < ccline.cmdspos % Columns + i)
1442 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001443# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (has_mbyte)
1445 {
1446 /* Count ">" for double-wide char that doesn't fit. */
1447 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001448 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 + ccline.cmdpos) - 1;
1450 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 ccline.cmdspos += i;
1453 }
1454 goto cmdline_not_changed;
1455
1456 /* Mouse scroll wheel: ignored here */
1457 case K_MOUSEDOWN:
1458 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001459 case K_MOUSELEFT:
1460 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 /* Alternate buttons ignored here */
1462 case K_X1MOUSE:
1463 case K_X1DRAG:
1464 case K_X1RELEASE:
1465 case K_X2MOUSE:
1466 case K_X2DRAG:
1467 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001468 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 goto cmdline_not_changed;
1470
1471#endif /* FEAT_MOUSE */
1472
1473#ifdef FEAT_GUI
1474 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1475 case K_LEFTRELEASE_NM:
1476 goto cmdline_not_changed;
1477
1478 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001479 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 gui_do_scroll();
1482 redrawcmd();
1483 }
1484 goto cmdline_not_changed;
1485
1486 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001487 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001489 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 redrawcmd();
1491 }
1492 goto cmdline_not_changed;
1493#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001494#ifdef FEAT_GUI_TABLINE
1495 case K_TABLINE:
1496 case K_TABMENU:
1497 /* Don't want to change any tabs here. Make sure the same tab
1498 * is still selected. */
1499 if (gui_use_tabline())
1500 gui_mch_set_curtab(tabpage_index(curtab));
1501 goto cmdline_not_changed;
1502#endif
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 case K_SELECT: /* end of Select mode mapping - ignore */
1505 goto cmdline_not_changed;
1506
1507 case Ctrl_B: /* begin of command line */
1508 case K_HOME:
1509 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 case K_S_HOME:
1511 case K_C_HOME:
1512 ccline.cmdpos = 0;
1513 set_cmdspos();
1514 goto cmdline_not_changed;
1515
1516 case Ctrl_E: /* end of command line */
1517 case K_END:
1518 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 case K_S_END:
1520 case K_C_END:
1521 ccline.cmdpos = ccline.cmdlen;
1522 set_cmdspos_cursor();
1523 goto cmdline_not_changed;
1524
1525 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001526 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 break;
1528 goto cmdline_changed;
1529
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001530 case Ctrl_L:
1531#ifdef FEAT_SEARCH_EXTRA
1532 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1533 {
1534 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001535 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001536 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001537 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001538 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001539 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001540 c = gchar_cursor();
1541 /* If 'ignorecase' and 'smartcase' are set and the
1542 * command line has no uppercase characters, convert
1543 * the character to lowercase */
1544 if (p_ic && p_scs
1545 && !pat_has_uppercase(ccline.cmdbuff))
1546 c = MB_TOLOWER(c);
1547 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001548 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001549 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001550 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001551 != NULL)
1552 {
1553 /* put a backslash before special
1554 * characters */
1555 stuffcharReadbuff(c);
1556 c = '\\';
1557 }
1558 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001559 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001560 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001561 }
1562 goto cmdline_not_changed;
1563 }
1564#endif
1565
1566 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001567 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 break;
1569 goto cmdline_changed;
1570
1571 case Ctrl_N: /* next match */
1572 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001573 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001575 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1576 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001578 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001581 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 case K_UP:
1583 case K_DOWN:
1584 case K_S_UP:
1585 case K_S_DOWN:
1586 case K_PAGEUP:
1587 case K_KPAGEUP:
1588 case K_PAGEDOWN:
1589 case K_KPAGEDOWN:
1590 if (hislen == 0 || firstc == NUL) /* no history */
1591 goto cmdline_not_changed;
1592
1593 i = hiscnt;
1594
1595 /* save current command string so it can be restored later */
1596 if (lookfor == NULL)
1597 {
1598 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1599 goto cmdline_not_changed;
1600 lookfor[ccline.cmdpos] = NUL;
1601 }
1602
1603 j = (int)STRLEN(lookfor);
1604 for (;;)
1605 {
1606 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001607 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001608 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 if (hiscnt == hislen) /* first time */
1611 hiscnt = hisidx[histype];
1612 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1613 hiscnt = hislen - 1;
1614 else if (hiscnt != hisidx[histype] + 1)
1615 --hiscnt;
1616 else /* at top of list */
1617 {
1618 hiscnt = i;
1619 break;
1620 }
1621 }
1622 else /* one step forwards */
1623 {
1624 /* on last entry, clear the line */
1625 if (hiscnt == hisidx[histype])
1626 {
1627 hiscnt = hislen;
1628 break;
1629 }
1630
1631 /* not on a history line, nothing to do */
1632 if (hiscnt == hislen)
1633 break;
1634 if (hiscnt == hislen - 1) /* wrap around */
1635 hiscnt = 0;
1636 else
1637 ++hiscnt;
1638 }
1639 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1640 {
1641 hiscnt = i;
1642 break;
1643 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001644 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001645 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 || STRNCMP(history[histype][hiscnt].hisstr,
1647 lookfor, (size_t)j) == 0)
1648 break;
1649 }
1650
1651 if (hiscnt != i) /* jumped to other entry */
1652 {
1653 char_u *p;
1654 int len;
1655 int old_firstc;
1656
1657 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001658 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (hiscnt == hislen)
1660 p = lookfor; /* back to the old one */
1661 else
1662 p = history[histype][hiscnt].hisstr;
1663
1664 if (histype == HIST_SEARCH
1665 && p != lookfor
1666 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1667 {
1668 /* Correct for the separator character used when
1669 * adding the history entry vs the one used now.
1670 * First loop: count length.
1671 * Second loop: copy the characters. */
1672 for (i = 0; i <= 1; ++i)
1673 {
1674 len = 0;
1675 for (j = 0; p[j] != NUL; ++j)
1676 {
1677 /* Replace old sep with new sep, unless it is
1678 * escaped. */
1679 if (p[j] == old_firstc
1680 && (j == 0 || p[j - 1] != '\\'))
1681 {
1682 if (i > 0)
1683 ccline.cmdbuff[len] = firstc;
1684 }
1685 else
1686 {
1687 /* Escape new sep, unless it is already
1688 * escaped. */
1689 if (p[j] == firstc
1690 && (j == 0 || p[j - 1] != '\\'))
1691 {
1692 if (i > 0)
1693 ccline.cmdbuff[len] = '\\';
1694 ++len;
1695 }
1696 if (i > 0)
1697 ccline.cmdbuff[len] = p[j];
1698 }
1699 ++len;
1700 }
1701 if (i == 0)
1702 {
1703 alloc_cmdbuff(len);
1704 if (ccline.cmdbuff == NULL)
1705 goto returncmd;
1706 }
1707 }
1708 ccline.cmdbuff[len] = NUL;
1709 }
1710 else
1711 {
1712 alloc_cmdbuff((int)STRLEN(p));
1713 if (ccline.cmdbuff == NULL)
1714 goto returncmd;
1715 STRCPY(ccline.cmdbuff, p);
1716 }
1717
1718 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1719 redrawcmd();
1720 goto cmdline_changed;
1721 }
1722 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001723#endif
1724 goto cmdline_not_changed;
1725
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001726#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001727 case Ctrl_G: /* next match */
1728 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001729 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1730 {
1731 pos_T t;
Bram Moolenaard0480092017-11-16 22:20:39 +01001732 char_u *pat;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001733 int search_flags = SEARCH_NOOF;
Bram Moolenaar11956692016-08-27 16:26:56 +02001734
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001735 if (ccline.cmdlen == 0)
1736 goto cmdline_not_changed;
1737
Bram Moolenaard0480092017-11-16 22:20:39 +01001738 if (firstc == ccline.cmdbuff[0])
1739 pat = last_search_pattern();
1740 else
1741 pat = ccline.cmdbuff;
1742
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001743 save_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001744 cursor_off();
1745 out_flush();
1746 if (c == Ctrl_G)
1747 {
1748 t = match_end;
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001749 if (LT_POS(match_start, match_end))
1750 /* start searching at the end of the match
1751 * not at the beginning of the next column */
1752 (void)decl(&t);
Bram Moolenaar11956692016-08-27 16:26:56 +02001753 search_flags += SEARCH_COL;
1754 }
1755 else
1756 t = match_start;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001757 if (!p_hls)
1758 search_flags += SEARCH_KEEP;
Bram Moolenaar11956692016-08-27 16:26:56 +02001759 ++emsg_off;
1760 i = searchit(curwin, curbuf, &t,
1761 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaard0480092017-11-16 22:20:39 +01001762 pat, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001763 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001764 --emsg_off;
1765 if (i)
1766 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001767 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001768 match_end = t;
1769 match_start = t;
1770 if (c == Ctrl_T && firstc == '/')
1771 {
1772 /* move just before the current match, so that
1773 * when nv_search finishes the cursor will be
1774 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001775 search_start = t;
1776 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001777 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001778 else if (c == Ctrl_G && firstc == '?')
1779 {
1780 /* move just after the current match, so that
1781 * when nv_search finishes the cursor will be
1782 * put back on the match */
1783 search_start = t;
1784 (void)incl(&search_start);
1785 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001786 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001787 {
1788 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001789 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001790 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001791 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001792 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001793 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001794 }
1795
1796 set_search_match(&match_end);
1797 curwin->w_cursor = match_start;
1798 changed_cline_bef_curs();
1799 update_topline();
1800 validate_cursor();
1801 highlight_match = TRUE;
1802 old_curswant = curwin->w_curswant;
1803 old_leftcol = curwin->w_leftcol;
1804 old_topline = curwin->w_topline;
1805# ifdef FEAT_DIFF
1806 old_topfill = curwin->w_topfill;
1807# endif
1808 old_botline = curwin->w_botline;
1809 update_screen(NOT_VALID);
1810 redrawcmdline();
1811 }
1812 else
1813 vim_beep(BO_ERROR);
Bram Moolenaara1d5c152017-12-16 19:05:22 +01001814 restore_last_search_pattern();
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001815 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001816 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818#endif
1819
1820 case Ctrl_V:
1821 case Ctrl_Q:
1822#ifdef FEAT_MOUSE
1823 ignore_drag_release = TRUE;
1824#endif
1825 putcmdline('^', TRUE);
1826 c = get_literal(); /* get next (two) character(s) */
1827 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001828 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829#ifdef FEAT_MBYTE
1830 /* may need to remove ^ when composing char was typed */
1831 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1832 {
1833 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1834 msg_putchar(' ');
1835 cursorcmd();
1836 }
1837#endif
1838 break;
1839
1840#ifdef FEAT_DIGRAPHS
1841 case Ctrl_K:
1842#ifdef FEAT_MOUSE
1843 ignore_drag_release = TRUE;
1844#endif
1845 putcmdline('?', TRUE);
1846#ifdef USE_ON_FLY_SCROLL
1847 dont_scroll = TRUE; /* disallow scrolling here */
1848#endif
1849 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001850 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (c != NUL)
1852 break;
1853
1854 redrawcmd();
1855 goto cmdline_not_changed;
1856#endif /* FEAT_DIGRAPHS */
1857
1858#ifdef FEAT_RIGHTLEFT
1859 case Ctrl__: /* CTRL-_: switch language mode */
1860 if (!p_ari)
1861 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001862# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (p_altkeymap)
1864 {
1865 cmd_fkmap = !cmd_fkmap;
1866 if (cmd_fkmap) /* in Farsi always in Insert mode */
1867 ccline.overstrike = FALSE;
1868 }
1869 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 cmd_hkmap = !cmd_hkmap;
1872 goto cmdline_not_changed;
1873#endif
1874
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001875 case K_PS:
1876 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1877 goto cmdline_changed;
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 default:
1880#ifdef UNIX
1881 if (c == intr_char)
1882 {
1883 gotesc = TRUE; /* will free ccline.cmdbuff after
1884 putting it in history */
1885 goto returncmd; /* back to Normal mode */
1886 }
1887#endif
1888 /*
1889 * Normal character with no special meaning. Just set mod_mask
1890 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1891 * the string <S-Space>. This should only happen after ^V.
1892 */
1893 if (!IS_SPECIAL(c))
1894 mod_mask = 0x0;
1895 break;
1896 }
1897 /*
1898 * End of switch on command line character.
1899 * We come here if we have a normal character.
1900 */
1901
Bram Moolenaarede3e632013-06-23 16:16:19 +02001902 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903#ifdef FEAT_MBYTE
1904 /* Add ABBR_OFF for characters above 0x100, this is
1905 * what check_abbr() expects. */
1906 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1907#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001908 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 goto cmdline_changed;
1910
1911 /*
1912 * put the character in the command line
1913 */
1914 if (IS_SPECIAL(c) || mod_mask != 0)
1915 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1916 else
1917 {
1918#ifdef FEAT_MBYTE
1919 if (has_mbyte)
1920 {
1921 j = (*mb_char2bytes)(c, IObuff);
1922 IObuff[j] = NUL; /* exclude composing chars */
1923 put_on_cmdline(IObuff, j, TRUE);
1924 }
1925 else
1926#endif
1927 {
1928 IObuff[0] = c;
1929 put_on_cmdline(IObuff, 1, TRUE);
1930 }
1931 }
1932 goto cmdline_changed;
1933
1934/*
1935 * This part implements incremental searches for "/" and "?"
1936 * Jump to cmdline_not_changed when a character has been read but the command
1937 * line did not change. Then we only search and redraw if something changed in
1938 * the past.
1939 * Jump to cmdline_changed when the command line did change.
1940 * (Sorry for the goto's, I know it is ugly).
1941 */
1942cmdline_not_changed:
1943#ifdef FEAT_SEARCH_EXTRA
1944 if (!incsearch_postponed)
1945 continue;
1946#endif
1947
1948cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01001949#ifdef FEAT_AUTOCMD
1950 /* Trigger CmdlineChanged autocommands. */
1951 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
1952#endif
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#ifdef FEAT_SEARCH_EXTRA
1955 /*
1956 * 'incsearch' highlighting.
1957 */
1958 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1959 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001960 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001961#ifdef FEAT_RELTIME
1962 proftime_T tm;
1963#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 /* if there is a character waiting, search and redraw later */
1966 if (char_avail())
1967 {
1968 incsearch_postponed = TRUE;
1969 continue;
1970 }
1971 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001972 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001973 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 /* If there is no command line, don't do anything */
1976 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 i = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001979 SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001980 redraw_all_later(SOME_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 else
1983 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001984 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 cursor_off(); /* so the user knows we're busy */
1986 out_flush();
1987 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001988#ifdef FEAT_RELTIME
1989 /* Set the time limit to half a second. */
1990 profile_setlimit(500L, &tm);
1991#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001992 if (!p_hls)
1993 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001995 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001996#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001997 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001998#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001999 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002000#endif
2001 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 --emsg_off;
2003 /* if interrupted while searching, behave like it failed */
2004 if (got_int)
2005 {
2006 (void)vpeekc(); /* remove <C-C> from input stream */
2007 got_int = FALSE; /* don't abandon the command line */
2008 i = 0;
2009 }
2010 else if (char_avail())
2011 /* cancelled searching because a char was typed */
2012 incsearch_postponed = TRUE;
2013 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002014 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 highlight_match = TRUE; /* highlight position */
2016 else
2017 highlight_match = FALSE; /* remove highlight */
2018
2019 /* first restore the old curwin values, so the screen is
2020 * positioned in the same way as the actual search command */
2021 curwin->w_leftcol = old_leftcol;
2022 curwin->w_topline = old_topline;
2023# ifdef FEAT_DIFF
2024 curwin->w_topfill = old_topfill;
2025# endif
2026 curwin->w_botline = old_botline;
2027 changed_cline_bef_curs();
2028 update_topline();
2029
2030 if (i != 0)
2031 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002032 pos_T save_pos = curwin->w_cursor;
2033
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002034 match_start = curwin->w_cursor;
2035 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002037 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002038 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002039 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002041 else
2042 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002043
Bram Moolenaar66216052017-12-16 16:33:44 +01002044 /* Disable 'hlsearch' highlighting if the pattern matches
2045 * everything. Avoids a flash when typing "foo\|". */
2046 if (empty_pattern(ccline.cmdbuff))
2047 SET_NO_HLSEARCH(TRUE);
2048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002050 /* May redraw the status line to show the cursor position. */
2051 if (p_ru && curwin->w_status_height > 0)
2052 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002054 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002055 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002056 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002057 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002058
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002059 /* Leave it at the end to make CTRL-R CTRL-W work. */
2060 if (i != 0)
2061 curwin->w_cursor = end_pos;
2062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 msg_starthere();
2064 redrawcmdline();
2065 did_incsearch = TRUE;
2066 }
2067#else /* FEAT_SEARCH_EXTRA */
2068 ;
2069#endif
2070
2071#ifdef FEAT_RIGHTLEFT
2072 if (cmdmsg_rl
2073# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002074 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075# endif
2076 )
2077 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002078 * right-left typing. Not efficient, but it works.
2079 * Do it only when there are no characters left to read
2080 * to avoid useless intermediate redraws. */
2081 if (vpeekc() == NUL)
2082 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083#endif
2084 }
2085
2086returncmd:
2087
2088#ifdef FEAT_RIGHTLEFT
2089 cmdmsg_rl = FALSE;
2090#endif
2091
2092#ifdef FEAT_FKMAP
2093 cmd_fkmap = 0;
2094#endif
2095
2096 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002097 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099#ifdef FEAT_SEARCH_EXTRA
2100 if (did_incsearch)
2101 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002102 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002103 curwin->w_cursor = save_cursor;
2104 else
2105 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002106 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002107 {
2108 /* put the '" mark at the original position */
2109 curwin->w_cursor = save_cursor;
2110 setpcmark();
2111 }
2112 curwin->w_cursor = search_start;
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 curwin->w_curswant = old_curswant;
2115 curwin->w_leftcol = old_leftcol;
2116 curwin->w_topline = old_topline;
2117# ifdef FEAT_DIFF
2118 curwin->w_topfill = old_topfill;
2119# endif
2120 curwin->w_botline = old_botline;
2121 highlight_match = FALSE;
2122 validate_cursor(); /* needed for TAB */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01002123 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125#endif
2126
2127 if (ccline.cmdbuff != NULL)
2128 {
2129 /*
2130 * Put line in history buffer (":" and "=" only when it was typed).
2131 */
2132#ifdef FEAT_CMDHIST
2133 if (ccline.cmdlen && firstc != NUL
2134 && (some_key_typed || histype == HIST_SEARCH))
2135 {
2136 add_to_history(histype, ccline.cmdbuff, TRUE,
2137 histype == HIST_SEARCH ? firstc : NUL);
2138 if (firstc == ':')
2139 {
2140 vim_free(new_last_cmdline);
2141 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2142 }
2143 }
2144#endif
2145
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002146 if (gotesc)
2147 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
2149
2150 /*
2151 * If the screen was shifted up, redraw the whole screen (later).
2152 * If the line is too long, clear it, so ruler and shown command do
2153 * not get printed in the middle of it.
2154 */
2155 msg_check();
2156 msg_scroll = save_msg_scroll;
2157 redir_off = FALSE;
2158
2159 /* When the command line was typed, no need for a wait-return prompt. */
2160 if (some_key_typed)
2161 need_wait_return = FALSE;
2162
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002163#ifdef FEAT_AUTOCMD
2164 /* Trigger CmdlineLeave autocommands. */
2165 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
2166#endif
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 State = save_State;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01002169#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2171 im_save_status(b_im_ptr);
2172 im_set_active(FALSE);
2173#endif
2174#ifdef FEAT_MOUSE
2175 setmouse();
2176#endif
2177#ifdef CURSOR_SHAPE
2178 ui_cursor_shape(); /* may show different cursor shape */
2179#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002180 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002182 {
2183 char_u *p = ccline.cmdbuff;
2184
2185 /* Make ccline empty, getcmdline() may try to use it. */
2186 ccline.cmdbuff = NULL;
2187 return p;
2188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189}
2190
2191#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2192/*
2193 * Get a command line with a prompt.
2194 * This is prepared to be called recursively from getcmdline() (e.g. by
2195 * f_input() when evaluating an expression from CTRL-R =).
2196 * Returns the command line in allocated memory, or NULL.
2197 */
2198 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002199getcmdline_prompt(
2200 int firstc,
2201 char_u *prompt, /* command line prompt */
2202 int attr, /* attributes for prompt */
2203 int xp_context, /* type of expansion */
2204 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 char_u *s;
2207 struct cmdline_info save_ccline;
2208 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002209 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002211 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ccline.cmdprompt = prompt;
2213 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002214# ifdef FEAT_EVAL
2215 ccline.xp_context = xp_context;
2216 ccline.xp_arg = xp_arg;
2217 ccline.input_fn = (firstc == '@');
2218# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002219 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002221 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002222 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002223 /* Restore msg_col, the prompt from input() may have changed it.
2224 * But only if called recursively and the commandline is therefore being
2225 * restored to an old one; if not, the input() prompt stays on the screen,
2226 * so we need its modified msg_col left intact. */
2227 if (ccline.cmdbuff != NULL)
2228 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 return s;
2231}
2232#endif
2233
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002234/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002235 * Return TRUE when the text must not be changed and we can't switch to
2236 * another window or buffer. Used when editing the command line, evaluating
2237 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002238 */
2239 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002240text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002241{
2242#ifdef FEAT_CMDWIN
2243 if (cmdwin_type != 0)
2244 return TRUE;
2245#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002246 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002247}
2248
2249/*
2250 * Give an error message for a command that isn't allowed while the cmdline
2251 * window is open or editing the cmdline in another way.
2252 */
2253 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002254text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002255{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002256 EMSG(_(get_text_locked_msg()));
2257}
2258
2259 char_u *
2260get_text_locked_msg(void)
2261{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002262#ifdef FEAT_CMDWIN
2263 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002264 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002265#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002266 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002267}
2268
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#if defined(FEAT_AUTOCMD) || defined(PROTO)
2270/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002271 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2272 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002273 */
2274 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002275curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276{
2277 if (curbuf_lock > 0)
2278 {
2279 EMSG(_("E788: Not allowed to edit another buffer now"));
2280 return TRUE;
2281 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002282 return allbuf_locked();
2283}
2284
2285/*
2286 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2287 * message.
2288 */
2289 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002290allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002291{
2292 if (allbuf_lock > 0)
2293 {
2294 EMSG(_("E811: Not allowed to change buffer information now"));
2295 return TRUE;
2296 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297 return FALSE;
2298}
2299#endif
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002302cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
2304#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2305 if (cmdline_star > 0) /* showing '*', always 1 position */
2306 return 1;
2307#endif
2308 return ptr2cells(ccline.cmdbuff + idx);
2309}
2310
2311/*
2312 * Compute the offset of the cursor on the command line for the prompt and
2313 * indent.
2314 */
2315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002316set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002318 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 ccline.cmdspos = 1 + ccline.cmdindent;
2320 else
2321 ccline.cmdspos = 0 + ccline.cmdindent;
2322}
2323
2324/*
2325 * Compute the screen position for the cursor on the command line.
2326 */
2327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002328set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 int i, m, c;
2331
2332 set_cmdspos();
2333 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002336 if (m < 0) /* overflow, Columns or Rows at weird value */
2337 m = MAXCOL;
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 m = MAXCOL;
2341 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2342 {
2343 c = cmdline_charsize(i);
2344#ifdef FEAT_MBYTE
2345 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2346 if (has_mbyte)
2347 correct_cmdspos(i, c);
2348#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002349 /* If the cmdline doesn't fit, show cursor on last visible char.
2350 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if ((ccline.cmdspos += c) >= m)
2352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 ccline.cmdspos -= c;
2354 break;
2355 }
2356#ifdef FEAT_MBYTE
2357 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002358 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#endif
2360 }
2361}
2362
2363#ifdef FEAT_MBYTE
2364/*
2365 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2366 * character that doesn't fit, so that a ">" must be displayed.
2367 */
2368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002369correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002371 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2373 && ccline.cmdspos % Columns + cells > Columns)
2374 ccline.cmdspos++;
2375}
2376#endif
2377
2378/*
2379 * Get an Ex command line for the ":" command.
2380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002382getexline(
2383 int c, /* normally ':', NUL for ":append" */
2384 void *cookie UNUSED,
2385 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
2387 /* When executing a register, remove ':' that's in front of each line. */
2388 if (exec_from_reg && vpeekc() == ':')
2389 (void)vgetc();
2390 return getcmdline(c, 1L, indent);
2391}
2392
2393/*
2394 * Get an Ex command line for Ex mode.
2395 * In Ex mode we only use the OS supplied line editing features and no
2396 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002397 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002400getexmodeline(
2401 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002403 void *cookie UNUSED,
2404 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002406 garray_T line_ga;
2407 char_u *pend;
2408 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002409 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002410 int escaped = FALSE; /* CTRL-V typed */
2411 int vcol = 0;
2412 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002413 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002414 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 /* Switch cursor on now. This avoids that it happens after the "\n", which
2417 * confuses the system function that computes tabstops. */
2418 cursor_on();
2419
2420 /* always start in column 0; write a newline if necessary */
2421 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002422 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002424 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002426 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002427 if (p_prompt)
2428 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 while (indent-- > 0)
2430 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433
2434 ga_init2(&line_ga, 1, 30);
2435
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002436 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002437 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002438 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002439 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002440 while (indent >= 8)
2441 {
2442 ga_append(&line_ga, TAB);
2443 msg_puts((char_u *)" ");
2444 indent -= 8;
2445 }
2446 while (indent-- > 0)
2447 {
2448 ga_append(&line_ga, ' ');
2449 msg_putchar(' ');
2450 }
2451 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002452 ++no_mapping;
2453 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /*
2456 * Get the line, one character at a time.
2457 */
2458 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002459 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002461 long sw;
2462 char_u *s;
2463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (ga_grow(&line_ga, 40) == FAIL)
2465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaarba748c82017-02-26 14:00:07 +01002467 /*
2468 * Get one character at a time.
2469 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002470 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002471
2472 /* Check for a ":normal" command and no more characters left. */
2473 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2474 c1 = '\n';
2475 else
2476 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002479 * Handle line editing.
2480 * Previously this was left to the system, putting the terminal in
2481 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002483 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002485 msg_putchar('\n');
2486 break;
2487 }
2488
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002489 if (c1 == K_PS)
2490 {
2491 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2492 goto redraw;
2493 }
2494
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002495 if (!escaped)
2496 {
2497 /* CR typed means "enter", which is NL */
2498 if (c1 == '\r')
2499 c1 = '\n';
2500
2501 if (c1 == BS || c1 == K_BS
2502 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002504 if (line_ga.ga_len > 0)
2505 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002506#ifdef FEAT_MBYTE
2507 if (has_mbyte)
2508 {
2509 p = (char_u *)line_ga.ga_data;
2510 p[line_ga.ga_len] = NUL;
2511 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2512 line_ga.ga_len -= len;
2513 }
2514 else
2515#endif
2516 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002517 goto redraw;
2518 }
2519 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
2521
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002522 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002524 msg_col = startcol;
2525 msg_clr_eos();
2526 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002527 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002528 }
2529
2530 if (c1 == Ctrl_T)
2531 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002532 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002533 p = (char_u *)line_ga.ga_data;
2534 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002535 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002536 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002537add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002538 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002540 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002541 p = (char_u *)line_ga.ga_data;
2542 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002543 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2544 *s = ' ';
2545 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002547redraw:
2548 /* redraw the line */
2549 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002550 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002551 p = (char_u *)line_ga.ga_data;
2552 p[line_ga.ga_len] = NUL;
2553 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002557 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002559 msg_putchar(' ');
2560 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002561 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002565 len = MB_PTR2LEN(p);
2566 msg_outtrans_len(p, len);
2567 vcol += ptr2cells(p);
2568 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002571 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002572 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002573 continue;
2574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 if (c1 == Ctrl_D)
2577 {
2578 /* Delete one shiftwidth. */
2579 p = (char_u *)line_ga.ga_data;
2580 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002582 if (prev_char == '^')
2583 ex_keep_indent = TRUE;
2584 indent = 0;
2585 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587 else
2588 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002589 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002590 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002591 if (indent > 0)
2592 {
2593 --indent;
2594 indent -= indent % get_sw_value(curbuf);
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002597 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002598 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002599 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002600 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2601 --line_ga.ga_len;
2602 }
2603 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002605
2606 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2607 {
2608 escaped = TRUE;
2609 continue;
2610 }
2611
2612 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2613 if (IS_SPECIAL(c1))
2614 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002616
2617 if (IS_SPECIAL(c1))
2618 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002619#ifdef FEAT_MBYTE
2620 if (has_mbyte)
2621 len = (*mb_char2bytes)(c1,
2622 (char_u *)line_ga.ga_data + line_ga.ga_len);
2623 else
2624#endif
2625 {
2626 len = 1;
2627 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2628 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002629 if (c1 == '\n')
2630 msg_putchar('\n');
2631 else if (c1 == TAB)
2632 {
2633 /* Don't use chartabsize(), 'ts' can be different */
2634 do
2635 {
2636 msg_putchar(' ');
2637 } while (++vcol % 8);
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002642 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002645 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 escaped = FALSE;
2647
2648 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002649 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002650
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002651 /* We are done when a NL is entered, but not when it comes after an
2652 * odd number of backslashes, that results in a NUL. */
2653 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002655 int bcount = 0;
2656
2657 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2658 ++bcount;
2659
2660 if (bcount > 0)
2661 {
2662 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2663 * "\NL", etc. */
2664 line_ga.ga_len -= (bcount + 1) / 2;
2665 pend -= (bcount + 1) / 2;
2666 pend[-1] = '\n';
2667 }
2668
2669 if ((bcount & 1) == 0)
2670 {
2671 --line_ga.ga_len;
2672 --pend;
2673 *pend = NUL;
2674 break;
2675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 }
2678
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002679 --no_mapping;
2680 --allow_keys;
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 /* make following messages go to the next line */
2683 msg_didout = FALSE;
2684 msg_col = 0;
2685 if (msg_row < Rows - 1)
2686 ++msg_row;
2687 emsg_on_display = FALSE; /* don't want ui_delay() */
2688
2689 if (got_int)
2690 ga_clear(&line_ga);
2691
2692 return (char_u *)line_ga.ga_data;
2693}
2694
Bram Moolenaare344bea2005-09-01 20:46:49 +00002695# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2696 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * Return TRUE if ccline.overstrike is on.
2699 */
2700 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002701cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 return ccline.overstrike;
2704}
2705
2706/*
2707 * Return TRUE if the cursor is at the end of the cmdline.
2708 */
2709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002710cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 return (ccline.cmdpos >= ccline.cmdlen);
2713}
2714#endif
2715
Bram Moolenaar9372a112005-12-06 19:59:18 +00002716#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717/*
2718 * Return the virtual column number at the current cursor position.
2719 * This is used by the IM code to obtain the start of the preedit string.
2720 */
2721 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002722cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2725 return MAXCOL;
2726
2727# ifdef FEAT_MBYTE
2728 if (has_mbyte)
2729 {
2730 colnr_T col;
2731 int i = 0;
2732
2733 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002734 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 return col;
2737 }
2738 else
2739# endif
2740 return ccline.cmdpos;
2741}
2742#endif
2743
2744#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2745/*
2746 * If part of the command line is an IM preedit string, redraw it with
2747 * IM feedback attributes. The cursor position is restored after drawing.
2748 */
2749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002750redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 if ((State & CMDLINE)
2753 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002754 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 && !p_imdisable
2756 && im_is_preediting())
2757 {
2758 int cmdpos = 0;
2759 int cmdspos;
2760 int old_row;
2761 int old_col;
2762 colnr_T col;
2763
2764 old_row = msg_row;
2765 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002766 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768# ifdef FEAT_MBYTE
2769 if (has_mbyte)
2770 {
2771 for (col = 0; col < preedit_start_col
2772 && cmdpos < ccline.cmdlen; ++col)
2773 {
2774 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002775 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 }
2778 else
2779# endif
2780 {
2781 cmdspos += preedit_start_col;
2782 cmdpos += preedit_start_col;
2783 }
2784
2785 msg_row = cmdline_row + (cmdspos / (int)Columns);
2786 msg_col = cmdspos % (int)Columns;
2787 if (msg_row >= Rows)
2788 msg_row = Rows - 1;
2789
2790 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2791 {
2792 int char_len;
2793 int char_attr;
2794
2795 char_attr = im_get_feedback_attr(col);
2796 if (char_attr < 0)
2797 break; /* end of preedit string */
2798
2799# ifdef FEAT_MBYTE
2800 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002801 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else
2803# endif
2804 char_len = 1;
2805
2806 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2807 cmdpos += char_len;
2808 }
2809
2810 msg_row = old_row;
2811 msg_col = old_col;
2812 }
2813}
2814#endif /* FEAT_XIM && FEAT_GUI_GTK */
2815
2816/*
2817 * Allocate a new command line buffer.
2818 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2819 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2820 */
2821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002822alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
2824 /*
2825 * give some extra space to avoid having to allocate all the time
2826 */
2827 if (len < 80)
2828 len = 100;
2829 else
2830 len += 20;
2831
2832 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2833 ccline.cmdbufflen = len;
2834}
2835
2836/*
2837 * Re-allocate the command line to length len + something extra.
2838 * return FAIL for failure, OK otherwise
2839 */
2840 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002841realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 char_u *p;
2844
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002845 if (len < ccline.cmdbufflen)
2846 return OK; /* no need to resize */
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 p = ccline.cmdbuff;
2849 alloc_cmdbuff(len); /* will get some more */
2850 if (ccline.cmdbuff == NULL) /* out of memory */
2851 {
2852 ccline.cmdbuff = p; /* keep the old one */
2853 return FAIL;
2854 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002855 /* There isn't always a NUL after the command, but it may need to be
2856 * there, thus copy up to the NUL and add a NUL. */
2857 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2858 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002860
2861 if (ccline.xpc != NULL
2862 && ccline.xpc->xp_pattern != NULL
2863 && ccline.xpc->xp_context != EXPAND_NOTHING
2864 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2865 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002866 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002867
2868 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2869 * to point into the newly allocated memory. */
2870 if (i >= 0 && i <= ccline.cmdlen)
2871 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2872 }
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 return OK;
2875}
2876
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002877#if defined(FEAT_ARABIC) || defined(PROTO)
2878static char_u *arshape_buf = NULL;
2879
2880# if defined(EXITFREE) || defined(PROTO)
2881 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002882free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002883{
2884 vim_free(arshape_buf);
2885}
2886# endif
2887#endif
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889/*
2890 * Draw part of the cmdline at the current cursor position. But draw stars
2891 * when cmdline_star is TRUE.
2892 */
2893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002894draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2897 int i;
2898
2899 if (cmdline_star > 0)
2900 for (i = 0; i < len; ++i)
2901 {
2902 msg_putchar('*');
2903# ifdef FEAT_MBYTE
2904 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002905 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906# endif
2907 }
2908 else
2909#endif
2910#ifdef FEAT_ARABIC
2911 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 static int buflen = 0;
2914 char_u *p;
2915 int j;
2916 int newlen = 0;
2917 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002918 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 int prev_c = 0;
2920 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002921 int u8c;
2922 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /*
2926 * Do arabic shaping into a temporary buffer. This is very
2927 * inefficient!
2928 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002929 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
2931 /* Re-allocate the buffer. We keep it around to avoid a lot of
2932 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002933 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002934 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 arshape_buf = alloc(buflen);
2936 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return; /* out of memory */
2938 }
2939
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002940 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2941 {
2942 /* Prepend a space to draw the leading composing char on. */
2943 arshape_buf[0] = ' ';
2944 newlen = 1;
2945 }
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 for (j = start; j < start + len; j += mb_l)
2948 {
2949 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002950 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002951 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (ARABIC_CHAR(u8c))
2953 {
2954 /* Do Arabic shaping. */
2955 if (cmdmsg_rl)
2956 {
2957 /* displaying from right to left */
2958 pc = prev_c;
2959 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002960 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (j + mb_l >= start + len)
2962 nc = NUL;
2963 else
2964 nc = utf_ptr2char(p + mb_l);
2965 }
2966 else
2967 {
2968 /* displaying from left to right */
2969 if (j + mb_l >= start + len)
2970 pc = NUL;
2971 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002972 {
2973 int pcc[MAX_MCO];
2974
2975 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002977 pc1 = pcc[0];
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 nc = prev_c;
2980 }
2981 prev_c = u8c;
2982
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002983 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002985 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002986 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002988 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2989 if (u8cc[1] != 0)
2990 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002991 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993 }
2994 else
2995 {
2996 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002997 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 newlen += mb_l;
2999 }
3000 }
3001
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003002 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 else
3005#endif
3006 msg_outtrans_len(ccline.cmdbuff + start, len);
3007}
3008
3009/*
3010 * Put a character on the command line. Shifts the following text to the
3011 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3012 * "c" must be printable (fit in one display cell)!
3013 */
3014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003015putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 if (cmd_silent)
3018 return;
3019 msg_no_more = TRUE;
3020 msg_putchar(c);
3021 if (shift)
3022 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3023 msg_no_more = FALSE;
3024 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003025 extra_char = c;
3026 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027}
3028
3029/*
3030 * Undo a putcmdline(c, FALSE).
3031 */
3032 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003033unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 if (cmd_silent)
3036 return;
3037 msg_no_more = TRUE;
3038 if (ccline.cmdlen == ccline.cmdpos)
3039 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003040#ifdef FEAT_MBYTE
3041 else if (has_mbyte)
3042 draw_cmdline(ccline.cmdpos,
3043 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 else
3046 draw_cmdline(ccline.cmdpos, 1);
3047 msg_no_more = FALSE;
3048 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003049 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050}
3051
3052/*
3053 * Put the given string, of the given length, onto the command line.
3054 * If len is -1, then STRLEN() is used to calculate the length.
3055 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3056 * part will be redrawn, otherwise it will not. If this function is called
3057 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3058 * called afterwards.
3059 */
3060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 int retval;
3064 int i;
3065 int m;
3066 int c;
3067
3068 if (len < 0)
3069 len = (int)STRLEN(str);
3070
3071 /* Check if ccline.cmdbuff needs to be longer */
3072 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003073 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else
3075 retval = OK;
3076 if (retval == OK)
3077 {
3078 if (!ccline.overstrike)
3079 {
3080 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3081 ccline.cmdbuff + ccline.cmdpos,
3082 (size_t)(ccline.cmdlen - ccline.cmdpos));
3083 ccline.cmdlen += len;
3084 }
3085 else
3086 {
3087#ifdef FEAT_MBYTE
3088 if (has_mbyte)
3089 {
3090 /* Count nr of characters in the new string. */
3091 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003092 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 ++m;
3094 /* Count nr of bytes in cmdline that are overwritten by these
3095 * characters. */
3096 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003097 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 --m;
3099 if (i < ccline.cmdlen)
3100 {
3101 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3102 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3103 ccline.cmdlen += ccline.cmdpos + len - i;
3104 }
3105 else
3106 ccline.cmdlen = ccline.cmdpos + len;
3107 }
3108 else
3109#endif
3110 if (ccline.cmdpos + len > ccline.cmdlen)
3111 ccline.cmdlen = ccline.cmdpos + len;
3112 }
3113 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3114 ccline.cmdbuff[ccline.cmdlen] = NUL;
3115
3116#ifdef FEAT_MBYTE
3117 if (enc_utf8)
3118 {
3119 /* When the inserted text starts with a composing character,
3120 * backup to the character before it. There could be two of them.
3121 */
3122 i = 0;
3123 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3124 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3125 {
3126 i = (*mb_head_off)(ccline.cmdbuff,
3127 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3128 ccline.cmdpos -= i;
3129 len += i;
3130 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3131 }
3132# ifdef FEAT_ARABIC
3133 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3134 {
3135 /* Check the previous character for Arabic combining pair. */
3136 i = (*mb_head_off)(ccline.cmdbuff,
3137 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3138 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3139 + ccline.cmdpos - i), c))
3140 {
3141 ccline.cmdpos -= i;
3142 len += i;
3143 }
3144 else
3145 i = 0;
3146 }
3147# endif
3148 if (i != 0)
3149 {
3150 /* Also backup the cursor position. */
3151 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3152 ccline.cmdspos -= i;
3153 msg_col -= i;
3154 if (msg_col < 0)
3155 {
3156 msg_col += Columns;
3157 --msg_row;
3158 }
3159 }
3160 }
3161#endif
3162
3163 if (redraw && !cmd_silent)
3164 {
3165 msg_no_more = TRUE;
3166 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003167 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3169 /* Avoid clearing the rest of the line too often. */
3170 if (cmdline_row != i || ccline.overstrike)
3171 msg_clr_eos();
3172 msg_no_more = FALSE;
3173 }
3174#ifdef FEAT_FKMAP
3175 /*
3176 * If we are in Farsi command mode, the character input must be in
3177 * Insert mode. So do not advance the cmdpos.
3178 */
3179 if (!cmd_fkmap)
3180#endif
3181 {
3182 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003185 if (m < 0) /* overflow, Columns or Rows at weird value */
3186 m = MAXCOL;
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 else
3189 m = MAXCOL;
3190 for (i = 0; i < len; ++i)
3191 {
3192 c = cmdline_charsize(ccline.cmdpos);
3193#ifdef FEAT_MBYTE
3194 /* count ">" for a double-wide char that doesn't fit. */
3195 if (has_mbyte)
3196 correct_cmdspos(ccline.cmdpos, c);
3197#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003198 /* Stop cursor at the end of the screen, but do increment the
3199 * insert position, so that entering a very long command
3200 * works, even though you can't see it. */
3201 if (ccline.cmdspos + c < m)
3202 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#ifdef FEAT_MBYTE
3204 if (has_mbyte)
3205 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003206 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (c > len - i - 1)
3208 c = len - i - 1;
3209 ccline.cmdpos += c;
3210 i += c;
3211 }
3212#endif
3213 ++ccline.cmdpos;
3214 }
3215 }
3216 }
3217 if (redraw)
3218 msg_check();
3219 return retval;
3220}
3221
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003222static struct cmdline_info prev_ccline;
3223static int prev_ccline_used = FALSE;
3224
3225/*
3226 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3227 * and overwrite it. But get_cmdline_str() may need it, thus make it
3228 * available globally in prev_ccline.
3229 */
3230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003231save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003232{
3233 if (!prev_ccline_used)
3234 {
3235 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3236 prev_ccline_used = TRUE;
3237 }
3238 *ccp = prev_ccline;
3239 prev_ccline = ccline;
3240 ccline.cmdbuff = NULL;
3241 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003242 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003243}
3244
3245/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003246 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003247 */
3248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003249restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003250{
3251 ccline = prev_ccline;
3252 prev_ccline = *ccp;
3253}
3254
Bram Moolenaar5a305422006-04-28 22:38:25 +00003255#if defined(FEAT_EVAL) || defined(PROTO)
3256/*
3257 * Save the command line into allocated memory. Returns a pointer to be
3258 * passed to restore_cmdline_alloc() later.
3259 * Returns NULL when failed.
3260 */
3261 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003262save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003263{
3264 struct cmdline_info *p;
3265
3266 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3267 if (p != NULL)
3268 save_cmdline(p);
3269 return (char_u *)p;
3270}
3271
3272/*
3273 * Restore the command line from the return value of save_cmdline_alloc().
3274 */
3275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003276restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003277{
3278 if (p != NULL)
3279 {
3280 restore_cmdline((struct cmdline_info *)p);
3281 vim_free(p);
3282 }
3283}
3284#endif
3285
Bram Moolenaar8299df92004-07-10 09:47:34 +00003286/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003287 * Paste a yank register into the command line.
3288 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003289 * insert_reg() can't be used here, because special characters from the
3290 * register contents will be interpreted as commands.
3291 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003292 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003293 */
3294 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003295cmdline_paste(
3296 int regname,
3297 int literally, /* Insert text literally instead of "as typed" */
3298 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003299{
3300 long i;
3301 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003302 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003303 int allocated;
3304 struct cmdline_info save_ccline;
3305
3306 /* check for valid regname; also accept special characters for CTRL-R in
3307 * the command line */
3308 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3309 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3310 return FAIL;
3311
3312 /* A register containing CTRL-R can cause an endless loop. Allow using
3313 * CTRL-C to break the loop. */
3314 line_breakcheck();
3315 if (got_int)
3316 return FAIL;
3317
3318#ifdef FEAT_CLIPBOARD
3319 regname = may_get_selection(regname);
3320#endif
3321
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003322 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003323 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003324 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003325 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003326 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003327 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003328 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003329
3330 if (i)
3331 {
3332 /* Got the value of a special register in "arg". */
3333 if (arg == NULL)
3334 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003335
3336 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3337 * part of the word. */
3338 p = arg;
3339 if (p_is && regname == Ctrl_W)
3340 {
3341 char_u *w;
3342 int len;
3343
3344 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003345 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003346 {
3347#ifdef FEAT_MBYTE
3348 if (has_mbyte)
3349 {
3350 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3351 if (!vim_iswordc(mb_ptr2char(w - len)))
3352 break;
3353 w -= len;
3354 }
3355 else
3356#endif
3357 {
3358 if (!vim_iswordc(w[-1]))
3359 break;
3360 --w;
3361 }
3362 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003363 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003364 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3365 p += len;
3366 }
3367
3368 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003369 if (allocated)
3370 vim_free(arg);
3371 return OK;
3372 }
3373
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003374 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003375}
3376
3377/*
3378 * Put a string on the command line.
3379 * When "literally" is TRUE, insert literally.
3380 * When "literally" is FALSE, insert as typed, but don't leave the command
3381 * line.
3382 */
3383 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003384cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003385{
3386 int c, cv;
3387
3388 if (literally)
3389 put_on_cmdline(s, -1, TRUE);
3390 else
3391 while (*s != NUL)
3392 {
3393 cv = *s;
3394 if (cv == Ctrl_V && s[1])
3395 ++s;
3396#ifdef FEAT_MBYTE
3397 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003398 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003399 else
3400#endif
3401 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003402 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3403 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003404#ifdef UNIX
3405 || c == intr_char
3406#endif
3407 || (c == Ctrl_BSL && *s == Ctrl_N))
3408 stuffcharReadbuff(Ctrl_V);
3409 stuffcharReadbuff(c);
3410 }
3411}
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#ifdef FEAT_WILDMENU
3414/*
3415 * Delete characters on the command line, from "from" to the current
3416 * position.
3417 */
3418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003419cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3422 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3423 ccline.cmdlen -= ccline.cmdpos - from;
3424 ccline.cmdpos = from;
3425}
3426#endif
3427
3428/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003429 * This function is called when the screen size changes and with incremental
3430 * search and in other situations where the command line may have been
3431 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003434redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003436 redrawcmdline_ex(TRUE);
3437}
3438
3439 void
3440redrawcmdline_ex(int do_compute_cmdrow)
3441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (cmd_silent)
3443 return;
3444 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003445 if (do_compute_cmdrow)
3446 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 redrawcmd();
3448 cursorcmd();
3449}
3450
3451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 int i;
3455
3456 if (cmd_silent)
3457 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003458 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 msg_putchar(ccline.cmdfirstc);
3460 if (ccline.cmdprompt != NULL)
3461 {
3462 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3463 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3464 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003465 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 --ccline.cmdindent;
3467 }
3468 else
3469 for (i = ccline.cmdindent; i > 0; --i)
3470 msg_putchar(' ');
3471}
3472
3473/*
3474 * Redraw what is currently on the command line.
3475 */
3476 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003477redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 if (cmd_silent)
3480 return;
3481
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003482 /* when 'incsearch' is set there may be no command line while redrawing */
3483 if (ccline.cmdbuff == NULL)
3484 {
3485 windgoto(cmdline_row, 0);
3486 msg_clr_eos();
3487 return;
3488 }
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 msg_start();
3491 redrawcmdprompt();
3492
3493 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3494 msg_no_more = TRUE;
3495 draw_cmdline(0, ccline.cmdlen);
3496 msg_clr_eos();
3497 msg_no_more = FALSE;
3498
3499 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003500 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003501 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 /*
3504 * An emsg() before may have set msg_scroll. This is used in normal mode,
3505 * in cmdline mode we can reset them now.
3506 */
3507 msg_scroll = FALSE; /* next message overwrites cmdline */
3508
3509 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3510 * in cmdline mode */
3511 skip_redraw = FALSE;
3512}
3513
3514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003515compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003517 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 cmdline_row = Rows - 1;
3519 else
3520 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003521 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 if (cmd_silent)
3528 return;
3529
3530#ifdef FEAT_RIGHTLEFT
3531 if (cmdmsg_rl)
3532 {
3533 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3534 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3535 if (msg_row <= 0)
3536 msg_row = Rows - 1;
3537 }
3538 else
3539#endif
3540 {
3541 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3542 msg_col = ccline.cmdspos % (int)Columns;
3543 if (msg_row >= Rows)
3544 msg_row = Rows - 1;
3545 }
3546
3547 windgoto(msg_row, msg_col);
3548#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003549 if (p_imst == IM_ON_THE_SPOT)
3550 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#endif
3552#ifdef MCH_CURSOR_SHAPE
3553 mch_update_cursor();
3554#endif
3555}
3556
3557 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003558gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560 msg_start();
3561#ifdef FEAT_RIGHTLEFT
3562 if (cmdmsg_rl)
3563 msg_col = Columns - 1;
3564 else
3565#endif
3566 msg_col = 0; /* always start in column 0 */
3567 if (clr) /* clear the bottom line(s) */
3568 msg_clr_eos(); /* will reset clear_cmdline */
3569 windgoto(cmdline_row, 0);
3570}
3571
3572/*
3573 * Check the word in front of the cursor for an abbreviation.
3574 * Called when the non-id character "c" has been entered.
3575 * When an abbreviation is recognized it is removed from the text with
3576 * backspaces and the replacement string is inserted, followed by "c".
3577 */
3578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003579ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
3581 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3582 return FALSE;
3583
3584 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3585}
3586
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003587#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3588 static int
3589#ifdef __BORLANDC__
3590_RTLENTRYF
3591#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003592sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003593{
3594 char_u *p1 = *(char_u **)s1;
3595 char_u *p2 = *(char_u **)s2;
3596
3597 if (*p1 != '<' && *p2 == '<') return -1;
3598 if (*p1 == '<' && *p2 != '<') return 1;
3599 return STRCMP(p1, p2);
3600}
3601#endif
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603/*
3604 * Return FAIL if this is not an appropriate context in which to do
3605 * completion of anything, return OK if it is (even if there are no matches).
3606 * For the caller, this means that the character is just passed through like a
3607 * normal character (instead of being expanded). This allows :s/^I^D etc.
3608 */
3609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003610nextwild(
3611 expand_T *xp,
3612 int type,
3613 int options, /* extra options for ExpandOne() */
3614 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 int i, j;
3617 char_u *p1;
3618 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 int difflen;
3620 int v;
3621
3622 if (xp->xp_numfiles == -1)
3623 {
3624 set_expand_context(xp);
3625 cmd_showtail = expand_showtail(xp);
3626 }
3627
3628 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3629 {
3630 beep_flush();
3631 return OK; /* Something illegal on command line */
3632 }
3633 if (xp->xp_context == EXPAND_NOTHING)
3634 {
3635 /* Caller can use the character as a normal char instead */
3636 return FAIL;
3637 }
3638
3639 MSG_PUTS("..."); /* show that we are busy */
3640 out_flush();
3641
3642 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003643 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 if (type == WILD_NEXT || type == WILD_PREV)
3646 {
3647 /*
3648 * Get next/previous match for a previous expanded pattern.
3649 */
3650 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3651 }
3652 else
3653 {
3654 /*
3655 * Translate string into pattern and expand it.
3656 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003657 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3658 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 p2 = NULL;
3660 else
3661 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003662 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003663 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3664 if (escape)
3665 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003666
3667 if (p_wic)
3668 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003669 p2 = ExpandOne(xp, p1,
3670 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003671 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003673 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (p2 != NULL && type == WILD_LONGEST)
3675 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003676 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (ccline.cmdbuff[i + j] == '*'
3678 || ccline.cmdbuff[i + j] == '?')
3679 break;
3680 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003681 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683 }
3684 }
3685
3686 if (p2 != NULL && !got_int)
3687 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003688 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003689 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003691 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 xp->xp_pattern = ccline.cmdbuff + i;
3693 }
3694 else
3695 v = OK;
3696 if (v == OK)
3697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003698 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3699 &ccline.cmdbuff[ccline.cmdpos],
3700 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3701 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 ccline.cmdlen += difflen;
3703 ccline.cmdpos += difflen;
3704 }
3705 }
3706 vim_free(p2);
3707
3708 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003709 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
3711 /* When expanding a ":map" command and no matches are found, assume that
3712 * the key is supposed to be inserted literally */
3713 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3714 return FAIL;
3715
3716 if (xp->xp_numfiles <= 0 && p2 == NULL)
3717 beep_flush();
3718 else if (xp->xp_numfiles == 1)
3719 /* free expanded pattern */
3720 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3721
3722 return OK;
3723}
3724
3725/*
3726 * Do wildcard expansion on the string 'str'.
3727 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003728 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 * Return NULL for failure.
3730 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003731 * "orig" is the originally expanded string, copied to allocated memory. It
3732 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3733 * WILD_PREV "orig" should be NULL.
3734 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003735 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3736 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 *
3738 * mode = WILD_FREE: just free previously expanded matches
3739 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3740 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3741 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3742 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3743 * mode = WILD_ALL: return all matches concatenated
3744 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003745 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 *
3747 * options = WILD_LIST_NOTFOUND: list entries without a match
3748 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3749 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3750 * options = WILD_NO_BEEP: Don't beep for multiple matches
3751 * options = WILD_ADD_SLASH: add a slash after directory names
3752 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3753 * options = WILD_SILENT: don't print warning messages
3754 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003755 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 *
3757 * The variables xp->xp_context and xp->xp_backslash must have been set!
3758 */
3759 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003760ExpandOne(
3761 expand_T *xp,
3762 char_u *str,
3763 char_u *orig, /* allocated copy of original of expanded string */
3764 int options,
3765 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
3767 char_u *ss = NULL;
3768 static int findex;
3769 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003770 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 int i;
3772 long_u len;
3773 int non_suf_match; /* number without matching suffix */
3774
3775 /*
3776 * first handle the case of using an old match
3777 */
3778 if (mode == WILD_NEXT || mode == WILD_PREV)
3779 {
3780 if (xp->xp_numfiles > 0)
3781 {
3782 if (mode == WILD_PREV)
3783 {
3784 if (findex == -1)
3785 findex = xp->xp_numfiles;
3786 --findex;
3787 }
3788 else /* mode == WILD_NEXT */
3789 ++findex;
3790
3791 /*
3792 * When wrapping around, return the original string, set findex to
3793 * -1.
3794 */
3795 if (findex < 0)
3796 {
3797 if (orig_save == NULL)
3798 findex = xp->xp_numfiles - 1;
3799 else
3800 findex = -1;
3801 }
3802 if (findex >= xp->xp_numfiles)
3803 {
3804 if (orig_save == NULL)
3805 findex = 0;
3806 else
3807 findex = -1;
3808 }
3809#ifdef FEAT_WILDMENU
3810 if (p_wmnu)
3811 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3812 findex, cmd_showtail);
3813#endif
3814 if (findex == -1)
3815 return vim_strsave(orig_save);
3816 return vim_strsave(xp->xp_files[findex]);
3817 }
3818 else
3819 return NULL;
3820 }
3821
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003822 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3824 {
3825 FreeWild(xp->xp_numfiles, xp->xp_files);
3826 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003827 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829 findex = 0;
3830
3831 if (mode == WILD_FREE) /* only release file name */
3832 return NULL;
3833
3834 if (xp->xp_numfiles == -1)
3835 {
3836 vim_free(orig_save);
3837 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003838 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
3840 /*
3841 * Do the expansion.
3842 */
3843 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3844 options) == FAIL)
3845 {
3846#ifdef FNAME_ILLEGAL
3847 /* Illegal file name has been silently skipped. But when there
3848 * are wildcards, the real problem is that there was no match,
3849 * causing the pattern to be added, which has illegal characters.
3850 */
3851 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3852 EMSG2(_(e_nomatch2), str);
3853#endif
3854 }
3855 else if (xp->xp_numfiles == 0)
3856 {
3857 if (!(options & WILD_SILENT))
3858 EMSG2(_(e_nomatch2), str);
3859 }
3860 else
3861 {
3862 /* Escape the matches for use on the command line. */
3863 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3864
3865 /*
3866 * Check for matching suffixes in file names.
3867 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003868 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3869 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 if (xp->xp_numfiles)
3872 non_suf_match = xp->xp_numfiles;
3873 else
3874 non_suf_match = 1;
3875 if ((xp->xp_context == EXPAND_FILES
3876 || xp->xp_context == EXPAND_DIRECTORIES)
3877 && xp->xp_numfiles > 1)
3878 {
3879 /*
3880 * More than one match; check suffix.
3881 * The files will have been sorted on matching suffix in
3882 * expand_wildcards, only need to check the first two.
3883 */
3884 non_suf_match = 0;
3885 for (i = 0; i < 2; ++i)
3886 if (match_suffix(xp->xp_files[i]))
3887 ++non_suf_match;
3888 }
3889 if (non_suf_match != 1)
3890 {
3891 /* Can we ever get here unless it's while expanding
3892 * interactively? If not, we can get rid of this all
3893 * together. Don't really want to wait for this message
3894 * (and possibly have to hit return to continue!).
3895 */
3896 if (!(options & WILD_SILENT))
3897 EMSG(_(e_toomany));
3898 else if (!(options & WILD_NO_BEEP))
3899 beep_flush();
3900 }
3901 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3902 ss = vim_strsave(xp->xp_files[0]);
3903 }
3904 }
3905 }
3906
3907 /* Find longest common part */
3908 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3909 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003910 int mb_len = 1;
3911 int c0, ci;
3912
3913 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003915#ifdef FEAT_MBYTE
3916 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003918 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3919 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3920 }
3921 else
3922#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003923 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003924 for (i = 1; i < xp->xp_numfiles; ++i)
3925 {
3926#ifdef FEAT_MBYTE
3927 if (has_mbyte)
3928 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3929 else
3930#endif
3931 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003932 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003934 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003935 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003937 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003940 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942 }
3943 if (i < xp->xp_numfiles)
3944 {
3945 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003946 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948 }
3949 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 ss = alloc((unsigned)len + 1);
3952 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003953 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 findex = -1; /* next p_wc gets first one */
3955 }
3956
3957 /* Concatenate all matching names */
3958 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3959 {
3960 len = 0;
3961 for (i = 0; i < xp->xp_numfiles; ++i)
3962 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3963 ss = lalloc(len, TRUE);
3964 if (ss != NULL)
3965 {
3966 *ss = NUL;
3967 for (i = 0; i < xp->xp_numfiles; ++i)
3968 {
3969 STRCAT(ss, xp->xp_files[i]);
3970 if (i != xp->xp_numfiles - 1)
3971 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3972 }
3973 }
3974 }
3975
3976 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3977 ExpandCleanup(xp);
3978
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003979 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003980 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003981 vim_free(orig);
3982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return ss;
3984}
3985
3986/*
3987 * Prepare an expand structure for use.
3988 */
3989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003990ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003992 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003993 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003995#ifndef BACKSLASH_IN_FILENAME
3996 xp->xp_shell = FALSE;
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 xp->xp_numfiles = -1;
3999 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004000#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4001 xp->xp_arg = NULL;
4002#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004003 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004}
4005
4006/*
4007 * Cleanup an expand structure after use.
4008 */
4009 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004010ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
4012 if (xp->xp_numfiles >= 0)
4013 {
4014 FreeWild(xp->xp_numfiles, xp->xp_files);
4015 xp->xp_numfiles = -1;
4016 }
4017}
4018
4019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004020ExpandEscape(
4021 expand_T *xp,
4022 char_u *str,
4023 int numfiles,
4024 char_u **files,
4025 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
4027 int i;
4028 char_u *p;
4029
4030 /*
4031 * May change home directory back to "~"
4032 */
4033 if (options & WILD_HOME_REPLACE)
4034 tilde_replace(str, numfiles, files);
4035
4036 if (options & WILD_ESCAPE)
4037 {
4038 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004039 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004040 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 || xp->xp_context == EXPAND_BUFFERS
4042 || xp->xp_context == EXPAND_DIRECTORIES)
4043 {
4044 /*
4045 * Insert a backslash into a file name before a space, \, %, #
4046 * and wildmatch characters, except '~'.
4047 */
4048 for (i = 0; i < numfiles; ++i)
4049 {
4050 /* for ":set path=" we need to escape spaces twice */
4051 if (xp->xp_backslash == XP_BS_THREE)
4052 {
4053 p = vim_strsave_escaped(files[i], (char_u *)" ");
4054 if (p != NULL)
4055 {
4056 vim_free(files[i]);
4057 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004058#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 p = vim_strsave_escaped(files[i], (char_u *)" ");
4060 if (p != NULL)
4061 {
4062 vim_free(files[i]);
4063 files[i] = p;
4064 }
4065#endif
4066 }
4067 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004068#ifdef BACKSLASH_IN_FILENAME
4069 p = vim_strsave_fnameescape(files[i], FALSE);
4070#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004071 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (p != NULL)
4074 {
4075 vim_free(files[i]);
4076 files[i] = p;
4077 }
4078
4079 /* If 'str' starts with "\~", replace "~" at start of
4080 * files[i] with "\~". */
4081 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004082 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004085
4086 /* If the first file starts with a '+' escape it. Otherwise it
4087 * could be seen as "+cmd". */
4088 if (*files[0] == '+')
4089 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 else if (xp->xp_context == EXPAND_TAGS)
4092 {
4093 /*
4094 * Insert a backslash before characters in a tag name that
4095 * would terminate the ":tag" command.
4096 */
4097 for (i = 0; i < numfiles; ++i)
4098 {
4099 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4100 if (p != NULL)
4101 {
4102 vim_free(files[i]);
4103 files[i] = p;
4104 }
4105 }
4106 }
4107 }
4108}
4109
4110/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004111 * Escape special characters in "fname" for when used as a file name argument
4112 * after a Vim command, or, when "shell" is non-zero, a shell command.
4113 * Returns the result in allocated memory.
4114 */
4115 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004116vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004117{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004118 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004119#ifdef BACKSLASH_IN_FILENAME
4120 char_u buf[20];
4121 int j = 0;
4122
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004123 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004124 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004125 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004126 buf[j++] = *p;
4127 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004128 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004129#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004130 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4131 if (shell && csh_like_shell() && p != NULL)
4132 {
4133 char_u *s;
4134
4135 /* For csh and similar shells need to put two backslashes before '!'.
4136 * One is taken by Vim, one by the shell. */
4137 s = vim_strsave_escaped(p, (char_u *)"!");
4138 vim_free(p);
4139 p = s;
4140 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004141#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004142
4143 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4144 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004145 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004146 escape_fname(&p);
4147
4148 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004149}
4150
4151/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004152 * Put a backslash before the file name in "pp", which is in allocated memory.
4153 */
4154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004155escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004156{
4157 char_u *p;
4158
4159 p = alloc((unsigned)(STRLEN(*pp) + 2));
4160 if (p != NULL)
4161 {
4162 p[0] = '\\';
4163 STRCPY(p + 1, *pp);
4164 vim_free(*pp);
4165 *pp = p;
4166 }
4167}
4168
4169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 * For each file name in files[num_files]:
4171 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4172 */
4173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004174tilde_replace(
4175 char_u *orig_pat,
4176 int num_files,
4177 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 int i;
4180 char_u *p;
4181
4182 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4183 {
4184 for (i = 0; i < num_files; ++i)
4185 {
4186 p = home_replace_save(NULL, files[i]);
4187 if (p != NULL)
4188 {
4189 vim_free(files[i]);
4190 files[i] = p;
4191 }
4192 }
4193 }
4194}
4195
4196/*
4197 * Show all matches for completion on the command line.
4198 * Returns EXPAND_NOTHING when the character that triggered expansion should
4199 * be inserted like a normal character.
4200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004202showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4205 int num_files;
4206 char_u **files_found;
4207 int i, j, k;
4208 int maxlen;
4209 int lines;
4210 int columns;
4211 char_u *p;
4212 int lastlen;
4213 int attr;
4214 int showtail;
4215
4216 if (xp->xp_numfiles == -1)
4217 {
4218 set_expand_context(xp);
4219 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4220 &num_files, &files_found);
4221 showtail = expand_showtail(xp);
4222 if (i != EXPAND_OK)
4223 return i;
4224
4225 }
4226 else
4227 {
4228 num_files = xp->xp_numfiles;
4229 files_found = xp->xp_files;
4230 showtail = cmd_showtail;
4231 }
4232
4233#ifdef FEAT_WILDMENU
4234 if (!wildmenu)
4235 {
4236#endif
4237 msg_didany = FALSE; /* lines_left will be set */
4238 msg_start(); /* prepare for paging */
4239 msg_putchar('\n');
4240 out_flush();
4241 cmdline_row = msg_row;
4242 msg_didany = FALSE; /* lines_left will be set again */
4243 msg_start(); /* prepare for paging */
4244#ifdef FEAT_WILDMENU
4245 }
4246#endif
4247
4248 if (got_int)
4249 got_int = FALSE; /* only int. the completion, not the cmd line */
4250#ifdef FEAT_WILDMENU
4251 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004252 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253#endif
4254 else
4255 {
4256 /* find the length of the longest file name */
4257 maxlen = 0;
4258 for (i = 0; i < num_files; ++i)
4259 {
4260 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004261 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 || xp->xp_context == EXPAND_BUFFERS))
4263 {
4264 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4265 j = vim_strsize(NameBuff);
4266 }
4267 else
4268 j = vim_strsize(L_SHOWFILE(i));
4269 if (j > maxlen)
4270 maxlen = j;
4271 }
4272
4273 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4274 lines = num_files;
4275 else
4276 {
4277 /* compute the number of columns and lines for the listing */
4278 maxlen += 2; /* two spaces between file names */
4279 columns = ((int)Columns + 2) / maxlen;
4280 if (columns < 1)
4281 columns = 1;
4282 lines = (num_files + columns - 1) / columns;
4283 }
4284
Bram Moolenaar8820b482017-03-16 17:23:31 +01004285 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4288 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004289 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 msg_clr_eos();
4291 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004292 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294
4295 /* list the files line by line */
4296 for (i = 0; i < lines; ++i)
4297 {
4298 lastlen = 999;
4299 for (k = i; k < num_files; k += lines)
4300 {
4301 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4302 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004303 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 p = files_found[k] + STRLEN(files_found[k]) + 1;
4305 msg_advance(maxlen + 1);
4306 msg_puts(p);
4307 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004308 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 break;
4310 }
4311 for (j = maxlen - lastlen; --j >= 0; )
4312 msg_putchar(' ');
4313 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004314 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 || xp->xp_context == EXPAND_BUFFERS)
4316 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004317 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004318 if (xp->xp_numfiles != -1)
4319 {
4320 char_u *halved_slash;
4321 char_u *exp_path;
4322
4323 /* Expansion was done before and special characters
4324 * were escaped, need to halve backslashes. Also
4325 * $HOME has been replaced with ~/. */
4326 exp_path = expand_env_save_opt(files_found[k], TRUE);
4327 halved_slash = backslash_halve_save(
4328 exp_path != NULL ? exp_path : files_found[k]);
4329 j = mch_isdir(halved_slash != NULL ? halved_slash
4330 : files_found[k]);
4331 vim_free(exp_path);
4332 vim_free(halved_slash);
4333 }
4334 else
4335 /* Expansion was done here, file names are literal. */
4336 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (showtail)
4338 p = L_SHOWFILE(k);
4339 else
4340 {
4341 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4342 TRUE);
4343 p = NameBuff;
4344 }
4345 }
4346 else
4347 {
4348 j = FALSE;
4349 p = L_SHOWFILE(k);
4350 }
4351 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4352 }
4353 if (msg_col > 0) /* when not wrapped around */
4354 {
4355 msg_clr_eos();
4356 msg_putchar('\n');
4357 }
4358 out_flush(); /* show one line at a time */
4359 if (got_int)
4360 {
4361 got_int = FALSE;
4362 break;
4363 }
4364 }
4365
4366 /*
4367 * we redraw the command below the lines that we have just listed
4368 * This is a bit tricky, but it saves a lot of screen updating.
4369 */
4370 cmdline_row = msg_row; /* will put it back later */
4371 }
4372
4373 if (xp->xp_numfiles == -1)
4374 FreeWild(num_files, files_found);
4375
4376 return EXPAND_OK;
4377}
4378
4379/*
4380 * Private gettail for showmatches() (and win_redr_status_matches()):
4381 * Find tail of file name path, but ignore trailing "/".
4382 */
4383 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004384sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
4386 char_u *p;
4387 char_u *t = s;
4388 int had_sep = FALSE;
4389
4390 for (p = s; *p != NUL; )
4391 {
4392 if (vim_ispathsep(*p)
4393#ifdef BACKSLASH_IN_FILENAME
4394 && !rem_backslash(p)
4395#endif
4396 )
4397 had_sep = TRUE;
4398 else if (had_sep)
4399 {
4400 t = p;
4401 had_sep = FALSE;
4402 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004403 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
4405 return t;
4406}
4407
4408/*
4409 * Return TRUE if we only need to show the tail of completion matches.
4410 * When not completing file names or there is a wildcard in the path FALSE is
4411 * returned.
4412 */
4413 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004414expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 char_u *s;
4417 char_u *end;
4418
4419 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004420 if (xp->xp_context != EXPAND_FILES
4421 && xp->xp_context != EXPAND_SHELLCMD
4422 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 return FALSE;
4424
4425 end = gettail(xp->xp_pattern);
4426 if (end == xp->xp_pattern) /* there is no path separator */
4427 return FALSE;
4428
4429 for (s = xp->xp_pattern; s < end; s++)
4430 {
4431 /* Skip escaped wildcards. Only when the backslash is not a path
4432 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4433 if (rem_backslash(s))
4434 ++s;
4435 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4436 return FALSE;
4437 }
4438 return TRUE;
4439}
4440
4441/*
4442 * Prepare a string for expansion.
4443 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004444 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 * When expanding other names: The string will be used with regcomp(). Copy
4446 * the name into allocated memory and prepend "^".
4447 */
4448 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004449addstar(
4450 char_u *fname,
4451 int len,
4452 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454 char_u *retval;
4455 int i, j;
4456 int new_len;
4457 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004458 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004460 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004461 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004462 && context != EXPAND_SHELLCMD
4463 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
4465 /*
4466 * Matching will be done internally (on something other than files).
4467 * So we convert the file-matching-type wildcards into our kind for
4468 * use with vim_regcomp(). First work out how long it will be:
4469 */
4470
4471 /* For help tags the translation is done in find_help_tags().
4472 * For a tag pattern starting with "/" no translation is needed. */
4473 if (context == EXPAND_HELP
4474 || context == EXPAND_COLORS
4475 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004476 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004477 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004478 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004479 || ((context == EXPAND_TAGS_LISTFILES
4480 || context == EXPAND_TAGS)
4481 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 retval = vim_strnsave(fname, len);
4483 else
4484 {
4485 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4486 for (i = 0; i < len; i++)
4487 {
4488 if (fname[i] == '*' || fname[i] == '~')
4489 new_len++; /* '*' needs to be replaced by ".*"
4490 '~' needs to be replaced by "\~" */
4491
4492 /* Buffer names are like file names. "." should be literal */
4493 if (context == EXPAND_BUFFERS && fname[i] == '.')
4494 new_len++; /* "." becomes "\." */
4495
4496 /* Custom expansion takes care of special things, match
4497 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004498 if ((context == EXPAND_USER_DEFINED
4499 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 new_len++; /* '\' becomes "\\" */
4501 }
4502 retval = alloc(new_len);
4503 if (retval != NULL)
4504 {
4505 retval[0] = '^';
4506 j = 1;
4507 for (i = 0; i < len; i++, j++)
4508 {
4509 /* Skip backslash. But why? At least keep it for custom
4510 * expansion. */
4511 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004512 && context != EXPAND_USER_LIST
4513 && fname[i] == '\\'
4514 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516
4517 switch (fname[i])
4518 {
4519 case '*': retval[j++] = '.';
4520 break;
4521 case '~': retval[j++] = '\\';
4522 break;
4523 case '?': retval[j] = '.';
4524 continue;
4525 case '.': if (context == EXPAND_BUFFERS)
4526 retval[j++] = '\\';
4527 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004528 case '\\': if (context == EXPAND_USER_DEFINED
4529 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 retval[j++] = '\\';
4531 break;
4532 }
4533 retval[j] = fname[i];
4534 }
4535 retval[j] = NUL;
4536 }
4537 }
4538 }
4539 else
4540 {
4541 retval = alloc(len + 4);
4542 if (retval != NULL)
4543 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004544 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004547 * Don't add a star to *, ~, ~user, $var or `cmd`.
4548 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 * ~ would be at the start of the file name, but not the tail.
4550 * $ could be anywhere in the tail.
4551 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004552 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 */
4554 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004555 ends_in_star = (len > 0 && retval[len - 1] == '*');
4556#ifndef BACKSLASH_IN_FILENAME
4557 for (i = len - 2; i >= 0; --i)
4558 {
4559 if (retval[i] != '\\')
4560 break;
4561 ends_in_star = !ends_in_star;
4562 }
4563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004565 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 && vim_strchr(tail, '$') == NULL
4567 && vim_strchr(retval, '`') == NULL)
4568 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004569 else if (len > 0 && retval[len - 1] == '$')
4570 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 retval[len] = NUL;
4572 }
4573 }
4574 return retval;
4575}
4576
4577/*
4578 * Must parse the command line so far to work out what context we are in.
4579 * Completion can then be done based on that context.
4580 * This routine sets the variables:
4581 * xp->xp_pattern The start of the pattern to be expanded within
4582 * the command line (ends at the cursor).
4583 * xp->xp_context The type of thing to expand. Will be one of:
4584 *
4585 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4586 * the command line, like an unknown command. Caller
4587 * should beep.
4588 * EXPAND_NOTHING Unrecognised context for completion, use char like
4589 * a normal char, rather than for completion. eg
4590 * :s/^I/
4591 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4592 * it.
4593 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4594 * EXPAND_FILES After command with XFILE set, or after setting
4595 * with P_EXPAND set. eg :e ^I, :w>>^I
4596 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4597 * when we know only directories are of interest. eg
4598 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004599 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4601 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4602 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4603 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4604 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4605 * EXPAND_EVENTS Complete event names
4606 * EXPAND_SYNTAX Complete :syntax command arguments
4607 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4608 * EXPAND_AUGROUP Complete autocommand group names
4609 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4610 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4611 * eg :unmap a^I , :cunab x^I
4612 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4613 * eg :call sub^I
4614 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4615 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4616 * names in expressions, eg :while s^I
4617 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004618 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 */
4620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004621set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004623 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (ccline.cmdfirstc != ':'
4625#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004626 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004627 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628#endif
4629 )
4630 {
4631 xp->xp_context = EXPAND_NOTHING;
4632 return;
4633 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004634 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635}
4636
4637 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004638set_cmd_context(
4639 expand_T *xp,
4640 char_u *str, /* start of command line */
4641 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004642 int col, /* position of cursor */
4643 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 int old_char = NUL;
4646 char_u *nextcomm;
4647
4648 /*
4649 * Avoid a UMR warning from Purify, only save the character if it has been
4650 * written before.
4651 */
4652 if (col < len)
4653 old_char = str[col];
4654 str[col] = NUL;
4655 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004656
4657#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004658 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004659 {
4660# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004661 /* pass CMD_SIZE because there is no real command */
4662 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004663# endif
4664 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004665 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004666 {
4667 xp->xp_context = ccline.xp_context;
4668 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004669# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004670 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004671# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004672 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004673 else
4674#endif
4675 while (nextcomm != NULL)
4676 nextcomm = set_one_cmd_context(xp, nextcomm);
4677
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004678 /* Store the string here so that call_user_expand_func() can get to them
4679 * easily. */
4680 xp->xp_line = str;
4681 xp->xp_col = col;
4682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 str[col] = old_char;
4684}
4685
4686/*
4687 * Expand the command line "str" from context "xp".
4688 * "xp" must have been set by set_cmd_context().
4689 * xp->xp_pattern points into "str", to where the text that is to be expanded
4690 * starts.
4691 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4692 * cursor.
4693 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4694 * key that triggered expansion literally.
4695 * Returns EXPAND_OK otherwise.
4696 */
4697 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004698expand_cmdline(
4699 expand_T *xp,
4700 char_u *str, /* start of command line */
4701 int col, /* position of cursor */
4702 int *matchcount, /* return: nr of matches */
4703 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004706 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4709 {
4710 beep_flush();
4711 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4712 }
4713 if (xp->xp_context == EXPAND_NOTHING)
4714 {
4715 /* Caller can use the character as a normal char instead */
4716 return EXPAND_NOTHING;
4717 }
4718
4719 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004720 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4721 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 if (file_str == NULL)
4723 return EXPAND_UNSUCCESSFUL;
4724
Bram Moolenaar94950a92010-12-02 16:01:29 +01004725 if (p_wic)
4726 options += WILD_ICASE;
4727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004729 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 *matchcount = 0;
4732 *matches = NULL;
4733 }
4734 vim_free(file_str);
4735
4736 return EXPAND_OK;
4737}
4738
4739#ifdef FEAT_MULTI_LANG
4740/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004741 * Cleanup matches for help tags:
4742 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4743 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004745static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004748cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 int i, j;
4751 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004752 char_u buf[4];
4753 char_u *p = buf;
4754
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004755 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004756 {
4757 *p++ = '@';
4758 *p++ = p_hlg[0];
4759 *p++ = p_hlg[1];
4760 }
4761 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 for (i = 0; i < num_file; ++i)
4764 {
4765 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004766 if (len <= 0)
4767 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004768 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 /* Sorting on priority means the same item in another language may
4771 * be anywhere. Search all items for a match up to the "@en". */
4772 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004773 if (j != i && (int)STRLEN(file[j]) == len + 3
4774 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 break;
4776 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004777 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 file[i][len] = NUL;
4779 }
4780 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004781
4782 if (*buf != NUL)
4783 for (i = 0; i < num_file; ++i)
4784 {
4785 len = (int)STRLEN(file[i]) - 3;
4786 if (len <= 0)
4787 continue;
4788 if (STRCMP(file[i] + len, buf) == 0)
4789 {
4790 /* remove the default language */
4791 file[i][len] = NUL;
4792 }
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794}
4795#endif
4796
4797/*
4798 * Do the expansion based on xp->xp_context and "pat".
4799 */
4800 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004801ExpandFromContext(
4802 expand_T *xp,
4803 char_u *pat,
4804 int *num_file,
4805 char_u ***file,
4806 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808#ifdef FEAT_CMDL_COMPL
4809 regmatch_T regmatch;
4810#endif
4811 int ret;
4812 int flags;
4813
4814 flags = EW_DIR; /* include directories */
4815 if (options & WILD_LIST_NOTFOUND)
4816 flags |= EW_NOTFOUND;
4817 if (options & WILD_ADD_SLASH)
4818 flags |= EW_ADDSLASH;
4819 if (options & WILD_KEEP_ALL)
4820 flags |= EW_KEEPALL;
4821 if (options & WILD_SILENT)
4822 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004823 if (options & WILD_ALLLINKS)
4824 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004826 if (xp->xp_context == EXPAND_FILES
4827 || xp->xp_context == EXPAND_DIRECTORIES
4828 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
4830 /*
4831 * Expand file or directory names.
4832 */
4833 int free_pat = FALSE;
4834 int i;
4835
4836 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4837 * space */
4838 if (xp->xp_backslash != XP_BS_NONE)
4839 {
4840 free_pat = TRUE;
4841 pat = vim_strsave(pat);
4842 for (i = 0; pat[i]; ++i)
4843 if (pat[i] == '\\')
4844 {
4845 if (xp->xp_backslash == XP_BS_THREE
4846 && pat[i + 1] == '\\'
4847 && pat[i + 2] == '\\'
4848 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004849 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (xp->xp_backslash == XP_BS_ONE
4851 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004852 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
4855
4856 if (xp->xp_context == EXPAND_FILES)
4857 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004858 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4859 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 else
4861 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004862 if (options & WILD_ICASE)
4863 flags |= EW_ICASE;
4864
Bram Moolenaard7834d32009-12-02 16:14:36 +00004865 /* Expand wildcards, supporting %:h and the like. */
4866 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 if (free_pat)
4868 vim_free(pat);
4869 return ret;
4870 }
4871
4872 *file = (char_u **)"";
4873 *num_file = 0;
4874 if (xp->xp_context == EXPAND_HELP)
4875 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004876 /* With an empty argument we would get all the help tags, which is
4877 * very slow. Get matches for "help" instead. */
4878 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4879 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881#ifdef FEAT_MULTI_LANG
4882 cleanup_help_tags(*num_file, *file);
4883#endif
4884 return OK;
4885 }
4886 return FAIL;
4887 }
4888
4889#ifndef FEAT_CMDL_COMPL
4890 return FAIL;
4891#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004892 if (xp->xp_context == EXPAND_SHELLCMD)
4893 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (xp->xp_context == EXPAND_OLD_SETTING)
4895 return ExpandOldSetting(num_file, file);
4896 if (xp->xp_context == EXPAND_BUFFERS)
4897 return ExpandBufnames(pat, num_file, file, options);
4898 if (xp->xp_context == EXPAND_TAGS
4899 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4900 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4901 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004902 {
4903 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004904 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4905 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004908 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004909 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004910 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004911 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004912 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004913 {
4914 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004915 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004916 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004917 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004918 {
4919 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004920 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004921 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004922# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4923 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004924 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004925# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004926 if (xp->xp_context == EXPAND_PACKADD)
4927 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4930 if (regmatch.regprog == NULL)
4931 return FAIL;
4932
4933 /* set ignore-case according to p_ic, p_scs and pat */
4934 regmatch.rm_ic = ignorecase(pat);
4935
4936 if (xp->xp_context == EXPAND_SETTINGS
4937 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4938 ret = ExpandSettings(xp, &regmatch, num_file, file);
4939 else if (xp->xp_context == EXPAND_MAPPINGS)
4940 ret = ExpandMappings(&regmatch, num_file, file);
4941# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4942 else if (xp->xp_context == EXPAND_USER_DEFINED)
4943 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4944# endif
4945 else
4946 {
4947 static struct expgen
4948 {
4949 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004950 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004952 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 } tab[] =
4954 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004955 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4956 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004957 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004958 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004959#ifdef FEAT_CMDHIST
4960 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004963 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004964 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004965 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4966 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4967 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968#endif
4969#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004970 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4971 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4972 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4973 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#endif
4975#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004976 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4977 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978#endif
4979#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004980 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004982#ifdef FEAT_PROFILE
4983 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4984#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004985 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004987 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4988 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004990#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004991 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004992#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004993#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004994 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004995#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004996#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004997 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5000 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005001 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5002 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005004 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005005 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 };
5007 int i;
5008
5009 /*
5010 * Find a context in the table and call the ExpandGeneric() with the
5011 * right function to do the expansion.
5012 */
5013 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005014 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (xp->xp_context == tab[i].context)
5016 {
5017 if (tab[i].ic)
5018 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005019 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005020 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 break;
5022 }
5023 }
5024
Bram Moolenaar473de612013-06-08 18:19:48 +02005025 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 return ret;
5028#endif /* FEAT_CMDL_COMPL */
5029}
5030
5031#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5032/*
5033 * Expand a list of names.
5034 *
5035 * Generic function for command line completion. It calls a function to
5036 * obtain strings, one by one. The strings are matched against a regexp
5037 * program. Matching strings are copied into an array, which is returned.
5038 *
5039 * Returns OK when no problems encountered, FAIL for error (out of memory).
5040 */
5041 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005042ExpandGeneric(
5043 expand_T *xp,
5044 regmatch_T *regmatch,
5045 int *num_file,
5046 char_u ***file,
5047 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005049 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051 int i;
5052 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005053 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u *str;
5055
5056 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005057 * round == 0: count the number of matching names
5058 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005060 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 for (i = 0; ; ++i)
5063 {
5064 str = (*func)(xp, i);
5065 if (str == NULL) /* end of list */
5066 break;
5067 if (*str == NUL) /* skip empty strings */
5068 continue;
5069
5070 if (vim_regexec(regmatch, str, (colnr_T)0))
5071 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005072 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005074 if (escaped)
5075 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5076 else
5077 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 (*file)[count] = str;
5079#ifdef FEAT_MENU
5080 if (func == get_menu_names && str != NULL)
5081 {
5082 /* test for separator added by get_menu_names() */
5083 str += STRLEN(str) - 1;
5084 if (*str == '\001')
5085 *str = '.';
5086 }
5087#endif
5088 }
5089 ++count;
5090 }
5091 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005092 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 if (count == 0)
5095 return OK;
5096 *num_file = count;
5097 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5098 if (*file == NULL)
5099 {
5100 *file = (char_u **)"";
5101 return FAIL;
5102 }
5103 count = 0;
5104 }
5105 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005106
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005107 /* Sort the results. Keep menu's in the specified order. */
5108 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005109 {
5110 if (xp->xp_context == EXPAND_EXPRESSION
5111 || xp->xp_context == EXPAND_FUNCTIONS
5112 || xp->xp_context == EXPAND_USER_FUNC)
5113 /* <SNR> functions should be sorted to the end. */
5114 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5115 sort_func_compare);
5116 else
5117 sort_strings(*file, *num_file);
5118 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005119
Bram Moolenaar4f688582007-07-24 12:34:30 +00005120#ifdef FEAT_CMDL_COMPL
5121 /* Reset the variables used for special highlight names expansion, so that
5122 * they don't show up when getting normal highlight names by ID. */
5123 reset_expand_highlight();
5124#endif
5125
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 return OK;
5127}
5128
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005129/*
5130 * Complete a shell command.
5131 * Returns FAIL or OK;
5132 */
5133 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005134expand_shellcmd(
5135 char_u *filepat, /* pattern to match with command names */
5136 int *num_file, /* return: number of matches */
5137 char_u ***file, /* return: array with matches */
5138 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005139{
5140 char_u *pat;
5141 int i;
5142 char_u *path;
5143 int mustfree = FALSE;
5144 garray_T ga;
5145 char_u *buf = alloc(MAXPATHL);
5146 size_t l;
5147 char_u *s, *e;
5148 int flags = flagsarg;
5149 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005150 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005151
5152 if (buf == NULL)
5153 return FAIL;
5154
5155 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5156 * space */
5157 pat = vim_strsave(filepat);
5158 for (i = 0; pat[i]; ++i)
5159 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005160 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005161
Bram Moolenaarb5971142015-03-21 17:32:19 +01005162 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005163
5164 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005165 if (mch_isFullName(pat))
5166 path = (char_u *)" ";
5167 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005168 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5169 path = (char_u *)".";
5170 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005171 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005172 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005173 if (path == NULL)
5174 path = (char_u *)"";
5175 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005176
5177 /*
5178 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005179 * collect them in "ga". When "." is not in $PATH also expand for the
5180 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005181 */
5182 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005183 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005184 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005185 if (*s == NUL)
5186 {
5187 if (did_curdir)
5188 break;
5189 /* Find directories in the current directory, path is empty. */
5190 did_curdir = TRUE;
5191 }
5192 else if (*s == '.')
5193 did_curdir = TRUE;
5194
Bram Moolenaar68c31742006-09-02 15:54:18 +00005195 if (*s == ' ')
5196 ++s; /* Skip space used for absolute path name. */
5197
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005198#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005199 e = vim_strchr(s, ';');
5200#else
5201 e = vim_strchr(s, ':');
5202#endif
5203 if (e == NULL)
5204 e = s + STRLEN(s);
5205
5206 l = e - s;
5207 if (l > MAXPATHL - 5)
5208 break;
5209 vim_strncpy(buf, s, l);
5210 add_pathsep(buf);
5211 l = STRLEN(buf);
5212 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5213
5214 /* Expand matches in one directory of $PATH. */
5215 ret = expand_wildcards(1, &buf, num_file, file, flags);
5216 if (ret == OK)
5217 {
5218 if (ga_grow(&ga, *num_file) == FAIL)
5219 FreeWild(*num_file, *file);
5220 else
5221 {
5222 for (i = 0; i < *num_file; ++i)
5223 {
5224 s = (*file)[i];
5225 if (STRLEN(s) > l)
5226 {
5227 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005228 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005229 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5230 }
5231 else
5232 vim_free(s);
5233 }
5234 vim_free(*file);
5235 }
5236 }
5237 if (*e != NUL)
5238 ++e;
5239 }
5240 *file = ga.ga_data;
5241 *num_file = ga.ga_len;
5242
5243 vim_free(buf);
5244 vim_free(pat);
5245 if (mustfree)
5246 vim_free(path);
5247 return OK;
5248}
5249
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005252static void * call_user_expand_func(void *(*user_expand_func)(char_u *, int, char_u **, int), expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005255 * Call "user_expand_func()" to invoke a user defined Vim script function and
5256 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005258 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005259call_user_expand_func(
5260 void *(*user_expand_func)(char_u *, int, char_u **, int),
5261 expand_T *xp,
5262 int *num_file,
5263 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005265 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005266 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005269 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005270 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
Bram Moolenaarb7515462013-06-29 12:58:33 +02005272 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005273 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 *num_file = 0;
5275 *file = NULL;
5276
Bram Moolenaarb7515462013-06-29 12:58:33 +02005277 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005278 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005279 keep = ccline.cmdbuff[ccline.cmdlen];
5280 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005281 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005282
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005283 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005284 args[1] = xp->xp_line;
5285 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 args[2] = num;
5287
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005288 /* Save the cmdline, we don't know what the function may do. */
5289 save_ccline = ccline;
5290 ccline.cmdbuff = NULL;
5291 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005293
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005294 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005295
5296 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005298 if (ccline.cmdbuff != NULL)
5299 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005300
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005301 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005302 return ret;
5303}
5304
5305/*
5306 * Expand names with a function defined by the user.
5307 */
5308 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005309ExpandUserDefined(
5310 expand_T *xp,
5311 regmatch_T *regmatch,
5312 int *num_file,
5313 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005314{
5315 char_u *retstr;
5316 char_u *s;
5317 char_u *e;
5318 char_u keep;
5319 garray_T ga;
5320
5321 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5322 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 return FAIL;
5324
5325 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005326 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
5328 e = vim_strchr(s, '\n');
5329 if (e == NULL)
5330 e = s + STRLEN(s);
5331 keep = *e;
5332 *e = 0;
5333
5334 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5335 {
5336 *e = keep;
5337 if (*e != NUL)
5338 ++e;
5339 continue;
5340 }
5341
5342 if (ga_grow(&ga, 1) == FAIL)
5343 break;
5344
5345 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5346 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
5348 *e = keep;
5349 if (*e != NUL)
5350 ++e;
5351 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005352 vim_free(retstr);
5353 *file = ga.ga_data;
5354 *num_file = ga.ga_len;
5355 return OK;
5356}
5357
5358/*
5359 * Expand names with a list returned by a function defined by the user.
5360 */
5361 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362ExpandUserList(
5363 expand_T *xp,
5364 int *num_file,
5365 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005366{
5367 list_T *retlist;
5368 listitem_T *li;
5369 garray_T ga;
5370
5371 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5372 if (retlist == NULL)
5373 return FAIL;
5374
5375 ga_init2(&ga, (int)sizeof(char *), 3);
5376 /* Loop over the items in the list. */
5377 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5378 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005379 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5380 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005381
5382 if (ga_grow(&ga, 1) == FAIL)
5383 break;
5384
5385 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005386 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005387 ++ga.ga_len;
5388 }
5389 list_unref(retlist);
5390
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 *file = ga.ga_data;
5392 *num_file = ga.ga_len;
5393 return OK;
5394}
5395#endif
5396
5397/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005398 * Expand color scheme, compiler or filetype names.
5399 * Search from 'runtimepath':
5400 * 'runtimepath'/{dirnames}/{pat}.vim
5401 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5402 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5403 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5404 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005405 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 */
5407 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408ExpandRTDir(
5409 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005410 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411 int *num_file,
5412 char_u ***file,
5413 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 char_u *s;
5416 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005417 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005419 int i;
5420 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
5422 *num_file = 0;
5423 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005424 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005425 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005427 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005429 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5430 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005432 ga_clear_strings(&ga);
5433 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005435 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005436 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005437 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005439
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005440 if (flags & DIP_START) {
5441 for (i = 0; dirnames[i] != NULL; ++i)
5442 {
5443 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5444 if (s == NULL)
5445 {
5446 ga_clear_strings(&ga);
5447 return FAIL;
5448 }
5449 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5450 globpath(p_pp, s, &ga, 0);
5451 vim_free(s);
5452 }
5453 }
5454
5455 if (flags & DIP_OPT) {
5456 for (i = 0; dirnames[i] != NULL; ++i)
5457 {
5458 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5459 if (s == NULL)
5460 {
5461 ga_clear_strings(&ga);
5462 return FAIL;
5463 }
5464 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5465 globpath(p_pp, s, &ga, 0);
5466 vim_free(s);
5467 }
5468 }
5469
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005470 for (i = 0; i < ga.ga_len; ++i)
5471 {
5472 match = ((char_u **)ga.ga_data)[i];
5473 s = match;
5474 e = s + STRLEN(s);
5475 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5476 {
5477 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005478 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005479 if (s < match || vim_ispathsep(*s))
5480 break;
5481 ++s;
5482 *e = NUL;
5483 mch_memmove(match, s, e - s + 1);
5484 }
5485 }
5486
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005487 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005488 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005489
5490 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005491 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005492 remove_duplicates(&ga);
5493
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 *file = ga.ga_data;
5495 *num_file = ga.ga_len;
5496 return OK;
5497}
5498
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005499/*
5500 * Expand loadplugin names:
5501 * 'packpath'/pack/ * /opt/{pat}
5502 */
5503 static int
5504ExpandPackAddDir(
5505 char_u *pat,
5506 int *num_file,
5507 char_u ***file)
5508{
5509 char_u *s;
5510 char_u *e;
5511 char_u *match;
5512 garray_T ga;
5513 int i;
5514 int pat_len;
5515
5516 *num_file = 0;
5517 *file = NULL;
5518 pat_len = (int)STRLEN(pat);
5519 ga_init2(&ga, (int)sizeof(char *), 10);
5520
5521 s = alloc((unsigned)(pat_len + 26));
5522 if (s == NULL)
5523 {
5524 ga_clear_strings(&ga);
5525 return FAIL;
5526 }
5527 sprintf((char *)s, "pack/*/opt/%s*", pat);
5528 globpath(p_pp, s, &ga, 0);
5529 vim_free(s);
5530
5531 for (i = 0; i < ga.ga_len; ++i)
5532 {
5533 match = ((char_u **)ga.ga_data)[i];
5534 s = gettail(match);
5535 e = s + STRLEN(s);
5536 mch_memmove(match, s, e - s + 1);
5537 }
5538
5539 if (ga.ga_len == 0)
5540 return FAIL;
5541
5542 /* Sort and remove duplicates which can happen when specifying multiple
5543 * directories in dirnames. */
5544 remove_duplicates(&ga);
5545
5546 *file = ga.ga_data;
5547 *num_file = ga.ga_len;
5548 return OK;
5549}
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551#endif
5552
5553#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5554/*
5555 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005556 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559globpath(
5560 char_u *path,
5561 char_u *file,
5562 garray_T *ga,
5563 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 expand_T xpc;
5566 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 int num_p;
5569 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 buf = alloc(MAXPATHL);
5572 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005573 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005575 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 /* Loop over all entries in {path}. */
5579 while (*path != NUL)
5580 {
5581 /* Copy one item of the path to buf[] and concatenate the file name. */
5582 copy_option_part(&path, buf, MAXPATHL, ",");
5583 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5584 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005585# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005586 /* Using the platform's path separator (\) makes vim incorrectly
5587 * treat it as an escape character, use '/' instead. */
5588 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5589 STRCAT(buf, "/");
5590# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005594 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5595 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005597 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005599 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 for (i = 0; i < num_p; ++i)
5602 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005603 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005604 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005605 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 FreeWild(num_p, p);
5610 }
5611 }
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616
5617#endif
5618
5619#if defined(FEAT_CMDHIST) || defined(PROTO)
5620
5621/*********************************
5622 * Command line history stuff *
5623 *********************************/
5624
5625/*
5626 * Translate a history character to the associated type number.
5627 */
5628 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 if (c == ':')
5632 return HIST_CMD;
5633 if (c == '=')
5634 return HIST_EXPR;
5635 if (c == '@')
5636 return HIST_INPUT;
5637 if (c == '>')
5638 return HIST_DEBUG;
5639 return HIST_SEARCH; /* must be '?' or '/' */
5640}
5641
5642/*
5643 * Table of history names.
5644 * These names are used in :history and various hist...() functions.
5645 * It is sufficient to give the significant prefix of a history name.
5646 */
5647
5648static char *(history_names[]) =
5649{
5650 "cmd",
5651 "search",
5652 "expr",
5653 "input",
5654 "debug",
5655 NULL
5656};
5657
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005658#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5659/*
5660 * Function given to ExpandGeneric() to obtain the possible first
5661 * arguments of the ":history command.
5662 */
5663 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005664get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005665{
5666 static char_u compl[2] = { NUL, NUL };
5667 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005668 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005669 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5670
5671 if (idx < short_names_count)
5672 {
5673 compl[0] = (char_u)short_names[idx];
5674 return compl;
5675 }
5676 if (idx < short_names_count + history_name_count)
5677 return (char_u *)history_names[idx - short_names_count];
5678 if (idx == short_names_count + history_name_count)
5679 return (char_u *)"all";
5680 return NULL;
5681}
5682#endif
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684/*
5685 * init_history() - Initialize the command line history.
5686 * Also used to re-allocate the history when the size changes.
5687 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
5691 int newlen; /* new length of history table */
5692 histentry_T *temp;
5693 int i;
5694 int j;
5695 int type;
5696
5697 /*
5698 * If size of history table changed, reallocate it
5699 */
5700 newlen = (int)p_hi;
5701 if (newlen != hislen) /* history length changed */
5702 {
5703 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5704 {
5705 if (newlen)
5706 {
5707 temp = (histentry_T *)lalloc(
5708 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5709 if (temp == NULL) /* out of memory! */
5710 {
5711 if (type == 0) /* first one: just keep the old length */
5712 {
5713 newlen = hislen;
5714 break;
5715 }
5716 /* Already changed one table, now we can only have zero
5717 * length for all tables. */
5718 newlen = 0;
5719 type = -1;
5720 continue;
5721 }
5722 }
5723 else
5724 temp = NULL;
5725 if (newlen == 0 || temp != NULL)
5726 {
5727 if (hisidx[type] < 0) /* there are no entries yet */
5728 {
5729 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005730 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732 else if (newlen > hislen) /* array becomes bigger */
5733 {
5734 for (i = 0; i <= hisidx[type]; ++i)
5735 temp[i] = history[type][i];
5736 j = i;
5737 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005738 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 for ( ; j < hislen; ++i, ++j)
5740 temp[i] = history[type][j];
5741 }
5742 else /* array becomes smaller or 0 */
5743 {
5744 j = hisidx[type];
5745 for (i = newlen - 1; ; --i)
5746 {
5747 if (i >= 0) /* copy newest entries */
5748 temp[i] = history[type][j];
5749 else /* remove older entries */
5750 vim_free(history[type][j].hisstr);
5751 if (--j < 0)
5752 j = hislen - 1;
5753 if (j == hisidx[type])
5754 break;
5755 }
5756 hisidx[type] = newlen - 1;
5757 }
5758 vim_free(history[type]);
5759 history[type] = temp;
5760 }
5761 }
5762 hislen = newlen;
5763 }
5764}
5765
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005767clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005768{
5769 hisptr->hisnum = 0;
5770 hisptr->viminfo = FALSE;
5771 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005772 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005773}
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775/*
5776 * Check if command line 'str' is already in history.
5777 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5778 */
5779 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005780in_history(
5781 int type,
5782 char_u *str,
5783 int move_to_front, /* Move the entry to the front if it exists */
5784 int sep,
5785 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 int i;
5788 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005789 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 if (hisidx[type] < 0)
5792 return FALSE;
5793 i = hisidx[type];
5794 do
5795 {
5796 if (history[type][i].hisstr == NULL)
5797 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005798
5799 /* For search history, check that the separator character matches as
5800 * well. */
5801 p = history[type][i].hisstr;
5802 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005803 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005804 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
5806 if (!move_to_front)
5807 return TRUE;
5808 last_i = i;
5809 break;
5810 }
5811 if (--i < 0)
5812 i = hislen - 1;
5813 } while (i != hisidx[type]);
5814
5815 if (last_i >= 0)
5816 {
5817 str = history[type][i].hisstr;
5818 while (i != hisidx[type])
5819 {
5820 if (++i >= hislen)
5821 i = 0;
5822 history[type][last_i] = history[type][i];
5823 last_i = i;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005826 history[type][i].viminfo = FALSE;
5827 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005828 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 return TRUE;
5830 }
5831 return FALSE;
5832}
5833
5834/*
5835 * Convert history name (from table above) to its HIST_ equivalent.
5836 * When "name" is empty, return "cmd" history.
5837 * Returns -1 for unknown history name.
5838 */
5839 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005840get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 int i;
5843 int len = (int)STRLEN(name);
5844
5845 /* No argument: use current history. */
5846 if (len == 0)
5847 return hist_char2type(ccline.cmdfirstc);
5848
5849 for (i = 0; history_names[i] != NULL; ++i)
5850 if (STRNICMP(name, history_names[i], len) == 0)
5851 return i;
5852
5853 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5854 return hist_char2type(name[0]);
5855
5856 return -1;
5857}
5858
5859static int last_maptick = -1; /* last seen maptick */
5860
5861/*
5862 * Add the given string to the given history. If the string is already in the
5863 * history then it is moved to the front. "histype" may be one of he HIST_
5864 * values.
5865 */
5866 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867add_to_history(
5868 int histype,
5869 char_u *new_entry,
5870 int in_map, /* consider maptick when inside a mapping */
5871 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 histentry_T *hisptr;
5874 int len;
5875
5876 if (hislen == 0) /* no history */
5877 return;
5878
Bram Moolenaara939e432013-11-09 05:30:26 +01005879 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5880 return;
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 /*
5883 * Searches inside the same mapping overwrite each other, so that only
5884 * the last line is kept. Be careful not to remove a line that was moved
5885 * down, only lines that were added.
5886 */
5887 if (histype == HIST_SEARCH && in_map)
5888 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005889 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 /* Current line is from the same mapping, remove it */
5892 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5893 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005894 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 --hisnum[histype];
5896 if (--hisidx[HIST_SEARCH] < 0)
5897 hisidx[HIST_SEARCH] = hislen - 1;
5898 }
5899 last_maptick = -1;
5900 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005901 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {
5903 if (++hisidx[histype] == hislen)
5904 hisidx[histype] = 0;
5905 hisptr = &history[histype][hisidx[histype]];
5906 vim_free(hisptr->hisstr);
5907
5908 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005909 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5911 if (hisptr->hisstr != NULL)
5912 hisptr->hisstr[len + 1] = sep;
5913
5914 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005915 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005916 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 if (histype == HIST_SEARCH && in_map)
5918 last_maptick = maptick;
5919 }
5920}
5921
5922#if defined(FEAT_EVAL) || defined(PROTO)
5923
5924/*
5925 * Get identifier of newest history entry.
5926 * "histype" may be one of the HIST_ values.
5927 */
5928 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005929get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5932 || hisidx[histype] < 0)
5933 return -1;
5934
5935 return history[histype][hisidx[histype]].hisnum;
5936}
5937
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 * Calculate history index from a number:
5940 * num > 0: seen as identifying number of a history entry
5941 * num < 0: relative position in history wrt newest entry
5942 * "histype" may be one of the HIST_ values.
5943 */
5944 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005945calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
5947 int i;
5948 histentry_T *hist;
5949 int wrapped = FALSE;
5950
5951 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5952 || (i = hisidx[histype]) < 0 || num == 0)
5953 return -1;
5954
5955 hist = history[histype];
5956 if (num > 0)
5957 {
5958 while (hist[i].hisnum > num)
5959 if (--i < 0)
5960 {
5961 if (wrapped)
5962 break;
5963 i += hislen;
5964 wrapped = TRUE;
5965 }
5966 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5967 return i;
5968 }
5969 else if (-num <= hislen)
5970 {
5971 i += num + 1;
5972 if (i < 0)
5973 i += hislen;
5974 if (hist[i].hisstr != NULL)
5975 return i;
5976 }
5977 return -1;
5978}
5979
5980/*
5981 * Get a history entry by its index.
5982 * "histype" may be one of the HIST_ values.
5983 */
5984 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005985get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 idx = calc_hist_idx(histype, idx);
5988 if (idx >= 0)
5989 return history[histype][idx].hisstr;
5990 else
5991 return (char_u *)"";
5992}
5993
5994/*
5995 * Clear all entries of a history.
5996 * "histype" may be one of the HIST_ values.
5997 */
5998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005999clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 int i;
6002 histentry_T *hisptr;
6003
6004 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6005 {
6006 hisptr = history[histype];
6007 for (i = hislen; i--;)
6008 {
6009 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006010 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006011 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 }
6013 hisidx[histype] = -1; /* mark history as cleared */
6014 hisnum[histype] = 0; /* reset identifier counter */
6015 return OK;
6016 }
6017 return FAIL;
6018}
6019
6020/*
6021 * Remove all entries matching {str} from a history.
6022 * "histype" may be one of the HIST_ values.
6023 */
6024 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 regmatch_T regmatch;
6028 histentry_T *hisptr;
6029 int idx;
6030 int i;
6031 int last;
6032 int found = FALSE;
6033
6034 regmatch.regprog = NULL;
6035 regmatch.rm_ic = FALSE; /* always match case */
6036 if (hislen != 0
6037 && histype >= 0
6038 && histype < HIST_COUNT
6039 && *str != NUL
6040 && (idx = hisidx[histype]) >= 0
6041 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6042 != NULL)
6043 {
6044 i = last = idx;
6045 do
6046 {
6047 hisptr = &history[histype][i];
6048 if (hisptr->hisstr == NULL)
6049 break;
6050 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6051 {
6052 found = TRUE;
6053 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006054 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 }
6056 else
6057 {
6058 if (i != last)
6059 {
6060 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006061 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
6063 if (--last < 0)
6064 last += hislen;
6065 }
6066 if (--i < 0)
6067 i += hislen;
6068 } while (i != idx);
6069 if (history[histype][idx].hisstr == NULL)
6070 hisidx[histype] = -1;
6071 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006072 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 return found;
6074}
6075
6076/*
6077 * Remove an indexed entry from a history.
6078 * "histype" may be one of the HIST_ values.
6079 */
6080 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006081del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
6083 int i, j;
6084
6085 i = calc_hist_idx(histype, idx);
6086 if (i < 0)
6087 return FALSE;
6088 idx = hisidx[histype];
6089 vim_free(history[histype][i].hisstr);
6090
6091 /* When deleting the last added search string in a mapping, reset
6092 * last_maptick, so that the last added search string isn't deleted again.
6093 */
6094 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6095 last_maptick = -1;
6096
6097 while (i != idx)
6098 {
6099 j = (i + 1) % hislen;
6100 history[histype][i] = history[histype][j];
6101 i = j;
6102 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006103 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (--i < 0)
6105 i += hislen;
6106 hisidx[histype] = i;
6107 return TRUE;
6108}
6109
6110#endif /* FEAT_EVAL */
6111
6112#if defined(FEAT_CRYPT) || defined(PROTO)
6113/*
6114 * Very specific function to remove the value in ":set key=val" from the
6115 * history.
6116 */
6117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006118remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 char_u *p;
6121 int i;
6122
6123 i = hisidx[HIST_CMD];
6124 if (i < 0)
6125 return;
6126 p = history[HIST_CMD][i].hisstr;
6127 if (p != NULL)
6128 for ( ; *p; ++p)
6129 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6130 {
6131 p = vim_strchr(p + 3, '=');
6132 if (p == NULL)
6133 break;
6134 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006135 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (p[i] == '\\' && p[i + 1])
6137 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006138 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 --p;
6140 }
6141}
6142#endif
6143
6144#endif /* FEAT_CMDHIST */
6145
Bram Moolenaar064154c2016-03-19 22:50:43 +01006146#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006147/*
6148 * Get pointer to the command line info to use. cmdline_paste() may clear
6149 * ccline and put the previous value in prev_ccline.
6150 */
6151 static struct cmdline_info *
6152get_ccline_ptr(void)
6153{
6154 if ((State & CMDLINE) == 0)
6155 return NULL;
6156 if (ccline.cmdbuff != NULL)
6157 return &ccline;
6158 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6159 return &prev_ccline;
6160 return NULL;
6161}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006162#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006163
Bram Moolenaar064154c2016-03-19 22:50:43 +01006164#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006165/*
6166 * Get the current command line in allocated memory.
6167 * Only works when the command line is being edited.
6168 * Returns NULL when something is wrong.
6169 */
6170 char_u *
6171get_cmdline_str(void)
6172{
6173 struct cmdline_info *p = get_ccline_ptr();
6174
6175 if (p == NULL)
6176 return NULL;
6177 return vim_strnsave(p->cmdbuff, p->cmdlen);
6178}
6179
6180/*
6181 * Get the current command line position, counted in bytes.
6182 * Zero is the first position.
6183 * Only works when the command line is being edited.
6184 * Returns -1 when something is wrong.
6185 */
6186 int
6187get_cmdline_pos(void)
6188{
6189 struct cmdline_info *p = get_ccline_ptr();
6190
6191 if (p == NULL)
6192 return -1;
6193 return p->cmdpos;
6194}
6195
6196/*
6197 * Set the command line byte position to "pos". Zero is the first position.
6198 * Only works when the command line is being edited.
6199 * Returns 1 when failed, 0 when OK.
6200 */
6201 int
6202set_cmdline_pos(
6203 int pos)
6204{
6205 struct cmdline_info *p = get_ccline_ptr();
6206
6207 if (p == NULL)
6208 return 1;
6209
6210 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6211 * changed the command line. */
6212 if (pos < 0)
6213 new_cmdpos = 0;
6214 else
6215 new_cmdpos = pos;
6216 return 0;
6217}
6218#endif
6219
6220#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6221/*
6222 * Get the current command-line type.
6223 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6224 * Only works when the command line is being edited.
6225 * Returns NUL when something is wrong.
6226 */
6227 int
6228get_cmdline_type(void)
6229{
6230 struct cmdline_info *p = get_ccline_ptr();
6231
6232 if (p == NULL)
6233 return NUL;
6234 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006235 return
6236# ifdef FEAT_EVAL
6237 (p->input_fn) ? '@' :
6238# endif
6239 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006240 return p->cmdfirstc;
6241}
6242#endif
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6245/*
6246 * Get indices "num1,num2" that specify a range within a list (not a range of
6247 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6248 * Returns OK if parsed successfully, otherwise FAIL.
6249 */
6250 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006251get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 int len;
6254 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006255 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257 *str = skipwhite(*str);
6258 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6259 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006260 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 *str += len;
6262 *num1 = (int)num;
6263 first = TRUE;
6264 }
6265 *str = skipwhite(*str);
6266 if (**str == ',') /* parse "to" part of range */
6267 {
6268 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006269 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 if (len > 0)
6271 {
6272 *num2 = (int)num;
6273 *str = skipwhite(*str + len);
6274 }
6275 else if (!first) /* no number given at all */
6276 return FAIL;
6277 }
6278 else if (first) /* only one number given */
6279 *num2 = *num1;
6280 return OK;
6281}
6282#endif
6283
6284#if defined(FEAT_CMDHIST) || defined(PROTO)
6285/*
6286 * :history command - print a history
6287 */
6288 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006289ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 histentry_T *hist;
6292 int histype1 = HIST_CMD;
6293 int histype2 = HIST_CMD;
6294 int hisidx1 = 1;
6295 int hisidx2 = -1;
6296 int idx;
6297 int i, j, k;
6298 char_u *end;
6299 char_u *arg = eap->arg;
6300
6301 if (hislen == 0)
6302 {
6303 MSG(_("'history' option is zero"));
6304 return;
6305 }
6306
6307 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6308 {
6309 end = arg;
6310 while (ASCII_ISALPHA(*end)
6311 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6312 end++;
6313 i = *end;
6314 *end = NUL;
6315 histype1 = get_histtype(arg);
6316 if (histype1 == -1)
6317 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006318 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
6320 histype1 = 0;
6321 histype2 = HIST_COUNT-1;
6322 }
6323 else
6324 {
6325 *end = i;
6326 EMSG(_(e_trailing));
6327 return;
6328 }
6329 }
6330 else
6331 histype2 = histype1;
6332 *end = i;
6333 }
6334 else
6335 end = arg;
6336 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6337 {
6338 EMSG(_(e_trailing));
6339 return;
6340 }
6341
6342 for (; !got_int && histype1 <= histype2; ++histype1)
6343 {
6344 STRCPY(IObuff, "\n # ");
6345 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6346 MSG_PUTS_TITLE(IObuff);
6347 idx = hisidx[histype1];
6348 hist = history[histype1];
6349 j = hisidx1;
6350 k = hisidx2;
6351 if (j < 0)
6352 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6353 if (k < 0)
6354 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6355 if (idx >= 0 && j <= k)
6356 for (i = idx + 1; !got_int; ++i)
6357 {
6358 if (i == hislen)
6359 i = 0;
6360 if (hist[i].hisstr != NULL
6361 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6362 {
6363 msg_putchar('\n');
6364 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6365 hist[i].hisnum);
6366 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6367 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006368 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 else
6370 STRCAT(IObuff, hist[i].hisstr);
6371 msg_outtrans(IObuff);
6372 out_flush();
6373 }
6374 if (i == idx)
6375 break;
6376 }
6377 }
6378}
6379#endif
6380
6381#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006382/*
6383 * Buffers for history read from a viminfo file. Only valid while reading.
6384 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006385static histentry_T *viminfo_history[HIST_COUNT] =
6386 {NULL, NULL, NULL, NULL, NULL};
6387static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6388static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389static int viminfo_add_at_front = FALSE;
6390
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006391static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393/*
6394 * Translate a history type number to the associated character.
6395 */
6396 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006397hist_type2char(
6398 int type,
6399 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400{
6401 if (type == HIST_CMD)
6402 return ':';
6403 if (type == HIST_SEARCH)
6404 {
6405 if (use_question)
6406 return '?';
6407 else
6408 return '/';
6409 }
6410 if (type == HIST_EXPR)
6411 return '=';
6412 return '@';
6413}
6414
6415/*
6416 * Prepare for reading the history from the viminfo file.
6417 * This allocates history arrays to store the read history lines.
6418 */
6419 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006420prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 int i;
6423 int num;
6424 int type;
6425 int len;
6426
6427 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006428 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (asklen > hislen)
6430 asklen = hislen;
6431
6432 for (type = 0; type < HIST_COUNT; ++type)
6433 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006434 /* Count the number of empty spaces in the history list. Entries read
6435 * from viminfo previously are also considered empty. If there are
6436 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006438 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 num++;
6440 len = asklen;
6441 if (num > len)
6442 len = num;
6443 if (len <= 0)
6444 viminfo_history[type] = NULL;
6445 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006446 viminfo_history[type] = (histentry_T *)lalloc(
6447 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (viminfo_history[type] == NULL)
6449 len = 0;
6450 viminfo_hislen[type] = len;
6451 viminfo_hisidx[type] = 0;
6452 }
6453}
6454
6455/*
6456 * Accept a line from the viminfo, store it in the history array when it's
6457 * new.
6458 */
6459 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006460read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
6462 int type;
6463 long_u len;
6464 char_u *val;
6465 char_u *p;
6466
6467 type = hist_char2type(virp->vir_line[0]);
6468 if (viminfo_hisidx[type] < viminfo_hislen[type])
6469 {
6470 val = viminfo_readstring(virp, 1, TRUE);
6471 if (val != NULL && *val != NUL)
6472 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006473 int sep = (*val == ' ' ? NUL : *val);
6474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006476 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
6478 /* Need to re-allocate to append the separator byte. */
6479 len = STRLEN(val);
6480 p = lalloc(len + 2, TRUE);
6481 if (p != NULL)
6482 {
6483 if (type == HIST_SEARCH)
6484 {
6485 /* Search entry: Move the separator from the first
6486 * column to after the NUL. */
6487 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006488 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 }
6490 else
6491 {
6492 /* Not a search entry: No separator in the viminfo
6493 * file, add a NUL separator. */
6494 mch_memmove(p, val, (size_t)len + 1);
6495 p[len + 1] = NUL;
6496 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006497 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6498 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006499 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6500 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006501 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 }
6503 }
6504 }
6505 vim_free(val);
6506 }
6507 return viminfo_readline(virp);
6508}
6509
Bram Moolenaar07219f92013-04-14 23:19:36 +02006510/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006511 * Accept a new style history line from the viminfo, store it in the history
6512 * array when it's new.
6513 */
6514 void
6515handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006516 garray_T *values,
6517 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006518{
6519 int type;
6520 long_u len;
6521 char_u *val;
6522 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006523 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006524
6525 /* Check the format:
6526 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006527 if (values->ga_len < 4
6528 || vp[0].bv_type != BVAL_NR
6529 || vp[1].bv_type != BVAL_NR
6530 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6531 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006532 return;
6533
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006534 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006535 if (type >= HIST_COUNT)
6536 return;
6537 if (viminfo_hisidx[type] < viminfo_hislen[type])
6538 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006539 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006540 if (val != NULL && *val != NUL)
6541 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006542 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6543 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006544 int idx;
6545 int overwrite = FALSE;
6546
6547 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6548 {
6549 /* If lines were written by an older Vim we need to avoid
6550 * getting duplicates. See if the entry already exists. */
6551 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6552 {
6553 p = viminfo_history[type][idx].hisstr;
6554 if (STRCMP(val, p) == 0
6555 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6556 {
6557 overwrite = TRUE;
6558 break;
6559 }
6560 }
6561
6562 if (!overwrite)
6563 {
6564 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006565 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006566 p = lalloc(len + 2, TRUE);
6567 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006568 else
6569 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006570 if (p != NULL)
6571 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006572 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006573 if (!overwrite)
6574 {
6575 mch_memmove(p, val, (size_t)len + 1);
6576 /* Put the separator after the NUL. */
6577 p[len + 1] = sep;
6578 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006579 viminfo_history[type][idx].hisnum = 0;
6580 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006581 viminfo_hisidx[type]++;
6582 }
6583 }
6584 }
6585 }
6586 }
6587}
6588
6589/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006590 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006591 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006592 static void
6593concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 int idx;
6596 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006597
6598 idx = hisidx[type] + viminfo_hisidx[type];
6599 if (idx >= hislen)
6600 idx -= hislen;
6601 else if (idx < 0)
6602 idx = hislen - 1;
6603 if (viminfo_add_at_front)
6604 hisidx[type] = idx;
6605 else
6606 {
6607 if (hisidx[type] == -1)
6608 hisidx[type] = hislen - 1;
6609 do
6610 {
6611 if (history[type][idx].hisstr != NULL
6612 || history[type][idx].viminfo)
6613 break;
6614 if (++idx == hislen)
6615 idx = 0;
6616 } while (idx != hisidx[type]);
6617 if (idx != hisidx[type] && --idx < 0)
6618 idx = hislen - 1;
6619 }
6620 for (i = 0; i < viminfo_hisidx[type]; i++)
6621 {
6622 vim_free(history[type][idx].hisstr);
6623 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6624 history[type][idx].viminfo = TRUE;
6625 history[type][idx].time_set = viminfo_history[type][i].time_set;
6626 if (--idx < 0)
6627 idx = hislen - 1;
6628 }
6629 idx += 1;
6630 idx %= hislen;
6631 for (i = 0; i < viminfo_hisidx[type]; i++)
6632 {
6633 history[type][idx++].hisnum = ++hisnum[type];
6634 idx %= hislen;
6635 }
6636}
6637
6638#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6639 static int
6640#ifdef __BORLANDC__
6641_RTLENTRYF
6642#endif
6643sort_hist(const void *s1, const void *s2)
6644{
6645 histentry_T *p1 = *(histentry_T **)s1;
6646 histentry_T *p2 = *(histentry_T **)s2;
6647
6648 if (p1->time_set < p2->time_set) return -1;
6649 if (p1->time_set > p2->time_set) return 1;
6650 return 0;
6651}
6652#endif
6653
6654/*
6655 * Merge history lines from viminfo and lines typed in this Vim based on the
6656 * timestamp;
6657 */
6658 static void
6659merge_history(int type)
6660{
6661 int max_len;
6662 histentry_T **tot_hist;
6663 histentry_T *new_hist;
6664 int i;
6665 int len;
6666
6667 /* Make one long list with all entries. */
6668 max_len = hislen + viminfo_hisidx[type];
6669 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6670 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6671 if (tot_hist == NULL || new_hist == NULL)
6672 {
6673 vim_free(tot_hist);
6674 vim_free(new_hist);
6675 return;
6676 }
6677 for (i = 0; i < viminfo_hisidx[type]; i++)
6678 tot_hist[i] = &viminfo_history[type][i];
6679 len = i;
6680 for (i = 0; i < hislen; i++)
6681 if (history[type][i].hisstr != NULL)
6682 tot_hist[len++] = &history[type][i];
6683
6684 /* Sort the list on timestamp. */
6685 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6686
6687 /* Keep the newest ones. */
6688 for (i = 0; i < hislen; i++)
6689 {
6690 if (i < len)
6691 {
6692 new_hist[i] = *tot_hist[i];
6693 tot_hist[i]->hisstr = NULL;
6694 if (new_hist[i].hisnum == 0)
6695 new_hist[i].hisnum = ++hisnum[type];
6696 }
6697 else
6698 clear_hist_entry(&new_hist[i]);
6699 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006700 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006701
6702 /* Free what is not kept. */
6703 for (i = 0; i < viminfo_hisidx[type]; i++)
6704 vim_free(viminfo_history[type][i].hisstr);
6705 for (i = 0; i < hislen; i++)
6706 vim_free(history[type][i].hisstr);
6707 vim_free(history[type]);
6708 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006709 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006710}
6711
6712/*
6713 * Finish reading history lines from viminfo. Not used when writing viminfo.
6714 */
6715 void
6716finish_viminfo_history(vir_T *virp)
6717{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006719 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 for (type = 0; type < HIST_COUNT; ++type)
6722 {
6723 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006724 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006725
6726 if (merge)
6727 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006729 concat_history(type);
6730
Bram Moolenaard23a8232018-02-10 18:45:26 +01006731 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006732 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 }
6734}
6735
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006736/*
6737 * Write history to viminfo file in "fp".
6738 * When "merge" is TRUE merge history lines with a previously read viminfo
6739 * file, data is in viminfo_history[].
6740 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006743write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 int i;
6746 int type;
6747 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006748 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
6750 init_history();
6751 if (hislen == 0)
6752 return;
6753 for (type = 0; type < HIST_COUNT; ++type)
6754 {
6755 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6756 if (num_saved == 0)
6757 continue;
6758 if (num_saved < 0) /* Use default */
6759 num_saved = hislen;
6760 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6761 type == HIST_CMD ? _("Command Line") :
6762 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006763 type == HIST_EXPR ? _("Expression") :
6764 type == HIST_INPUT ? _("Input Line") :
6765 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (num_saved > hislen)
6767 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006768
6769 /*
6770 * Merge typed and viminfo history:
6771 * round 1: history of typed commands.
6772 * round 2: history from recently read viminfo.
6773 */
6774 for (round = 1; round <= 2; ++round)
6775 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006776 if (round == 1)
6777 /* start at newest entry, somewhere in the list */
6778 i = hisidx[type];
6779 else if (viminfo_hisidx[type] > 0)
6780 /* start at newest entry, first in the list */
6781 i = 0;
6782 else
6783 /* empty list */
6784 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006785 if (i >= 0)
6786 while (num_saved > 0
6787 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006789 char_u *p;
6790 time_t timestamp;
6791 int c = NUL;
6792
6793 if (round == 1)
6794 {
6795 p = history[type][i].hisstr;
6796 timestamp = history[type][i].time_set;
6797 }
6798 else
6799 {
6800 p = viminfo_history[type] == NULL ? NULL
6801 : viminfo_history[type][i].hisstr;
6802 timestamp = viminfo_history[type] == NULL ? 0
6803 : viminfo_history[type][i].time_set;
6804 }
6805
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006806 if (p != NULL && (round == 2
6807 || !merge
6808 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006810 --num_saved;
6811 fputc(hist_type2char(type, TRUE), fp);
6812 /* For the search history: put the separator in the
6813 * second column; use a space if there isn't one. */
6814 if (type == HIST_SEARCH)
6815 {
6816 c = p[STRLEN(p) + 1];
6817 putc(c == NUL ? ' ' : c, fp);
6818 }
6819 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006820
6821 {
6822 char cbuf[NUMBUFLEN];
6823
6824 /* New style history with a bar line. Format:
6825 * |{bartype},{histtype},{timestamp},{separator},"text" */
6826 if (c == NUL)
6827 cbuf[0] = NUL;
6828 else
6829 sprintf(cbuf, "%d", c);
6830 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6831 type, (long)timestamp, cbuf);
6832 barline_writestring(fp, p, LSIZE - 20);
6833 putc('\n', fp);
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006836 if (round == 1)
6837 {
6838 /* Decrement index, loop around and stop when back at
6839 * the start. */
6840 if (--i < 0)
6841 i = hislen - 1;
6842 if (i == hisidx[type])
6843 break;
6844 }
6845 else
6846 {
6847 /* Increment index. Stop at the end in the while. */
6848 ++i;
6849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006851 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006852 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006853 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006854 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006855 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006856 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 }
6858}
6859#endif /* FEAT_VIMINFO */
6860
6861#if defined(FEAT_FKMAP) || defined(PROTO)
6862/*
6863 * Write a character at the current cursor+offset position.
6864 * It is directly written into the command buffer block.
6865 */
6866 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006867cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868{
6869 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6870 {
6871 EMSG(_("E198: cmd_pchar beyond the command length"));
6872 return;
6873 }
6874 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6875 ccline.cmdbuff[ccline.cmdlen] = NUL;
6876}
6877
6878 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006879cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
6881 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6882 {
6883 /* EMSG(_("cmd_gchar beyond the command length")); */
6884 return NUL;
6885 }
6886 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6887}
6888#endif
6889
6890#if defined(FEAT_CMDWIN) || defined(PROTO)
6891/*
6892 * Open a window on the current command line and history. Allow editing in
6893 * the window. Returns when the window is closed.
6894 * Returns:
6895 * CR if the command is to be executed
6896 * Ctrl_C if it is to be abandoned
6897 * K_IGNORE if editing continues
6898 */
6899 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006900open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
6902 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006903 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006905 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 win_T *wp;
6907 int i;
6908 linenr_T lnum;
6909 int histtype;
6910 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 int save_restart_edit = restart_edit;
6912 int save_State = State;
6913 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006914#ifdef FEAT_RIGHTLEFT
6915 int save_cmdmsg_rl = cmdmsg_rl;
6916#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006917#ifdef FEAT_FOLDING
6918 int save_KeyTyped;
6919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 /* Can't do this recursively. Can't do it when typing a password. */
6922 if (cmdwin_type != 0
6923# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6924 || cmdline_star > 0
6925# endif
6926 )
6927 {
6928 beep_flush();
6929 return K_IGNORE;
6930 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006931 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
6933 /* Save current window sizes. */
6934 win_size_save(&winsizes);
6935
6936# ifdef FEAT_AUTOCMD
6937 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006938 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006940 /* don't use a new tab page */
6941 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006942 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 /* Create a window for the command-line buffer. */
6945 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6946 {
6947 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006948# ifdef FEAT_AUTOCMD
6949 unblock_autocmds();
6950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 return K_IGNORE;
6952 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006953 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006956 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006957 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006960#ifdef FEAT_FOLDING
6961 curwin->w_p_fen = FALSE;
6962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006964 curwin->w_p_rl = cmdmsg_rl;
6965 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006967 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
6969# ifdef FEAT_AUTOCMD
6970 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006971 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006972 /* But don't allow switching to another buffer. */
6973 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974# endif
6975
Bram Moolenaar46152342005-09-07 21:18:43 +00006976 /* Showing the prompt may have set need_wait_return, reset it. */
6977 need_wait_return = FALSE;
6978
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006979 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6981 {
6982 if (p_wc == TAB)
6983 {
6984 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6985 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6986 }
6987 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6988 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006989# ifdef FEAT_AUTOCMD
6990 --curbuf_lock;
6991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006993 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6994 * sets 'textwidth' to 78). */
6995 curbuf->b_p_tw = 0;
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 /* Fill the buffer with the history. */
6998 init_history();
6999 if (hislen > 0)
7000 {
7001 i = hisidx[histtype];
7002 if (i >= 0)
7003 {
7004 lnum = 0;
7005 do
7006 {
7007 if (++i == hislen)
7008 i = 0;
7009 if (history[histtype][i].hisstr != NULL)
7010 ml_append(lnum++, history[histtype][i].hisstr,
7011 (colnr_T)0, FALSE);
7012 }
7013 while (i != hisidx[histtype]);
7014 }
7015 }
7016
7017 /* Replace the empty last line with the current command-line and put the
7018 * cursor there. */
7019 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7020 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7021 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007022 changed_line_abv_curs();
7023 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007024 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
7026 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007027 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
7029 /* No Ex mode here! */
7030 exmode_active = 0;
7031
7032 State = NORMAL;
7033# ifdef FEAT_MOUSE
7034 setmouse();
7035# endif
7036
7037# ifdef FEAT_AUTOCMD
7038 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007039 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007040 if (restart_edit != 0) /* autocmd with ":startinsert" */
7041 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042# endif
7043
7044 i = RedrawingDisabled;
7045 RedrawingDisabled = 0;
7046
7047 /*
7048 * Call the main loop until <CR> or CTRL-C is typed.
7049 */
7050 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007051 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
7053 RedrawingDisabled = i;
7054
7055# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007056
7057# ifdef FEAT_FOLDING
7058 save_KeyTyped = KeyTyped;
7059# endif
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007062 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007063
7064# ifdef FEAT_FOLDING
7065 /* Restore KeyTyped in case it is modified by autocommands */
7066 KeyTyped = save_KeyTyped;
7067# endif
7068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069# endif
7070
Bram Moolenaarccc18222007-05-10 18:25:20 +00007071 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007072 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 cmdwin_type = 0;
7074
7075 exmode_active = save_exmode;
7076
Bram Moolenaarf9821062008-06-20 16:31:07 +00007077 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007079 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
7081 cmdwin_result = Ctrl_C;
7082 EMSG(_("E199: Active window or buffer deleted"));
7083 }
7084 else
7085 {
7086# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7087 /* autocmds may abort script processing */
7088 if (aborting() && cmdwin_result != K_IGNORE)
7089 cmdwin_result = Ctrl_C;
7090# endif
7091 /* Set the new command line from the cmdline buffer. */
7092 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007093 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007095 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7096
7097 if (histtype == HIST_CMD)
7098 {
7099 /* Execute the command directly. */
7100 ccline.cmdbuff = vim_strsave((char_u *)p);
7101 cmdwin_result = CAR;
7102 }
7103 else
7104 {
7105 /* First need to cancel what we were doing. */
7106 ccline.cmdbuff = NULL;
7107 stuffcharReadbuff(':');
7108 stuffReadbuff((char_u *)p);
7109 stuffcharReadbuff(CAR);
7110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 }
7112 else if (cmdwin_result == K_XF2) /* :qa typed */
7113 {
7114 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7115 cmdwin_result = CAR;
7116 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007117 else if (cmdwin_result == Ctrl_C)
7118 {
7119 /* :q or :close, don't execute any command
7120 * and don't modify the cmd window. */
7121 ccline.cmdbuff = NULL;
7122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 else
7124 ccline.cmdbuff = vim_strsave(ml_get_curline());
7125 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007126 {
7127 ccline.cmdbuff = vim_strsave((char_u *)"");
7128 ccline.cmdlen = 0;
7129 ccline.cmdbufflen = 1;
7130 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 else
7134 {
7135 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7136 ccline.cmdbufflen = ccline.cmdlen + 1;
7137 ccline.cmdpos = curwin->w_cursor.col;
7138 if (ccline.cmdpos > ccline.cmdlen)
7139 ccline.cmdpos = ccline.cmdlen;
7140 if (cmdwin_result == K_IGNORE)
7141 {
7142 set_cmdspos_cursor();
7143 redrawcmd();
7144 }
7145 }
7146
7147# ifdef FEAT_AUTOCMD
7148 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007149 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007151# ifdef FEAT_CONCEAL
7152 /* Avoid command-line window first character being concealed. */
7153 curwin->w_p_cole = 0;
7154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007156 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 win_goto(old_curwin);
7158 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007159
7160 /* win_close() may have already wiped the buffer when 'bh' is
7161 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007162 if (bufref_valid(&bufref))
7163 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
7165 /* Restore window sizes. */
7166 win_size_restore(&winsizes);
7167
7168# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007169 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170# endif
7171 }
7172
7173 ga_clear(&winsizes);
7174 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007175# ifdef FEAT_RIGHTLEFT
7176 cmdmsg_rl = save_cmdmsg_rl;
7177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
7179 State = save_State;
7180# ifdef FEAT_MOUSE
7181 setmouse();
7182# endif
7183
7184 return cmdwin_result;
7185}
7186#endif /* FEAT_CMDWIN */
7187
7188/*
7189 * Used for commands that either take a simple command string argument, or:
7190 * cmd << endmarker
7191 * {script}
7192 * endmarker
7193 * Returns a pointer to allocated memory with {script} or NULL.
7194 */
7195 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 char_u *theline;
7199 char *end_pattern = NULL;
7200 char dot[] = ".";
7201 garray_T ga;
7202
7203 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7204 return NULL;
7205
7206 ga_init2(&ga, 1, 0x400);
7207
7208 if (cmd[2] != NUL)
7209 end_pattern = (char *)skipwhite(cmd + 2);
7210 else
7211 end_pattern = dot;
7212
7213 for (;;)
7214 {
7215 theline = eap->getline(
7216#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007217 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218#endif
7219 NUL, eap->cookie, 0);
7220
7221 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007222 {
7223 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227 ga_concat(&ga, theline);
7228 ga_append(&ga, '\n');
7229 vim_free(theline);
7230 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007231 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233 return (char_u *)ga.ga_data;
7234}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007235
7236#ifdef FEAT_SEARCH_EXTRA
7237 static void
7238set_search_match(pos_T *t)
7239{
7240 /*
7241 * First move cursor to end of match, then to the start. This
7242 * moves the whole match onto the screen when 'nowrap' is set.
7243 */
7244 t->lnum += search_match_lines;
7245 t->col = search_match_endcol;
7246 if (t->lnum > curbuf->b_ml.ml_line_count)
7247 {
7248 t->lnum = curbuf->b_ml.ml_line_count;
7249 coladvance((colnr_T)MAXCOL);
7250 }
7251}
7252#endif