blob: 6119ad216361794447d21b8a169fc4ebe5cc2865 [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
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200149 static void
150trigger_cmd_autocmd(int typechar, int evt)
151{
152 char_u typestr[2];
153
154 typestr[0] = typechar;
155 typestr[1] = NUL;
156 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
157}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200160 * Abandon the command line.
161 */
162 static void
163abandon_cmdline(void)
164{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100165 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200166 if (msg_scrolled == 0)
167 compute_cmdrow();
168 MSG("");
169 redraw_cmdline = TRUE;
170}
171
Bram Moolenaaree219b02017-12-17 14:55:01 +0100172#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200173/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100174 * Guess that the pattern matches everything. Only finds specific cases, such
175 * as a trailing \|, which can happen while typing a pattern.
176 */
177 static int
178empty_pattern(char_u *p)
179{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100180 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100181
182 /* remove trailing \v and the like */
183 while (n >= 2 && p[n - 2] == '\\'
184 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
185 n -= 2;
186 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
187}
Bram Moolenaaree219b02017-12-17 14:55:01 +0100188#endif
Bram Moolenaar66216052017-12-16 16:33:44 +0100189
190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 * getcmdline() - accept a command line starting with firstc.
192 *
193 * firstc == ':' get ":" command line.
194 * firstc == '/' or '?' get search pattern
195 * firstc == '=' get expression
196 * firstc == '@' get text for input() function
197 * firstc == '>' get text for debug mode
198 * firstc == NUL get text for :insert command
199 * firstc == -1 like NUL, and break on CTRL-C
200 *
201 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
202 * command line.
203 *
204 * Careful: getcmdline() can be called recursively!
205 *
206 * Return pointer to allocated string if there is a commandline, NULL
207 * otherwise.
208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100210getcmdline(
211 int firstc,
212 long count UNUSED, /* only used for incremental search */
213 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214{
215 int c;
216 int i;
217 int j;
218 int gotesc = FALSE; /* TRUE when <ESC> just typed */
219 int do_abbr; /* when TRUE check for abbr. */
220#ifdef FEAT_CMDHIST
221 char_u *lookfor = NULL; /* string to match */
222 int hiscnt; /* current history line in use */
223 int histype; /* history type to be used */
224#endif
225#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200226 pos_T search_start; /* where 'incsearch' starts searching */
227 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200229 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200231 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200233 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200234 pos_T match_start = curwin->w_cursor;
235 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# ifdef FEAT_DIFF
237 int old_topfill;
Bram Moolenaard0480092017-11-16 22:20:39 +0100238 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# endif
240 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200241 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 int did_incsearch = FALSE;
243 int incsearch_postponed = FALSE;
244#endif
245 int did_wild_list = FALSE; /* did wild_list() recently */
246 int wim_index = 0; /* index in wim_flags[] */
247 int res;
248 int save_msg_scroll = msg_scroll;
249 int save_State = State; /* remember State when called */
250 int some_key_typed = FALSE; /* one of the keys was typed */
251#ifdef FEAT_MOUSE
252 /* mouse drag and release events are ignored, unless they are
253 * preceded with a mouse down event */
254 int ignore_drag_release = TRUE;
255#endif
256#ifdef FEAT_EVAL
257 int break_ctrl_c = FALSE;
258#endif
259 expand_T xpc;
260 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100261#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000262 /* Everything that may work recursively should save and restore the
263 * current command line in save_ccline. That includes update_screen(), a
264 * custom status line may invoke ":normal". */
265 struct cmdline_info save_ccline;
266#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200267 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269#ifdef FEAT_EVAL
270 if (firstc == -1)
271 {
272 firstc = NUL;
273 break_ctrl_c = TRUE;
274 }
275#endif
276#ifdef FEAT_RIGHTLEFT
277 /* start without Hebrew mapping for a command line */
278 if (firstc == ':' || firstc == '=' || firstc == '>')
279 cmd_hkmap = 0;
280#endif
281
282 ccline.overstrike = FALSE; /* always start in insert mode */
283#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100284 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200285 save_cursor = curwin->w_cursor; /* may be restored later */
286 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 old_curswant = curwin->w_curswant;
288 old_leftcol = curwin->w_leftcol;
289 old_topline = curwin->w_topline;
290# ifdef FEAT_DIFF
291 old_topfill = curwin->w_topfill;
292# endif
293 old_botline = curwin->w_botline;
294#endif
295
296 /*
297 * set some variables for redrawcmd()
298 */
299 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000300 ccline.cmdindent = (firstc > 0 ? indent : 0);
301
302 /* alloc initial ccline.cmdbuff */
303 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 if (ccline.cmdbuff == NULL)
305 return NULL; /* out of memory */
306 ccline.cmdlen = ccline.cmdpos = 0;
307 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100308 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000310 /* autoindent for :insert and :append */
311 if (firstc <= 0)
312 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200313 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000314 ccline.cmdbuff[indent] = NUL;
315 ccline.cmdpos = indent;
316 ccline.cmdspos = indent;
317 ccline.cmdlen = indent;
318 }
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000321 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
323#ifdef FEAT_RIGHTLEFT
324 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
325 && (firstc == '/' || firstc == '?'))
326 cmdmsg_rl = TRUE;
327 else
328 cmdmsg_rl = FALSE;
329#endif
330
331 redir_off = TRUE; /* don't redirect the typed command */
332 if (!cmd_silent)
333 {
334 i = msg_scrolled;
335 msg_scrolled = 0; /* avoid wait_return message */
336 gotocmdline(TRUE);
337 msg_scrolled += i;
338 redrawcmdprompt(); /* draw prompt or indent */
339 set_cmdspos();
340 }
341 xpc.xp_context = EXPAND_NOTHING;
342 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000343#ifndef BACKSLASH_IN_FILENAME
344 xpc.xp_shell = FALSE;
345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000347#if defined(FEAT_EVAL)
348 if (ccline.input_fn)
349 {
350 xpc.xp_context = ccline.xp_context;
351 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000352# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000353 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000354# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000355 }
356#endif
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /*
359 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
360 * doing ":@0" when register 0 doesn't contain a CR.
361 */
362 msg_scroll = FALSE;
363
364 State = CMDLINE;
365
366 if (firstc == '/' || firstc == '?' || firstc == '@')
367 {
368 /* Use ":lmap" mappings for search pattern and input(). */
369 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
370 b_im_ptr = &curbuf->b_p_iminsert;
371 else
372 b_im_ptr = &curbuf->b_p_imsearch;
373 if (*b_im_ptr == B_IMODE_LMAP)
374 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100375#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 im_set_active(*b_im_ptr == B_IMODE_IM);
377#endif
378 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100379#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 else if (p_imcmdline)
381 im_set_active(TRUE);
382#endif
383
384#ifdef FEAT_MOUSE
385 setmouse();
386#endif
387#ifdef CURSOR_SHAPE
388 ui_cursor_shape(); /* may show different cursor shape */
389#endif
390
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000391 /* When inside an autocommand for writing "exiting" may be set and
392 * terminal mode set to cooked. Need to set raw mode here then. */
393 settmode(TMODE_RAW);
394
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200395 /* Trigger CmdlineEnter autocommands. */
396 cmdline_type = firstc == NUL ? '-' : firstc;
397 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200398
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#ifdef FEAT_CMDHIST
400 init_history();
401 hiscnt = hislen; /* set hiscnt to impossible history value */
402 histype = hist_char2type(firstc);
403#endif
404
405#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000406 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#endif
408
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200409 /* If something above caused an error, reset the flags, we do want to type
410 * and execute commands. Display may be messed up a bit. */
411 if (did_emsg)
412 redrawcmd();
413 did_emsg = FALSE;
414 got_int = FALSE;
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 /*
417 * Collect the command string, handling editing keys.
418 */
419 for (;;)
420 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000421 redir_off = TRUE; /* Don't redirect the typed command.
422 Repeated, because a ":redir" inside
423 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424#ifdef USE_ON_FLY_SCROLL
425 dont_scroll = FALSE; /* allow scrolling here */
426#endif
427 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
428
429 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000430
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100431 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
432 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000433 do
434 {
435 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100436 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if (KeyTyped)
439 {
440 some_key_typed = TRUE;
441#ifdef FEAT_RIGHTLEFT
442 if (cmd_hkmap)
443 c = hkmap(c);
444# ifdef FEAT_FKMAP
445 if (cmd_fkmap)
446 c = cmdl_fkmap(c);
447# endif
448 if (cmdmsg_rl && !KeyStuffed)
449 {
450 /* Invert horizontal movements and operations. Only when
451 * typed by the user directly, not when the result of a
452 * mapping. */
453 switch (c)
454 {
455 case K_RIGHT: c = K_LEFT; break;
456 case K_S_RIGHT: c = K_S_LEFT; break;
457 case K_C_RIGHT: c = K_C_LEFT; break;
458 case K_LEFT: c = K_RIGHT; break;
459 case K_S_LEFT: c = K_S_RIGHT; break;
460 case K_C_LEFT: c = K_C_RIGHT; break;
461 }
462 }
463#endif
464 }
465
466 /*
467 * Ignore got_int when CTRL-C was typed here.
468 * Don't ignore it in :global, we really need to break then, e.g., for
469 * ":g/pat/normal /pat" (without the <CR>).
470 * Don't ignore it for the input() function.
471 */
472 if ((c == Ctrl_C
473#ifdef UNIX
474 || c == intr_char
475#endif
476 )
477#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
478 && firstc != '@'
479#endif
480#ifdef FEAT_EVAL
481 && !break_ctrl_c
482#endif
483 && !global_busy)
484 got_int = FALSE;
485
486#ifdef FEAT_CMDHIST
487 /* free old command line when finished moving around in the history
488 * list */
489 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000490 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000491 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 && c != K_PAGEDOWN && c != K_PAGEUP
493 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000494 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100496 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#endif
498
499 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000500 * When there are matching completions to select <S-Tab> works like
501 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000503 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 c = Ctrl_P;
505
506#ifdef FEAT_WILDMENU
507 /* Special translations for 'wildmenu' */
508 if (did_wild_list && p_wmnu)
509 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000510 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000512 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 c = Ctrl_N;
514 }
515 /* Hitting CR after "emenu Name.": complete submenu */
516 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
517 && ccline.cmdpos > 1
518 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
519 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
520 && (c == '\n' || c == '\r' || c == K_KENTER))
521 c = K_DOWN;
522#endif
523
524 /* free expanded names when finished walking through matches */
525 if (xpc.xp_numfiles != -1
526 && !(c == p_wc && KeyTyped) && c != p_wcm
527 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
528 && c != Ctrl_L)
529 {
530 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
531 did_wild_list = FALSE;
532#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000533 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534#endif
535 xpc.xp_context = EXPAND_NOTHING;
536 wim_index = 0;
537#ifdef FEAT_WILDMENU
538 if (p_wmnu && wild_menu_showing != 0)
539 {
540 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000541 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000542
543 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000544 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
546 if (wild_menu_showing == WM_SCROLLED)
547 {
548 /* Entered command line, move it up */
549 cmdline_row--;
550 redrawcmd();
551 }
552 else if (save_p_ls != -1)
553 {
554 /* restore 'laststatus' and 'winminheight' */
555 p_ls = save_p_ls;
556 p_wmh = save_p_wmh;
557 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000558 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000560 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 redrawcmd();
562 save_p_ls = -1;
563 }
564 else
565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 redraw_statuslines();
568 }
569 KeyTyped = skt;
570 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000571 if (ccline.input_fn)
572 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574#endif
575 }
576
577#ifdef FEAT_WILDMENU
578 /* Special translations for 'wildmenu' */
579 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
580 {
581 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000582 if (c == K_DOWN && ccline.cmdpos > 0
583 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {
587 /* Hitting <Up>: Remove one submenu name in front of the
588 * cursor */
589 int found = FALSE;
590
591 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
592 i = 0;
593 while (--j > 0)
594 {
595 /* check for start of menu name */
596 if (ccline.cmdbuff[j] == ' '
597 && ccline.cmdbuff[j - 1] != '\\')
598 {
599 i = j + 1;
600 break;
601 }
602 /* check for start of submenu name */
603 if (ccline.cmdbuff[j] == '.'
604 && ccline.cmdbuff[j - 1] != '\\')
605 {
606 if (found)
607 {
608 i = j + 1;
609 break;
610 }
611 else
612 found = TRUE;
613 }
614 }
615 if (i > 0)
616 cmdline_del(i);
617 c = p_wc;
618 xpc.xp_context = EXPAND_NOTHING;
619 }
620 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000621 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000622 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000623 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {
625 char_u upseg[5];
626
627 upseg[0] = PATHSEP;
628 upseg[1] = '.';
629 upseg[2] = '.';
630 upseg[3] = PATHSEP;
631 upseg[4] = NUL;
632
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000633 if (c == K_DOWN
634 && ccline.cmdpos > 0
635 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
636 && (ccline.cmdpos < 3
637 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
639 {
640 /* go down a directory */
641 c = p_wc;
642 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000643 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
645 /* If in a direct ancestor, strip off one ../ to go down */
646 int found = FALSE;
647
648 j = ccline.cmdpos;
649 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
650 while (--j > i)
651 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000652#ifdef FEAT_MBYTE
653 if (has_mbyte)
654 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (vim_ispathsep(ccline.cmdbuff[j]))
657 {
658 found = TRUE;
659 break;
660 }
661 }
662 if (found
663 && ccline.cmdbuff[j - 1] == '.'
664 && ccline.cmdbuff[j - 2] == '.'
665 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
666 {
667 cmdline_del(j - 2);
668 c = p_wc;
669 }
670 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000671 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
673 /* go up a directory */
674 int found = FALSE;
675
676 j = ccline.cmdpos - 1;
677 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
678 while (--j > i)
679 {
680#ifdef FEAT_MBYTE
681 if (has_mbyte)
682 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
683#endif
684 if (vim_ispathsep(ccline.cmdbuff[j])
685#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100686 && vim_strchr((char_u *)" *?[{`$%#",
687 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#endif
689 )
690 {
691 if (found)
692 {
693 i = j + 1;
694 break;
695 }
696 else
697 found = TRUE;
698 }
699 }
700
701 if (!found)
702 j = i;
703 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
704 j += 4;
705 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
706 && j == i)
707 j += 3;
708 else
709 j = 0;
710 if (j > 0)
711 {
712 /* TODO this is only for DOS/UNIX systems - need to put in
713 * machine-specific stuff here and in upseg init */
714 cmdline_del(j);
715 put_on_cmdline(upseg + 1, 3, FALSE);
716 }
717 else if (ccline.cmdpos > i)
718 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100719
720 /* Now complete in the new directory. Set KeyTyped in case the
721 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100723 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 }
725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
727#endif /* FEAT_WILDMENU */
728
729 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
730 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
731 if (c == Ctrl_BSL)
732 {
733 ++no_mapping;
734 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000735 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 --no_mapping;
737 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200738 /* CTRL-\ e doesn't work when obtaining an expression, unless it
739 * is in a mapping. */
740 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
741 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
743 vungetc(c);
744 c = Ctrl_BSL;
745 }
746#ifdef FEAT_EVAL
747 else if (c == 'e')
748 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200749 char_u *p = NULL;
750 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
752 /*
753 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000754 * Need to save and restore the current command line, to be
755 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 if (ccline.cmdpos == ccline.cmdlen)
758 new_cmdpos = 99999; /* keep it at the end */
759 else
760 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000761
762 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000764 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (c == '=')
766 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000767 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000768 * to avoid nasty things like going to another buffer when
769 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000770 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000771 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000773 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000774 restore_cmdline(&save_ccline);
775
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200776 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200778 len = (int)STRLEN(p);
779 if (realloc_cmdbuff(len + 1) == OK)
780 {
781 ccline.cmdlen = len;
782 STRCPY(ccline.cmdbuff, p);
783 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200785 /* Restore the cursor or use the position set with
786 * set_cmdline_pos(). */
787 if (new_cmdpos > ccline.cmdlen)
788 ccline.cmdpos = ccline.cmdlen;
789 else
790 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200792 KeyTyped = FALSE; /* Don't do p_wc completion. */
793 redrawcmd();
794 goto cmdline_changed;
795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 }
798 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100799 got_int = FALSE; /* don't abandon the command line */
800 did_emsg = FALSE;
801 emsg_on_display = FALSE;
802 redrawcmd();
803 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805#endif
806 else
807 {
808 if (c == Ctrl_G && p_im && restart_edit == 0)
809 restart_edit = 'a';
810 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
811 in history */
812 goto returncmd; /* back to Normal mode */
813 }
814 }
815
816#ifdef FEAT_CMDWIN
817 if (c == cedit_key || c == K_CMDWIN)
818 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200819 if (ex_normal_busy == 0 && got_int == FALSE)
820 {
821 /*
822 * Open a window to edit the command line (and history).
823 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200824 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200825 some_key_typed = TRUE;
826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
828# ifdef FEAT_DIGRAPHS
829 else
830# endif
831#endif
832#ifdef FEAT_DIGRAPHS
833 c = do_digraph(c);
834#endif
835
836 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
837 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
838 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000839 /* In Ex mode a backslash escapes a newline. */
840 if (exmode_active
841 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000842 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000843 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000844 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000846 if (c == K_KENTER)
847 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000849 else
850 {
851 gotesc = FALSE; /* Might have typed ESC previously, don't
852 truncate the cmdline now. */
853 if (ccheck_abbr(c + ABBR_OFF))
854 goto cmdline_changed;
855 if (!cmd_silent)
856 {
857 windgoto(msg_row, 0);
858 out_flush();
859 }
860 break;
861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863
864 /*
865 * Completion for 'wildchar' or 'wildcharm' key.
866 * - hitting <ESC> twice means: abandon command line.
867 * - wildcard expansion is only done when the 'wildchar' key is really
868 * typed, not when it comes from a macro
869 */
870 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
871 {
872 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
873 {
874 /* if 'wildmode' contains "list" may still need to list */
875 if (xpc.xp_numfiles > 1
876 && !did_wild_list
877 && (wim_flags[wim_index] & WIM_LIST))
878 {
879 (void)showmatches(&xpc, FALSE);
880 redrawcmd();
881 did_wild_list = TRUE;
882 }
883 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100884 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
885 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100887 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
888 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 else
890 res = OK; /* don't insert 'wildchar' now */
891 }
892 else /* typed p_wc first time */
893 {
894 wim_index = 0;
895 j = ccline.cmdpos;
896 /* if 'wildmode' first contains "longest", get longest
897 * common part */
898 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100899 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
900 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100902 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
903 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
905 /* if interrupted while completing, behave like it failed */
906 if (got_int)
907 {
908 (void)vpeekc(); /* remove <C-C> from input stream */
909 got_int = FALSE; /* don't abandon the command line */
910 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
911#ifdef FEAT_WILDMENU
912 xpc.xp_context = EXPAND_NOTHING;
913#endif
914 goto cmdline_changed;
915 }
916
917 /* when more than one match, and 'wildmode' first contains
918 * "list", or no change and 'wildmode' contains "longest,list",
919 * list all matches */
920 if (res == OK && xpc.xp_numfiles > 1)
921 {
922 /* a "longest" that didn't do anything is skipped (but not
923 * "list:longest") */
924 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
925 wim_index = 1;
926 if ((wim_flags[wim_index] & WIM_LIST)
927#ifdef FEAT_WILDMENU
928 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
929#endif
930 )
931 {
932 if (!(wim_flags[0] & WIM_LONGEST))
933 {
934#ifdef FEAT_WILDMENU
935 int p_wmnu_save = p_wmnu;
936 p_wmnu = 0;
937#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100938 /* remove match */
939 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#ifdef FEAT_WILDMENU
941 p_wmnu = p_wmnu_save;
942#endif
943 }
944#ifdef FEAT_WILDMENU
945 (void)showmatches(&xpc, p_wmnu
946 && ((wim_flags[wim_index] & WIM_LIST) == 0));
947#else
948 (void)showmatches(&xpc, FALSE);
949#endif
950 redrawcmd();
951 did_wild_list = TRUE;
952 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100953 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
954 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100956 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
957 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200960 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
962#ifdef FEAT_WILDMENU
963 else if (xpc.xp_numfiles == -1)
964 xpc.xp_context = EXPAND_NOTHING;
965#endif
966 }
967 if (wim_index < 3)
968 ++wim_index;
969 if (c == ESC)
970 gotesc = TRUE;
971 if (res == OK)
972 goto cmdline_changed;
973 }
974
975 gotesc = FALSE;
976
977 /* <S-Tab> goes to last match, in a clumsy way */
978 if (c == K_S_TAB && KeyTyped)
979 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100980 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
981 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
982 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 goto cmdline_changed;
984 }
985
986 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
987 c = NL;
988
989 do_abbr = TRUE; /* default: check for abbreviation */
990
991 /*
992 * Big switch for a typed command line character.
993 */
994 switch (c)
995 {
996 case K_BS:
997 case Ctrl_H:
998 case K_DEL:
999 case K_KDEL:
1000 case Ctrl_W:
1001#ifdef FEAT_FKMAP
1002 if (cmd_fkmap && c == K_BS)
1003 c = K_DEL;
1004#endif
1005 if (c == K_KDEL)
1006 c = K_DEL;
1007
1008 /*
1009 * delete current character is the same as backspace on next
1010 * character, except at end of line
1011 */
1012 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1013 ++ccline.cmdpos;
1014#ifdef FEAT_MBYTE
1015 if (has_mbyte && c == K_DEL)
1016 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1017 ccline.cmdbuff + ccline.cmdpos);
1018#endif
1019 if (ccline.cmdpos > 0)
1020 {
1021 char_u *p;
1022
1023 j = ccline.cmdpos;
1024 p = ccline.cmdbuff + j;
1025#ifdef FEAT_MBYTE
1026 if (has_mbyte)
1027 {
1028 p = mb_prevptr(ccline.cmdbuff, p);
1029 if (c == Ctrl_W)
1030 {
1031 while (p > ccline.cmdbuff && vim_isspace(*p))
1032 p = mb_prevptr(ccline.cmdbuff, p);
1033 i = mb_get_class(p);
1034 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1035 p = mb_prevptr(ccline.cmdbuff, p);
1036 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001037 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039 }
1040 else
1041#endif
1042 if (c == Ctrl_W)
1043 {
1044 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1045 --p;
1046 i = vim_iswordc(p[-1]);
1047 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1048 && vim_iswordc(p[-1]) == i)
1049 --p;
1050 }
1051 else
1052 --p;
1053 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1054 ccline.cmdlen -= j - ccline.cmdpos;
1055 i = ccline.cmdpos;
1056 while (i < ccline.cmdlen)
1057 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1058
1059 /* Truncate at the end, required for multi-byte chars. */
1060 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001061#ifdef FEAT_SEARCH_EXTRA
1062 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001063 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001064 search_start = save_cursor;
1065 /* save view settings, so that the screen
1066 * won't be restored at the wrong position */
1067 old_curswant = init_curswant;
1068 old_leftcol = init_leftcol;
1069 old_topline = init_topline;
1070# ifdef FEAT_DIFF
1071 old_topfill = init_topfill;
1072# endif
1073 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001074 }
1075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 redrawcmd();
1077 }
1078 else if (ccline.cmdlen == 0 && c != Ctrl_W
1079 && ccline.cmdprompt == NULL && indent == 0)
1080 {
1081 /* In ex and debug mode it doesn't make sense to return. */
1082 if (exmode_active
1083#ifdef FEAT_EVAL
1084 || ccline.cmdfirstc == '>'
1085#endif
1086 )
1087 goto cmdline_not_changed;
1088
Bram Moolenaard23a8232018-02-10 18:45:26 +01001089 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (!cmd_silent)
1091 {
1092#ifdef FEAT_RIGHTLEFT
1093 if (cmdmsg_rl)
1094 msg_col = Columns;
1095 else
1096#endif
1097 msg_col = 0;
1098 msg_putchar(' '); /* delete ':' */
1099 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001100#ifdef FEAT_SEARCH_EXTRA
1101 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001102 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 redraw_cmdline = TRUE;
1105 goto returncmd; /* back to cmd mode */
1106 }
1107 goto cmdline_changed;
1108
1109 case K_INS:
1110 case K_KINS:
1111#ifdef FEAT_FKMAP
1112 /* if Farsi mode set, we are in reverse insert mode -
1113 Do not change the mode */
1114 if (cmd_fkmap)
1115 beep_flush();
1116 else
1117#endif
1118 ccline.overstrike = !ccline.overstrike;
1119#ifdef CURSOR_SHAPE
1120 ui_cursor_shape(); /* may show different cursor shape */
1121#endif
1122 goto cmdline_not_changed;
1123
1124 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001125 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
1127 /* ":lmap" mappings exists, toggle use of mappings. */
1128 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001129#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 im_set_active(FALSE); /* Disable input method */
1131#endif
1132 if (b_im_ptr != NULL)
1133 {
1134 if (State & LANGMAP)
1135 *b_im_ptr = B_IMODE_LMAP;
1136 else
1137 *b_im_ptr = B_IMODE_NONE;
1138 }
1139 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001140#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 else
1142 {
1143 /* There are no ":lmap" mappings, toggle IM. When
1144 * 'imdisable' is set don't try getting the status, it's
1145 * always off. */
1146 if ((p_imdisable && b_im_ptr != NULL)
1147 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1148 {
1149 im_set_active(FALSE); /* Disable input method */
1150 if (b_im_ptr != NULL)
1151 *b_im_ptr = B_IMODE_NONE;
1152 }
1153 else
1154 {
1155 im_set_active(TRUE); /* Enable input method */
1156 if (b_im_ptr != NULL)
1157 *b_im_ptr = B_IMODE_IM;
1158 }
1159 }
1160#endif
1161 if (b_im_ptr != NULL)
1162 {
1163 if (b_im_ptr == &curbuf->b_p_iminsert)
1164 set_iminsert_global();
1165 else
1166 set_imsearch_global();
1167 }
1168#ifdef CURSOR_SHAPE
1169 ui_cursor_shape(); /* may show different cursor shape */
1170#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001171#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 /* Show/unshow value of 'keymap' in status lines later. */
1173 status_redraw_curbuf();
1174#endif
1175 goto cmdline_not_changed;
1176
1177/* case '@': only in very old vi */
1178 case Ctrl_U:
1179 /* delete all characters left of the cursor */
1180 j = ccline.cmdpos;
1181 ccline.cmdlen -= j;
1182 i = ccline.cmdpos = 0;
1183 while (i < ccline.cmdlen)
1184 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1185 /* Truncate at the end, required for multi-byte chars. */
1186 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001187#ifdef FEAT_SEARCH_EXTRA
1188 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001189 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 redrawcmd();
1192 goto cmdline_changed;
1193
1194#ifdef FEAT_CLIPBOARD
1195 case Ctrl_Y:
1196 /* Copy the modeless selection, if there is one. */
1197 if (clip_star.state != SELECT_CLEARED)
1198 {
1199 if (clip_star.state == SELECT_DONE)
1200 clip_copy_modeless_selection(TRUE);
1201 goto cmdline_not_changed;
1202 }
1203 break;
1204#endif
1205
1206 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1207 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001208 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001209 * ":normal" runs out of characters. */
1210 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001211 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 goto cmdline_not_changed;
1213
1214 gotesc = TRUE; /* will free ccline.cmdbuff after
1215 putting it in history */
1216 goto returncmd; /* back to cmd mode */
1217
1218 case Ctrl_R: /* insert register */
1219#ifdef USE_ON_FLY_SCROLL
1220 dont_scroll = TRUE; /* disallow scrolling here */
1221#endif
1222 putcmdline('"', TRUE);
1223 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001224 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (i == Ctrl_O)
1226 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1227 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001228 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001229 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 --no_mapping;
1231#ifdef FEAT_EVAL
1232 /*
1233 * Insert the result of an expression.
1234 * Need to save the current command line, to be able to enter
1235 * a new one...
1236 */
1237 new_cmdpos = -1;
1238 if (c == '=')
1239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1241 {
1242 beep_flush();
1243 c = ESC;
1244 }
1245 else
1246 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001247 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001249 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 }
1252#endif
1253 if (c != ESC) /* use ESC to cancel inserting register */
1254 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001255 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001256
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001257#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001258 /* When there was a serious error abort getting the
1259 * command line. */
1260 if (aborting())
1261 {
1262 gotesc = TRUE; /* will free ccline.cmdbuff after
1263 putting it in history */
1264 goto returncmd; /* back to cmd mode */
1265 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 KeyTyped = FALSE; /* Don't do p_wc completion. */
1268#ifdef FEAT_EVAL
1269 if (new_cmdpos >= 0)
1270 {
1271 /* set_cmdline_pos() was used */
1272 if (new_cmdpos > ccline.cmdlen)
1273 ccline.cmdpos = ccline.cmdlen;
1274 else
1275 ccline.cmdpos = new_cmdpos;
1276 }
1277#endif
1278 }
1279 redrawcmd();
1280 goto cmdline_changed;
1281
1282 case Ctrl_D:
1283 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1284 break; /* Use ^D as normal char instead */
1285
1286 redrawcmd();
1287 continue; /* don't do incremental search now */
1288
1289 case K_RIGHT:
1290 case K_S_RIGHT:
1291 case K_C_RIGHT:
1292 do
1293 {
1294 if (ccline.cmdpos >= ccline.cmdlen)
1295 break;
1296 i = cmdline_charsize(ccline.cmdpos);
1297 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1298 break;
1299 ccline.cmdspos += i;
1300#ifdef FEAT_MBYTE
1301 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001302 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 + ccline.cmdpos);
1304 else
1305#endif
1306 ++ccline.cmdpos;
1307 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001308 while ((c == K_S_RIGHT || c == K_C_RIGHT
1309 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1311#ifdef FEAT_MBYTE
1312 if (has_mbyte)
1313 set_cmdspos_cursor();
1314#endif
1315 goto cmdline_not_changed;
1316
1317 case K_LEFT:
1318 case K_S_LEFT:
1319 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001320 if (ccline.cmdpos == 0)
1321 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 do
1323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 --ccline.cmdpos;
1325#ifdef FEAT_MBYTE
1326 if (has_mbyte) /* move to first byte of char */
1327 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1328 ccline.cmdbuff + ccline.cmdpos);
1329#endif
1330 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1331 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001332 while (ccline.cmdpos > 0
1333 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001334 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1336#ifdef FEAT_MBYTE
1337 if (has_mbyte)
1338 set_cmdspos_cursor();
1339#endif
1340 goto cmdline_not_changed;
1341
1342 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001343 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001344 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
Bram Moolenaar4770d092006-01-12 23:22:24 +00001346#ifdef FEAT_GUI_W32
1347 /* On Win32 ignore <M-F4>, we get it when closing the window was
1348 * cancelled. */
1349 case K_F4:
1350 if (mod_mask == MOD_MASK_ALT)
1351 {
1352 redrawcmd(); /* somehow the cmdline is cleared */
1353 goto cmdline_not_changed;
1354 }
1355 break;
1356#endif
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358#ifdef FEAT_MOUSE
1359 case K_MIDDLEDRAG:
1360 case K_MIDDLERELEASE:
1361 goto cmdline_not_changed; /* Ignore mouse */
1362
1363 case K_MIDDLEMOUSE:
1364# ifdef FEAT_GUI
1365 /* When GUI is active, also paste when 'mouse' is empty */
1366 if (!gui.in_use)
1367# endif
1368 if (!mouse_has(MOUSE_COMMAND))
1369 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001370# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001372 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001374# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001375 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 redrawcmd();
1377 goto cmdline_changed;
1378
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001379# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001381 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 redrawcmd();
1383 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386 case K_LEFTDRAG:
1387 case K_LEFTRELEASE:
1388 case K_RIGHTDRAG:
1389 case K_RIGHTRELEASE:
1390 /* Ignore drag and release events when the button-down wasn't
1391 * seen before. */
1392 if (ignore_drag_release)
1393 goto cmdline_not_changed;
1394 /* FALLTHROUGH */
1395 case K_LEFTMOUSE:
1396 case K_RIGHTMOUSE:
1397 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1398 ignore_drag_release = TRUE;
1399 else
1400 ignore_drag_release = FALSE;
1401# ifdef FEAT_GUI
1402 /* When GUI is active, also move when 'mouse' is empty */
1403 if (!gui.in_use)
1404# endif
1405 if (!mouse_has(MOUSE_COMMAND))
1406 goto cmdline_not_changed; /* Ignore mouse */
1407# ifdef FEAT_CLIPBOARD
1408 if (mouse_row < cmdline_row && clip_star.available)
1409 {
1410 int button, is_click, is_drag;
1411
1412 /*
1413 * Handle modeless selection.
1414 */
1415 button = get_mouse_button(KEY2TERMCAP1(c),
1416 &is_click, &is_drag);
1417 if (mouse_model_popup() && button == MOUSE_LEFT
1418 && (mod_mask & MOD_MASK_SHIFT))
1419 {
1420 /* Translate shift-left to right button. */
1421 button = MOUSE_RIGHT;
1422 mod_mask &= ~MOD_MASK_SHIFT;
1423 }
1424 clip_modeless(button, is_click, is_drag);
1425 goto cmdline_not_changed;
1426 }
1427# endif
1428
1429 set_cmdspos();
1430 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1431 ++ccline.cmdpos)
1432 {
1433 i = cmdline_charsize(ccline.cmdpos);
1434 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1435 && mouse_col < ccline.cmdspos % Columns + i)
1436 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001437# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (has_mbyte)
1439 {
1440 /* Count ">" for double-wide char that doesn't fit. */
1441 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001442 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 + ccline.cmdpos) - 1;
1444 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 ccline.cmdspos += i;
1447 }
1448 goto cmdline_not_changed;
1449
1450 /* Mouse scroll wheel: ignored here */
1451 case K_MOUSEDOWN:
1452 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001453 case K_MOUSELEFT:
1454 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /* Alternate buttons ignored here */
1456 case K_X1MOUSE:
1457 case K_X1DRAG:
1458 case K_X1RELEASE:
1459 case K_X2MOUSE:
1460 case K_X2DRAG:
1461 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001462 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 goto cmdline_not_changed;
1464
1465#endif /* FEAT_MOUSE */
1466
1467#ifdef FEAT_GUI
1468 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1469 case K_LEFTRELEASE_NM:
1470 goto cmdline_not_changed;
1471
1472 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001473 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 gui_do_scroll();
1476 redrawcmd();
1477 }
1478 goto cmdline_not_changed;
1479
1480 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001481 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001483 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 redrawcmd();
1485 }
1486 goto cmdline_not_changed;
1487#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001488#ifdef FEAT_GUI_TABLINE
1489 case K_TABLINE:
1490 case K_TABMENU:
1491 /* Don't want to change any tabs here. Make sure the same tab
1492 * is still selected. */
1493 if (gui_use_tabline())
1494 gui_mch_set_curtab(tabpage_index(curtab));
1495 goto cmdline_not_changed;
1496#endif
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 case K_SELECT: /* end of Select mode mapping - ignore */
1499 goto cmdline_not_changed;
1500
1501 case Ctrl_B: /* begin of command line */
1502 case K_HOME:
1503 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 case K_S_HOME:
1505 case K_C_HOME:
1506 ccline.cmdpos = 0;
1507 set_cmdspos();
1508 goto cmdline_not_changed;
1509
1510 case Ctrl_E: /* end of command line */
1511 case K_END:
1512 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 case K_S_END:
1514 case K_C_END:
1515 ccline.cmdpos = ccline.cmdlen;
1516 set_cmdspos_cursor();
1517 goto cmdline_not_changed;
1518
1519 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001520 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 break;
1522 goto cmdline_changed;
1523
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001524 case Ctrl_L:
1525#ifdef FEAT_SEARCH_EXTRA
1526 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1527 {
1528 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001529 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001530 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001531 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001532 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001533 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001534 c = gchar_cursor();
1535 /* If 'ignorecase' and 'smartcase' are set and the
1536 * command line has no uppercase characters, convert
1537 * the character to lowercase */
1538 if (p_ic && p_scs
1539 && !pat_has_uppercase(ccline.cmdbuff))
1540 c = MB_TOLOWER(c);
1541 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001542 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001543 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001544 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001545 != NULL)
1546 {
1547 /* put a backslash before special
1548 * characters */
1549 stuffcharReadbuff(c);
1550 c = '\\';
1551 }
1552 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001553 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001554 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001555 }
1556 goto cmdline_not_changed;
1557 }
1558#endif
1559
1560 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001561 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 break;
1563 goto cmdline_changed;
1564
1565 case Ctrl_N: /* next match */
1566 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001567 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001569 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1570 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001572 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001575 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 case K_UP:
1577 case K_DOWN:
1578 case K_S_UP:
1579 case K_S_DOWN:
1580 case K_PAGEUP:
1581 case K_KPAGEUP:
1582 case K_PAGEDOWN:
1583 case K_KPAGEDOWN:
1584 if (hislen == 0 || firstc == NUL) /* no history */
1585 goto cmdline_not_changed;
1586
1587 i = hiscnt;
1588
1589 /* save current command string so it can be restored later */
1590 if (lookfor == NULL)
1591 {
1592 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1593 goto cmdline_not_changed;
1594 lookfor[ccline.cmdpos] = NUL;
1595 }
1596
1597 j = (int)STRLEN(lookfor);
1598 for (;;)
1599 {
1600 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001601 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001602 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 if (hiscnt == hislen) /* first time */
1605 hiscnt = hisidx[histype];
1606 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1607 hiscnt = hislen - 1;
1608 else if (hiscnt != hisidx[histype] + 1)
1609 --hiscnt;
1610 else /* at top of list */
1611 {
1612 hiscnt = i;
1613 break;
1614 }
1615 }
1616 else /* one step forwards */
1617 {
1618 /* on last entry, clear the line */
1619 if (hiscnt == hisidx[histype])
1620 {
1621 hiscnt = hislen;
1622 break;
1623 }
1624
1625 /* not on a history line, nothing to do */
1626 if (hiscnt == hislen)
1627 break;
1628 if (hiscnt == hislen - 1) /* wrap around */
1629 hiscnt = 0;
1630 else
1631 ++hiscnt;
1632 }
1633 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1634 {
1635 hiscnt = i;
1636 break;
1637 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001638 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001639 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 || STRNCMP(history[histype][hiscnt].hisstr,
1641 lookfor, (size_t)j) == 0)
1642 break;
1643 }
1644
1645 if (hiscnt != i) /* jumped to other entry */
1646 {
1647 char_u *p;
1648 int len;
1649 int old_firstc;
1650
1651 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001652 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (hiscnt == hislen)
1654 p = lookfor; /* back to the old one */
1655 else
1656 p = history[histype][hiscnt].hisstr;
1657
1658 if (histype == HIST_SEARCH
1659 && p != lookfor
1660 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1661 {
1662 /* Correct for the separator character used when
1663 * adding the history entry vs the one used now.
1664 * First loop: count length.
1665 * Second loop: copy the characters. */
1666 for (i = 0; i <= 1; ++i)
1667 {
1668 len = 0;
1669 for (j = 0; p[j] != NUL; ++j)
1670 {
1671 /* Replace old sep with new sep, unless it is
1672 * escaped. */
1673 if (p[j] == old_firstc
1674 && (j == 0 || p[j - 1] != '\\'))
1675 {
1676 if (i > 0)
1677 ccline.cmdbuff[len] = firstc;
1678 }
1679 else
1680 {
1681 /* Escape new sep, unless it is already
1682 * escaped. */
1683 if (p[j] == firstc
1684 && (j == 0 || p[j - 1] != '\\'))
1685 {
1686 if (i > 0)
1687 ccline.cmdbuff[len] = '\\';
1688 ++len;
1689 }
1690 if (i > 0)
1691 ccline.cmdbuff[len] = p[j];
1692 }
1693 ++len;
1694 }
1695 if (i == 0)
1696 {
1697 alloc_cmdbuff(len);
1698 if (ccline.cmdbuff == NULL)
1699 goto returncmd;
1700 }
1701 }
1702 ccline.cmdbuff[len] = NUL;
1703 }
1704 else
1705 {
1706 alloc_cmdbuff((int)STRLEN(p));
1707 if (ccline.cmdbuff == NULL)
1708 goto returncmd;
1709 STRCPY(ccline.cmdbuff, p);
1710 }
1711
1712 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1713 redrawcmd();
1714 goto cmdline_changed;
1715 }
1716 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001717#endif
1718 goto cmdline_not_changed;
1719
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001720#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001721 case Ctrl_G: /* next match */
1722 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001723 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1724 {
1725 pos_T t;
Bram Moolenaard0480092017-11-16 22:20:39 +01001726 char_u *pat;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001727 int search_flags = SEARCH_NOOF;
Bram Moolenaar11956692016-08-27 16:26:56 +02001728
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001729 if (ccline.cmdlen == 0)
1730 goto cmdline_not_changed;
1731
Bram Moolenaard0480092017-11-16 22:20:39 +01001732 if (firstc == ccline.cmdbuff[0])
1733 pat = last_search_pattern();
1734 else
1735 pat = ccline.cmdbuff;
1736
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001737 save_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001738 cursor_off();
1739 out_flush();
1740 if (c == Ctrl_G)
1741 {
1742 t = match_end;
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001743 if (LT_POS(match_start, match_end))
1744 /* start searching at the end of the match
1745 * not at the beginning of the next column */
1746 (void)decl(&t);
Bram Moolenaar11956692016-08-27 16:26:56 +02001747 search_flags += SEARCH_COL;
1748 }
1749 else
1750 t = match_start;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001751 if (!p_hls)
1752 search_flags += SEARCH_KEEP;
Bram Moolenaar11956692016-08-27 16:26:56 +02001753 ++emsg_off;
1754 i = searchit(curwin, curbuf, &t,
1755 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaard0480092017-11-16 22:20:39 +01001756 pat, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001757 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001758 --emsg_off;
1759 if (i)
1760 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001761 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001762 match_end = t;
1763 match_start = t;
1764 if (c == Ctrl_T && firstc == '/')
1765 {
1766 /* move just before the current match, so that
1767 * when nv_search finishes the cursor will be
1768 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001769 search_start = t;
1770 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001771 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001772 else if (c == Ctrl_G && firstc == '?')
1773 {
1774 /* move just after the current match, so that
1775 * when nv_search finishes the cursor will be
1776 * put back on the match */
1777 search_start = t;
1778 (void)incl(&search_start);
1779 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001780 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001781 {
1782 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001783 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001784 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001785 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001786 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001787 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001788 }
1789
1790 set_search_match(&match_end);
1791 curwin->w_cursor = match_start;
1792 changed_cline_bef_curs();
1793 update_topline();
1794 validate_cursor();
1795 highlight_match = TRUE;
1796 old_curswant = curwin->w_curswant;
1797 old_leftcol = curwin->w_leftcol;
1798 old_topline = curwin->w_topline;
1799# ifdef FEAT_DIFF
1800 old_topfill = curwin->w_topfill;
1801# endif
1802 old_botline = curwin->w_botline;
1803 update_screen(NOT_VALID);
1804 redrawcmdline();
1805 }
1806 else
1807 vim_beep(BO_ERROR);
Bram Moolenaara1d5c152017-12-16 19:05:22 +01001808 restore_last_search_pattern();
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001809 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001810 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#endif
1813
1814 case Ctrl_V:
1815 case Ctrl_Q:
1816#ifdef FEAT_MOUSE
1817 ignore_drag_release = TRUE;
1818#endif
1819 putcmdline('^', TRUE);
1820 c = get_literal(); /* get next (two) character(s) */
1821 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001822 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#ifdef FEAT_MBYTE
1824 /* may need to remove ^ when composing char was typed */
1825 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1826 {
1827 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1828 msg_putchar(' ');
1829 cursorcmd();
1830 }
1831#endif
1832 break;
1833
1834#ifdef FEAT_DIGRAPHS
1835 case Ctrl_K:
1836#ifdef FEAT_MOUSE
1837 ignore_drag_release = TRUE;
1838#endif
1839 putcmdline('?', TRUE);
1840#ifdef USE_ON_FLY_SCROLL
1841 dont_scroll = TRUE; /* disallow scrolling here */
1842#endif
1843 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001844 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (c != NUL)
1846 break;
1847
1848 redrawcmd();
1849 goto cmdline_not_changed;
1850#endif /* FEAT_DIGRAPHS */
1851
1852#ifdef FEAT_RIGHTLEFT
1853 case Ctrl__: /* CTRL-_: switch language mode */
1854 if (!p_ari)
1855 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001856# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (p_altkeymap)
1858 {
1859 cmd_fkmap = !cmd_fkmap;
1860 if (cmd_fkmap) /* in Farsi always in Insert mode */
1861 ccline.overstrike = FALSE;
1862 }
1863 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 cmd_hkmap = !cmd_hkmap;
1866 goto cmdline_not_changed;
1867#endif
1868
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001869 case K_PS:
1870 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1871 goto cmdline_changed;
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 default:
1874#ifdef UNIX
1875 if (c == intr_char)
1876 {
1877 gotesc = TRUE; /* will free ccline.cmdbuff after
1878 putting it in history */
1879 goto returncmd; /* back to Normal mode */
1880 }
1881#endif
1882 /*
1883 * Normal character with no special meaning. Just set mod_mask
1884 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1885 * the string <S-Space>. This should only happen after ^V.
1886 */
1887 if (!IS_SPECIAL(c))
1888 mod_mask = 0x0;
1889 break;
1890 }
1891 /*
1892 * End of switch on command line character.
1893 * We come here if we have a normal character.
1894 */
1895
Bram Moolenaarede3e632013-06-23 16:16:19 +02001896 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897#ifdef FEAT_MBYTE
1898 /* Add ABBR_OFF for characters above 0x100, this is
1899 * what check_abbr() expects. */
1900 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1901#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001902 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 goto cmdline_changed;
1904
1905 /*
1906 * put the character in the command line
1907 */
1908 if (IS_SPECIAL(c) || mod_mask != 0)
1909 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1910 else
1911 {
1912#ifdef FEAT_MBYTE
1913 if (has_mbyte)
1914 {
1915 j = (*mb_char2bytes)(c, IObuff);
1916 IObuff[j] = NUL; /* exclude composing chars */
1917 put_on_cmdline(IObuff, j, TRUE);
1918 }
1919 else
1920#endif
1921 {
1922 IObuff[0] = c;
1923 put_on_cmdline(IObuff, 1, TRUE);
1924 }
1925 }
1926 goto cmdline_changed;
1927
1928/*
1929 * This part implements incremental searches for "/" and "?"
1930 * Jump to cmdline_not_changed when a character has been read but the command
1931 * line did not change. Then we only search and redraw if something changed in
1932 * the past.
1933 * Jump to cmdline_changed when the command line did change.
1934 * (Sorry for the goto's, I know it is ugly).
1935 */
1936cmdline_not_changed:
1937#ifdef FEAT_SEARCH_EXTRA
1938 if (!incsearch_postponed)
1939 continue;
1940#endif
1941
1942cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01001943 /* Trigger CmdlineChanged autocommands. */
1944 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01001945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946#ifdef FEAT_SEARCH_EXTRA
1947 /*
1948 * 'incsearch' highlighting.
1949 */
1950 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1951 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001952 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001953#ifdef FEAT_RELTIME
1954 proftime_T tm;
1955#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 /* if there is a character waiting, search and redraw later */
1958 if (char_avail())
1959 {
1960 incsearch_postponed = TRUE;
1961 continue;
1962 }
1963 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001964 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001965 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967 /* If there is no command line, don't do anything */
1968 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001969 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 i = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001971 SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001972 redraw_all_later(SOME_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 else
1975 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001976 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 cursor_off(); /* so the user knows we're busy */
1978 out_flush();
1979 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001980#ifdef FEAT_RELTIME
1981 /* Set the time limit to half a second. */
1982 profile_setlimit(500L, &tm);
1983#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001984 if (!p_hls)
1985 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001987 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001988#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001989 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001990#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001991 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001992#endif
1993 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 --emsg_off;
1995 /* if interrupted while searching, behave like it failed */
1996 if (got_int)
1997 {
1998 (void)vpeekc(); /* remove <C-C> from input stream */
1999 got_int = FALSE; /* don't abandon the command line */
2000 i = 0;
2001 }
2002 else if (char_avail())
2003 /* cancelled searching because a char was typed */
2004 incsearch_postponed = TRUE;
2005 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002006 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 highlight_match = TRUE; /* highlight position */
2008 else
2009 highlight_match = FALSE; /* remove highlight */
2010
2011 /* first restore the old curwin values, so the screen is
2012 * positioned in the same way as the actual search command */
2013 curwin->w_leftcol = old_leftcol;
2014 curwin->w_topline = old_topline;
2015# ifdef FEAT_DIFF
2016 curwin->w_topfill = old_topfill;
2017# endif
2018 curwin->w_botline = old_botline;
2019 changed_cline_bef_curs();
2020 update_topline();
2021
2022 if (i != 0)
2023 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002024 pos_T save_pos = curwin->w_cursor;
2025
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002026 match_start = curwin->w_cursor;
2027 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002029 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002030 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002031 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002033 else
2034 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002035
Bram Moolenaar66216052017-12-16 16:33:44 +01002036 /* Disable 'hlsearch' highlighting if the pattern matches
2037 * everything. Avoids a flash when typing "foo\|". */
2038 if (empty_pattern(ccline.cmdbuff))
2039 SET_NO_HLSEARCH(TRUE);
2040
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002042 /* May redraw the status line to show the cursor position. */
2043 if (p_ru && curwin->w_status_height > 0)
2044 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002046 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002047 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002048 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002049 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002050
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002051 /* Leave it at the end to make CTRL-R CTRL-W work. */
2052 if (i != 0)
2053 curwin->w_cursor = end_pos;
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 msg_starthere();
2056 redrawcmdline();
2057 did_incsearch = TRUE;
2058 }
2059#else /* FEAT_SEARCH_EXTRA */
2060 ;
2061#endif
2062
2063#ifdef FEAT_RIGHTLEFT
2064 if (cmdmsg_rl
2065# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002066 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067# endif
2068 )
2069 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002070 * right-left typing. Not efficient, but it works.
2071 * Do it only when there are no characters left to read
2072 * to avoid useless intermediate redraws. */
2073 if (vpeekc() == NUL)
2074 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075#endif
2076 }
2077
2078returncmd:
2079
2080#ifdef FEAT_RIGHTLEFT
2081 cmdmsg_rl = FALSE;
2082#endif
2083
2084#ifdef FEAT_FKMAP
2085 cmd_fkmap = 0;
2086#endif
2087
2088 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002089 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091#ifdef FEAT_SEARCH_EXTRA
2092 if (did_incsearch)
2093 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002094 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002095 curwin->w_cursor = save_cursor;
2096 else
2097 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002098 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002099 {
2100 /* put the '" mark at the original position */
2101 curwin->w_cursor = save_cursor;
2102 setpcmark();
2103 }
2104 curwin->w_cursor = search_start;
2105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 curwin->w_curswant = old_curswant;
2107 curwin->w_leftcol = old_leftcol;
2108 curwin->w_topline = old_topline;
2109# ifdef FEAT_DIFF
2110 curwin->w_topfill = old_topfill;
2111# endif
2112 curwin->w_botline = old_botline;
2113 highlight_match = FALSE;
2114 validate_cursor(); /* needed for TAB */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01002115 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117#endif
2118
2119 if (ccline.cmdbuff != NULL)
2120 {
2121 /*
2122 * Put line in history buffer (":" and "=" only when it was typed).
2123 */
2124#ifdef FEAT_CMDHIST
2125 if (ccline.cmdlen && firstc != NUL
2126 && (some_key_typed || histype == HIST_SEARCH))
2127 {
2128 add_to_history(histype, ccline.cmdbuff, TRUE,
2129 histype == HIST_SEARCH ? firstc : NUL);
2130 if (firstc == ':')
2131 {
2132 vim_free(new_last_cmdline);
2133 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2134 }
2135 }
2136#endif
2137
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002138 if (gotesc)
2139 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141
2142 /*
2143 * If the screen was shifted up, redraw the whole screen (later).
2144 * If the line is too long, clear it, so ruler and shown command do
2145 * not get printed in the middle of it.
2146 */
2147 msg_check();
2148 msg_scroll = save_msg_scroll;
2149 redir_off = FALSE;
2150
2151 /* When the command line was typed, no need for a wait-return prompt. */
2152 if (some_key_typed)
2153 need_wait_return = FALSE;
2154
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002155 /* Trigger CmdlineLeave autocommands. */
2156 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002159#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2161 im_save_status(b_im_ptr);
2162 im_set_active(FALSE);
2163#endif
2164#ifdef FEAT_MOUSE
2165 setmouse();
2166#endif
2167#ifdef CURSOR_SHAPE
2168 ui_cursor_shape(); /* may show different cursor shape */
2169#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002170 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002172 {
2173 char_u *p = ccline.cmdbuff;
2174
2175 /* Make ccline empty, getcmdline() may try to use it. */
2176 ccline.cmdbuff = NULL;
2177 return p;
2178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179}
2180
2181#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2182/*
2183 * Get a command line with a prompt.
2184 * This is prepared to be called recursively from getcmdline() (e.g. by
2185 * f_input() when evaluating an expression from CTRL-R =).
2186 * Returns the command line in allocated memory, or NULL.
2187 */
2188 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002189getcmdline_prompt(
2190 int firstc,
2191 char_u *prompt, /* command line prompt */
2192 int attr, /* attributes for prompt */
2193 int xp_context, /* type of expansion */
2194 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195{
2196 char_u *s;
2197 struct cmdline_info save_ccline;
2198 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002199 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002201 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 ccline.cmdprompt = prompt;
2203 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002204# ifdef FEAT_EVAL
2205 ccline.xp_context = xp_context;
2206 ccline.xp_arg = xp_arg;
2207 ccline.input_fn = (firstc == '@');
2208# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002209 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002211 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002212 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002213 /* Restore msg_col, the prompt from input() may have changed it.
2214 * But only if called recursively and the commandline is therefore being
2215 * restored to an old one; if not, the input() prompt stays on the screen,
2216 * so we need its modified msg_col left intact. */
2217 if (ccline.cmdbuff != NULL)
2218 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 return s;
2221}
2222#endif
2223
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002224/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002225 * Return TRUE when the text must not be changed and we can't switch to
2226 * another window or buffer. Used when editing the command line, evaluating
2227 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002228 */
2229 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002230text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002231{
2232#ifdef FEAT_CMDWIN
2233 if (cmdwin_type != 0)
2234 return TRUE;
2235#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002236 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002237}
2238
2239/*
2240 * Give an error message for a command that isn't allowed while the cmdline
2241 * window is open or editing the cmdline in another way.
2242 */
2243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002244text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002245{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002246 EMSG(_(get_text_locked_msg()));
2247}
2248
2249 char_u *
2250get_text_locked_msg(void)
2251{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002252#ifdef FEAT_CMDWIN
2253 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002254 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002255#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002256 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002257}
2258
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002260 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2261 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002262 */
2263 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002264curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002265{
2266 if (curbuf_lock > 0)
2267 {
2268 EMSG(_("E788: Not allowed to edit another buffer now"));
2269 return TRUE;
2270 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002271 return allbuf_locked();
2272}
2273
2274/*
2275 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2276 * message.
2277 */
2278 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002279allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002280{
2281 if (allbuf_lock > 0)
2282 {
2283 EMSG(_("E811: Not allowed to change buffer information now"));
2284 return TRUE;
2285 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002286 return FALSE;
2287}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002290cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2293 if (cmdline_star > 0) /* showing '*', always 1 position */
2294 return 1;
2295#endif
2296 return ptr2cells(ccline.cmdbuff + idx);
2297}
2298
2299/*
2300 * Compute the offset of the cursor on the command line for the prompt and
2301 * indent.
2302 */
2303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002304set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002306 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 ccline.cmdspos = 1 + ccline.cmdindent;
2308 else
2309 ccline.cmdspos = 0 + ccline.cmdindent;
2310}
2311
2312/*
2313 * Compute the screen position for the cursor on the command line.
2314 */
2315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002316set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 int i, m, c;
2319
2320 set_cmdspos();
2321 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002324 if (m < 0) /* overflow, Columns or Rows at weird value */
2325 m = MAXCOL;
2326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 else
2328 m = MAXCOL;
2329 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2330 {
2331 c = cmdline_charsize(i);
2332#ifdef FEAT_MBYTE
2333 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2334 if (has_mbyte)
2335 correct_cmdspos(i, c);
2336#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002337 /* If the cmdline doesn't fit, show cursor on last visible char.
2338 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if ((ccline.cmdspos += c) >= m)
2340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 ccline.cmdspos -= c;
2342 break;
2343 }
2344#ifdef FEAT_MBYTE
2345 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002346 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#endif
2348 }
2349}
2350
2351#ifdef FEAT_MBYTE
2352/*
2353 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2354 * character that doesn't fit, so that a ">" must be displayed.
2355 */
2356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002357correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002359 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2361 && ccline.cmdspos % Columns + cells > Columns)
2362 ccline.cmdspos++;
2363}
2364#endif
2365
2366/*
2367 * Get an Ex command line for the ":" command.
2368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002370getexline(
2371 int c, /* normally ':', NUL for ":append" */
2372 void *cookie UNUSED,
2373 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 /* When executing a register, remove ':' that's in front of each line. */
2376 if (exec_from_reg && vpeekc() == ':')
2377 (void)vgetc();
2378 return getcmdline(c, 1L, indent);
2379}
2380
2381/*
2382 * Get an Ex command line for Ex mode.
2383 * In Ex mode we only use the OS supplied line editing features and no
2384 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002385 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002388getexmodeline(
2389 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002390 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002391 void *cookie UNUSED,
2392 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002394 garray_T line_ga;
2395 char_u *pend;
2396 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002397 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002398 int escaped = FALSE; /* CTRL-V typed */
2399 int vcol = 0;
2400 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002401 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002402 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /* Switch cursor on now. This avoids that it happens after the "\n", which
2405 * confuses the system function that computes tabstops. */
2406 cursor_on();
2407
2408 /* always start in column 0; write a newline if necessary */
2409 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002410 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002412 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002414 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002415 if (p_prompt)
2416 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 while (indent-- > 0)
2418 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421
2422 ga_init2(&line_ga, 1, 30);
2423
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002424 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002425 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002426 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002427 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002428 while (indent >= 8)
2429 {
2430 ga_append(&line_ga, TAB);
2431 msg_puts((char_u *)" ");
2432 indent -= 8;
2433 }
2434 while (indent-- > 0)
2435 {
2436 ga_append(&line_ga, ' ');
2437 msg_putchar(' ');
2438 }
2439 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002440 ++no_mapping;
2441 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 /*
2444 * Get the line, one character at a time.
2445 */
2446 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002447 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002449 long sw;
2450 char_u *s;
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (ga_grow(&line_ga, 40) == FAIL)
2453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaarba748c82017-02-26 14:00:07 +01002455 /*
2456 * Get one character at a time.
2457 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002458 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002459
2460 /* Check for a ":normal" command and no more characters left. */
2461 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2462 c1 = '\n';
2463 else
2464 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002467 * Handle line editing.
2468 * Previously this was left to the system, putting the terminal in
2469 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002471 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002473 msg_putchar('\n');
2474 break;
2475 }
2476
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002477 if (c1 == K_PS)
2478 {
2479 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2480 goto redraw;
2481 }
2482
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002483 if (!escaped)
2484 {
2485 /* CR typed means "enter", which is NL */
2486 if (c1 == '\r')
2487 c1 = '\n';
2488
2489 if (c1 == BS || c1 == K_BS
2490 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002492 if (line_ga.ga_len > 0)
2493 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002494#ifdef FEAT_MBYTE
2495 if (has_mbyte)
2496 {
2497 p = (char_u *)line_ga.ga_data;
2498 p[line_ga.ga_len] = NUL;
2499 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2500 line_ga.ga_len -= len;
2501 }
2502 else
2503#endif
2504 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002505 goto redraw;
2506 }
2507 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
2509
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002510 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002512 msg_col = startcol;
2513 msg_clr_eos();
2514 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002515 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002516 }
2517
2518 if (c1 == Ctrl_T)
2519 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002520 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002521 p = (char_u *)line_ga.ga_data;
2522 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002523 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002524 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002525add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002526 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002528 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002529 p = (char_u *)line_ga.ga_data;
2530 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002531 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2532 *s = ' ';
2533 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002535redraw:
2536 /* redraw the line */
2537 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002538 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002539 p = (char_u *)line_ga.ga_data;
2540 p[line_ga.ga_len] = NUL;
2541 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002543 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002545 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002547 msg_putchar(' ');
2548 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002549 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002553 len = MB_PTR2LEN(p);
2554 msg_outtrans_len(p, len);
2555 vcol += ptr2cells(p);
2556 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002559 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002560 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002561 continue;
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002564 if (c1 == Ctrl_D)
2565 {
2566 /* Delete one shiftwidth. */
2567 p = (char_u *)line_ga.ga_data;
2568 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002570 if (prev_char == '^')
2571 ex_keep_indent = TRUE;
2572 indent = 0;
2573 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 else
2576 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002578 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002579 if (indent > 0)
2580 {
2581 --indent;
2582 indent -= indent % get_sw_value(curbuf);
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002585 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002586 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002587 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002588 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2589 --line_ga.ga_len;
2590 }
2591 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002593
2594 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2595 {
2596 escaped = TRUE;
2597 continue;
2598 }
2599
2600 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2601 if (IS_SPECIAL(c1))
2602 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002604
2605 if (IS_SPECIAL(c1))
2606 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002607#ifdef FEAT_MBYTE
2608 if (has_mbyte)
2609 len = (*mb_char2bytes)(c1,
2610 (char_u *)line_ga.ga_data + line_ga.ga_len);
2611 else
2612#endif
2613 {
2614 len = 1;
2615 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2616 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002617 if (c1 == '\n')
2618 msg_putchar('\n');
2619 else if (c1 == TAB)
2620 {
2621 /* Don't use chartabsize(), 'ts' can be different */
2622 do
2623 {
2624 msg_putchar(' ');
2625 } while (++vcol % 8);
2626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002629 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002630 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002633 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002634 escaped = FALSE;
2635
2636 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002637 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002639 /* We are done when a NL is entered, but not when it comes after an
2640 * odd number of backslashes, that results in a NUL. */
2641 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002643 int bcount = 0;
2644
2645 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2646 ++bcount;
2647
2648 if (bcount > 0)
2649 {
2650 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2651 * "\NL", etc. */
2652 line_ga.ga_len -= (bcount + 1) / 2;
2653 pend -= (bcount + 1) / 2;
2654 pend[-1] = '\n';
2655 }
2656
2657 if ((bcount & 1) == 0)
2658 {
2659 --line_ga.ga_len;
2660 --pend;
2661 *pend = NUL;
2662 break;
2663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665 }
2666
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002667 --no_mapping;
2668 --allow_keys;
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 /* make following messages go to the next line */
2671 msg_didout = FALSE;
2672 msg_col = 0;
2673 if (msg_row < Rows - 1)
2674 ++msg_row;
2675 emsg_on_display = FALSE; /* don't want ui_delay() */
2676
2677 if (got_int)
2678 ga_clear(&line_ga);
2679
2680 return (char_u *)line_ga.ga_data;
2681}
2682
Bram Moolenaare344bea2005-09-01 20:46:49 +00002683# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2684 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685/*
2686 * Return TRUE if ccline.overstrike is on.
2687 */
2688 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002689cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 return ccline.overstrike;
2692}
2693
2694/*
2695 * Return TRUE if the cursor is at the end of the cmdline.
2696 */
2697 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002698cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
2700 return (ccline.cmdpos >= ccline.cmdlen);
2701}
2702#endif
2703
Bram Moolenaar9372a112005-12-06 19:59:18 +00002704#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705/*
2706 * Return the virtual column number at the current cursor position.
2707 * This is used by the IM code to obtain the start of the preedit string.
2708 */
2709 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002710cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2713 return MAXCOL;
2714
2715# ifdef FEAT_MBYTE
2716 if (has_mbyte)
2717 {
2718 colnr_T col;
2719 int i = 0;
2720
2721 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002722 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
2724 return col;
2725 }
2726 else
2727# endif
2728 return ccline.cmdpos;
2729}
2730#endif
2731
2732#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2733/*
2734 * If part of the command line is an IM preedit string, redraw it with
2735 * IM feedback attributes. The cursor position is restored after drawing.
2736 */
2737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002738redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 if ((State & CMDLINE)
2741 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002742 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 && !p_imdisable
2744 && im_is_preediting())
2745 {
2746 int cmdpos = 0;
2747 int cmdspos;
2748 int old_row;
2749 int old_col;
2750 colnr_T col;
2751
2752 old_row = msg_row;
2753 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002754 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
2756# ifdef FEAT_MBYTE
2757 if (has_mbyte)
2758 {
2759 for (col = 0; col < preedit_start_col
2760 && cmdpos < ccline.cmdlen; ++col)
2761 {
2762 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002763 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765 }
2766 else
2767# endif
2768 {
2769 cmdspos += preedit_start_col;
2770 cmdpos += preedit_start_col;
2771 }
2772
2773 msg_row = cmdline_row + (cmdspos / (int)Columns);
2774 msg_col = cmdspos % (int)Columns;
2775 if (msg_row >= Rows)
2776 msg_row = Rows - 1;
2777
2778 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2779 {
2780 int char_len;
2781 int char_attr;
2782
2783 char_attr = im_get_feedback_attr(col);
2784 if (char_attr < 0)
2785 break; /* end of preedit string */
2786
2787# ifdef FEAT_MBYTE
2788 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002789 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 else
2791# endif
2792 char_len = 1;
2793
2794 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2795 cmdpos += char_len;
2796 }
2797
2798 msg_row = old_row;
2799 msg_col = old_col;
2800 }
2801}
2802#endif /* FEAT_XIM && FEAT_GUI_GTK */
2803
2804/*
2805 * Allocate a new command line buffer.
2806 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2807 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2808 */
2809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002810alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
2812 /*
2813 * give some extra space to avoid having to allocate all the time
2814 */
2815 if (len < 80)
2816 len = 100;
2817 else
2818 len += 20;
2819
2820 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2821 ccline.cmdbufflen = len;
2822}
2823
2824/*
2825 * Re-allocate the command line to length len + something extra.
2826 * return FAIL for failure, OK otherwise
2827 */
2828 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002829realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 char_u *p;
2832
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002833 if (len < ccline.cmdbufflen)
2834 return OK; /* no need to resize */
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 p = ccline.cmdbuff;
2837 alloc_cmdbuff(len); /* will get some more */
2838 if (ccline.cmdbuff == NULL) /* out of memory */
2839 {
2840 ccline.cmdbuff = p; /* keep the old one */
2841 return FAIL;
2842 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002843 /* There isn't always a NUL after the command, but it may need to be
2844 * there, thus copy up to the NUL and add a NUL. */
2845 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2846 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002848
2849 if (ccline.xpc != NULL
2850 && ccline.xpc->xp_pattern != NULL
2851 && ccline.xpc->xp_context != EXPAND_NOTHING
2852 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2853 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002854 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002855
2856 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2857 * to point into the newly allocated memory. */
2858 if (i >= 0 && i <= ccline.cmdlen)
2859 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2860 }
2861
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 return OK;
2863}
2864
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002865#if defined(FEAT_ARABIC) || defined(PROTO)
2866static char_u *arshape_buf = NULL;
2867
2868# if defined(EXITFREE) || defined(PROTO)
2869 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002870free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002871{
2872 vim_free(arshape_buf);
2873}
2874# endif
2875#endif
2876
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877/*
2878 * Draw part of the cmdline at the current cursor position. But draw stars
2879 * when cmdline_star is TRUE.
2880 */
2881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002882draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
2884#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2885 int i;
2886
2887 if (cmdline_star > 0)
2888 for (i = 0; i < len; ++i)
2889 {
2890 msg_putchar('*');
2891# ifdef FEAT_MBYTE
2892 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002893 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894# endif
2895 }
2896 else
2897#endif
2898#ifdef FEAT_ARABIC
2899 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 static int buflen = 0;
2902 char_u *p;
2903 int j;
2904 int newlen = 0;
2905 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002906 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 int prev_c = 0;
2908 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002909 int u8c;
2910 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 /*
2914 * Do arabic shaping into a temporary buffer. This is very
2915 * inefficient!
2916 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002917 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 /* Re-allocate the buffer. We keep it around to avoid a lot of
2920 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002921 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002922 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002923 arshape_buf = alloc(buflen);
2924 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 return; /* out of memory */
2926 }
2927
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002928 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2929 {
2930 /* Prepend a space to draw the leading composing char on. */
2931 arshape_buf[0] = ' ';
2932 newlen = 1;
2933 }
2934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 for (j = start; j < start + len; j += mb_l)
2936 {
2937 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002938 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002939 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if (ARABIC_CHAR(u8c))
2941 {
2942 /* Do Arabic shaping. */
2943 if (cmdmsg_rl)
2944 {
2945 /* displaying from right to left */
2946 pc = prev_c;
2947 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002948 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (j + mb_l >= start + len)
2950 nc = NUL;
2951 else
2952 nc = utf_ptr2char(p + mb_l);
2953 }
2954 else
2955 {
2956 /* displaying from left to right */
2957 if (j + mb_l >= start + len)
2958 pc = NUL;
2959 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002960 {
2961 int pcc[MAX_MCO];
2962
2963 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002965 pc1 = pcc[0];
2966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 nc = prev_c;
2968 }
2969 prev_c = u8c;
2970
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002971 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002973 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002974 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002976 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2977 if (u8cc[1] != 0)
2978 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002979 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 }
2982 else
2983 {
2984 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002985 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 newlen += mb_l;
2987 }
2988 }
2989
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002990 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 }
2992 else
2993#endif
2994 msg_outtrans_len(ccline.cmdbuff + start, len);
2995}
2996
2997/*
2998 * Put a character on the command line. Shifts the following text to the
2999 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3000 * "c" must be printable (fit in one display cell)!
3001 */
3002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003003putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 if (cmd_silent)
3006 return;
3007 msg_no_more = TRUE;
3008 msg_putchar(c);
3009 if (shift)
3010 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3011 msg_no_more = FALSE;
3012 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003013 extra_char = c;
3014 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015}
3016
3017/*
3018 * Undo a putcmdline(c, FALSE).
3019 */
3020 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003021unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 if (cmd_silent)
3024 return;
3025 msg_no_more = TRUE;
3026 if (ccline.cmdlen == ccline.cmdpos)
3027 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003028#ifdef FEAT_MBYTE
3029 else if (has_mbyte)
3030 draw_cmdline(ccline.cmdpos,
3031 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
3034 draw_cmdline(ccline.cmdpos, 1);
3035 msg_no_more = FALSE;
3036 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003037 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038}
3039
3040/*
3041 * Put the given string, of the given length, onto the command line.
3042 * If len is -1, then STRLEN() is used to calculate the length.
3043 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3044 * part will be redrawn, otherwise it will not. If this function is called
3045 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3046 * called afterwards.
3047 */
3048 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 int retval;
3052 int i;
3053 int m;
3054 int c;
3055
3056 if (len < 0)
3057 len = (int)STRLEN(str);
3058
3059 /* Check if ccline.cmdbuff needs to be longer */
3060 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003061 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else
3063 retval = OK;
3064 if (retval == OK)
3065 {
3066 if (!ccline.overstrike)
3067 {
3068 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3069 ccline.cmdbuff + ccline.cmdpos,
3070 (size_t)(ccline.cmdlen - ccline.cmdpos));
3071 ccline.cmdlen += len;
3072 }
3073 else
3074 {
3075#ifdef FEAT_MBYTE
3076 if (has_mbyte)
3077 {
3078 /* Count nr of characters in the new string. */
3079 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003080 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 ++m;
3082 /* Count nr of bytes in cmdline that are overwritten by these
3083 * characters. */
3084 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003085 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 --m;
3087 if (i < ccline.cmdlen)
3088 {
3089 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3090 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3091 ccline.cmdlen += ccline.cmdpos + len - i;
3092 }
3093 else
3094 ccline.cmdlen = ccline.cmdpos + len;
3095 }
3096 else
3097#endif
3098 if (ccline.cmdpos + len > ccline.cmdlen)
3099 ccline.cmdlen = ccline.cmdpos + len;
3100 }
3101 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3102 ccline.cmdbuff[ccline.cmdlen] = NUL;
3103
3104#ifdef FEAT_MBYTE
3105 if (enc_utf8)
3106 {
3107 /* When the inserted text starts with a composing character,
3108 * backup to the character before it. There could be two of them.
3109 */
3110 i = 0;
3111 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3112 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3113 {
3114 i = (*mb_head_off)(ccline.cmdbuff,
3115 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3116 ccline.cmdpos -= i;
3117 len += i;
3118 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3119 }
3120# ifdef FEAT_ARABIC
3121 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3122 {
3123 /* Check the previous character for Arabic combining pair. */
3124 i = (*mb_head_off)(ccline.cmdbuff,
3125 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3126 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3127 + ccline.cmdpos - i), c))
3128 {
3129 ccline.cmdpos -= i;
3130 len += i;
3131 }
3132 else
3133 i = 0;
3134 }
3135# endif
3136 if (i != 0)
3137 {
3138 /* Also backup the cursor position. */
3139 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3140 ccline.cmdspos -= i;
3141 msg_col -= i;
3142 if (msg_col < 0)
3143 {
3144 msg_col += Columns;
3145 --msg_row;
3146 }
3147 }
3148 }
3149#endif
3150
3151 if (redraw && !cmd_silent)
3152 {
3153 msg_no_more = TRUE;
3154 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003155 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3157 /* Avoid clearing the rest of the line too often. */
3158 if (cmdline_row != i || ccline.overstrike)
3159 msg_clr_eos();
3160 msg_no_more = FALSE;
3161 }
3162#ifdef FEAT_FKMAP
3163 /*
3164 * If we are in Farsi command mode, the character input must be in
3165 * Insert mode. So do not advance the cmdpos.
3166 */
3167 if (!cmd_fkmap)
3168#endif
3169 {
3170 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003173 if (m < 0) /* overflow, Columns or Rows at weird value */
3174 m = MAXCOL;
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 else
3177 m = MAXCOL;
3178 for (i = 0; i < len; ++i)
3179 {
3180 c = cmdline_charsize(ccline.cmdpos);
3181#ifdef FEAT_MBYTE
3182 /* count ">" for a double-wide char that doesn't fit. */
3183 if (has_mbyte)
3184 correct_cmdspos(ccline.cmdpos, c);
3185#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003186 /* Stop cursor at the end of the screen, but do increment the
3187 * insert position, so that entering a very long command
3188 * works, even though you can't see it. */
3189 if (ccline.cmdspos + c < m)
3190 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#ifdef FEAT_MBYTE
3192 if (has_mbyte)
3193 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003194 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (c > len - i - 1)
3196 c = len - i - 1;
3197 ccline.cmdpos += c;
3198 i += c;
3199 }
3200#endif
3201 ++ccline.cmdpos;
3202 }
3203 }
3204 }
3205 if (redraw)
3206 msg_check();
3207 return retval;
3208}
3209
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003210static struct cmdline_info prev_ccline;
3211static int prev_ccline_used = FALSE;
3212
3213/*
3214 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3215 * and overwrite it. But get_cmdline_str() may need it, thus make it
3216 * available globally in prev_ccline.
3217 */
3218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003219save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003220{
3221 if (!prev_ccline_used)
3222 {
3223 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3224 prev_ccline_used = TRUE;
3225 }
3226 *ccp = prev_ccline;
3227 prev_ccline = ccline;
3228 ccline.cmdbuff = NULL;
3229 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003230 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003231}
3232
3233/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003234 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003235 */
3236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003237restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003238{
3239 ccline = prev_ccline;
3240 prev_ccline = *ccp;
3241}
3242
Bram Moolenaar5a305422006-04-28 22:38:25 +00003243#if defined(FEAT_EVAL) || defined(PROTO)
3244/*
3245 * Save the command line into allocated memory. Returns a pointer to be
3246 * passed to restore_cmdline_alloc() later.
3247 * Returns NULL when failed.
3248 */
3249 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003250save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003251{
3252 struct cmdline_info *p;
3253
3254 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3255 if (p != NULL)
3256 save_cmdline(p);
3257 return (char_u *)p;
3258}
3259
3260/*
3261 * Restore the command line from the return value of save_cmdline_alloc().
3262 */
3263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003264restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003265{
3266 if (p != NULL)
3267 {
3268 restore_cmdline((struct cmdline_info *)p);
3269 vim_free(p);
3270 }
3271}
3272#endif
3273
Bram Moolenaar8299df92004-07-10 09:47:34 +00003274/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003275 * Paste a yank register into the command line.
3276 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003277 * insert_reg() can't be used here, because special characters from the
3278 * register contents will be interpreted as commands.
3279 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003280 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003281 */
3282 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003283cmdline_paste(
3284 int regname,
3285 int literally, /* Insert text literally instead of "as typed" */
3286 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003287{
3288 long i;
3289 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003290 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003291 int allocated;
3292 struct cmdline_info save_ccline;
3293
3294 /* check for valid regname; also accept special characters for CTRL-R in
3295 * the command line */
3296 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3297 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3298 return FAIL;
3299
3300 /* A register containing CTRL-R can cause an endless loop. Allow using
3301 * CTRL-C to break the loop. */
3302 line_breakcheck();
3303 if (got_int)
3304 return FAIL;
3305
3306#ifdef FEAT_CLIPBOARD
3307 regname = may_get_selection(regname);
3308#endif
3309
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003310 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003311 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003312 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003313 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003314 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003315 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003316 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003317
3318 if (i)
3319 {
3320 /* Got the value of a special register in "arg". */
3321 if (arg == NULL)
3322 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003323
3324 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3325 * part of the word. */
3326 p = arg;
3327 if (p_is && regname == Ctrl_W)
3328 {
3329 char_u *w;
3330 int len;
3331
3332 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003333 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003334 {
3335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 {
3338 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3339 if (!vim_iswordc(mb_ptr2char(w - len)))
3340 break;
3341 w -= len;
3342 }
3343 else
3344#endif
3345 {
3346 if (!vim_iswordc(w[-1]))
3347 break;
3348 --w;
3349 }
3350 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003351 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003352 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3353 p += len;
3354 }
3355
3356 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003357 if (allocated)
3358 vim_free(arg);
3359 return OK;
3360 }
3361
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003362 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003363}
3364
3365/*
3366 * Put a string on the command line.
3367 * When "literally" is TRUE, insert literally.
3368 * When "literally" is FALSE, insert as typed, but don't leave the command
3369 * line.
3370 */
3371 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003372cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003373{
3374 int c, cv;
3375
3376 if (literally)
3377 put_on_cmdline(s, -1, TRUE);
3378 else
3379 while (*s != NUL)
3380 {
3381 cv = *s;
3382 if (cv == Ctrl_V && s[1])
3383 ++s;
3384#ifdef FEAT_MBYTE
3385 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003386 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003387 else
3388#endif
3389 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003390 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3391 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003392#ifdef UNIX
3393 || c == intr_char
3394#endif
3395 || (c == Ctrl_BSL && *s == Ctrl_N))
3396 stuffcharReadbuff(Ctrl_V);
3397 stuffcharReadbuff(c);
3398 }
3399}
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#ifdef FEAT_WILDMENU
3402/*
3403 * Delete characters on the command line, from "from" to the current
3404 * position.
3405 */
3406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003407cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3410 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3411 ccline.cmdlen -= ccline.cmdpos - from;
3412 ccline.cmdpos = from;
3413}
3414#endif
3415
3416/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003417 * This function is called when the screen size changes and with incremental
3418 * search and in other situations where the command line may have been
3419 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
3421 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003422redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003424 redrawcmdline_ex(TRUE);
3425}
3426
3427 void
3428redrawcmdline_ex(int do_compute_cmdrow)
3429{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (cmd_silent)
3431 return;
3432 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003433 if (do_compute_cmdrow)
3434 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 redrawcmd();
3436 cursorcmd();
3437}
3438
3439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003440redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 int i;
3443
3444 if (cmd_silent)
3445 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003446 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 msg_putchar(ccline.cmdfirstc);
3448 if (ccline.cmdprompt != NULL)
3449 {
3450 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3451 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3452 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003453 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 --ccline.cmdindent;
3455 }
3456 else
3457 for (i = ccline.cmdindent; i > 0; --i)
3458 msg_putchar(' ');
3459}
3460
3461/*
3462 * Redraw what is currently on the command line.
3463 */
3464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003465redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 if (cmd_silent)
3468 return;
3469
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003470 /* when 'incsearch' is set there may be no command line while redrawing */
3471 if (ccline.cmdbuff == NULL)
3472 {
3473 windgoto(cmdline_row, 0);
3474 msg_clr_eos();
3475 return;
3476 }
3477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 msg_start();
3479 redrawcmdprompt();
3480
3481 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3482 msg_no_more = TRUE;
3483 draw_cmdline(0, ccline.cmdlen);
3484 msg_clr_eos();
3485 msg_no_more = FALSE;
3486
3487 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003488 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003489 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491 /*
3492 * An emsg() before may have set msg_scroll. This is used in normal mode,
3493 * in cmdline mode we can reset them now.
3494 */
3495 msg_scroll = FALSE; /* next message overwrites cmdline */
3496
3497 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3498 * in cmdline mode */
3499 skip_redraw = FALSE;
3500}
3501
3502 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003503compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003505 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 cmdline_row = Rows - 1;
3507 else
3508 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003509 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510}
3511
3512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003513cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 if (cmd_silent)
3516 return;
3517
3518#ifdef FEAT_RIGHTLEFT
3519 if (cmdmsg_rl)
3520 {
3521 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3522 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3523 if (msg_row <= 0)
3524 msg_row = Rows - 1;
3525 }
3526 else
3527#endif
3528 {
3529 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3530 msg_col = ccline.cmdspos % (int)Columns;
3531 if (msg_row >= Rows)
3532 msg_row = Rows - 1;
3533 }
3534
3535 windgoto(msg_row, msg_col);
3536#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003537 if (p_imst == IM_ON_THE_SPOT)
3538 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#endif
3540#ifdef MCH_CURSOR_SHAPE
3541 mch_update_cursor();
3542#endif
3543}
3544
3545 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003546gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
3548 msg_start();
3549#ifdef FEAT_RIGHTLEFT
3550 if (cmdmsg_rl)
3551 msg_col = Columns - 1;
3552 else
3553#endif
3554 msg_col = 0; /* always start in column 0 */
3555 if (clr) /* clear the bottom line(s) */
3556 msg_clr_eos(); /* will reset clear_cmdline */
3557 windgoto(cmdline_row, 0);
3558}
3559
3560/*
3561 * Check the word in front of the cursor for an abbreviation.
3562 * Called when the non-id character "c" has been entered.
3563 * When an abbreviation is recognized it is removed from the text with
3564 * backspaces and the replacement string is inserted, followed by "c".
3565 */
3566 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003567ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
3569 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3570 return FALSE;
3571
3572 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3573}
3574
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003575#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3576 static int
3577#ifdef __BORLANDC__
3578_RTLENTRYF
3579#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003580sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003581{
3582 char_u *p1 = *(char_u **)s1;
3583 char_u *p2 = *(char_u **)s2;
3584
3585 if (*p1 != '<' && *p2 == '<') return -1;
3586 if (*p1 == '<' && *p2 != '<') return 1;
3587 return STRCMP(p1, p2);
3588}
3589#endif
3590
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591/*
3592 * Return FAIL if this is not an appropriate context in which to do
3593 * completion of anything, return OK if it is (even if there are no matches).
3594 * For the caller, this means that the character is just passed through like a
3595 * normal character (instead of being expanded). This allows :s/^I^D etc.
3596 */
3597 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003598nextwild(
3599 expand_T *xp,
3600 int type,
3601 int options, /* extra options for ExpandOne() */
3602 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 int i, j;
3605 char_u *p1;
3606 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 int difflen;
3608 int v;
3609
3610 if (xp->xp_numfiles == -1)
3611 {
3612 set_expand_context(xp);
3613 cmd_showtail = expand_showtail(xp);
3614 }
3615
3616 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3617 {
3618 beep_flush();
3619 return OK; /* Something illegal on command line */
3620 }
3621 if (xp->xp_context == EXPAND_NOTHING)
3622 {
3623 /* Caller can use the character as a normal char instead */
3624 return FAIL;
3625 }
3626
3627 MSG_PUTS("..."); /* show that we are busy */
3628 out_flush();
3629
3630 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003631 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 if (type == WILD_NEXT || type == WILD_PREV)
3634 {
3635 /*
3636 * Get next/previous match for a previous expanded pattern.
3637 */
3638 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3639 }
3640 else
3641 {
3642 /*
3643 * Translate string into pattern and expand it.
3644 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003645 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3646 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 p2 = NULL;
3648 else
3649 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003650 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003651 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3652 if (escape)
3653 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003654
3655 if (p_wic)
3656 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003657 p2 = ExpandOne(xp, p1,
3658 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003659 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003661 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (p2 != NULL && type == WILD_LONGEST)
3663 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003664 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (ccline.cmdbuff[i + j] == '*'
3666 || ccline.cmdbuff[i + j] == '?')
3667 break;
3668 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003669 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 }
3672 }
3673
3674 if (p2 != NULL && !got_int)
3675 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003676 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003677 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003679 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 xp->xp_pattern = ccline.cmdbuff + i;
3681 }
3682 else
3683 v = OK;
3684 if (v == OK)
3685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003686 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3687 &ccline.cmdbuff[ccline.cmdpos],
3688 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3689 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 ccline.cmdlen += difflen;
3691 ccline.cmdpos += difflen;
3692 }
3693 }
3694 vim_free(p2);
3695
3696 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003697 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
3699 /* When expanding a ":map" command and no matches are found, assume that
3700 * the key is supposed to be inserted literally */
3701 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3702 return FAIL;
3703
3704 if (xp->xp_numfiles <= 0 && p2 == NULL)
3705 beep_flush();
3706 else if (xp->xp_numfiles == 1)
3707 /* free expanded pattern */
3708 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3709
3710 return OK;
3711}
3712
3713/*
3714 * Do wildcard expansion on the string 'str'.
3715 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003716 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 * Return NULL for failure.
3718 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003719 * "orig" is the originally expanded string, copied to allocated memory. It
3720 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3721 * WILD_PREV "orig" should be NULL.
3722 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003723 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3724 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 *
3726 * mode = WILD_FREE: just free previously expanded matches
3727 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3728 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3729 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3730 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3731 * mode = WILD_ALL: return all matches concatenated
3732 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003733 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 *
3735 * options = WILD_LIST_NOTFOUND: list entries without a match
3736 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3737 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3738 * options = WILD_NO_BEEP: Don't beep for multiple matches
3739 * options = WILD_ADD_SLASH: add a slash after directory names
3740 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3741 * options = WILD_SILENT: don't print warning messages
3742 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003743 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 *
3745 * The variables xp->xp_context and xp->xp_backslash must have been set!
3746 */
3747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003748ExpandOne(
3749 expand_T *xp,
3750 char_u *str,
3751 char_u *orig, /* allocated copy of original of expanded string */
3752 int options,
3753 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754{
3755 char_u *ss = NULL;
3756 static int findex;
3757 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003758 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 int i;
3760 long_u len;
3761 int non_suf_match; /* number without matching suffix */
3762
3763 /*
3764 * first handle the case of using an old match
3765 */
3766 if (mode == WILD_NEXT || mode == WILD_PREV)
3767 {
3768 if (xp->xp_numfiles > 0)
3769 {
3770 if (mode == WILD_PREV)
3771 {
3772 if (findex == -1)
3773 findex = xp->xp_numfiles;
3774 --findex;
3775 }
3776 else /* mode == WILD_NEXT */
3777 ++findex;
3778
3779 /*
3780 * When wrapping around, return the original string, set findex to
3781 * -1.
3782 */
3783 if (findex < 0)
3784 {
3785 if (orig_save == NULL)
3786 findex = xp->xp_numfiles - 1;
3787 else
3788 findex = -1;
3789 }
3790 if (findex >= xp->xp_numfiles)
3791 {
3792 if (orig_save == NULL)
3793 findex = 0;
3794 else
3795 findex = -1;
3796 }
3797#ifdef FEAT_WILDMENU
3798 if (p_wmnu)
3799 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3800 findex, cmd_showtail);
3801#endif
3802 if (findex == -1)
3803 return vim_strsave(orig_save);
3804 return vim_strsave(xp->xp_files[findex]);
3805 }
3806 else
3807 return NULL;
3808 }
3809
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003810 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3812 {
3813 FreeWild(xp->xp_numfiles, xp->xp_files);
3814 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003815 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 findex = 0;
3818
3819 if (mode == WILD_FREE) /* only release file name */
3820 return NULL;
3821
3822 if (xp->xp_numfiles == -1)
3823 {
3824 vim_free(orig_save);
3825 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003826 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 /*
3829 * Do the expansion.
3830 */
3831 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3832 options) == FAIL)
3833 {
3834#ifdef FNAME_ILLEGAL
3835 /* Illegal file name has been silently skipped. But when there
3836 * are wildcards, the real problem is that there was no match,
3837 * causing the pattern to be added, which has illegal characters.
3838 */
3839 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3840 EMSG2(_(e_nomatch2), str);
3841#endif
3842 }
3843 else if (xp->xp_numfiles == 0)
3844 {
3845 if (!(options & WILD_SILENT))
3846 EMSG2(_(e_nomatch2), str);
3847 }
3848 else
3849 {
3850 /* Escape the matches for use on the command line. */
3851 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3852
3853 /*
3854 * Check for matching suffixes in file names.
3855 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003856 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3857 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859 if (xp->xp_numfiles)
3860 non_suf_match = xp->xp_numfiles;
3861 else
3862 non_suf_match = 1;
3863 if ((xp->xp_context == EXPAND_FILES
3864 || xp->xp_context == EXPAND_DIRECTORIES)
3865 && xp->xp_numfiles > 1)
3866 {
3867 /*
3868 * More than one match; check suffix.
3869 * The files will have been sorted on matching suffix in
3870 * expand_wildcards, only need to check the first two.
3871 */
3872 non_suf_match = 0;
3873 for (i = 0; i < 2; ++i)
3874 if (match_suffix(xp->xp_files[i]))
3875 ++non_suf_match;
3876 }
3877 if (non_suf_match != 1)
3878 {
3879 /* Can we ever get here unless it's while expanding
3880 * interactively? If not, we can get rid of this all
3881 * together. Don't really want to wait for this message
3882 * (and possibly have to hit return to continue!).
3883 */
3884 if (!(options & WILD_SILENT))
3885 EMSG(_(e_toomany));
3886 else if (!(options & WILD_NO_BEEP))
3887 beep_flush();
3888 }
3889 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3890 ss = vim_strsave(xp->xp_files[0]);
3891 }
3892 }
3893 }
3894
3895 /* Find longest common part */
3896 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3897 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003898 int mb_len = 1;
3899 int c0, ci;
3900
3901 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003903#ifdef FEAT_MBYTE
3904 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003906 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3907 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3908 }
3909 else
3910#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003911 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003912 for (i = 1; i < xp->xp_numfiles; ++i)
3913 {
3914#ifdef FEAT_MBYTE
3915 if (has_mbyte)
3916 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3917 else
3918#endif
3919 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003920 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003922 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003923 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003925 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 break;
3927 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003928 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 break;
3930 }
3931 if (i < xp->xp_numfiles)
3932 {
3933 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003934 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936 }
3937 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 ss = alloc((unsigned)len + 1);
3940 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003941 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 findex = -1; /* next p_wc gets first one */
3943 }
3944
3945 /* Concatenate all matching names */
3946 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3947 {
3948 len = 0;
3949 for (i = 0; i < xp->xp_numfiles; ++i)
3950 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3951 ss = lalloc(len, TRUE);
3952 if (ss != NULL)
3953 {
3954 *ss = NUL;
3955 for (i = 0; i < xp->xp_numfiles; ++i)
3956 {
3957 STRCAT(ss, xp->xp_files[i]);
3958 if (i != xp->xp_numfiles - 1)
3959 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3960 }
3961 }
3962 }
3963
3964 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3965 ExpandCleanup(xp);
3966
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003967 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003968 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003969 vim_free(orig);
3970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 return ss;
3972}
3973
3974/*
3975 * Prepare an expand structure for use.
3976 */
3977 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003978ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003980 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003981 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003983#ifndef BACKSLASH_IN_FILENAME
3984 xp->xp_shell = FALSE;
3985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 xp->xp_numfiles = -1;
3987 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003988#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3989 xp->xp_arg = NULL;
3990#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003991 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993
3994/*
3995 * Cleanup an expand structure after use.
3996 */
3997 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003998ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
4000 if (xp->xp_numfiles >= 0)
4001 {
4002 FreeWild(xp->xp_numfiles, xp->xp_files);
4003 xp->xp_numfiles = -1;
4004 }
4005}
4006
4007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004008ExpandEscape(
4009 expand_T *xp,
4010 char_u *str,
4011 int numfiles,
4012 char_u **files,
4013 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 int i;
4016 char_u *p;
4017
4018 /*
4019 * May change home directory back to "~"
4020 */
4021 if (options & WILD_HOME_REPLACE)
4022 tilde_replace(str, numfiles, files);
4023
4024 if (options & WILD_ESCAPE)
4025 {
4026 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004027 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004028 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 || xp->xp_context == EXPAND_BUFFERS
4030 || xp->xp_context == EXPAND_DIRECTORIES)
4031 {
4032 /*
4033 * Insert a backslash into a file name before a space, \, %, #
4034 * and wildmatch characters, except '~'.
4035 */
4036 for (i = 0; i < numfiles; ++i)
4037 {
4038 /* for ":set path=" we need to escape spaces twice */
4039 if (xp->xp_backslash == XP_BS_THREE)
4040 {
4041 p = vim_strsave_escaped(files[i], (char_u *)" ");
4042 if (p != NULL)
4043 {
4044 vim_free(files[i]);
4045 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004046#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 p = vim_strsave_escaped(files[i], (char_u *)" ");
4048 if (p != NULL)
4049 {
4050 vim_free(files[i]);
4051 files[i] = p;
4052 }
4053#endif
4054 }
4055 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004056#ifdef BACKSLASH_IN_FILENAME
4057 p = vim_strsave_fnameescape(files[i], FALSE);
4058#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004059 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if (p != NULL)
4062 {
4063 vim_free(files[i]);
4064 files[i] = p;
4065 }
4066
4067 /* If 'str' starts with "\~", replace "~" at start of
4068 * files[i] with "\~". */
4069 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004070 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004073
4074 /* If the first file starts with a '+' escape it. Otherwise it
4075 * could be seen as "+cmd". */
4076 if (*files[0] == '+')
4077 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 else if (xp->xp_context == EXPAND_TAGS)
4080 {
4081 /*
4082 * Insert a backslash before characters in a tag name that
4083 * would terminate the ":tag" command.
4084 */
4085 for (i = 0; i < numfiles; ++i)
4086 {
4087 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4088 if (p != NULL)
4089 {
4090 vim_free(files[i]);
4091 files[i] = p;
4092 }
4093 }
4094 }
4095 }
4096}
4097
4098/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004099 * Escape special characters in "fname" for when used as a file name argument
4100 * after a Vim command, or, when "shell" is non-zero, a shell command.
4101 * Returns the result in allocated memory.
4102 */
4103 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004104vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004105{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004106 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004107#ifdef BACKSLASH_IN_FILENAME
4108 char_u buf[20];
4109 int j = 0;
4110
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004111 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004112 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004113 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004114 buf[j++] = *p;
4115 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004116 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004117#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004118 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4119 if (shell && csh_like_shell() && p != NULL)
4120 {
4121 char_u *s;
4122
4123 /* For csh and similar shells need to put two backslashes before '!'.
4124 * One is taken by Vim, one by the shell. */
4125 s = vim_strsave_escaped(p, (char_u *)"!");
4126 vim_free(p);
4127 p = s;
4128 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004129#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004130
4131 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4132 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004133 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004134 escape_fname(&p);
4135
4136 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004137}
4138
4139/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004140 * Put a backslash before the file name in "pp", which is in allocated memory.
4141 */
4142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004143escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004144{
4145 char_u *p;
4146
4147 p = alloc((unsigned)(STRLEN(*pp) + 2));
4148 if (p != NULL)
4149 {
4150 p[0] = '\\';
4151 STRCPY(p + 1, *pp);
4152 vim_free(*pp);
4153 *pp = p;
4154 }
4155}
4156
4157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * For each file name in files[num_files]:
4159 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4160 */
4161 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004162tilde_replace(
4163 char_u *orig_pat,
4164 int num_files,
4165 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 int i;
4168 char_u *p;
4169
4170 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4171 {
4172 for (i = 0; i < num_files; ++i)
4173 {
4174 p = home_replace_save(NULL, files[i]);
4175 if (p != NULL)
4176 {
4177 vim_free(files[i]);
4178 files[i] = p;
4179 }
4180 }
4181 }
4182}
4183
4184/*
4185 * Show all matches for completion on the command line.
4186 * Returns EXPAND_NOTHING when the character that triggered expansion should
4187 * be inserted like a normal character.
4188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004190showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4193 int num_files;
4194 char_u **files_found;
4195 int i, j, k;
4196 int maxlen;
4197 int lines;
4198 int columns;
4199 char_u *p;
4200 int lastlen;
4201 int attr;
4202 int showtail;
4203
4204 if (xp->xp_numfiles == -1)
4205 {
4206 set_expand_context(xp);
4207 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4208 &num_files, &files_found);
4209 showtail = expand_showtail(xp);
4210 if (i != EXPAND_OK)
4211 return i;
4212
4213 }
4214 else
4215 {
4216 num_files = xp->xp_numfiles;
4217 files_found = xp->xp_files;
4218 showtail = cmd_showtail;
4219 }
4220
4221#ifdef FEAT_WILDMENU
4222 if (!wildmenu)
4223 {
4224#endif
4225 msg_didany = FALSE; /* lines_left will be set */
4226 msg_start(); /* prepare for paging */
4227 msg_putchar('\n');
4228 out_flush();
4229 cmdline_row = msg_row;
4230 msg_didany = FALSE; /* lines_left will be set again */
4231 msg_start(); /* prepare for paging */
4232#ifdef FEAT_WILDMENU
4233 }
4234#endif
4235
4236 if (got_int)
4237 got_int = FALSE; /* only int. the completion, not the cmd line */
4238#ifdef FEAT_WILDMENU
4239 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004240 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241#endif
4242 else
4243 {
4244 /* find the length of the longest file name */
4245 maxlen = 0;
4246 for (i = 0; i < num_files; ++i)
4247 {
4248 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 || xp->xp_context == EXPAND_BUFFERS))
4251 {
4252 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4253 j = vim_strsize(NameBuff);
4254 }
4255 else
4256 j = vim_strsize(L_SHOWFILE(i));
4257 if (j > maxlen)
4258 maxlen = j;
4259 }
4260
4261 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4262 lines = num_files;
4263 else
4264 {
4265 /* compute the number of columns and lines for the listing */
4266 maxlen += 2; /* two spaces between file names */
4267 columns = ((int)Columns + 2) / maxlen;
4268 if (columns < 1)
4269 columns = 1;
4270 lines = (num_files + columns - 1) / columns;
4271 }
4272
Bram Moolenaar8820b482017-03-16 17:23:31 +01004273 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4276 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004277 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 msg_clr_eos();
4279 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004280 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 /* list the files line by line */
4284 for (i = 0; i < lines; ++i)
4285 {
4286 lastlen = 999;
4287 for (k = i; k < num_files; k += lines)
4288 {
4289 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4290 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004291 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 p = files_found[k] + STRLEN(files_found[k]) + 1;
4293 msg_advance(maxlen + 1);
4294 msg_puts(p);
4295 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004296 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 break;
4298 }
4299 for (j = maxlen - lastlen; --j >= 0; )
4300 msg_putchar(' ');
4301 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004302 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 || xp->xp_context == EXPAND_BUFFERS)
4304 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004305 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004306 if (xp->xp_numfiles != -1)
4307 {
4308 char_u *halved_slash;
4309 char_u *exp_path;
4310
4311 /* Expansion was done before and special characters
4312 * were escaped, need to halve backslashes. Also
4313 * $HOME has been replaced with ~/. */
4314 exp_path = expand_env_save_opt(files_found[k], TRUE);
4315 halved_slash = backslash_halve_save(
4316 exp_path != NULL ? exp_path : files_found[k]);
4317 j = mch_isdir(halved_slash != NULL ? halved_slash
4318 : files_found[k]);
4319 vim_free(exp_path);
4320 vim_free(halved_slash);
4321 }
4322 else
4323 /* Expansion was done here, file names are literal. */
4324 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (showtail)
4326 p = L_SHOWFILE(k);
4327 else
4328 {
4329 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4330 TRUE);
4331 p = NameBuff;
4332 }
4333 }
4334 else
4335 {
4336 j = FALSE;
4337 p = L_SHOWFILE(k);
4338 }
4339 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4340 }
4341 if (msg_col > 0) /* when not wrapped around */
4342 {
4343 msg_clr_eos();
4344 msg_putchar('\n');
4345 }
4346 out_flush(); /* show one line at a time */
4347 if (got_int)
4348 {
4349 got_int = FALSE;
4350 break;
4351 }
4352 }
4353
4354 /*
4355 * we redraw the command below the lines that we have just listed
4356 * This is a bit tricky, but it saves a lot of screen updating.
4357 */
4358 cmdline_row = msg_row; /* will put it back later */
4359 }
4360
4361 if (xp->xp_numfiles == -1)
4362 FreeWild(num_files, files_found);
4363
4364 return EXPAND_OK;
4365}
4366
4367/*
4368 * Private gettail for showmatches() (and win_redr_status_matches()):
4369 * Find tail of file name path, but ignore trailing "/".
4370 */
4371 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004372sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
4374 char_u *p;
4375 char_u *t = s;
4376 int had_sep = FALSE;
4377
4378 for (p = s; *p != NUL; )
4379 {
4380 if (vim_ispathsep(*p)
4381#ifdef BACKSLASH_IN_FILENAME
4382 && !rem_backslash(p)
4383#endif
4384 )
4385 had_sep = TRUE;
4386 else if (had_sep)
4387 {
4388 t = p;
4389 had_sep = FALSE;
4390 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004391 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
4393 return t;
4394}
4395
4396/*
4397 * Return TRUE if we only need to show the tail of completion matches.
4398 * When not completing file names or there is a wildcard in the path FALSE is
4399 * returned.
4400 */
4401 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004402expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 char_u *s;
4405 char_u *end;
4406
4407 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004408 if (xp->xp_context != EXPAND_FILES
4409 && xp->xp_context != EXPAND_SHELLCMD
4410 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return FALSE;
4412
4413 end = gettail(xp->xp_pattern);
4414 if (end == xp->xp_pattern) /* there is no path separator */
4415 return FALSE;
4416
4417 for (s = xp->xp_pattern; s < end; s++)
4418 {
4419 /* Skip escaped wildcards. Only when the backslash is not a path
4420 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4421 if (rem_backslash(s))
4422 ++s;
4423 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4424 return FALSE;
4425 }
4426 return TRUE;
4427}
4428
4429/*
4430 * Prepare a string for expansion.
4431 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004432 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * When expanding other names: The string will be used with regcomp(). Copy
4434 * the name into allocated memory and prepend "^".
4435 */
4436 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004437addstar(
4438 char_u *fname,
4439 int len,
4440 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442 char_u *retval;
4443 int i, j;
4444 int new_len;
4445 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004446 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004448 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004449 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004450 && context != EXPAND_SHELLCMD
4451 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 /*
4454 * Matching will be done internally (on something other than files).
4455 * So we convert the file-matching-type wildcards into our kind for
4456 * use with vim_regcomp(). First work out how long it will be:
4457 */
4458
4459 /* For help tags the translation is done in find_help_tags().
4460 * For a tag pattern starting with "/" no translation is needed. */
4461 if (context == EXPAND_HELP
4462 || context == EXPAND_COLORS
4463 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004464 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004465 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004466 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004467 || ((context == EXPAND_TAGS_LISTFILES
4468 || context == EXPAND_TAGS)
4469 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 retval = vim_strnsave(fname, len);
4471 else
4472 {
4473 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4474 for (i = 0; i < len; i++)
4475 {
4476 if (fname[i] == '*' || fname[i] == '~')
4477 new_len++; /* '*' needs to be replaced by ".*"
4478 '~' needs to be replaced by "\~" */
4479
4480 /* Buffer names are like file names. "." should be literal */
4481 if (context == EXPAND_BUFFERS && fname[i] == '.')
4482 new_len++; /* "." becomes "\." */
4483
4484 /* Custom expansion takes care of special things, match
4485 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004486 if ((context == EXPAND_USER_DEFINED
4487 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 new_len++; /* '\' becomes "\\" */
4489 }
4490 retval = alloc(new_len);
4491 if (retval != NULL)
4492 {
4493 retval[0] = '^';
4494 j = 1;
4495 for (i = 0; i < len; i++, j++)
4496 {
4497 /* Skip backslash. But why? At least keep it for custom
4498 * expansion. */
4499 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004500 && context != EXPAND_USER_LIST
4501 && fname[i] == '\\'
4502 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 break;
4504
4505 switch (fname[i])
4506 {
4507 case '*': retval[j++] = '.';
4508 break;
4509 case '~': retval[j++] = '\\';
4510 break;
4511 case '?': retval[j] = '.';
4512 continue;
4513 case '.': if (context == EXPAND_BUFFERS)
4514 retval[j++] = '\\';
4515 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004516 case '\\': if (context == EXPAND_USER_DEFINED
4517 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 retval[j++] = '\\';
4519 break;
4520 }
4521 retval[j] = fname[i];
4522 }
4523 retval[j] = NUL;
4524 }
4525 }
4526 }
4527 else
4528 {
4529 retval = alloc(len + 4);
4530 if (retval != NULL)
4531 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004532 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004535 * Don't add a star to *, ~, ~user, $var or `cmd`.
4536 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * ~ would be at the start of the file name, but not the tail.
4538 * $ could be anywhere in the tail.
4539 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004540 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 */
4542 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004543 ends_in_star = (len > 0 && retval[len - 1] == '*');
4544#ifndef BACKSLASH_IN_FILENAME
4545 for (i = len - 2; i >= 0; --i)
4546 {
4547 if (retval[i] != '\\')
4548 break;
4549 ends_in_star = !ends_in_star;
4550 }
4551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004553 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 && vim_strchr(tail, '$') == NULL
4555 && vim_strchr(retval, '`') == NULL)
4556 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004557 else if (len > 0 && retval[len - 1] == '$')
4558 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 retval[len] = NUL;
4560 }
4561 }
4562 return retval;
4563}
4564
4565/*
4566 * Must parse the command line so far to work out what context we are in.
4567 * Completion can then be done based on that context.
4568 * This routine sets the variables:
4569 * xp->xp_pattern The start of the pattern to be expanded within
4570 * the command line (ends at the cursor).
4571 * xp->xp_context The type of thing to expand. Will be one of:
4572 *
4573 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4574 * the command line, like an unknown command. Caller
4575 * should beep.
4576 * EXPAND_NOTHING Unrecognised context for completion, use char like
4577 * a normal char, rather than for completion. eg
4578 * :s/^I/
4579 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4580 * it.
4581 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4582 * EXPAND_FILES After command with XFILE set, or after setting
4583 * with P_EXPAND set. eg :e ^I, :w>>^I
4584 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4585 * when we know only directories are of interest. eg
4586 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4589 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4590 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4591 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4592 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4593 * EXPAND_EVENTS Complete event names
4594 * EXPAND_SYNTAX Complete :syntax command arguments
4595 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4596 * EXPAND_AUGROUP Complete autocommand group names
4597 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4598 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4599 * eg :unmap a^I , :cunab x^I
4600 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4601 * eg :call sub^I
4602 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4603 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4604 * names in expressions, eg :while s^I
4605 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004606 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 */
4608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004609set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004611 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (ccline.cmdfirstc != ':'
4613#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004614 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004615 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616#endif
4617 )
4618 {
4619 xp->xp_context = EXPAND_NOTHING;
4620 return;
4621 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004622 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623}
4624
4625 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004626set_cmd_context(
4627 expand_T *xp,
4628 char_u *str, /* start of command line */
4629 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004630 int col, /* position of cursor */
4631 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 int old_char = NUL;
4634 char_u *nextcomm;
4635
4636 /*
4637 * Avoid a UMR warning from Purify, only save the character if it has been
4638 * written before.
4639 */
4640 if (col < len)
4641 old_char = str[col];
4642 str[col] = NUL;
4643 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004644
4645#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004646 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004647 {
4648# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004649 /* pass CMD_SIZE because there is no real command */
4650 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004651# endif
4652 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004653 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004654 {
4655 xp->xp_context = ccline.xp_context;
4656 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004657# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004658 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004659# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004660 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004661 else
4662#endif
4663 while (nextcomm != NULL)
4664 nextcomm = set_one_cmd_context(xp, nextcomm);
4665
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004666 /* Store the string here so that call_user_expand_func() can get to them
4667 * easily. */
4668 xp->xp_line = str;
4669 xp->xp_col = col;
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 str[col] = old_char;
4672}
4673
4674/*
4675 * Expand the command line "str" from context "xp".
4676 * "xp" must have been set by set_cmd_context().
4677 * xp->xp_pattern points into "str", to where the text that is to be expanded
4678 * starts.
4679 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4680 * cursor.
4681 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4682 * key that triggered expansion literally.
4683 * Returns EXPAND_OK otherwise.
4684 */
4685 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004686expand_cmdline(
4687 expand_T *xp,
4688 char_u *str, /* start of command line */
4689 int col, /* position of cursor */
4690 int *matchcount, /* return: nr of matches */
4691 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004694 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4697 {
4698 beep_flush();
4699 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4700 }
4701 if (xp->xp_context == EXPAND_NOTHING)
4702 {
4703 /* Caller can use the character as a normal char instead */
4704 return EXPAND_NOTHING;
4705 }
4706
4707 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004708 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4709 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (file_str == NULL)
4711 return EXPAND_UNSUCCESSFUL;
4712
Bram Moolenaar94950a92010-12-02 16:01:29 +01004713 if (p_wic)
4714 options += WILD_ICASE;
4715
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004717 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
4719 *matchcount = 0;
4720 *matches = NULL;
4721 }
4722 vim_free(file_str);
4723
4724 return EXPAND_OK;
4725}
4726
4727#ifdef FEAT_MULTI_LANG
4728/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004729 * Cleanup matches for help tags:
4730 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4731 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004733static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
4735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
4738 int i, j;
4739 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004740 char_u buf[4];
4741 char_u *p = buf;
4742
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004743 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004744 {
4745 *p++ = '@';
4746 *p++ = p_hlg[0];
4747 *p++ = p_hlg[1];
4748 }
4749 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 for (i = 0; i < num_file; ++i)
4752 {
4753 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004754 if (len <= 0)
4755 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004756 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 /* Sorting on priority means the same item in another language may
4759 * be anywhere. Search all items for a match up to the "@en". */
4760 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004761 if (j != i && (int)STRLEN(file[j]) == len + 3
4762 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 break;
4764 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004765 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 file[i][len] = NUL;
4767 }
4768 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004769
4770 if (*buf != NUL)
4771 for (i = 0; i < num_file; ++i)
4772 {
4773 len = (int)STRLEN(file[i]) - 3;
4774 if (len <= 0)
4775 continue;
4776 if (STRCMP(file[i] + len, buf) == 0)
4777 {
4778 /* remove the default language */
4779 file[i][len] = NUL;
4780 }
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782}
4783#endif
4784
4785/*
4786 * Do the expansion based on xp->xp_context and "pat".
4787 */
4788 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004789ExpandFromContext(
4790 expand_T *xp,
4791 char_u *pat,
4792 int *num_file,
4793 char_u ***file,
4794 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796#ifdef FEAT_CMDL_COMPL
4797 regmatch_T regmatch;
4798#endif
4799 int ret;
4800 int flags;
4801
4802 flags = EW_DIR; /* include directories */
4803 if (options & WILD_LIST_NOTFOUND)
4804 flags |= EW_NOTFOUND;
4805 if (options & WILD_ADD_SLASH)
4806 flags |= EW_ADDSLASH;
4807 if (options & WILD_KEEP_ALL)
4808 flags |= EW_KEEPALL;
4809 if (options & WILD_SILENT)
4810 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004811 if (options & WILD_ALLLINKS)
4812 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004814 if (xp->xp_context == EXPAND_FILES
4815 || xp->xp_context == EXPAND_DIRECTORIES
4816 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 /*
4819 * Expand file or directory names.
4820 */
4821 int free_pat = FALSE;
4822 int i;
4823
4824 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4825 * space */
4826 if (xp->xp_backslash != XP_BS_NONE)
4827 {
4828 free_pat = TRUE;
4829 pat = vim_strsave(pat);
4830 for (i = 0; pat[i]; ++i)
4831 if (pat[i] == '\\')
4832 {
4833 if (xp->xp_backslash == XP_BS_THREE
4834 && pat[i + 1] == '\\'
4835 && pat[i + 2] == '\\'
4836 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004837 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (xp->xp_backslash == XP_BS_ONE
4839 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004840 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 }
4843
4844 if (xp->xp_context == EXPAND_FILES)
4845 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004846 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4847 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 else
4849 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004850 if (options & WILD_ICASE)
4851 flags |= EW_ICASE;
4852
Bram Moolenaard7834d32009-12-02 16:14:36 +00004853 /* Expand wildcards, supporting %:h and the like. */
4854 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (free_pat)
4856 vim_free(pat);
4857 return ret;
4858 }
4859
4860 *file = (char_u **)"";
4861 *num_file = 0;
4862 if (xp->xp_context == EXPAND_HELP)
4863 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004864 /* With an empty argument we would get all the help tags, which is
4865 * very slow. Get matches for "help" instead. */
4866 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4867 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869#ifdef FEAT_MULTI_LANG
4870 cleanup_help_tags(*num_file, *file);
4871#endif
4872 return OK;
4873 }
4874 return FAIL;
4875 }
4876
4877#ifndef FEAT_CMDL_COMPL
4878 return FAIL;
4879#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004880 if (xp->xp_context == EXPAND_SHELLCMD)
4881 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (xp->xp_context == EXPAND_OLD_SETTING)
4883 return ExpandOldSetting(num_file, file);
4884 if (xp->xp_context == EXPAND_BUFFERS)
4885 return ExpandBufnames(pat, num_file, file, options);
4886 if (xp->xp_context == EXPAND_TAGS
4887 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4888 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4889 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004890 {
4891 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004892 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4893 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004896 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004897 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004898 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004899 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004900 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004901 {
4902 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004903 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004904 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004905 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004906 {
4907 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004908 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004909 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004910# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4911 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004912 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004913# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004914 if (xp->xp_context == EXPAND_PACKADD)
4915 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4918 if (regmatch.regprog == NULL)
4919 return FAIL;
4920
4921 /* set ignore-case according to p_ic, p_scs and pat */
4922 regmatch.rm_ic = ignorecase(pat);
4923
4924 if (xp->xp_context == EXPAND_SETTINGS
4925 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4926 ret = ExpandSettings(xp, &regmatch, num_file, file);
4927 else if (xp->xp_context == EXPAND_MAPPINGS)
4928 ret = ExpandMappings(&regmatch, num_file, file);
4929# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4930 else if (xp->xp_context == EXPAND_USER_DEFINED)
4931 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4932# endif
4933 else
4934 {
4935 static struct expgen
4936 {
4937 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004938 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004940 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 } tab[] =
4942 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004943 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4944 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004945 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004946 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004947#ifdef FEAT_CMDHIST
4948 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004951 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004952 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004953 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4954 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4955 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#endif
4957#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004958 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4959 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4960 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4961 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962#endif
4963#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004964 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4965 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
4967#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004968 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004970#ifdef FEAT_PROFILE
4971 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4972#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004973 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004974 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4975 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004976#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004977 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004978#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004979#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004980 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004981#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004982#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004983 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4986 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004987 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4988 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004990 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004991 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 };
4993 int i;
4994
4995 /*
4996 * Find a context in the table and call the ExpandGeneric() with the
4997 * right function to do the expansion.
4998 */
4999 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005000 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (xp->xp_context == tab[i].context)
5002 {
5003 if (tab[i].ic)
5004 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005005 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005006 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 break;
5008 }
5009 }
5010
Bram Moolenaar473de612013-06-08 18:19:48 +02005011 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 return ret;
5014#endif /* FEAT_CMDL_COMPL */
5015}
5016
5017#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5018/*
5019 * Expand a list of names.
5020 *
5021 * Generic function for command line completion. It calls a function to
5022 * obtain strings, one by one. The strings are matched against a regexp
5023 * program. Matching strings are copied into an array, which is returned.
5024 *
5025 * Returns OK when no problems encountered, FAIL for error (out of memory).
5026 */
5027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028ExpandGeneric(
5029 expand_T *xp,
5030 regmatch_T *regmatch,
5031 int *num_file,
5032 char_u ***file,
5033 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005035 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037 int i;
5038 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005039 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 char_u *str;
5041
5042 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005043 * round == 0: count the number of matching names
5044 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005046 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 for (i = 0; ; ++i)
5049 {
5050 str = (*func)(xp, i);
5051 if (str == NULL) /* end of list */
5052 break;
5053 if (*str == NUL) /* skip empty strings */
5054 continue;
5055
5056 if (vim_regexec(regmatch, str, (colnr_T)0))
5057 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005058 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005060 if (escaped)
5061 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5062 else
5063 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 (*file)[count] = str;
5065#ifdef FEAT_MENU
5066 if (func == get_menu_names && str != NULL)
5067 {
5068 /* test for separator added by get_menu_names() */
5069 str += STRLEN(str) - 1;
5070 if (*str == '\001')
5071 *str = '.';
5072 }
5073#endif
5074 }
5075 ++count;
5076 }
5077 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005078 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 if (count == 0)
5081 return OK;
5082 *num_file = count;
5083 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5084 if (*file == NULL)
5085 {
5086 *file = (char_u **)"";
5087 return FAIL;
5088 }
5089 count = 0;
5090 }
5091 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005092
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005093 /* Sort the results. Keep menu's in the specified order. */
5094 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005095 {
5096 if (xp->xp_context == EXPAND_EXPRESSION
5097 || xp->xp_context == EXPAND_FUNCTIONS
5098 || xp->xp_context == EXPAND_USER_FUNC)
5099 /* <SNR> functions should be sorted to the end. */
5100 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5101 sort_func_compare);
5102 else
5103 sort_strings(*file, *num_file);
5104 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005105
Bram Moolenaar4f688582007-07-24 12:34:30 +00005106#ifdef FEAT_CMDL_COMPL
5107 /* Reset the variables used for special highlight names expansion, so that
5108 * they don't show up when getting normal highlight names by ID. */
5109 reset_expand_highlight();
5110#endif
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 return OK;
5113}
5114
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005115/*
5116 * Complete a shell command.
5117 * Returns FAIL or OK;
5118 */
5119 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005120expand_shellcmd(
5121 char_u *filepat, /* pattern to match with command names */
5122 int *num_file, /* return: number of matches */
5123 char_u ***file, /* return: array with matches */
5124 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005125{
5126 char_u *pat;
5127 int i;
5128 char_u *path;
5129 int mustfree = FALSE;
5130 garray_T ga;
5131 char_u *buf = alloc(MAXPATHL);
5132 size_t l;
5133 char_u *s, *e;
5134 int flags = flagsarg;
5135 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005136 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005137
5138 if (buf == NULL)
5139 return FAIL;
5140
5141 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5142 * space */
5143 pat = vim_strsave(filepat);
5144 for (i = 0; pat[i]; ++i)
5145 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005146 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005147
Bram Moolenaarb5971142015-03-21 17:32:19 +01005148 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005149
5150 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005151 if (mch_isFullName(pat))
5152 path = (char_u *)" ";
5153 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005154 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5155 path = (char_u *)".";
5156 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005157 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005158 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005159 if (path == NULL)
5160 path = (char_u *)"";
5161 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005162
5163 /*
5164 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005165 * collect them in "ga". When "." is not in $PATH also expand for the
5166 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005167 */
5168 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005169 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005170 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005171 if (*s == NUL)
5172 {
5173 if (did_curdir)
5174 break;
5175 /* Find directories in the current directory, path is empty. */
5176 did_curdir = TRUE;
5177 }
5178 else if (*s == '.')
5179 did_curdir = TRUE;
5180
Bram Moolenaar68c31742006-09-02 15:54:18 +00005181 if (*s == ' ')
5182 ++s; /* Skip space used for absolute path name. */
5183
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005184#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005185 e = vim_strchr(s, ';');
5186#else
5187 e = vim_strchr(s, ':');
5188#endif
5189 if (e == NULL)
5190 e = s + STRLEN(s);
5191
5192 l = e - s;
5193 if (l > MAXPATHL - 5)
5194 break;
5195 vim_strncpy(buf, s, l);
5196 add_pathsep(buf);
5197 l = STRLEN(buf);
5198 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5199
5200 /* Expand matches in one directory of $PATH. */
5201 ret = expand_wildcards(1, &buf, num_file, file, flags);
5202 if (ret == OK)
5203 {
5204 if (ga_grow(&ga, *num_file) == FAIL)
5205 FreeWild(*num_file, *file);
5206 else
5207 {
5208 for (i = 0; i < *num_file; ++i)
5209 {
5210 s = (*file)[i];
5211 if (STRLEN(s) > l)
5212 {
5213 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005214 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005215 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5216 }
5217 else
5218 vim_free(s);
5219 }
5220 vim_free(*file);
5221 }
5222 }
5223 if (*e != NUL)
5224 ++e;
5225 }
5226 *file = ga.ga_data;
5227 *num_file = ga.ga_len;
5228
5229 vim_free(buf);
5230 vim_free(pat);
5231 if (mustfree)
5232 vim_free(path);
5233 return OK;
5234}
5235
5236
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005238static 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 +00005239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005241 * Call "user_expand_func()" to invoke a user defined Vim script function and
5242 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005244 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005245call_user_expand_func(
5246 void *(*user_expand_func)(char_u *, int, char_u **, int),
5247 expand_T *xp,
5248 int *num_file,
5249 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005251 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005252 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005255 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005256 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
Bram Moolenaarb7515462013-06-29 12:58:33 +02005258 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005259 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 *num_file = 0;
5261 *file = NULL;
5262
Bram Moolenaarb7515462013-06-29 12:58:33 +02005263 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005264 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005265 keep = ccline.cmdbuff[ccline.cmdlen];
5266 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005267 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005268
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005269 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005270 args[1] = xp->xp_line;
5271 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 args[2] = num;
5273
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005274 /* Save the cmdline, we don't know what the function may do. */
5275 save_ccline = ccline;
5276 ccline.cmdbuff = NULL;
5277 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005279
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005280 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005281
5282 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005284 if (ccline.cmdbuff != NULL)
5285 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005286
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005287 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005288 return ret;
5289}
5290
5291/*
5292 * Expand names with a function defined by the user.
5293 */
5294 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295ExpandUserDefined(
5296 expand_T *xp,
5297 regmatch_T *regmatch,
5298 int *num_file,
5299 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005300{
5301 char_u *retstr;
5302 char_u *s;
5303 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005304 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005305 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005306 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005307
5308 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5309 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 return FAIL;
5311
5312 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005313 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
5315 e = vim_strchr(s, '\n');
5316 if (e == NULL)
5317 e = s + STRLEN(s);
5318 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005319 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005321 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5322 *e = keep;
5323
5324 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005326 if (ga_grow(&ga, 1) == FAIL)
5327 break;
5328 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5329 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (*e != NUL)
5333 ++e;
5334 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005335 vim_free(retstr);
5336 *file = ga.ga_data;
5337 *num_file = ga.ga_len;
5338 return OK;
5339}
5340
5341/*
5342 * Expand names with a list returned by a function defined by the user.
5343 */
5344 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ExpandUserList(
5346 expand_T *xp,
5347 int *num_file,
5348 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005349{
5350 list_T *retlist;
5351 listitem_T *li;
5352 garray_T ga;
5353
5354 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5355 if (retlist == NULL)
5356 return FAIL;
5357
5358 ga_init2(&ga, (int)sizeof(char *), 3);
5359 /* Loop over the items in the list. */
5360 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5361 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005362 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5363 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005364
5365 if (ga_grow(&ga, 1) == FAIL)
5366 break;
5367
5368 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005369 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005370 ++ga.ga_len;
5371 }
5372 list_unref(retlist);
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 *file = ga.ga_data;
5375 *num_file = ga.ga_len;
5376 return OK;
5377}
5378#endif
5379
5380/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005381 * Expand color scheme, compiler or filetype names.
5382 * Search from 'runtimepath':
5383 * 'runtimepath'/{dirnames}/{pat}.vim
5384 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5385 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5386 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5387 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005388 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 */
5390 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005391ExpandRTDir(
5392 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005393 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005394 int *num_file,
5395 char_u ***file,
5396 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 char_u *s;
5399 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005400 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005402 int i;
5403 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405 *num_file = 0;
5406 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005407 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005408 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005410 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005412 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5413 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005415 ga_clear_strings(&ga);
5416 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005418 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005419 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005420 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005422
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005423 if (flags & DIP_START) {
5424 for (i = 0; dirnames[i] != NULL; ++i)
5425 {
5426 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5427 if (s == NULL)
5428 {
5429 ga_clear_strings(&ga);
5430 return FAIL;
5431 }
5432 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5433 globpath(p_pp, s, &ga, 0);
5434 vim_free(s);
5435 }
5436 }
5437
5438 if (flags & DIP_OPT) {
5439 for (i = 0; dirnames[i] != NULL; ++i)
5440 {
5441 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5442 if (s == NULL)
5443 {
5444 ga_clear_strings(&ga);
5445 return FAIL;
5446 }
5447 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5448 globpath(p_pp, s, &ga, 0);
5449 vim_free(s);
5450 }
5451 }
5452
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005453 for (i = 0; i < ga.ga_len; ++i)
5454 {
5455 match = ((char_u **)ga.ga_data)[i];
5456 s = match;
5457 e = s + STRLEN(s);
5458 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5459 {
5460 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005461 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005462 if (s < match || vim_ispathsep(*s))
5463 break;
5464 ++s;
5465 *e = NUL;
5466 mch_memmove(match, s, e - s + 1);
5467 }
5468 }
5469
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005470 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005471 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005472
5473 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005474 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005475 remove_duplicates(&ga);
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 *file = ga.ga_data;
5478 *num_file = ga.ga_len;
5479 return OK;
5480}
5481
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005482/*
5483 * Expand loadplugin names:
5484 * 'packpath'/pack/ * /opt/{pat}
5485 */
5486 static int
5487ExpandPackAddDir(
5488 char_u *pat,
5489 int *num_file,
5490 char_u ***file)
5491{
5492 char_u *s;
5493 char_u *e;
5494 char_u *match;
5495 garray_T ga;
5496 int i;
5497 int pat_len;
5498
5499 *num_file = 0;
5500 *file = NULL;
5501 pat_len = (int)STRLEN(pat);
5502 ga_init2(&ga, (int)sizeof(char *), 10);
5503
5504 s = alloc((unsigned)(pat_len + 26));
5505 if (s == NULL)
5506 {
5507 ga_clear_strings(&ga);
5508 return FAIL;
5509 }
5510 sprintf((char *)s, "pack/*/opt/%s*", pat);
5511 globpath(p_pp, s, &ga, 0);
5512 vim_free(s);
5513
5514 for (i = 0; i < ga.ga_len; ++i)
5515 {
5516 match = ((char_u **)ga.ga_data)[i];
5517 s = gettail(match);
5518 e = s + STRLEN(s);
5519 mch_memmove(match, s, e - s + 1);
5520 }
5521
5522 if (ga.ga_len == 0)
5523 return FAIL;
5524
5525 /* Sort and remove duplicates which can happen when specifying multiple
5526 * directories in dirnames. */
5527 remove_duplicates(&ga);
5528
5529 *file = ga.ga_data;
5530 *num_file = ga.ga_len;
5531 return OK;
5532}
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534#endif
5535
5536#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5537/*
5538 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005539 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005541 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542globpath(
5543 char_u *path,
5544 char_u *file,
5545 garray_T *ga,
5546 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 expand_T xpc;
5549 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 int num_p;
5552 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
5554 buf = alloc(MAXPATHL);
5555 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005556 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005558 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 /* Loop over all entries in {path}. */
5562 while (*path != NUL)
5563 {
5564 /* Copy one item of the path to buf[] and concatenate the file name. */
5565 copy_option_part(&path, buf, MAXPATHL, ",");
5566 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5567 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005568# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005569 /* Using the platform's path separator (\) makes vim incorrectly
5570 * treat it as an escape character, use '/' instead. */
5571 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5572 STRCAT(buf, "/");
5573# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005577 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5578 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005580 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005582 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 for (i = 0; i < num_p; ++i)
5585 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005586 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005587 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005588 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005591
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 FreeWild(num_p, p);
5593 }
5594 }
5595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
5597 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598}
5599
5600#endif
5601
5602#if defined(FEAT_CMDHIST) || defined(PROTO)
5603
5604/*********************************
5605 * Command line history stuff *
5606 *********************************/
5607
5608/*
5609 * Translate a history character to the associated type number.
5610 */
5611 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 if (c == ':')
5615 return HIST_CMD;
5616 if (c == '=')
5617 return HIST_EXPR;
5618 if (c == '@')
5619 return HIST_INPUT;
5620 if (c == '>')
5621 return HIST_DEBUG;
5622 return HIST_SEARCH; /* must be '?' or '/' */
5623}
5624
5625/*
5626 * Table of history names.
5627 * These names are used in :history and various hist...() functions.
5628 * It is sufficient to give the significant prefix of a history name.
5629 */
5630
5631static char *(history_names[]) =
5632{
5633 "cmd",
5634 "search",
5635 "expr",
5636 "input",
5637 "debug",
5638 NULL
5639};
5640
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005641#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5642/*
5643 * Function given to ExpandGeneric() to obtain the possible first
5644 * arguments of the ":history command.
5645 */
5646 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005648{
5649 static char_u compl[2] = { NUL, NUL };
5650 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005651 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005652 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5653
5654 if (idx < short_names_count)
5655 {
5656 compl[0] = (char_u)short_names[idx];
5657 return compl;
5658 }
5659 if (idx < short_names_count + history_name_count)
5660 return (char_u *)history_names[idx - short_names_count];
5661 if (idx == short_names_count + history_name_count)
5662 return (char_u *)"all";
5663 return NULL;
5664}
5665#endif
5666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667/*
5668 * init_history() - Initialize the command line history.
5669 * Also used to re-allocate the history when the size changes.
5670 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005671 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
5674 int newlen; /* new length of history table */
5675 histentry_T *temp;
5676 int i;
5677 int j;
5678 int type;
5679
5680 /*
5681 * If size of history table changed, reallocate it
5682 */
5683 newlen = (int)p_hi;
5684 if (newlen != hislen) /* history length changed */
5685 {
5686 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5687 {
5688 if (newlen)
5689 {
5690 temp = (histentry_T *)lalloc(
5691 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5692 if (temp == NULL) /* out of memory! */
5693 {
5694 if (type == 0) /* first one: just keep the old length */
5695 {
5696 newlen = hislen;
5697 break;
5698 }
5699 /* Already changed one table, now we can only have zero
5700 * length for all tables. */
5701 newlen = 0;
5702 type = -1;
5703 continue;
5704 }
5705 }
5706 else
5707 temp = NULL;
5708 if (newlen == 0 || temp != NULL)
5709 {
5710 if (hisidx[type] < 0) /* there are no entries yet */
5711 {
5712 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005713 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715 else if (newlen > hislen) /* array becomes bigger */
5716 {
5717 for (i = 0; i <= hisidx[type]; ++i)
5718 temp[i] = history[type][i];
5719 j = i;
5720 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005721 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 for ( ; j < hislen; ++i, ++j)
5723 temp[i] = history[type][j];
5724 }
5725 else /* array becomes smaller or 0 */
5726 {
5727 j = hisidx[type];
5728 for (i = newlen - 1; ; --i)
5729 {
5730 if (i >= 0) /* copy newest entries */
5731 temp[i] = history[type][j];
5732 else /* remove older entries */
5733 vim_free(history[type][j].hisstr);
5734 if (--j < 0)
5735 j = hislen - 1;
5736 if (j == hisidx[type])
5737 break;
5738 }
5739 hisidx[type] = newlen - 1;
5740 }
5741 vim_free(history[type]);
5742 history[type] = temp;
5743 }
5744 }
5745 hislen = newlen;
5746 }
5747}
5748
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005750clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005751{
5752 hisptr->hisnum = 0;
5753 hisptr->viminfo = FALSE;
5754 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005755 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005756}
5757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758/*
5759 * Check if command line 'str' is already in history.
5760 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5761 */
5762 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763in_history(
5764 int type,
5765 char_u *str,
5766 int move_to_front, /* Move the entry to the front if it exists */
5767 int sep,
5768 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 int i;
5771 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005772 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774 if (hisidx[type] < 0)
5775 return FALSE;
5776 i = hisidx[type];
5777 do
5778 {
5779 if (history[type][i].hisstr == NULL)
5780 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005781
5782 /* For search history, check that the separator character matches as
5783 * well. */
5784 p = history[type][i].hisstr;
5785 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005786 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005787 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
5789 if (!move_to_front)
5790 return TRUE;
5791 last_i = i;
5792 break;
5793 }
5794 if (--i < 0)
5795 i = hislen - 1;
5796 } while (i != hisidx[type]);
5797
5798 if (last_i >= 0)
5799 {
5800 str = history[type][i].hisstr;
5801 while (i != hisidx[type])
5802 {
5803 if (++i >= hislen)
5804 i = 0;
5805 history[type][last_i] = history[type][i];
5806 last_i = i;
5807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005809 history[type][i].viminfo = FALSE;
5810 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005811 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 return TRUE;
5813 }
5814 return FALSE;
5815}
5816
5817/*
5818 * Convert history name (from table above) to its HIST_ equivalent.
5819 * When "name" is empty, return "cmd" history.
5820 * Returns -1 for unknown history name.
5821 */
5822 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005823get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 int i;
5826 int len = (int)STRLEN(name);
5827
5828 /* No argument: use current history. */
5829 if (len == 0)
5830 return hist_char2type(ccline.cmdfirstc);
5831
5832 for (i = 0; history_names[i] != NULL; ++i)
5833 if (STRNICMP(name, history_names[i], len) == 0)
5834 return i;
5835
5836 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5837 return hist_char2type(name[0]);
5838
5839 return -1;
5840}
5841
5842static int last_maptick = -1; /* last seen maptick */
5843
5844/*
5845 * Add the given string to the given history. If the string is already in the
5846 * history then it is moved to the front. "histype" may be one of he HIST_
5847 * values.
5848 */
5849 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005850add_to_history(
5851 int histype,
5852 char_u *new_entry,
5853 int in_map, /* consider maptick when inside a mapping */
5854 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855{
5856 histentry_T *hisptr;
5857 int len;
5858
5859 if (hislen == 0) /* no history */
5860 return;
5861
Bram Moolenaara939e432013-11-09 05:30:26 +01005862 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5863 return;
5864
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 /*
5866 * Searches inside the same mapping overwrite each other, so that only
5867 * the last line is kept. Be careful not to remove a line that was moved
5868 * down, only lines that were added.
5869 */
5870 if (histype == HIST_SEARCH && in_map)
5871 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005872 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
5874 /* Current line is from the same mapping, remove it */
5875 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5876 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005877 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 --hisnum[histype];
5879 if (--hisidx[HIST_SEARCH] < 0)
5880 hisidx[HIST_SEARCH] = hislen - 1;
5881 }
5882 last_maptick = -1;
5883 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005884 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
5886 if (++hisidx[histype] == hislen)
5887 hisidx[histype] = 0;
5888 hisptr = &history[histype][hisidx[histype]];
5889 vim_free(hisptr->hisstr);
5890
5891 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005892 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5894 if (hisptr->hisstr != NULL)
5895 hisptr->hisstr[len + 1] = sep;
5896
5897 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005898 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005899 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 if (histype == HIST_SEARCH && in_map)
5901 last_maptick = maptick;
5902 }
5903}
5904
5905#if defined(FEAT_EVAL) || defined(PROTO)
5906
5907/*
5908 * Get identifier of newest history entry.
5909 * "histype" may be one of the HIST_ values.
5910 */
5911 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005912get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5915 || hisidx[histype] < 0)
5916 return -1;
5917
5918 return history[histype][hisidx[histype]].hisnum;
5919}
5920
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 * Calculate history index from a number:
5923 * num > 0: seen as identifying number of a history entry
5924 * num < 0: relative position in history wrt newest entry
5925 * "histype" may be one of the HIST_ values.
5926 */
5927 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005928calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 int i;
5931 histentry_T *hist;
5932 int wrapped = FALSE;
5933
5934 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5935 || (i = hisidx[histype]) < 0 || num == 0)
5936 return -1;
5937
5938 hist = history[histype];
5939 if (num > 0)
5940 {
5941 while (hist[i].hisnum > num)
5942 if (--i < 0)
5943 {
5944 if (wrapped)
5945 break;
5946 i += hislen;
5947 wrapped = TRUE;
5948 }
5949 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5950 return i;
5951 }
5952 else if (-num <= hislen)
5953 {
5954 i += num + 1;
5955 if (i < 0)
5956 i += hislen;
5957 if (hist[i].hisstr != NULL)
5958 return i;
5959 }
5960 return -1;
5961}
5962
5963/*
5964 * Get a history entry by its index.
5965 * "histype" may be one of the HIST_ values.
5966 */
5967 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005968get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 idx = calc_hist_idx(histype, idx);
5971 if (idx >= 0)
5972 return history[histype][idx].hisstr;
5973 else
5974 return (char_u *)"";
5975}
5976
5977/*
5978 * Clear all entries of a history.
5979 * "histype" may be one of the HIST_ values.
5980 */
5981 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005982clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 int i;
5985 histentry_T *hisptr;
5986
5987 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5988 {
5989 hisptr = history[histype];
5990 for (i = hislen; i--;)
5991 {
5992 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005993 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005994 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996 hisidx[histype] = -1; /* mark history as cleared */
5997 hisnum[histype] = 0; /* reset identifier counter */
5998 return OK;
5999 }
6000 return FAIL;
6001}
6002
6003/*
6004 * Remove all entries matching {str} from a history.
6005 * "histype" may be one of the HIST_ values.
6006 */
6007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006008del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 regmatch_T regmatch;
6011 histentry_T *hisptr;
6012 int idx;
6013 int i;
6014 int last;
6015 int found = FALSE;
6016
6017 regmatch.regprog = NULL;
6018 regmatch.rm_ic = FALSE; /* always match case */
6019 if (hislen != 0
6020 && histype >= 0
6021 && histype < HIST_COUNT
6022 && *str != NUL
6023 && (idx = hisidx[histype]) >= 0
6024 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6025 != NULL)
6026 {
6027 i = last = idx;
6028 do
6029 {
6030 hisptr = &history[histype][i];
6031 if (hisptr->hisstr == NULL)
6032 break;
6033 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6034 {
6035 found = TRUE;
6036 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006037 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 }
6039 else
6040 {
6041 if (i != last)
6042 {
6043 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006044 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 }
6046 if (--last < 0)
6047 last += hislen;
6048 }
6049 if (--i < 0)
6050 i += hislen;
6051 } while (i != idx);
6052 if (history[histype][idx].hisstr == NULL)
6053 hisidx[histype] = -1;
6054 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006055 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 return found;
6057}
6058
6059/*
6060 * Remove an indexed entry from a history.
6061 * "histype" may be one of the HIST_ values.
6062 */
6063 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006064del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
6066 int i, j;
6067
6068 i = calc_hist_idx(histype, idx);
6069 if (i < 0)
6070 return FALSE;
6071 idx = hisidx[histype];
6072 vim_free(history[histype][i].hisstr);
6073
6074 /* When deleting the last added search string in a mapping, reset
6075 * last_maptick, so that the last added search string isn't deleted again.
6076 */
6077 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6078 last_maptick = -1;
6079
6080 while (i != idx)
6081 {
6082 j = (i + 1) % hislen;
6083 history[histype][i] = history[histype][j];
6084 i = j;
6085 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006086 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 if (--i < 0)
6088 i += hislen;
6089 hisidx[histype] = i;
6090 return TRUE;
6091}
6092
6093#endif /* FEAT_EVAL */
6094
6095#if defined(FEAT_CRYPT) || defined(PROTO)
6096/*
6097 * Very specific function to remove the value in ":set key=val" from the
6098 * history.
6099 */
6100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 char_u *p;
6104 int i;
6105
6106 i = hisidx[HIST_CMD];
6107 if (i < 0)
6108 return;
6109 p = history[HIST_CMD][i].hisstr;
6110 if (p != NULL)
6111 for ( ; *p; ++p)
6112 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6113 {
6114 p = vim_strchr(p + 3, '=');
6115 if (p == NULL)
6116 break;
6117 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006118 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (p[i] == '\\' && p[i + 1])
6120 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006121 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 --p;
6123 }
6124}
6125#endif
6126
6127#endif /* FEAT_CMDHIST */
6128
Bram Moolenaar064154c2016-03-19 22:50:43 +01006129#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006130/*
6131 * Get pointer to the command line info to use. cmdline_paste() may clear
6132 * ccline and put the previous value in prev_ccline.
6133 */
6134 static struct cmdline_info *
6135get_ccline_ptr(void)
6136{
6137 if ((State & CMDLINE) == 0)
6138 return NULL;
6139 if (ccline.cmdbuff != NULL)
6140 return &ccline;
6141 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6142 return &prev_ccline;
6143 return NULL;
6144}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006145#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006146
Bram Moolenaar064154c2016-03-19 22:50:43 +01006147#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006148/*
6149 * Get the current command line in allocated memory.
6150 * Only works when the command line is being edited.
6151 * Returns NULL when something is wrong.
6152 */
6153 char_u *
6154get_cmdline_str(void)
6155{
6156 struct cmdline_info *p = get_ccline_ptr();
6157
6158 if (p == NULL)
6159 return NULL;
6160 return vim_strnsave(p->cmdbuff, p->cmdlen);
6161}
6162
6163/*
6164 * Get the current command line position, counted in bytes.
6165 * Zero is the first position.
6166 * Only works when the command line is being edited.
6167 * Returns -1 when something is wrong.
6168 */
6169 int
6170get_cmdline_pos(void)
6171{
6172 struct cmdline_info *p = get_ccline_ptr();
6173
6174 if (p == NULL)
6175 return -1;
6176 return p->cmdpos;
6177}
6178
6179/*
6180 * Set the command line byte position to "pos". Zero is the first position.
6181 * Only works when the command line is being edited.
6182 * Returns 1 when failed, 0 when OK.
6183 */
6184 int
6185set_cmdline_pos(
6186 int pos)
6187{
6188 struct cmdline_info *p = get_ccline_ptr();
6189
6190 if (p == NULL)
6191 return 1;
6192
6193 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6194 * changed the command line. */
6195 if (pos < 0)
6196 new_cmdpos = 0;
6197 else
6198 new_cmdpos = pos;
6199 return 0;
6200}
6201#endif
6202
6203#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6204/*
6205 * Get the current command-line type.
6206 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6207 * Only works when the command line is being edited.
6208 * Returns NUL when something is wrong.
6209 */
6210 int
6211get_cmdline_type(void)
6212{
6213 struct cmdline_info *p = get_ccline_ptr();
6214
6215 if (p == NULL)
6216 return NUL;
6217 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006218 return
6219# ifdef FEAT_EVAL
6220 (p->input_fn) ? '@' :
6221# endif
6222 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006223 return p->cmdfirstc;
6224}
6225#endif
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6228/*
6229 * Get indices "num1,num2" that specify a range within a list (not a range of
6230 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6231 * Returns OK if parsed successfully, otherwise FAIL.
6232 */
6233 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006234get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 int len;
6237 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006238 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
6240 *str = skipwhite(*str);
6241 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6242 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006243 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 *str += len;
6245 *num1 = (int)num;
6246 first = TRUE;
6247 }
6248 *str = skipwhite(*str);
6249 if (**str == ',') /* parse "to" part of range */
6250 {
6251 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006252 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 if (len > 0)
6254 {
6255 *num2 = (int)num;
6256 *str = skipwhite(*str + len);
6257 }
6258 else if (!first) /* no number given at all */
6259 return FAIL;
6260 }
6261 else if (first) /* only one number given */
6262 *num2 = *num1;
6263 return OK;
6264}
6265#endif
6266
6267#if defined(FEAT_CMDHIST) || defined(PROTO)
6268/*
6269 * :history command - print a history
6270 */
6271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 histentry_T *hist;
6275 int histype1 = HIST_CMD;
6276 int histype2 = HIST_CMD;
6277 int hisidx1 = 1;
6278 int hisidx2 = -1;
6279 int idx;
6280 int i, j, k;
6281 char_u *end;
6282 char_u *arg = eap->arg;
6283
6284 if (hislen == 0)
6285 {
6286 MSG(_("'history' option is zero"));
6287 return;
6288 }
6289
6290 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6291 {
6292 end = arg;
6293 while (ASCII_ISALPHA(*end)
6294 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6295 end++;
6296 i = *end;
6297 *end = NUL;
6298 histype1 = get_histtype(arg);
6299 if (histype1 == -1)
6300 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006301 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
6303 histype1 = 0;
6304 histype2 = HIST_COUNT-1;
6305 }
6306 else
6307 {
6308 *end = i;
6309 EMSG(_(e_trailing));
6310 return;
6311 }
6312 }
6313 else
6314 histype2 = histype1;
6315 *end = i;
6316 }
6317 else
6318 end = arg;
6319 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6320 {
6321 EMSG(_(e_trailing));
6322 return;
6323 }
6324
6325 for (; !got_int && histype1 <= histype2; ++histype1)
6326 {
6327 STRCPY(IObuff, "\n # ");
6328 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6329 MSG_PUTS_TITLE(IObuff);
6330 idx = hisidx[histype1];
6331 hist = history[histype1];
6332 j = hisidx1;
6333 k = hisidx2;
6334 if (j < 0)
6335 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6336 if (k < 0)
6337 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6338 if (idx >= 0 && j <= k)
6339 for (i = idx + 1; !got_int; ++i)
6340 {
6341 if (i == hislen)
6342 i = 0;
6343 if (hist[i].hisstr != NULL
6344 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6345 {
6346 msg_putchar('\n');
6347 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6348 hist[i].hisnum);
6349 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6350 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006351 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 else
6353 STRCAT(IObuff, hist[i].hisstr);
6354 msg_outtrans(IObuff);
6355 out_flush();
6356 }
6357 if (i == idx)
6358 break;
6359 }
6360 }
6361}
6362#endif
6363
6364#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006365/*
6366 * Buffers for history read from a viminfo file. Only valid while reading.
6367 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006368static histentry_T *viminfo_history[HIST_COUNT] =
6369 {NULL, NULL, NULL, NULL, NULL};
6370static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6371static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372static int viminfo_add_at_front = FALSE;
6373
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006374static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376/*
6377 * Translate a history type number to the associated character.
6378 */
6379 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006380hist_type2char(
6381 int type,
6382 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 if (type == HIST_CMD)
6385 return ':';
6386 if (type == HIST_SEARCH)
6387 {
6388 if (use_question)
6389 return '?';
6390 else
6391 return '/';
6392 }
6393 if (type == HIST_EXPR)
6394 return '=';
6395 return '@';
6396}
6397
6398/*
6399 * Prepare for reading the history from the viminfo file.
6400 * This allocates history arrays to store the read history lines.
6401 */
6402 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 int i;
6406 int num;
6407 int type;
6408 int len;
6409
6410 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006411 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (asklen > hislen)
6413 asklen = hislen;
6414
6415 for (type = 0; type < HIST_COUNT; ++type)
6416 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006417 /* Count the number of empty spaces in the history list. Entries read
6418 * from viminfo previously are also considered empty. If there are
6419 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006421 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 num++;
6423 len = asklen;
6424 if (num > len)
6425 len = num;
6426 if (len <= 0)
6427 viminfo_history[type] = NULL;
6428 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006429 viminfo_history[type] = (histentry_T *)lalloc(
6430 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (viminfo_history[type] == NULL)
6432 len = 0;
6433 viminfo_hislen[type] = len;
6434 viminfo_hisidx[type] = 0;
6435 }
6436}
6437
6438/*
6439 * Accept a line from the viminfo, store it in the history array when it's
6440 * new.
6441 */
6442 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006443read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 int type;
6446 long_u len;
6447 char_u *val;
6448 char_u *p;
6449
6450 type = hist_char2type(virp->vir_line[0]);
6451 if (viminfo_hisidx[type] < viminfo_hislen[type])
6452 {
6453 val = viminfo_readstring(virp, 1, TRUE);
6454 if (val != NULL && *val != NUL)
6455 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006456 int sep = (*val == ' ' ? NUL : *val);
6457
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006459 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
6461 /* Need to re-allocate to append the separator byte. */
6462 len = STRLEN(val);
6463 p = lalloc(len + 2, TRUE);
6464 if (p != NULL)
6465 {
6466 if (type == HIST_SEARCH)
6467 {
6468 /* Search entry: Move the separator from the first
6469 * column to after the NUL. */
6470 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006471 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 }
6473 else
6474 {
6475 /* Not a search entry: No separator in the viminfo
6476 * file, add a NUL separator. */
6477 mch_memmove(p, val, (size_t)len + 1);
6478 p[len + 1] = NUL;
6479 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006480 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6481 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006482 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6483 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006484 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
6486 }
6487 }
6488 vim_free(val);
6489 }
6490 return viminfo_readline(virp);
6491}
6492
Bram Moolenaar07219f92013-04-14 23:19:36 +02006493/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006494 * Accept a new style history line from the viminfo, store it in the history
6495 * array when it's new.
6496 */
6497 void
6498handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006499 garray_T *values,
6500 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006501{
6502 int type;
6503 long_u len;
6504 char_u *val;
6505 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006506 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006507
6508 /* Check the format:
6509 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006510 if (values->ga_len < 4
6511 || vp[0].bv_type != BVAL_NR
6512 || vp[1].bv_type != BVAL_NR
6513 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6514 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006515 return;
6516
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006517 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006518 if (type >= HIST_COUNT)
6519 return;
6520 if (viminfo_hisidx[type] < viminfo_hislen[type])
6521 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006522 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006523 if (val != NULL && *val != NUL)
6524 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006525 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6526 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006527 int idx;
6528 int overwrite = FALSE;
6529
6530 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6531 {
6532 /* If lines were written by an older Vim we need to avoid
6533 * getting duplicates. See if the entry already exists. */
6534 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6535 {
6536 p = viminfo_history[type][idx].hisstr;
6537 if (STRCMP(val, p) == 0
6538 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6539 {
6540 overwrite = TRUE;
6541 break;
6542 }
6543 }
6544
6545 if (!overwrite)
6546 {
6547 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006548 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006549 p = lalloc(len + 2, TRUE);
6550 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006551 else
6552 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006553 if (p != NULL)
6554 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006555 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006556 if (!overwrite)
6557 {
6558 mch_memmove(p, val, (size_t)len + 1);
6559 /* Put the separator after the NUL. */
6560 p[len + 1] = sep;
6561 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006562 viminfo_history[type][idx].hisnum = 0;
6563 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006564 viminfo_hisidx[type]++;
6565 }
6566 }
6567 }
6568 }
6569 }
6570}
6571
6572/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006573 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006574 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006575 static void
6576concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 int idx;
6579 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006580
6581 idx = hisidx[type] + viminfo_hisidx[type];
6582 if (idx >= hislen)
6583 idx -= hislen;
6584 else if (idx < 0)
6585 idx = hislen - 1;
6586 if (viminfo_add_at_front)
6587 hisidx[type] = idx;
6588 else
6589 {
6590 if (hisidx[type] == -1)
6591 hisidx[type] = hislen - 1;
6592 do
6593 {
6594 if (history[type][idx].hisstr != NULL
6595 || history[type][idx].viminfo)
6596 break;
6597 if (++idx == hislen)
6598 idx = 0;
6599 } while (idx != hisidx[type]);
6600 if (idx != hisidx[type] && --idx < 0)
6601 idx = hislen - 1;
6602 }
6603 for (i = 0; i < viminfo_hisidx[type]; i++)
6604 {
6605 vim_free(history[type][idx].hisstr);
6606 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6607 history[type][idx].viminfo = TRUE;
6608 history[type][idx].time_set = viminfo_history[type][i].time_set;
6609 if (--idx < 0)
6610 idx = hislen - 1;
6611 }
6612 idx += 1;
6613 idx %= hislen;
6614 for (i = 0; i < viminfo_hisidx[type]; i++)
6615 {
6616 history[type][idx++].hisnum = ++hisnum[type];
6617 idx %= hislen;
6618 }
6619}
6620
6621#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6622 static int
6623#ifdef __BORLANDC__
6624_RTLENTRYF
6625#endif
6626sort_hist(const void *s1, const void *s2)
6627{
6628 histentry_T *p1 = *(histentry_T **)s1;
6629 histentry_T *p2 = *(histentry_T **)s2;
6630
6631 if (p1->time_set < p2->time_set) return -1;
6632 if (p1->time_set > p2->time_set) return 1;
6633 return 0;
6634}
6635#endif
6636
6637/*
6638 * Merge history lines from viminfo and lines typed in this Vim based on the
6639 * timestamp;
6640 */
6641 static void
6642merge_history(int type)
6643{
6644 int max_len;
6645 histentry_T **tot_hist;
6646 histentry_T *new_hist;
6647 int i;
6648 int len;
6649
6650 /* Make one long list with all entries. */
6651 max_len = hislen + viminfo_hisidx[type];
6652 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6653 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6654 if (tot_hist == NULL || new_hist == NULL)
6655 {
6656 vim_free(tot_hist);
6657 vim_free(new_hist);
6658 return;
6659 }
6660 for (i = 0; i < viminfo_hisidx[type]; i++)
6661 tot_hist[i] = &viminfo_history[type][i];
6662 len = i;
6663 for (i = 0; i < hislen; i++)
6664 if (history[type][i].hisstr != NULL)
6665 tot_hist[len++] = &history[type][i];
6666
6667 /* Sort the list on timestamp. */
6668 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6669
6670 /* Keep the newest ones. */
6671 for (i = 0; i < hislen; i++)
6672 {
6673 if (i < len)
6674 {
6675 new_hist[i] = *tot_hist[i];
6676 tot_hist[i]->hisstr = NULL;
6677 if (new_hist[i].hisnum == 0)
6678 new_hist[i].hisnum = ++hisnum[type];
6679 }
6680 else
6681 clear_hist_entry(&new_hist[i]);
6682 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006683 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006684
6685 /* Free what is not kept. */
6686 for (i = 0; i < viminfo_hisidx[type]; i++)
6687 vim_free(viminfo_history[type][i].hisstr);
6688 for (i = 0; i < hislen; i++)
6689 vim_free(history[type][i].hisstr);
6690 vim_free(history[type]);
6691 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006692 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006693}
6694
6695/*
6696 * Finish reading history lines from viminfo. Not used when writing viminfo.
6697 */
6698 void
6699finish_viminfo_history(vir_T *virp)
6700{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006702 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704 for (type = 0; type < HIST_COUNT; ++type)
6705 {
6706 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006707 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006708
6709 if (merge)
6710 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006712 concat_history(type);
6713
Bram Moolenaard23a8232018-02-10 18:45:26 +01006714 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006715 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 }
6717}
6718
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006719/*
6720 * Write history to viminfo file in "fp".
6721 * When "merge" is TRUE merge history lines with a previously read viminfo
6722 * file, data is in viminfo_history[].
6723 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006726write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 int i;
6729 int type;
6730 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006731 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733 init_history();
6734 if (hislen == 0)
6735 return;
6736 for (type = 0; type < HIST_COUNT; ++type)
6737 {
6738 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6739 if (num_saved == 0)
6740 continue;
6741 if (num_saved < 0) /* Use default */
6742 num_saved = hislen;
6743 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6744 type == HIST_CMD ? _("Command Line") :
6745 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006746 type == HIST_EXPR ? _("Expression") :
6747 type == HIST_INPUT ? _("Input Line") :
6748 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (num_saved > hislen)
6750 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006751
6752 /*
6753 * Merge typed and viminfo history:
6754 * round 1: history of typed commands.
6755 * round 2: history from recently read viminfo.
6756 */
6757 for (round = 1; round <= 2; ++round)
6758 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006759 if (round == 1)
6760 /* start at newest entry, somewhere in the list */
6761 i = hisidx[type];
6762 else if (viminfo_hisidx[type] > 0)
6763 /* start at newest entry, first in the list */
6764 i = 0;
6765 else
6766 /* empty list */
6767 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006768 if (i >= 0)
6769 while (num_saved > 0
6770 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006772 char_u *p;
6773 time_t timestamp;
6774 int c = NUL;
6775
6776 if (round == 1)
6777 {
6778 p = history[type][i].hisstr;
6779 timestamp = history[type][i].time_set;
6780 }
6781 else
6782 {
6783 p = viminfo_history[type] == NULL ? NULL
6784 : viminfo_history[type][i].hisstr;
6785 timestamp = viminfo_history[type] == NULL ? 0
6786 : viminfo_history[type][i].time_set;
6787 }
6788
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006789 if (p != NULL && (round == 2
6790 || !merge
6791 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006793 --num_saved;
6794 fputc(hist_type2char(type, TRUE), fp);
6795 /* For the search history: put the separator in the
6796 * second column; use a space if there isn't one. */
6797 if (type == HIST_SEARCH)
6798 {
6799 c = p[STRLEN(p) + 1];
6800 putc(c == NUL ? ' ' : c, fp);
6801 }
6802 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006803
6804 {
6805 char cbuf[NUMBUFLEN];
6806
6807 /* New style history with a bar line. Format:
6808 * |{bartype},{histtype},{timestamp},{separator},"text" */
6809 if (c == NUL)
6810 cbuf[0] = NUL;
6811 else
6812 sprintf(cbuf, "%d", c);
6813 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6814 type, (long)timestamp, cbuf);
6815 barline_writestring(fp, p, LSIZE - 20);
6816 putc('\n', fp);
6817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006819 if (round == 1)
6820 {
6821 /* Decrement index, loop around and stop when back at
6822 * the start. */
6823 if (--i < 0)
6824 i = hislen - 1;
6825 if (i == hisidx[type])
6826 break;
6827 }
6828 else
6829 {
6830 /* Increment index. Stop at the end in the while. */
6831 ++i;
6832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006834 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006835 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006836 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006837 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006838 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006839 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
6841}
6842#endif /* FEAT_VIMINFO */
6843
6844#if defined(FEAT_FKMAP) || defined(PROTO)
6845/*
6846 * Write a character at the current cursor+offset position.
6847 * It is directly written into the command buffer block.
6848 */
6849 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006850cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6853 {
6854 EMSG(_("E198: cmd_pchar beyond the command length"));
6855 return;
6856 }
6857 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6858 ccline.cmdbuff[ccline.cmdlen] = NUL;
6859}
6860
6861 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006862cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6865 {
6866 /* EMSG(_("cmd_gchar beyond the command length")); */
6867 return NUL;
6868 }
6869 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6870}
6871#endif
6872
6873#if defined(FEAT_CMDWIN) || defined(PROTO)
6874/*
6875 * Open a window on the current command line and history. Allow editing in
6876 * the window. Returns when the window is closed.
6877 * Returns:
6878 * CR if the command is to be executed
6879 * Ctrl_C if it is to be abandoned
6880 * K_IGNORE if editing continues
6881 */
6882 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006883open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006886 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006888 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 win_T *wp;
6890 int i;
6891 linenr_T lnum;
6892 int histtype;
6893 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 int save_restart_edit = restart_edit;
6895 int save_State = State;
6896 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006897#ifdef FEAT_RIGHTLEFT
6898 int save_cmdmsg_rl = cmdmsg_rl;
6899#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006900#ifdef FEAT_FOLDING
6901 int save_KeyTyped;
6902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904 /* Can't do this recursively. Can't do it when typing a password. */
6905 if (cmdwin_type != 0
6906# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6907 || cmdline_star > 0
6908# endif
6909 )
6910 {
6911 beep_flush();
6912 return K_IGNORE;
6913 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006914 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
6916 /* Save current window sizes. */
6917 win_size_save(&winsizes);
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006920 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006921
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006922 /* don't use a new tab page */
6923 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006924 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 /* Create a window for the command-line buffer. */
6927 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6928 {
6929 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006930 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 return K_IGNORE;
6932 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006933 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
6935 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006936 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006937 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006940#ifdef FEAT_FOLDING
6941 curwin->w_p_fen = FALSE;
6942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006944 curwin->w_p_rl = cmdmsg_rl;
6945 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006947 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006950 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006951 /* But don't allow switching to another buffer. */
6952 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar46152342005-09-07 21:18:43 +00006954 /* Showing the prompt may have set need_wait_return, reset it. */
6955 need_wait_return = FALSE;
6956
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006957 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6959 {
6960 if (p_wc == TAB)
6961 {
6962 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6963 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6964 }
6965 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6966 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006967 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006969 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6970 * sets 'textwidth' to 78). */
6971 curbuf->b_p_tw = 0;
6972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 /* Fill the buffer with the history. */
6974 init_history();
6975 if (hislen > 0)
6976 {
6977 i = hisidx[histtype];
6978 if (i >= 0)
6979 {
6980 lnum = 0;
6981 do
6982 {
6983 if (++i == hislen)
6984 i = 0;
6985 if (history[histtype][i].hisstr != NULL)
6986 ml_append(lnum++, history[histtype][i].hisstr,
6987 (colnr_T)0, FALSE);
6988 }
6989 while (i != hisidx[histtype]);
6990 }
6991 }
6992
6993 /* Replace the empty last line with the current command-line and put the
6994 * cursor there. */
6995 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6996 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6997 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006998 changed_line_abv_curs();
6999 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007000 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007003 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 /* No Ex mode here! */
7006 exmode_active = 0;
7007
7008 State = NORMAL;
7009# ifdef FEAT_MOUSE
7010 setmouse();
7011# endif
7012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007014 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007015 if (restart_edit != 0) /* autocmd with ":startinsert" */
7016 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018 i = RedrawingDisabled;
7019 RedrawingDisabled = 0;
7020
7021 /*
7022 * Call the main loop until <CR> or CTRL-C is typed.
7023 */
7024 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007025 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
7027 RedrawingDisabled = i;
7028
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007029# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007030 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007031# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007034 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007035
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007036# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007037 /* Restore KeyTyped in case it is modified by autocommands */
7038 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039# endif
7040
Bram Moolenaarccc18222007-05-10 18:25:20 +00007041 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007042 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 cmdwin_type = 0;
7044
7045 exmode_active = save_exmode;
7046
Bram Moolenaarf9821062008-06-20 16:31:07 +00007047 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007049 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
7051 cmdwin_result = Ctrl_C;
7052 EMSG(_("E199: Active window or buffer deleted"));
7053 }
7054 else
7055 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007056# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 /* autocmds may abort script processing */
7058 if (aborting() && cmdwin_result != K_IGNORE)
7059 cmdwin_result = Ctrl_C;
7060# endif
7061 /* Set the new command line from the cmdline buffer. */
7062 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007063 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007065 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7066
7067 if (histtype == HIST_CMD)
7068 {
7069 /* Execute the command directly. */
7070 ccline.cmdbuff = vim_strsave((char_u *)p);
7071 cmdwin_result = CAR;
7072 }
7073 else
7074 {
7075 /* First need to cancel what we were doing. */
7076 ccline.cmdbuff = NULL;
7077 stuffcharReadbuff(':');
7078 stuffReadbuff((char_u *)p);
7079 stuffcharReadbuff(CAR);
7080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 }
7082 else if (cmdwin_result == K_XF2) /* :qa typed */
7083 {
7084 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7085 cmdwin_result = CAR;
7086 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007087 else if (cmdwin_result == Ctrl_C)
7088 {
7089 /* :q or :close, don't execute any command
7090 * and don't modify the cmd window. */
7091 ccline.cmdbuff = NULL;
7092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 else
7094 ccline.cmdbuff = vim_strsave(ml_get_curline());
7095 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007096 {
7097 ccline.cmdbuff = vim_strsave((char_u *)"");
7098 ccline.cmdlen = 0;
7099 ccline.cmdbufflen = 1;
7100 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 else
7104 {
7105 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7106 ccline.cmdbufflen = ccline.cmdlen + 1;
7107 ccline.cmdpos = curwin->w_cursor.col;
7108 if (ccline.cmdpos > ccline.cmdlen)
7109 ccline.cmdpos = ccline.cmdlen;
7110 if (cmdwin_result == K_IGNORE)
7111 {
7112 set_cmdspos_cursor();
7113 redrawcmd();
7114 }
7115 }
7116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007118 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007119# ifdef FEAT_CONCEAL
7120 /* Avoid command-line window first character being concealed. */
7121 curwin->w_p_cole = 0;
7122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007124 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 win_goto(old_curwin);
7126 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007127
7128 /* win_close() may have already wiped the buffer when 'bh' is
7129 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007130 if (bufref_valid(&bufref))
7131 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
7133 /* Restore window sizes. */
7134 win_size_restore(&winsizes);
7135
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007136 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
7138
7139 ga_clear(&winsizes);
7140 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007141# ifdef FEAT_RIGHTLEFT
7142 cmdmsg_rl = save_cmdmsg_rl;
7143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
7145 State = save_State;
7146# ifdef FEAT_MOUSE
7147 setmouse();
7148# endif
7149
7150 return cmdwin_result;
7151}
7152#endif /* FEAT_CMDWIN */
7153
7154/*
7155 * Used for commands that either take a simple command string argument, or:
7156 * cmd << endmarker
7157 * {script}
7158 * endmarker
7159 * Returns a pointer to allocated memory with {script} or NULL.
7160 */
7161 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007162script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
7164 char_u *theline;
7165 char *end_pattern = NULL;
7166 char dot[] = ".";
7167 garray_T ga;
7168
7169 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7170 return NULL;
7171
7172 ga_init2(&ga, 1, 0x400);
7173
7174 if (cmd[2] != NUL)
7175 end_pattern = (char *)skipwhite(cmd + 2);
7176 else
7177 end_pattern = dot;
7178
7179 for (;;)
7180 {
7181 theline = eap->getline(
7182#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007183 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184#endif
7185 NUL, eap->cookie, 0);
7186
7187 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007188 {
7189 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
7193 ga_concat(&ga, theline);
7194 ga_append(&ga, '\n');
7195 vim_free(theline);
7196 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007197 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
7199 return (char_u *)ga.ga_data;
7200}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007201
7202#ifdef FEAT_SEARCH_EXTRA
7203 static void
7204set_search_match(pos_T *t)
7205{
7206 /*
7207 * First move cursor to end of match, then to the start. This
7208 * moves the whole match onto the screen when 'nowrap' is set.
7209 */
7210 t->lnum += search_match_lines;
7211 t->col = search_match_endcol;
7212 if (t->lnum > curbuf->b_ml.ml_line_count)
7213 {
7214 t->lnum = curbuf->b_ml.ml_line_count;
7215 coladvance((colnr_T)MAXCOL);
7216 }
7217}
7218#endif