blob: 421c6b7d58f06b7ffa0c7e5752f348543e59d63b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200144#ifdef FEAT_SEARCH_EXTRA
145static void set_search_match(pos_T *t);
146#endif
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200148
149#ifdef FEAT_AUTOCMD
150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
159#endif
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200162 * Abandon the command line.
163 */
164 static void
165abandon_cmdline(void)
166{
167 vim_free(ccline.cmdbuff);
168 ccline.cmdbuff = NULL;
169 if (msg_scrolled == 0)
170 compute_cmdrow();
171 MSG("");
172 redraw_cmdline = TRUE;
173}
174
Bram Moolenaaree219b02017-12-17 14:55:01 +0100175#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200176/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100177 * Guess that the pattern matches everything. Only finds specific cases, such
178 * as a trailing \|, which can happen while typing a pattern.
179 */
180 static int
181empty_pattern(char_u *p)
182{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100183 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100184
185 /* remove trailing \v and the like */
186 while (n >= 2 && p[n - 2] == '\\'
187 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
188 n -= 2;
189 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
190}
Bram Moolenaaree219b02017-12-17 14:55:01 +0100191#endif
Bram Moolenaar66216052017-12-16 16:33:44 +0100192
193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 * getcmdline() - accept a command line starting with firstc.
195 *
196 * firstc == ':' get ":" command line.
197 * firstc == '/' or '?' get search pattern
198 * firstc == '=' get expression
199 * firstc == '@' get text for input() function
200 * firstc == '>' get text for debug mode
201 * firstc == NUL get text for :insert command
202 * firstc == -1 like NUL, and break on CTRL-C
203 *
204 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
205 * command line.
206 *
207 * Careful: getcmdline() can be called recursively!
208 *
209 * Return pointer to allocated string if there is a commandline, NULL
210 * otherwise.
211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100213getcmdline(
214 int firstc,
215 long count UNUSED, /* only used for incremental search */
216 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 int c;
219 int i;
220 int j;
221 int gotesc = FALSE; /* TRUE when <ESC> just typed */
222 int do_abbr; /* when TRUE check for abbr. */
223#ifdef FEAT_CMDHIST
224 char_u *lookfor = NULL; /* string to match */
225 int hiscnt; /* current history line in use */
226 int histype; /* history type to be used */
227#endif
228#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200229 pos_T search_start; /* where 'incsearch' starts searching */
230 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200232 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200234 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200236 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200237 pos_T match_start = curwin->w_cursor;
238 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# ifdef FEAT_DIFF
240 int old_topfill;
Bram Moolenaard0480092017-11-16 22:20:39 +0100241 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242# endif
243 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200244 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 int did_incsearch = FALSE;
246 int incsearch_postponed = FALSE;
247#endif
248 int did_wild_list = FALSE; /* did wild_list() recently */
249 int wim_index = 0; /* index in wim_flags[] */
250 int res;
251 int save_msg_scroll = msg_scroll;
252 int save_State = State; /* remember State when called */
253 int some_key_typed = FALSE; /* one of the keys was typed */
254#ifdef FEAT_MOUSE
255 /* mouse drag and release events are ignored, unless they are
256 * preceded with a mouse down event */
257 int ignore_drag_release = TRUE;
258#endif
259#ifdef FEAT_EVAL
260 int break_ctrl_c = FALSE;
261#endif
262 expand_T xpc;
263 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100264#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000265 /* Everything that may work recursively should save and restore the
266 * current command line in save_ccline. That includes update_screen(), a
267 * custom status line may invoke ":normal". */
268 struct cmdline_info save_ccline;
269#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200270#ifdef FEAT_AUTOCMD
271 int cmdline_type;
272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#ifdef FEAT_EVAL
275 if (firstc == -1)
276 {
277 firstc = NUL;
278 break_ctrl_c = TRUE;
279 }
280#endif
281#ifdef FEAT_RIGHTLEFT
282 /* start without Hebrew mapping for a command line */
283 if (firstc == ':' || firstc == '=' || firstc == '>')
284 cmd_hkmap = 0;
285#endif
286
287 ccline.overstrike = FALSE; /* always start in insert mode */
288#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100289 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200290 save_cursor = curwin->w_cursor; /* may be restored later */
291 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 old_curswant = curwin->w_curswant;
293 old_leftcol = curwin->w_leftcol;
294 old_topline = curwin->w_topline;
295# ifdef FEAT_DIFF
296 old_topfill = curwin->w_topfill;
297# endif
298 old_botline = curwin->w_botline;
299#endif
300
301 /*
302 * set some variables for redrawcmd()
303 */
304 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000305 ccline.cmdindent = (firstc > 0 ? indent : 0);
306
307 /* alloc initial ccline.cmdbuff */
308 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 if (ccline.cmdbuff == NULL)
310 return NULL; /* out of memory */
311 ccline.cmdlen = ccline.cmdpos = 0;
312 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100313 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000315 /* autoindent for :insert and :append */
316 if (firstc <= 0)
317 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200318 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000319 ccline.cmdbuff[indent] = NUL;
320 ccline.cmdpos = indent;
321 ccline.cmdspos = indent;
322 ccline.cmdlen = indent;
323 }
324
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000326 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
328#ifdef FEAT_RIGHTLEFT
329 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
330 && (firstc == '/' || firstc == '?'))
331 cmdmsg_rl = TRUE;
332 else
333 cmdmsg_rl = FALSE;
334#endif
335
336 redir_off = TRUE; /* don't redirect the typed command */
337 if (!cmd_silent)
338 {
339 i = msg_scrolled;
340 msg_scrolled = 0; /* avoid wait_return message */
341 gotocmdline(TRUE);
342 msg_scrolled += i;
343 redrawcmdprompt(); /* draw prompt or indent */
344 set_cmdspos();
345 }
346 xpc.xp_context = EXPAND_NOTHING;
347 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000348#ifndef BACKSLASH_IN_FILENAME
349 xpc.xp_shell = FALSE;
350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000352#if defined(FEAT_EVAL)
353 if (ccline.input_fn)
354 {
355 xpc.xp_context = ccline.xp_context;
356 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000357# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000358 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000359# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000360 }
361#endif
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 /*
364 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
365 * doing ":@0" when register 0 doesn't contain a CR.
366 */
367 msg_scroll = FALSE;
368
369 State = CMDLINE;
370
371 if (firstc == '/' || firstc == '?' || firstc == '@')
372 {
373 /* Use ":lmap" mappings for search pattern and input(). */
374 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
375 b_im_ptr = &curbuf->b_p_iminsert;
376 else
377 b_im_ptr = &curbuf->b_p_imsearch;
378 if (*b_im_ptr == B_IMODE_LMAP)
379 State |= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100380#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 im_set_active(*b_im_ptr == B_IMODE_IM);
382#endif
383 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100384#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 else if (p_imcmdline)
386 im_set_active(TRUE);
387#endif
388
389#ifdef FEAT_MOUSE
390 setmouse();
391#endif
392#ifdef CURSOR_SHAPE
393 ui_cursor_shape(); /* may show different cursor shape */
394#endif
395
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000396 /* When inside an autocommand for writing "exiting" may be set and
397 * terminal mode set to cooked. Need to set raw mode here then. */
398 settmode(TMODE_RAW);
399
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200400#ifdef FEAT_AUTOCMD
401 /* Trigger CmdlineEnter autocommands. */
402 cmdline_type = firstc == NUL ? '-' : firstc;
403 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
404#endif
405
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#ifdef FEAT_CMDHIST
407 init_history();
408 hiscnt = hislen; /* set hiscnt to impossible history value */
409 histype = hist_char2type(firstc);
410#endif
411
412#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000413 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#endif
415
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200416 /* If something above caused an error, reset the flags, we do want to type
417 * and execute commands. Display may be messed up a bit. */
418 if (did_emsg)
419 redrawcmd();
420 did_emsg = FALSE;
421 got_int = FALSE;
422
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 /*
424 * Collect the command string, handling editing keys.
425 */
426 for (;;)
427 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000428 redir_off = TRUE; /* Don't redirect the typed command.
429 Repeated, because a ":redir" inside
430 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef USE_ON_FLY_SCROLL
432 dont_scroll = FALSE; /* allow scrolling here */
433#endif
434 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
435
436 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000437
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100438 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
439 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000440 do
441 {
442 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100443 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000444
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (KeyTyped)
446 {
447 some_key_typed = TRUE;
448#ifdef FEAT_RIGHTLEFT
449 if (cmd_hkmap)
450 c = hkmap(c);
451# ifdef FEAT_FKMAP
452 if (cmd_fkmap)
453 c = cmdl_fkmap(c);
454# endif
455 if (cmdmsg_rl && !KeyStuffed)
456 {
457 /* Invert horizontal movements and operations. Only when
458 * typed by the user directly, not when the result of a
459 * mapping. */
460 switch (c)
461 {
462 case K_RIGHT: c = K_LEFT; break;
463 case K_S_RIGHT: c = K_S_LEFT; break;
464 case K_C_RIGHT: c = K_C_LEFT; break;
465 case K_LEFT: c = K_RIGHT; break;
466 case K_S_LEFT: c = K_S_RIGHT; break;
467 case K_C_LEFT: c = K_C_RIGHT; break;
468 }
469 }
470#endif
471 }
472
473 /*
474 * Ignore got_int when CTRL-C was typed here.
475 * Don't ignore it in :global, we really need to break then, e.g., for
476 * ":g/pat/normal /pat" (without the <CR>).
477 * Don't ignore it for the input() function.
478 */
479 if ((c == Ctrl_C
480#ifdef UNIX
481 || c == intr_char
482#endif
483 )
484#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
485 && firstc != '@'
486#endif
487#ifdef FEAT_EVAL
488 && !break_ctrl_c
489#endif
490 && !global_busy)
491 got_int = FALSE;
492
493#ifdef FEAT_CMDHIST
494 /* free old command line when finished moving around in the history
495 * list */
496 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000497 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000498 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 && c != K_PAGEDOWN && c != K_PAGEUP
500 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000501 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
503 {
504 vim_free(lookfor);
505 lookfor = NULL;
506 }
507#endif
508
509 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000510 * When there are matching completions to select <S-Tab> works like
511 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000513 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 c = Ctrl_P;
515
516#ifdef FEAT_WILDMENU
517 /* Special translations for 'wildmenu' */
518 if (did_wild_list && p_wmnu)
519 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000520 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000522 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 c = Ctrl_N;
524 }
525 /* Hitting CR after "emenu Name.": complete submenu */
526 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
527 && ccline.cmdpos > 1
528 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
529 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
530 && (c == '\n' || c == '\r' || c == K_KENTER))
531 c = K_DOWN;
532#endif
533
534 /* free expanded names when finished walking through matches */
535 if (xpc.xp_numfiles != -1
536 && !(c == p_wc && KeyTyped) && c != p_wcm
537 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
538 && c != Ctrl_L)
539 {
540 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
541 did_wild_list = FALSE;
542#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000543 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544#endif
545 xpc.xp_context = EXPAND_NOTHING;
546 wim_index = 0;
547#ifdef FEAT_WILDMENU
548 if (p_wmnu && wild_menu_showing != 0)
549 {
550 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000551 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552
553 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000554 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 if (wild_menu_showing == WM_SCROLLED)
557 {
558 /* Entered command line, move it up */
559 cmdline_row--;
560 redrawcmd();
561 }
562 else if (save_p_ls != -1)
563 {
564 /* restore 'laststatus' and 'winminheight' */
565 p_ls = save_p_ls;
566 p_wmh = save_p_wmh;
567 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000568 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000570 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 redrawcmd();
572 save_p_ls = -1;
573 }
574 else
575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 redraw_statuslines();
578 }
579 KeyTyped = skt;
580 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000581 if (ccline.input_fn)
582 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584#endif
585 }
586
587#ifdef FEAT_WILDMENU
588 /* Special translations for 'wildmenu' */
589 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
590 {
591 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000592 if (c == K_DOWN && ccline.cmdpos > 0
593 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000595 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
597 /* Hitting <Up>: Remove one submenu name in front of the
598 * cursor */
599 int found = FALSE;
600
601 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
602 i = 0;
603 while (--j > 0)
604 {
605 /* check for start of menu name */
606 if (ccline.cmdbuff[j] == ' '
607 && ccline.cmdbuff[j - 1] != '\\')
608 {
609 i = j + 1;
610 break;
611 }
612 /* check for start of submenu name */
613 if (ccline.cmdbuff[j] == '.'
614 && ccline.cmdbuff[j - 1] != '\\')
615 {
616 if (found)
617 {
618 i = j + 1;
619 break;
620 }
621 else
622 found = TRUE;
623 }
624 }
625 if (i > 0)
626 cmdline_del(i);
627 c = p_wc;
628 xpc.xp_context = EXPAND_NOTHING;
629 }
630 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000631 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000632 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000633 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {
635 char_u upseg[5];
636
637 upseg[0] = PATHSEP;
638 upseg[1] = '.';
639 upseg[2] = '.';
640 upseg[3] = PATHSEP;
641 upseg[4] = NUL;
642
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000643 if (c == K_DOWN
644 && ccline.cmdpos > 0
645 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
646 && (ccline.cmdpos < 3
647 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
649 {
650 /* go down a directory */
651 c = p_wc;
652 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000653 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {
655 /* If in a direct ancestor, strip off one ../ to go down */
656 int found = FALSE;
657
658 j = ccline.cmdpos;
659 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
660 while (--j > i)
661 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000662#ifdef FEAT_MBYTE
663 if (has_mbyte)
664 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (vim_ispathsep(ccline.cmdbuff[j]))
667 {
668 found = TRUE;
669 break;
670 }
671 }
672 if (found
673 && ccline.cmdbuff[j - 1] == '.'
674 && ccline.cmdbuff[j - 2] == '.'
675 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
676 {
677 cmdline_del(j - 2);
678 c = p_wc;
679 }
680 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000681 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
683 /* go up a directory */
684 int found = FALSE;
685
686 j = ccline.cmdpos - 1;
687 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
688 while (--j > i)
689 {
690#ifdef FEAT_MBYTE
691 if (has_mbyte)
692 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
693#endif
694 if (vim_ispathsep(ccline.cmdbuff[j])
695#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100696 && vim_strchr((char_u *)" *?[{`$%#",
697 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#endif
699 )
700 {
701 if (found)
702 {
703 i = j + 1;
704 break;
705 }
706 else
707 found = TRUE;
708 }
709 }
710
711 if (!found)
712 j = i;
713 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
714 j += 4;
715 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
716 && j == i)
717 j += 3;
718 else
719 j = 0;
720 if (j > 0)
721 {
722 /* TODO this is only for DOS/UNIX systems - need to put in
723 * machine-specific stuff here and in upseg init */
724 cmdline_del(j);
725 put_on_cmdline(upseg + 1, 3, FALSE);
726 }
727 else if (ccline.cmdpos > i)
728 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100729
730 /* Now complete in the new directory. Set KeyTyped in case the
731 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100733 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737#endif /* FEAT_WILDMENU */
738
739 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
740 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
741 if (c == Ctrl_BSL)
742 {
743 ++no_mapping;
744 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000745 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 --no_mapping;
747 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200748 /* CTRL-\ e doesn't work when obtaining an expression, unless it
749 * is in a mapping. */
750 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
751 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
753 vungetc(c);
754 c = Ctrl_BSL;
755 }
756#ifdef FEAT_EVAL
757 else if (c == 'e')
758 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200759 char_u *p = NULL;
760 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
762 /*
763 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000764 * Need to save and restore the current command line, to be
765 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
767 if (ccline.cmdpos == ccline.cmdlen)
768 new_cmdpos = 99999; /* keep it at the end */
769 else
770 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000771
772 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000774 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (c == '=')
776 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000777 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000778 * to avoid nasty things like going to another buffer when
779 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000780 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000781 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000783 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000784 restore_cmdline(&save_ccline);
785
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200786 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200788 len = (int)STRLEN(p);
789 if (realloc_cmdbuff(len + 1) == OK)
790 {
791 ccline.cmdlen = len;
792 STRCPY(ccline.cmdbuff, p);
793 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200795 /* Restore the cursor or use the position set with
796 * set_cmdline_pos(). */
797 if (new_cmdpos > ccline.cmdlen)
798 ccline.cmdpos = ccline.cmdlen;
799 else
800 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200802 KeyTyped = FALSE; /* Don't do p_wc completion. */
803 redrawcmd();
804 goto cmdline_changed;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807 }
808 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100809 got_int = FALSE; /* don't abandon the command line */
810 did_emsg = FALSE;
811 emsg_on_display = FALSE;
812 redrawcmd();
813 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
815#endif
816 else
817 {
818 if (c == Ctrl_G && p_im && restart_edit == 0)
819 restart_edit = 'a';
820 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
821 in history */
822 goto returncmd; /* back to Normal mode */
823 }
824 }
825
826#ifdef FEAT_CMDWIN
827 if (c == cedit_key || c == K_CMDWIN)
828 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200829 if (ex_normal_busy == 0 && got_int == FALSE)
830 {
831 /*
832 * Open a window to edit the command line (and history).
833 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200834 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200835 some_key_typed = TRUE;
836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838# ifdef FEAT_DIGRAPHS
839 else
840# endif
841#endif
842#ifdef FEAT_DIGRAPHS
843 c = do_digraph(c);
844#endif
845
846 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
847 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
848 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000849 /* In Ex mode a backslash escapes a newline. */
850 if (exmode_active
851 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000852 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000853 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000854 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000856 if (c == K_KENTER)
857 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000859 else
860 {
861 gotesc = FALSE; /* Might have typed ESC previously, don't
862 truncate the cmdline now. */
863 if (ccheck_abbr(c + ABBR_OFF))
864 goto cmdline_changed;
865 if (!cmd_silent)
866 {
867 windgoto(msg_row, 0);
868 out_flush();
869 }
870 break;
871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873
874 /*
875 * Completion for 'wildchar' or 'wildcharm' key.
876 * - hitting <ESC> twice means: abandon command line.
877 * - wildcard expansion is only done when the 'wildchar' key is really
878 * typed, not when it comes from a macro
879 */
880 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
881 {
882 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
883 {
884 /* if 'wildmode' contains "list" may still need to list */
885 if (xpc.xp_numfiles > 1
886 && !did_wild_list
887 && (wim_flags[wim_index] & WIM_LIST))
888 {
889 (void)showmatches(&xpc, FALSE);
890 redrawcmd();
891 did_wild_list = TRUE;
892 }
893 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100894 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
895 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100897 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
898 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 else
900 res = OK; /* don't insert 'wildchar' now */
901 }
902 else /* typed p_wc first time */
903 {
904 wim_index = 0;
905 j = ccline.cmdpos;
906 /* if 'wildmode' first contains "longest", get longest
907 * common part */
908 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100909 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
910 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100912 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
913 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
915 /* if interrupted while completing, behave like it failed */
916 if (got_int)
917 {
918 (void)vpeekc(); /* remove <C-C> from input stream */
919 got_int = FALSE; /* don't abandon the command line */
920 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
921#ifdef FEAT_WILDMENU
922 xpc.xp_context = EXPAND_NOTHING;
923#endif
924 goto cmdline_changed;
925 }
926
927 /* when more than one match, and 'wildmode' first contains
928 * "list", or no change and 'wildmode' contains "longest,list",
929 * list all matches */
930 if (res == OK && xpc.xp_numfiles > 1)
931 {
932 /* a "longest" that didn't do anything is skipped (but not
933 * "list:longest") */
934 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
935 wim_index = 1;
936 if ((wim_flags[wim_index] & WIM_LIST)
937#ifdef FEAT_WILDMENU
938 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
939#endif
940 )
941 {
942 if (!(wim_flags[0] & WIM_LONGEST))
943 {
944#ifdef FEAT_WILDMENU
945 int p_wmnu_save = p_wmnu;
946 p_wmnu = 0;
947#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100948 /* remove match */
949 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#ifdef FEAT_WILDMENU
951 p_wmnu = p_wmnu_save;
952#endif
953 }
954#ifdef FEAT_WILDMENU
955 (void)showmatches(&xpc, p_wmnu
956 && ((wim_flags[wim_index] & WIM_LIST) == 0));
957#else
958 (void)showmatches(&xpc, FALSE);
959#endif
960 redrawcmd();
961 did_wild_list = TRUE;
962 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100963 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
964 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100966 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
967 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
969 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200970 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972#ifdef FEAT_WILDMENU
973 else if (xpc.xp_numfiles == -1)
974 xpc.xp_context = EXPAND_NOTHING;
975#endif
976 }
977 if (wim_index < 3)
978 ++wim_index;
979 if (c == ESC)
980 gotesc = TRUE;
981 if (res == OK)
982 goto cmdline_changed;
983 }
984
985 gotesc = FALSE;
986
987 /* <S-Tab> goes to last match, in a clumsy way */
988 if (c == K_S_TAB && KeyTyped)
989 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100990 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
991 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
992 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 goto cmdline_changed;
994 }
995
996 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
997 c = NL;
998
999 do_abbr = TRUE; /* default: check for abbreviation */
1000
1001 /*
1002 * Big switch for a typed command line character.
1003 */
1004 switch (c)
1005 {
1006 case K_BS:
1007 case Ctrl_H:
1008 case K_DEL:
1009 case K_KDEL:
1010 case Ctrl_W:
1011#ifdef FEAT_FKMAP
1012 if (cmd_fkmap && c == K_BS)
1013 c = K_DEL;
1014#endif
1015 if (c == K_KDEL)
1016 c = K_DEL;
1017
1018 /*
1019 * delete current character is the same as backspace on next
1020 * character, except at end of line
1021 */
1022 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1023 ++ccline.cmdpos;
1024#ifdef FEAT_MBYTE
1025 if (has_mbyte && c == K_DEL)
1026 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1027 ccline.cmdbuff + ccline.cmdpos);
1028#endif
1029 if (ccline.cmdpos > 0)
1030 {
1031 char_u *p;
1032
1033 j = ccline.cmdpos;
1034 p = ccline.cmdbuff + j;
1035#ifdef FEAT_MBYTE
1036 if (has_mbyte)
1037 {
1038 p = mb_prevptr(ccline.cmdbuff, p);
1039 if (c == Ctrl_W)
1040 {
1041 while (p > ccline.cmdbuff && vim_isspace(*p))
1042 p = mb_prevptr(ccline.cmdbuff, p);
1043 i = mb_get_class(p);
1044 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1045 p = mb_prevptr(ccline.cmdbuff, p);
1046 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001047 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049 }
1050 else
1051#endif
1052 if (c == Ctrl_W)
1053 {
1054 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1055 --p;
1056 i = vim_iswordc(p[-1]);
1057 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1058 && vim_iswordc(p[-1]) == i)
1059 --p;
1060 }
1061 else
1062 --p;
1063 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1064 ccline.cmdlen -= j - ccline.cmdpos;
1065 i = ccline.cmdpos;
1066 while (i < ccline.cmdlen)
1067 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1068
1069 /* Truncate at the end, required for multi-byte chars. */
1070 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001071#ifdef FEAT_SEARCH_EXTRA
1072 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001073 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001074 search_start = save_cursor;
1075 /* save view settings, so that the screen
1076 * won't be restored at the wrong position */
1077 old_curswant = init_curswant;
1078 old_leftcol = init_leftcol;
1079 old_topline = init_topline;
1080# ifdef FEAT_DIFF
1081 old_topfill = init_topfill;
1082# endif
1083 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001084 }
1085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 redrawcmd();
1087 }
1088 else if (ccline.cmdlen == 0 && c != Ctrl_W
1089 && ccline.cmdprompt == NULL && indent == 0)
1090 {
1091 /* In ex and debug mode it doesn't make sense to return. */
1092 if (exmode_active
1093#ifdef FEAT_EVAL
1094 || ccline.cmdfirstc == '>'
1095#endif
1096 )
1097 goto cmdline_not_changed;
1098
1099 vim_free(ccline.cmdbuff); /* no commandline to return */
1100 ccline.cmdbuff = NULL;
1101 if (!cmd_silent)
1102 {
1103#ifdef FEAT_RIGHTLEFT
1104 if (cmdmsg_rl)
1105 msg_col = Columns;
1106 else
1107#endif
1108 msg_col = 0;
1109 msg_putchar(' '); /* delete ':' */
1110 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001111#ifdef FEAT_SEARCH_EXTRA
1112 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001113 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 redraw_cmdline = TRUE;
1116 goto returncmd; /* back to cmd mode */
1117 }
1118 goto cmdline_changed;
1119
1120 case K_INS:
1121 case K_KINS:
1122#ifdef FEAT_FKMAP
1123 /* if Farsi mode set, we are in reverse insert mode -
1124 Do not change the mode */
1125 if (cmd_fkmap)
1126 beep_flush();
1127 else
1128#endif
1129 ccline.overstrike = !ccline.overstrike;
1130#ifdef CURSOR_SHAPE
1131 ui_cursor_shape(); /* may show different cursor shape */
1132#endif
1133 goto cmdline_not_changed;
1134
1135 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001136 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
1138 /* ":lmap" mappings exists, toggle use of mappings. */
1139 State ^= LANGMAP;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01001140#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 im_set_active(FALSE); /* Disable input method */
1142#endif
1143 if (b_im_ptr != NULL)
1144 {
1145 if (State & LANGMAP)
1146 *b_im_ptr = B_IMODE_LMAP;
1147 else
1148 *b_im_ptr = B_IMODE_NONE;
1149 }
1150 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +01001151#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 {
1154 /* There are no ":lmap" mappings, toggle IM. When
1155 * 'imdisable' is set don't try getting the status, it's
1156 * always off. */
1157 if ((p_imdisable && b_im_ptr != NULL)
1158 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1159 {
1160 im_set_active(FALSE); /* Disable input method */
1161 if (b_im_ptr != NULL)
1162 *b_im_ptr = B_IMODE_NONE;
1163 }
1164 else
1165 {
1166 im_set_active(TRUE); /* Enable input method */
1167 if (b_im_ptr != NULL)
1168 *b_im_ptr = B_IMODE_IM;
1169 }
1170 }
1171#endif
1172 if (b_im_ptr != NULL)
1173 {
1174 if (b_im_ptr == &curbuf->b_p_iminsert)
1175 set_iminsert_global();
1176 else
1177 set_imsearch_global();
1178 }
1179#ifdef CURSOR_SHAPE
1180 ui_cursor_shape(); /* may show different cursor shape */
1181#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001182#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Show/unshow value of 'keymap' in status lines later. */
1184 status_redraw_curbuf();
1185#endif
1186 goto cmdline_not_changed;
1187
1188/* case '@': only in very old vi */
1189 case Ctrl_U:
1190 /* delete all characters left of the cursor */
1191 j = ccline.cmdpos;
1192 ccline.cmdlen -= j;
1193 i = ccline.cmdpos = 0;
1194 while (i < ccline.cmdlen)
1195 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1196 /* Truncate at the end, required for multi-byte chars. */
1197 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001198#ifdef FEAT_SEARCH_EXTRA
1199 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001200 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 redrawcmd();
1203 goto cmdline_changed;
1204
1205#ifdef FEAT_CLIPBOARD
1206 case Ctrl_Y:
1207 /* Copy the modeless selection, if there is one. */
1208 if (clip_star.state != SELECT_CLEARED)
1209 {
1210 if (clip_star.state == SELECT_DONE)
1211 clip_copy_modeless_selection(TRUE);
1212 goto cmdline_not_changed;
1213 }
1214 break;
1215#endif
1216
1217 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1218 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001219 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001220 * ":normal" runs out of characters. */
1221 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001222 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 goto cmdline_not_changed;
1224
1225 gotesc = TRUE; /* will free ccline.cmdbuff after
1226 putting it in history */
1227 goto returncmd; /* back to cmd mode */
1228
1229 case Ctrl_R: /* insert register */
1230#ifdef USE_ON_FLY_SCROLL
1231 dont_scroll = TRUE; /* disallow scrolling here */
1232#endif
1233 putcmdline('"', TRUE);
1234 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001235 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (i == Ctrl_O)
1237 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1238 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001239 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001240 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 --no_mapping;
1242#ifdef FEAT_EVAL
1243 /*
1244 * Insert the result of an expression.
1245 * Need to save the current command line, to be able to enter
1246 * a new one...
1247 */
1248 new_cmdpos = -1;
1249 if (c == '=')
1250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1252 {
1253 beep_flush();
1254 c = ESC;
1255 }
1256 else
1257 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001258 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001260 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 }
1263#endif
1264 if (c != ESC) /* use ESC to cancel inserting register */
1265 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001266 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001267
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001268#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001269 /* When there was a serious error abort getting the
1270 * command line. */
1271 if (aborting())
1272 {
1273 gotesc = TRUE; /* will free ccline.cmdbuff after
1274 putting it in history */
1275 goto returncmd; /* back to cmd mode */
1276 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 KeyTyped = FALSE; /* Don't do p_wc completion. */
1279#ifdef FEAT_EVAL
1280 if (new_cmdpos >= 0)
1281 {
1282 /* set_cmdline_pos() was used */
1283 if (new_cmdpos > ccline.cmdlen)
1284 ccline.cmdpos = ccline.cmdlen;
1285 else
1286 ccline.cmdpos = new_cmdpos;
1287 }
1288#endif
1289 }
1290 redrawcmd();
1291 goto cmdline_changed;
1292
1293 case Ctrl_D:
1294 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1295 break; /* Use ^D as normal char instead */
1296
1297 redrawcmd();
1298 continue; /* don't do incremental search now */
1299
1300 case K_RIGHT:
1301 case K_S_RIGHT:
1302 case K_C_RIGHT:
1303 do
1304 {
1305 if (ccline.cmdpos >= ccline.cmdlen)
1306 break;
1307 i = cmdline_charsize(ccline.cmdpos);
1308 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1309 break;
1310 ccline.cmdspos += i;
1311#ifdef FEAT_MBYTE
1312 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001313 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 + ccline.cmdpos);
1315 else
1316#endif
1317 ++ccline.cmdpos;
1318 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001319 while ((c == K_S_RIGHT || c == K_C_RIGHT
1320 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1322#ifdef FEAT_MBYTE
1323 if (has_mbyte)
1324 set_cmdspos_cursor();
1325#endif
1326 goto cmdline_not_changed;
1327
1328 case K_LEFT:
1329 case K_S_LEFT:
1330 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001331 if (ccline.cmdpos == 0)
1332 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 do
1334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 --ccline.cmdpos;
1336#ifdef FEAT_MBYTE
1337 if (has_mbyte) /* move to first byte of char */
1338 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1339 ccline.cmdbuff + ccline.cmdpos);
1340#endif
1341 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1342 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001343 while (ccline.cmdpos > 0
1344 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001345 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1347#ifdef FEAT_MBYTE
1348 if (has_mbyte)
1349 set_cmdspos_cursor();
1350#endif
1351 goto cmdline_not_changed;
1352
1353 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001354 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001355 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar4770d092006-01-12 23:22:24 +00001357#ifdef FEAT_GUI_W32
1358 /* On Win32 ignore <M-F4>, we get it when closing the window was
1359 * cancelled. */
1360 case K_F4:
1361 if (mod_mask == MOD_MASK_ALT)
1362 {
1363 redrawcmd(); /* somehow the cmdline is cleared */
1364 goto cmdline_not_changed;
1365 }
1366 break;
1367#endif
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369#ifdef FEAT_MOUSE
1370 case K_MIDDLEDRAG:
1371 case K_MIDDLERELEASE:
1372 goto cmdline_not_changed; /* Ignore mouse */
1373
1374 case K_MIDDLEMOUSE:
1375# ifdef FEAT_GUI
1376 /* When GUI is active, also paste when 'mouse' is empty */
1377 if (!gui.in_use)
1378# endif
1379 if (!mouse_has(MOUSE_COMMAND))
1380 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001381# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001383 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001385# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001386 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 redrawcmd();
1388 goto cmdline_changed;
1389
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001390# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001392 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 redrawcmd();
1394 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397 case K_LEFTDRAG:
1398 case K_LEFTRELEASE:
1399 case K_RIGHTDRAG:
1400 case K_RIGHTRELEASE:
1401 /* Ignore drag and release events when the button-down wasn't
1402 * seen before. */
1403 if (ignore_drag_release)
1404 goto cmdline_not_changed;
1405 /* FALLTHROUGH */
1406 case K_LEFTMOUSE:
1407 case K_RIGHTMOUSE:
1408 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1409 ignore_drag_release = TRUE;
1410 else
1411 ignore_drag_release = FALSE;
1412# ifdef FEAT_GUI
1413 /* When GUI is active, also move when 'mouse' is empty */
1414 if (!gui.in_use)
1415# endif
1416 if (!mouse_has(MOUSE_COMMAND))
1417 goto cmdline_not_changed; /* Ignore mouse */
1418# ifdef FEAT_CLIPBOARD
1419 if (mouse_row < cmdline_row && clip_star.available)
1420 {
1421 int button, is_click, is_drag;
1422
1423 /*
1424 * Handle modeless selection.
1425 */
1426 button = get_mouse_button(KEY2TERMCAP1(c),
1427 &is_click, &is_drag);
1428 if (mouse_model_popup() && button == MOUSE_LEFT
1429 && (mod_mask & MOD_MASK_SHIFT))
1430 {
1431 /* Translate shift-left to right button. */
1432 button = MOUSE_RIGHT;
1433 mod_mask &= ~MOD_MASK_SHIFT;
1434 }
1435 clip_modeless(button, is_click, is_drag);
1436 goto cmdline_not_changed;
1437 }
1438# endif
1439
1440 set_cmdspos();
1441 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1442 ++ccline.cmdpos)
1443 {
1444 i = cmdline_charsize(ccline.cmdpos);
1445 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1446 && mouse_col < ccline.cmdspos % Columns + i)
1447 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001448# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (has_mbyte)
1450 {
1451 /* Count ">" for double-wide char that doesn't fit. */
1452 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001453 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 + ccline.cmdpos) - 1;
1455 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 ccline.cmdspos += i;
1458 }
1459 goto cmdline_not_changed;
1460
1461 /* Mouse scroll wheel: ignored here */
1462 case K_MOUSEDOWN:
1463 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001464 case K_MOUSELEFT:
1465 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 /* Alternate buttons ignored here */
1467 case K_X1MOUSE:
1468 case K_X1DRAG:
1469 case K_X1RELEASE:
1470 case K_X2MOUSE:
1471 case K_X2DRAG:
1472 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001473 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 goto cmdline_not_changed;
1475
1476#endif /* FEAT_MOUSE */
1477
1478#ifdef FEAT_GUI
1479 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1480 case K_LEFTRELEASE_NM:
1481 goto cmdline_not_changed;
1482
1483 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001484 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 gui_do_scroll();
1487 redrawcmd();
1488 }
1489 goto cmdline_not_changed;
1490
1491 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001492 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001494 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 redrawcmd();
1496 }
1497 goto cmdline_not_changed;
1498#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001499#ifdef FEAT_GUI_TABLINE
1500 case K_TABLINE:
1501 case K_TABMENU:
1502 /* Don't want to change any tabs here. Make sure the same tab
1503 * is still selected. */
1504 if (gui_use_tabline())
1505 gui_mch_set_curtab(tabpage_index(curtab));
1506 goto cmdline_not_changed;
1507#endif
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 case K_SELECT: /* end of Select mode mapping - ignore */
1510 goto cmdline_not_changed;
1511
1512 case Ctrl_B: /* begin of command line */
1513 case K_HOME:
1514 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 case K_S_HOME:
1516 case K_C_HOME:
1517 ccline.cmdpos = 0;
1518 set_cmdspos();
1519 goto cmdline_not_changed;
1520
1521 case Ctrl_E: /* end of command line */
1522 case K_END:
1523 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 case K_S_END:
1525 case K_C_END:
1526 ccline.cmdpos = ccline.cmdlen;
1527 set_cmdspos_cursor();
1528 goto cmdline_not_changed;
1529
1530 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001531 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 break;
1533 goto cmdline_changed;
1534
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001535 case Ctrl_L:
1536#ifdef FEAT_SEARCH_EXTRA
1537 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1538 {
1539 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001540 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001541 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001542 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001543 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001544 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001545 c = gchar_cursor();
1546 /* If 'ignorecase' and 'smartcase' are set and the
1547 * command line has no uppercase characters, convert
1548 * the character to lowercase */
1549 if (p_ic && p_scs
1550 && !pat_has_uppercase(ccline.cmdbuff))
1551 c = MB_TOLOWER(c);
1552 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001553 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001554 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001555 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001556 != NULL)
1557 {
1558 /* put a backslash before special
1559 * characters */
1560 stuffcharReadbuff(c);
1561 c = '\\';
1562 }
1563 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001564 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001565 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001566 }
1567 goto cmdline_not_changed;
1568 }
1569#endif
1570
1571 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001572 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 break;
1574 goto cmdline_changed;
1575
1576 case Ctrl_N: /* next match */
1577 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001578 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001580 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1581 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001583 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001586 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 case K_UP:
1588 case K_DOWN:
1589 case K_S_UP:
1590 case K_S_DOWN:
1591 case K_PAGEUP:
1592 case K_KPAGEUP:
1593 case K_PAGEDOWN:
1594 case K_KPAGEDOWN:
1595 if (hislen == 0 || firstc == NUL) /* no history */
1596 goto cmdline_not_changed;
1597
1598 i = hiscnt;
1599
1600 /* save current command string so it can be restored later */
1601 if (lookfor == NULL)
1602 {
1603 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1604 goto cmdline_not_changed;
1605 lookfor[ccline.cmdpos] = NUL;
1606 }
1607
1608 j = (int)STRLEN(lookfor);
1609 for (;;)
1610 {
1611 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001612 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001613 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 if (hiscnt == hislen) /* first time */
1616 hiscnt = hisidx[histype];
1617 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1618 hiscnt = hislen - 1;
1619 else if (hiscnt != hisidx[histype] + 1)
1620 --hiscnt;
1621 else /* at top of list */
1622 {
1623 hiscnt = i;
1624 break;
1625 }
1626 }
1627 else /* one step forwards */
1628 {
1629 /* on last entry, clear the line */
1630 if (hiscnt == hisidx[histype])
1631 {
1632 hiscnt = hislen;
1633 break;
1634 }
1635
1636 /* not on a history line, nothing to do */
1637 if (hiscnt == hislen)
1638 break;
1639 if (hiscnt == hislen - 1) /* wrap around */
1640 hiscnt = 0;
1641 else
1642 ++hiscnt;
1643 }
1644 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1645 {
1646 hiscnt = i;
1647 break;
1648 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001649 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001650 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 || STRNCMP(history[histype][hiscnt].hisstr,
1652 lookfor, (size_t)j) == 0)
1653 break;
1654 }
1655
1656 if (hiscnt != i) /* jumped to other entry */
1657 {
1658 char_u *p;
1659 int len;
1660 int old_firstc;
1661
1662 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001663 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (hiscnt == hislen)
1665 p = lookfor; /* back to the old one */
1666 else
1667 p = history[histype][hiscnt].hisstr;
1668
1669 if (histype == HIST_SEARCH
1670 && p != lookfor
1671 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1672 {
1673 /* Correct for the separator character used when
1674 * adding the history entry vs the one used now.
1675 * First loop: count length.
1676 * Second loop: copy the characters. */
1677 for (i = 0; i <= 1; ++i)
1678 {
1679 len = 0;
1680 for (j = 0; p[j] != NUL; ++j)
1681 {
1682 /* Replace old sep with new sep, unless it is
1683 * escaped. */
1684 if (p[j] == old_firstc
1685 && (j == 0 || p[j - 1] != '\\'))
1686 {
1687 if (i > 0)
1688 ccline.cmdbuff[len] = firstc;
1689 }
1690 else
1691 {
1692 /* Escape new sep, unless it is already
1693 * escaped. */
1694 if (p[j] == firstc
1695 && (j == 0 || p[j - 1] != '\\'))
1696 {
1697 if (i > 0)
1698 ccline.cmdbuff[len] = '\\';
1699 ++len;
1700 }
1701 if (i > 0)
1702 ccline.cmdbuff[len] = p[j];
1703 }
1704 ++len;
1705 }
1706 if (i == 0)
1707 {
1708 alloc_cmdbuff(len);
1709 if (ccline.cmdbuff == NULL)
1710 goto returncmd;
1711 }
1712 }
1713 ccline.cmdbuff[len] = NUL;
1714 }
1715 else
1716 {
1717 alloc_cmdbuff((int)STRLEN(p));
1718 if (ccline.cmdbuff == NULL)
1719 goto returncmd;
1720 STRCPY(ccline.cmdbuff, p);
1721 }
1722
1723 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1724 redrawcmd();
1725 goto cmdline_changed;
1726 }
1727 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001728#endif
1729 goto cmdline_not_changed;
1730
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001731#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001732 case Ctrl_G: /* next match */
1733 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001734 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1735 {
1736 pos_T t;
Bram Moolenaard0480092017-11-16 22:20:39 +01001737 char_u *pat;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001738 int search_flags = SEARCH_NOOF;
Bram Moolenaar11956692016-08-27 16:26:56 +02001739
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001740 if (ccline.cmdlen == 0)
1741 goto cmdline_not_changed;
1742
Bram Moolenaard0480092017-11-16 22:20:39 +01001743 if (firstc == ccline.cmdbuff[0])
1744 pat = last_search_pattern();
1745 else
1746 pat = ccline.cmdbuff;
1747
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001748 save_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001749 cursor_off();
1750 out_flush();
1751 if (c == Ctrl_G)
1752 {
1753 t = match_end;
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001754 if (LT_POS(match_start, match_end))
1755 /* start searching at the end of the match
1756 * not at the beginning of the next column */
1757 (void)decl(&t);
Bram Moolenaar11956692016-08-27 16:26:56 +02001758 search_flags += SEARCH_COL;
1759 }
1760 else
1761 t = match_start;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001762 if (!p_hls)
1763 search_flags += SEARCH_KEEP;
Bram Moolenaar11956692016-08-27 16:26:56 +02001764 ++emsg_off;
1765 i = searchit(curwin, curbuf, &t,
1766 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaard0480092017-11-16 22:20:39 +01001767 pat, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001768 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001769 --emsg_off;
1770 if (i)
1771 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001772 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001773 match_end = t;
1774 match_start = t;
1775 if (c == Ctrl_T && firstc == '/')
1776 {
1777 /* move just before the current match, so that
1778 * when nv_search finishes the cursor will be
1779 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001780 search_start = t;
1781 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001782 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001783 else if (c == Ctrl_G && firstc == '?')
1784 {
1785 /* move just after the current match, so that
1786 * when nv_search finishes the cursor will be
1787 * put back on the match */
1788 search_start = t;
1789 (void)incl(&search_start);
1790 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001791 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001792 {
1793 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001794 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001795 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001796 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001797 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001798 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001799 }
1800
1801 set_search_match(&match_end);
1802 curwin->w_cursor = match_start;
1803 changed_cline_bef_curs();
1804 update_topline();
1805 validate_cursor();
1806 highlight_match = TRUE;
1807 old_curswant = curwin->w_curswant;
1808 old_leftcol = curwin->w_leftcol;
1809 old_topline = curwin->w_topline;
1810# ifdef FEAT_DIFF
1811 old_topfill = curwin->w_topfill;
1812# endif
1813 old_botline = curwin->w_botline;
1814 update_screen(NOT_VALID);
1815 redrawcmdline();
1816 }
1817 else
1818 vim_beep(BO_ERROR);
Bram Moolenaara1d5c152017-12-16 19:05:22 +01001819 restore_last_search_pattern();
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001820 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001821 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001822 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#endif
1824
1825 case Ctrl_V:
1826 case Ctrl_Q:
1827#ifdef FEAT_MOUSE
1828 ignore_drag_release = TRUE;
1829#endif
1830 putcmdline('^', TRUE);
1831 c = get_literal(); /* get next (two) character(s) */
1832 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001833 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_MBYTE
1835 /* may need to remove ^ when composing char was typed */
1836 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1837 {
1838 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1839 msg_putchar(' ');
1840 cursorcmd();
1841 }
1842#endif
1843 break;
1844
1845#ifdef FEAT_DIGRAPHS
1846 case Ctrl_K:
1847#ifdef FEAT_MOUSE
1848 ignore_drag_release = TRUE;
1849#endif
1850 putcmdline('?', TRUE);
1851#ifdef USE_ON_FLY_SCROLL
1852 dont_scroll = TRUE; /* disallow scrolling here */
1853#endif
1854 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001855 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (c != NUL)
1857 break;
1858
1859 redrawcmd();
1860 goto cmdline_not_changed;
1861#endif /* FEAT_DIGRAPHS */
1862
1863#ifdef FEAT_RIGHTLEFT
1864 case Ctrl__: /* CTRL-_: switch language mode */
1865 if (!p_ari)
1866 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001867# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (p_altkeymap)
1869 {
1870 cmd_fkmap = !cmd_fkmap;
1871 if (cmd_fkmap) /* in Farsi always in Insert mode */
1872 ccline.overstrike = FALSE;
1873 }
1874 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 cmd_hkmap = !cmd_hkmap;
1877 goto cmdline_not_changed;
1878#endif
1879
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001880 case K_PS:
1881 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1882 goto cmdline_changed;
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 default:
1885#ifdef UNIX
1886 if (c == intr_char)
1887 {
1888 gotesc = TRUE; /* will free ccline.cmdbuff after
1889 putting it in history */
1890 goto returncmd; /* back to Normal mode */
1891 }
1892#endif
1893 /*
1894 * Normal character with no special meaning. Just set mod_mask
1895 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1896 * the string <S-Space>. This should only happen after ^V.
1897 */
1898 if (!IS_SPECIAL(c))
1899 mod_mask = 0x0;
1900 break;
1901 }
1902 /*
1903 * End of switch on command line character.
1904 * We come here if we have a normal character.
1905 */
1906
Bram Moolenaarede3e632013-06-23 16:16:19 +02001907 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908#ifdef FEAT_MBYTE
1909 /* Add ABBR_OFF for characters above 0x100, this is
1910 * what check_abbr() expects. */
1911 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1912#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001913 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 goto cmdline_changed;
1915
1916 /*
1917 * put the character in the command line
1918 */
1919 if (IS_SPECIAL(c) || mod_mask != 0)
1920 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1921 else
1922 {
1923#ifdef FEAT_MBYTE
1924 if (has_mbyte)
1925 {
1926 j = (*mb_char2bytes)(c, IObuff);
1927 IObuff[j] = NUL; /* exclude composing chars */
1928 put_on_cmdline(IObuff, j, TRUE);
1929 }
1930 else
1931#endif
1932 {
1933 IObuff[0] = c;
1934 put_on_cmdline(IObuff, 1, TRUE);
1935 }
1936 }
1937 goto cmdline_changed;
1938
1939/*
1940 * This part implements incremental searches for "/" and "?"
1941 * Jump to cmdline_not_changed when a character has been read but the command
1942 * line did not change. Then we only search and redraw if something changed in
1943 * the past.
1944 * Jump to cmdline_changed when the command line did change.
1945 * (Sorry for the goto's, I know it is ugly).
1946 */
1947cmdline_not_changed:
1948#ifdef FEAT_SEARCH_EXTRA
1949 if (!incsearch_postponed)
1950 continue;
1951#endif
1952
1953cmdline_changed:
1954#ifdef FEAT_SEARCH_EXTRA
1955 /*
1956 * 'incsearch' highlighting.
1957 */
1958 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1959 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001960 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001961#ifdef FEAT_RELTIME
1962 proftime_T tm;
1963#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 /* if there is a character waiting, search and redraw later */
1966 if (char_avail())
1967 {
1968 incsearch_postponed = TRUE;
1969 continue;
1970 }
1971 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001972 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001973 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 /* If there is no command line, don't do anything */
1976 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 i = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001979 SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001980 redraw_all_later(SOME_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 else
1983 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001984 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 cursor_off(); /* so the user knows we're busy */
1986 out_flush();
1987 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001988#ifdef FEAT_RELTIME
1989 /* Set the time limit to half a second. */
1990 profile_setlimit(500L, &tm);
1991#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001992 if (!p_hls)
1993 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001995 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001996#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001997 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001998#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001999 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002000#endif
2001 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 --emsg_off;
2003 /* if interrupted while searching, behave like it failed */
2004 if (got_int)
2005 {
2006 (void)vpeekc(); /* remove <C-C> from input stream */
2007 got_int = FALSE; /* don't abandon the command line */
2008 i = 0;
2009 }
2010 else if (char_avail())
2011 /* cancelled searching because a char was typed */
2012 incsearch_postponed = TRUE;
2013 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002014 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 highlight_match = TRUE; /* highlight position */
2016 else
2017 highlight_match = FALSE; /* remove highlight */
2018
2019 /* first restore the old curwin values, so the screen is
2020 * positioned in the same way as the actual search command */
2021 curwin->w_leftcol = old_leftcol;
2022 curwin->w_topline = old_topline;
2023# ifdef FEAT_DIFF
2024 curwin->w_topfill = old_topfill;
2025# endif
2026 curwin->w_botline = old_botline;
2027 changed_cline_bef_curs();
2028 update_topline();
2029
2030 if (i != 0)
2031 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002032 pos_T save_pos = curwin->w_cursor;
2033
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002034 match_start = curwin->w_cursor;
2035 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002037 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002038 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002039 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002041 else
2042 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002043
Bram Moolenaar66216052017-12-16 16:33:44 +01002044 /* Disable 'hlsearch' highlighting if the pattern matches
2045 * everything. Avoids a flash when typing "foo\|". */
2046 if (empty_pattern(ccline.cmdbuff))
2047 SET_NO_HLSEARCH(TRUE);
2048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002050 /* May redraw the status line to show the cursor position. */
2051 if (p_ru && curwin->w_status_height > 0)
2052 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002054 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002055 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002056 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002057 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002058
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002059 /* Leave it at the end to make CTRL-R CTRL-W work. */
2060 if (i != 0)
2061 curwin->w_cursor = end_pos;
2062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 msg_starthere();
2064 redrawcmdline();
2065 did_incsearch = TRUE;
2066 }
2067#else /* FEAT_SEARCH_EXTRA */
2068 ;
2069#endif
2070
2071#ifdef FEAT_RIGHTLEFT
2072 if (cmdmsg_rl
2073# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002074 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075# endif
2076 )
2077 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002078 * right-left typing. Not efficient, but it works.
2079 * Do it only when there are no characters left to read
2080 * to avoid useless intermediate redraws. */
2081 if (vpeekc() == NUL)
2082 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083#endif
2084 }
2085
2086returncmd:
2087
2088#ifdef FEAT_RIGHTLEFT
2089 cmdmsg_rl = FALSE;
2090#endif
2091
2092#ifdef FEAT_FKMAP
2093 cmd_fkmap = 0;
2094#endif
2095
2096 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002097 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099#ifdef FEAT_SEARCH_EXTRA
2100 if (did_incsearch)
2101 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002102 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002103 curwin->w_cursor = save_cursor;
2104 else
2105 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002106 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002107 {
2108 /* put the '" mark at the original position */
2109 curwin->w_cursor = save_cursor;
2110 setpcmark();
2111 }
2112 curwin->w_cursor = search_start;
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 curwin->w_curswant = old_curswant;
2115 curwin->w_leftcol = old_leftcol;
2116 curwin->w_topline = old_topline;
2117# ifdef FEAT_DIFF
2118 curwin->w_topfill = old_topfill;
2119# endif
2120 curwin->w_botline = old_botline;
2121 highlight_match = FALSE;
2122 validate_cursor(); /* needed for TAB */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01002123 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125#endif
2126
2127 if (ccline.cmdbuff != NULL)
2128 {
2129 /*
2130 * Put line in history buffer (":" and "=" only when it was typed).
2131 */
2132#ifdef FEAT_CMDHIST
2133 if (ccline.cmdlen && firstc != NUL
2134 && (some_key_typed || histype == HIST_SEARCH))
2135 {
2136 add_to_history(histype, ccline.cmdbuff, TRUE,
2137 histype == HIST_SEARCH ? firstc : NUL);
2138 if (firstc == ':')
2139 {
2140 vim_free(new_last_cmdline);
2141 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2142 }
2143 }
2144#endif
2145
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002146 if (gotesc)
2147 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
2149
2150 /*
2151 * If the screen was shifted up, redraw the whole screen (later).
2152 * If the line is too long, clear it, so ruler and shown command do
2153 * not get printed in the middle of it.
2154 */
2155 msg_check();
2156 msg_scroll = save_msg_scroll;
2157 redir_off = FALSE;
2158
2159 /* When the command line was typed, no need for a wait-return prompt. */
2160 if (some_key_typed)
2161 need_wait_return = FALSE;
2162
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002163#ifdef FEAT_AUTOCMD
2164 /* Trigger CmdlineLeave autocommands. */
2165 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
2166#endif
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 State = save_State;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01002169#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2171 im_save_status(b_im_ptr);
2172 im_set_active(FALSE);
2173#endif
2174#ifdef FEAT_MOUSE
2175 setmouse();
2176#endif
2177#ifdef CURSOR_SHAPE
2178 ui_cursor_shape(); /* may show different cursor shape */
2179#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002180 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002182 {
2183 char_u *p = ccline.cmdbuff;
2184
2185 /* Make ccline empty, getcmdline() may try to use it. */
2186 ccline.cmdbuff = NULL;
2187 return p;
2188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189}
2190
2191#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2192/*
2193 * Get a command line with a prompt.
2194 * This is prepared to be called recursively from getcmdline() (e.g. by
2195 * f_input() when evaluating an expression from CTRL-R =).
2196 * Returns the command line in allocated memory, or NULL.
2197 */
2198 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002199getcmdline_prompt(
2200 int firstc,
2201 char_u *prompt, /* command line prompt */
2202 int attr, /* attributes for prompt */
2203 int xp_context, /* type of expansion */
2204 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 char_u *s;
2207 struct cmdline_info save_ccline;
2208 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002209 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002211 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ccline.cmdprompt = prompt;
2213 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002214# ifdef FEAT_EVAL
2215 ccline.xp_context = xp_context;
2216 ccline.xp_arg = xp_arg;
2217 ccline.input_fn = (firstc == '@');
2218# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002219 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002221 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002222 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002223 /* Restore msg_col, the prompt from input() may have changed it.
2224 * But only if called recursively and the commandline is therefore being
2225 * restored to an old one; if not, the input() prompt stays on the screen,
2226 * so we need its modified msg_col left intact. */
2227 if (ccline.cmdbuff != NULL)
2228 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 return s;
2231}
2232#endif
2233
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002234/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002235 * Return TRUE when the text must not be changed and we can't switch to
2236 * another window or buffer. Used when editing the command line, evaluating
2237 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002238 */
2239 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002240text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002241{
2242#ifdef FEAT_CMDWIN
2243 if (cmdwin_type != 0)
2244 return TRUE;
2245#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002246 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002247}
2248
2249/*
2250 * Give an error message for a command that isn't allowed while the cmdline
2251 * window is open or editing the cmdline in another way.
2252 */
2253 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002254text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002255{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002256 EMSG(_(get_text_locked_msg()));
2257}
2258
2259 char_u *
2260get_text_locked_msg(void)
2261{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002262#ifdef FEAT_CMDWIN
2263 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002264 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002265#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002266 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002267}
2268
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#if defined(FEAT_AUTOCMD) || defined(PROTO)
2270/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002271 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2272 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002273 */
2274 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002275curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276{
2277 if (curbuf_lock > 0)
2278 {
2279 EMSG(_("E788: Not allowed to edit another buffer now"));
2280 return TRUE;
2281 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002282 return allbuf_locked();
2283}
2284
2285/*
2286 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2287 * message.
2288 */
2289 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002290allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002291{
2292 if (allbuf_lock > 0)
2293 {
2294 EMSG(_("E811: Not allowed to change buffer information now"));
2295 return TRUE;
2296 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297 return FALSE;
2298}
2299#endif
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002302cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
2304#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2305 if (cmdline_star > 0) /* showing '*', always 1 position */
2306 return 1;
2307#endif
2308 return ptr2cells(ccline.cmdbuff + idx);
2309}
2310
2311/*
2312 * Compute the offset of the cursor on the command line for the prompt and
2313 * indent.
2314 */
2315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002316set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002318 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 ccline.cmdspos = 1 + ccline.cmdindent;
2320 else
2321 ccline.cmdspos = 0 + ccline.cmdindent;
2322}
2323
2324/*
2325 * Compute the screen position for the cursor on the command line.
2326 */
2327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002328set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 int i, m, c;
2331
2332 set_cmdspos();
2333 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002336 if (m < 0) /* overflow, Columns or Rows at weird value */
2337 m = MAXCOL;
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 m = MAXCOL;
2341 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2342 {
2343 c = cmdline_charsize(i);
2344#ifdef FEAT_MBYTE
2345 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2346 if (has_mbyte)
2347 correct_cmdspos(i, c);
2348#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002349 /* If the cmdline doesn't fit, show cursor on last visible char.
2350 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if ((ccline.cmdspos += c) >= m)
2352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 ccline.cmdspos -= c;
2354 break;
2355 }
2356#ifdef FEAT_MBYTE
2357 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002358 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#endif
2360 }
2361}
2362
2363#ifdef FEAT_MBYTE
2364/*
2365 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2366 * character that doesn't fit, so that a ">" must be displayed.
2367 */
2368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002369correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002371 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2373 && ccline.cmdspos % Columns + cells > Columns)
2374 ccline.cmdspos++;
2375}
2376#endif
2377
2378/*
2379 * Get an Ex command line for the ":" command.
2380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002382getexline(
2383 int c, /* normally ':', NUL for ":append" */
2384 void *cookie UNUSED,
2385 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
2387 /* When executing a register, remove ':' that's in front of each line. */
2388 if (exec_from_reg && vpeekc() == ':')
2389 (void)vgetc();
2390 return getcmdline(c, 1L, indent);
2391}
2392
2393/*
2394 * Get an Ex command line for Ex mode.
2395 * In Ex mode we only use the OS supplied line editing features and no
2396 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002397 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002400getexmodeline(
2401 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002403 void *cookie UNUSED,
2404 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002406 garray_T line_ga;
2407 char_u *pend;
2408 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002409 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002410 int escaped = FALSE; /* CTRL-V typed */
2411 int vcol = 0;
2412 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002413 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002414 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 /* Switch cursor on now. This avoids that it happens after the "\n", which
2417 * confuses the system function that computes tabstops. */
2418 cursor_on();
2419
2420 /* always start in column 0; write a newline if necessary */
2421 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002422 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002424 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002426 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002427 if (p_prompt)
2428 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 while (indent-- > 0)
2430 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433
2434 ga_init2(&line_ga, 1, 30);
2435
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002436 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002437 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002438 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002439 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002440 while (indent >= 8)
2441 {
2442 ga_append(&line_ga, TAB);
2443 msg_puts((char_u *)" ");
2444 indent -= 8;
2445 }
2446 while (indent-- > 0)
2447 {
2448 ga_append(&line_ga, ' ');
2449 msg_putchar(' ');
2450 }
2451 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002452 ++no_mapping;
2453 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /*
2456 * Get the line, one character at a time.
2457 */
2458 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002459 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002461 long sw;
2462 char_u *s;
2463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (ga_grow(&line_ga, 40) == FAIL)
2465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaarba748c82017-02-26 14:00:07 +01002467 /*
2468 * Get one character at a time.
2469 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002470 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002471
2472 /* Check for a ":normal" command and no more characters left. */
2473 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2474 c1 = '\n';
2475 else
2476 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002479 * Handle line editing.
2480 * Previously this was left to the system, putting the terminal in
2481 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002483 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002485 msg_putchar('\n');
2486 break;
2487 }
2488
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002489 if (c1 == K_PS)
2490 {
2491 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2492 goto redraw;
2493 }
2494
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002495 if (!escaped)
2496 {
2497 /* CR typed means "enter", which is NL */
2498 if (c1 == '\r')
2499 c1 = '\n';
2500
2501 if (c1 == BS || c1 == K_BS
2502 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002504 if (line_ga.ga_len > 0)
2505 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002506#ifdef FEAT_MBYTE
2507 if (has_mbyte)
2508 {
2509 p = (char_u *)line_ga.ga_data;
2510 p[line_ga.ga_len] = NUL;
2511 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2512 line_ga.ga_len -= len;
2513 }
2514 else
2515#endif
2516 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002517 goto redraw;
2518 }
2519 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
2521
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002522 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002524 msg_col = startcol;
2525 msg_clr_eos();
2526 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002527 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002528 }
2529
2530 if (c1 == Ctrl_T)
2531 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002532 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002533 p = (char_u *)line_ga.ga_data;
2534 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002535 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002536 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002537add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002538 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002540 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002541 p = (char_u *)line_ga.ga_data;
2542 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002543 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2544 *s = ' ';
2545 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002547redraw:
2548 /* redraw the line */
2549 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002550 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002551 p = (char_u *)line_ga.ga_data;
2552 p[line_ga.ga_len] = NUL;
2553 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002557 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002559 msg_putchar(' ');
2560 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002561 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002565 len = MB_PTR2LEN(p);
2566 msg_outtrans_len(p, len);
2567 vcol += ptr2cells(p);
2568 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002571 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002572 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002573 continue;
2574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 if (c1 == Ctrl_D)
2577 {
2578 /* Delete one shiftwidth. */
2579 p = (char_u *)line_ga.ga_data;
2580 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002582 if (prev_char == '^')
2583 ex_keep_indent = TRUE;
2584 indent = 0;
2585 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587 else
2588 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002589 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002590 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002591 if (indent > 0)
2592 {
2593 --indent;
2594 indent -= indent % get_sw_value(curbuf);
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002597 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002598 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002599 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002600 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2601 --line_ga.ga_len;
2602 }
2603 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002605
2606 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2607 {
2608 escaped = TRUE;
2609 continue;
2610 }
2611
2612 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2613 if (IS_SPECIAL(c1))
2614 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002616
2617 if (IS_SPECIAL(c1))
2618 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002619#ifdef FEAT_MBYTE
2620 if (has_mbyte)
2621 len = (*mb_char2bytes)(c1,
2622 (char_u *)line_ga.ga_data + line_ga.ga_len);
2623 else
2624#endif
2625 {
2626 len = 1;
2627 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2628 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002629 if (c1 == '\n')
2630 msg_putchar('\n');
2631 else if (c1 == TAB)
2632 {
2633 /* Don't use chartabsize(), 'ts' can be different */
2634 do
2635 {
2636 msg_putchar(' ');
2637 } while (++vcol % 8);
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002642 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002645 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 escaped = FALSE;
2647
2648 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002649 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002650
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002651 /* We are done when a NL is entered, but not when it comes after an
2652 * odd number of backslashes, that results in a NUL. */
2653 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002655 int bcount = 0;
2656
2657 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2658 ++bcount;
2659
2660 if (bcount > 0)
2661 {
2662 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2663 * "\NL", etc. */
2664 line_ga.ga_len -= (bcount + 1) / 2;
2665 pend -= (bcount + 1) / 2;
2666 pend[-1] = '\n';
2667 }
2668
2669 if ((bcount & 1) == 0)
2670 {
2671 --line_ga.ga_len;
2672 --pend;
2673 *pend = NUL;
2674 break;
2675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 }
2678
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002679 --no_mapping;
2680 --allow_keys;
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 /* make following messages go to the next line */
2683 msg_didout = FALSE;
2684 msg_col = 0;
2685 if (msg_row < Rows - 1)
2686 ++msg_row;
2687 emsg_on_display = FALSE; /* don't want ui_delay() */
2688
2689 if (got_int)
2690 ga_clear(&line_ga);
2691
2692 return (char_u *)line_ga.ga_data;
2693}
2694
Bram Moolenaare344bea2005-09-01 20:46:49 +00002695# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2696 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * Return TRUE if ccline.overstrike is on.
2699 */
2700 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002701cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 return ccline.overstrike;
2704}
2705
2706/*
2707 * Return TRUE if the cursor is at the end of the cmdline.
2708 */
2709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002710cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 return (ccline.cmdpos >= ccline.cmdlen);
2713}
2714#endif
2715
Bram Moolenaar9372a112005-12-06 19:59:18 +00002716#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717/*
2718 * Return the virtual column number at the current cursor position.
2719 * This is used by the IM code to obtain the start of the preedit string.
2720 */
2721 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002722cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2725 return MAXCOL;
2726
2727# ifdef FEAT_MBYTE
2728 if (has_mbyte)
2729 {
2730 colnr_T col;
2731 int i = 0;
2732
2733 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002734 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 return col;
2737 }
2738 else
2739# endif
2740 return ccline.cmdpos;
2741}
2742#endif
2743
2744#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2745/*
2746 * If part of the command line is an IM preedit string, redraw it with
2747 * IM feedback attributes. The cursor position is restored after drawing.
2748 */
2749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002750redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 if ((State & CMDLINE)
2753 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002754 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 && !p_imdisable
2756 && im_is_preediting())
2757 {
2758 int cmdpos = 0;
2759 int cmdspos;
2760 int old_row;
2761 int old_col;
2762 colnr_T col;
2763
2764 old_row = msg_row;
2765 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002766 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768# ifdef FEAT_MBYTE
2769 if (has_mbyte)
2770 {
2771 for (col = 0; col < preedit_start_col
2772 && cmdpos < ccline.cmdlen; ++col)
2773 {
2774 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002775 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 }
2778 else
2779# endif
2780 {
2781 cmdspos += preedit_start_col;
2782 cmdpos += preedit_start_col;
2783 }
2784
2785 msg_row = cmdline_row + (cmdspos / (int)Columns);
2786 msg_col = cmdspos % (int)Columns;
2787 if (msg_row >= Rows)
2788 msg_row = Rows - 1;
2789
2790 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2791 {
2792 int char_len;
2793 int char_attr;
2794
2795 char_attr = im_get_feedback_attr(col);
2796 if (char_attr < 0)
2797 break; /* end of preedit string */
2798
2799# ifdef FEAT_MBYTE
2800 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002801 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else
2803# endif
2804 char_len = 1;
2805
2806 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2807 cmdpos += char_len;
2808 }
2809
2810 msg_row = old_row;
2811 msg_col = old_col;
2812 }
2813}
2814#endif /* FEAT_XIM && FEAT_GUI_GTK */
2815
2816/*
2817 * Allocate a new command line buffer.
2818 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2819 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2820 */
2821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002822alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
2824 /*
2825 * give some extra space to avoid having to allocate all the time
2826 */
2827 if (len < 80)
2828 len = 100;
2829 else
2830 len += 20;
2831
2832 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2833 ccline.cmdbufflen = len;
2834}
2835
2836/*
2837 * Re-allocate the command line to length len + something extra.
2838 * return FAIL for failure, OK otherwise
2839 */
2840 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002841realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 char_u *p;
2844
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002845 if (len < ccline.cmdbufflen)
2846 return OK; /* no need to resize */
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 p = ccline.cmdbuff;
2849 alloc_cmdbuff(len); /* will get some more */
2850 if (ccline.cmdbuff == NULL) /* out of memory */
2851 {
2852 ccline.cmdbuff = p; /* keep the old one */
2853 return FAIL;
2854 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002855 /* There isn't always a NUL after the command, but it may need to be
2856 * there, thus copy up to the NUL and add a NUL. */
2857 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2858 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002860
2861 if (ccline.xpc != NULL
2862 && ccline.xpc->xp_pattern != NULL
2863 && ccline.xpc->xp_context != EXPAND_NOTHING
2864 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2865 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002866 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002867
2868 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2869 * to point into the newly allocated memory. */
2870 if (i >= 0 && i <= ccline.cmdlen)
2871 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2872 }
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 return OK;
2875}
2876
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002877#if defined(FEAT_ARABIC) || defined(PROTO)
2878static char_u *arshape_buf = NULL;
2879
2880# if defined(EXITFREE) || defined(PROTO)
2881 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002882free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002883{
2884 vim_free(arshape_buf);
2885}
2886# endif
2887#endif
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889/*
2890 * Draw part of the cmdline at the current cursor position. But draw stars
2891 * when cmdline_star is TRUE.
2892 */
2893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002894draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2897 int i;
2898
2899 if (cmdline_star > 0)
2900 for (i = 0; i < len; ++i)
2901 {
2902 msg_putchar('*');
2903# ifdef FEAT_MBYTE
2904 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002905 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906# endif
2907 }
2908 else
2909#endif
2910#ifdef FEAT_ARABIC
2911 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 static int buflen = 0;
2914 char_u *p;
2915 int j;
2916 int newlen = 0;
2917 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002918 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 int prev_c = 0;
2920 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002921 int u8c;
2922 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /*
2926 * Do arabic shaping into a temporary buffer. This is very
2927 * inefficient!
2928 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002929 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
2931 /* Re-allocate the buffer. We keep it around to avoid a lot of
2932 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002933 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002934 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 arshape_buf = alloc(buflen);
2936 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return; /* out of memory */
2938 }
2939
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002940 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2941 {
2942 /* Prepend a space to draw the leading composing char on. */
2943 arshape_buf[0] = ' ';
2944 newlen = 1;
2945 }
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 for (j = start; j < start + len; j += mb_l)
2948 {
2949 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002950 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002951 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (ARABIC_CHAR(u8c))
2953 {
2954 /* Do Arabic shaping. */
2955 if (cmdmsg_rl)
2956 {
2957 /* displaying from right to left */
2958 pc = prev_c;
2959 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002960 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (j + mb_l >= start + len)
2962 nc = NUL;
2963 else
2964 nc = utf_ptr2char(p + mb_l);
2965 }
2966 else
2967 {
2968 /* displaying from left to right */
2969 if (j + mb_l >= start + len)
2970 pc = NUL;
2971 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002972 {
2973 int pcc[MAX_MCO];
2974
2975 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002977 pc1 = pcc[0];
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 nc = prev_c;
2980 }
2981 prev_c = u8c;
2982
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002983 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002985 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002986 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002988 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2989 if (u8cc[1] != 0)
2990 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002991 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993 }
2994 else
2995 {
2996 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002997 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 newlen += mb_l;
2999 }
3000 }
3001
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003002 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 else
3005#endif
3006 msg_outtrans_len(ccline.cmdbuff + start, len);
3007}
3008
3009/*
3010 * Put a character on the command line. Shifts the following text to the
3011 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3012 * "c" must be printable (fit in one display cell)!
3013 */
3014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003015putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 if (cmd_silent)
3018 return;
3019 msg_no_more = TRUE;
3020 msg_putchar(c);
3021 if (shift)
3022 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3023 msg_no_more = FALSE;
3024 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003025 extra_char = c;
3026 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027}
3028
3029/*
3030 * Undo a putcmdline(c, FALSE).
3031 */
3032 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003033unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 if (cmd_silent)
3036 return;
3037 msg_no_more = TRUE;
3038 if (ccline.cmdlen == ccline.cmdpos)
3039 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003040#ifdef FEAT_MBYTE
3041 else if (has_mbyte)
3042 draw_cmdline(ccline.cmdpos,
3043 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 else
3046 draw_cmdline(ccline.cmdpos, 1);
3047 msg_no_more = FALSE;
3048 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003049 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050}
3051
3052/*
3053 * Put the given string, of the given length, onto the command line.
3054 * If len is -1, then STRLEN() is used to calculate the length.
3055 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3056 * part will be redrawn, otherwise it will not. If this function is called
3057 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3058 * called afterwards.
3059 */
3060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 int retval;
3064 int i;
3065 int m;
3066 int c;
3067
3068 if (len < 0)
3069 len = (int)STRLEN(str);
3070
3071 /* Check if ccline.cmdbuff needs to be longer */
3072 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003073 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else
3075 retval = OK;
3076 if (retval == OK)
3077 {
3078 if (!ccline.overstrike)
3079 {
3080 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3081 ccline.cmdbuff + ccline.cmdpos,
3082 (size_t)(ccline.cmdlen - ccline.cmdpos));
3083 ccline.cmdlen += len;
3084 }
3085 else
3086 {
3087#ifdef FEAT_MBYTE
3088 if (has_mbyte)
3089 {
3090 /* Count nr of characters in the new string. */
3091 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003092 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 ++m;
3094 /* Count nr of bytes in cmdline that are overwritten by these
3095 * characters. */
3096 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003097 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 --m;
3099 if (i < ccline.cmdlen)
3100 {
3101 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3102 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3103 ccline.cmdlen += ccline.cmdpos + len - i;
3104 }
3105 else
3106 ccline.cmdlen = ccline.cmdpos + len;
3107 }
3108 else
3109#endif
3110 if (ccline.cmdpos + len > ccline.cmdlen)
3111 ccline.cmdlen = ccline.cmdpos + len;
3112 }
3113 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3114 ccline.cmdbuff[ccline.cmdlen] = NUL;
3115
3116#ifdef FEAT_MBYTE
3117 if (enc_utf8)
3118 {
3119 /* When the inserted text starts with a composing character,
3120 * backup to the character before it. There could be two of them.
3121 */
3122 i = 0;
3123 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3124 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3125 {
3126 i = (*mb_head_off)(ccline.cmdbuff,
3127 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3128 ccline.cmdpos -= i;
3129 len += i;
3130 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3131 }
3132# ifdef FEAT_ARABIC
3133 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3134 {
3135 /* Check the previous character for Arabic combining pair. */
3136 i = (*mb_head_off)(ccline.cmdbuff,
3137 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3138 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3139 + ccline.cmdpos - i), c))
3140 {
3141 ccline.cmdpos -= i;
3142 len += i;
3143 }
3144 else
3145 i = 0;
3146 }
3147# endif
3148 if (i != 0)
3149 {
3150 /* Also backup the cursor position. */
3151 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3152 ccline.cmdspos -= i;
3153 msg_col -= i;
3154 if (msg_col < 0)
3155 {
3156 msg_col += Columns;
3157 --msg_row;
3158 }
3159 }
3160 }
3161#endif
3162
3163 if (redraw && !cmd_silent)
3164 {
3165 msg_no_more = TRUE;
3166 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003167 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3169 /* Avoid clearing the rest of the line too often. */
3170 if (cmdline_row != i || ccline.overstrike)
3171 msg_clr_eos();
3172 msg_no_more = FALSE;
3173 }
3174#ifdef FEAT_FKMAP
3175 /*
3176 * If we are in Farsi command mode, the character input must be in
3177 * Insert mode. So do not advance the cmdpos.
3178 */
3179 if (!cmd_fkmap)
3180#endif
3181 {
3182 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003185 if (m < 0) /* overflow, Columns or Rows at weird value */
3186 m = MAXCOL;
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 else
3189 m = MAXCOL;
3190 for (i = 0; i < len; ++i)
3191 {
3192 c = cmdline_charsize(ccline.cmdpos);
3193#ifdef FEAT_MBYTE
3194 /* count ">" for a double-wide char that doesn't fit. */
3195 if (has_mbyte)
3196 correct_cmdspos(ccline.cmdpos, c);
3197#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003198 /* Stop cursor at the end of the screen, but do increment the
3199 * insert position, so that entering a very long command
3200 * works, even though you can't see it. */
3201 if (ccline.cmdspos + c < m)
3202 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#ifdef FEAT_MBYTE
3204 if (has_mbyte)
3205 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003206 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (c > len - i - 1)
3208 c = len - i - 1;
3209 ccline.cmdpos += c;
3210 i += c;
3211 }
3212#endif
3213 ++ccline.cmdpos;
3214 }
3215 }
3216 }
3217 if (redraw)
3218 msg_check();
3219 return retval;
3220}
3221
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003222static struct cmdline_info prev_ccline;
3223static int prev_ccline_used = FALSE;
3224
3225/*
3226 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3227 * and overwrite it. But get_cmdline_str() may need it, thus make it
3228 * available globally in prev_ccline.
3229 */
3230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003231save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003232{
3233 if (!prev_ccline_used)
3234 {
3235 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3236 prev_ccline_used = TRUE;
3237 }
3238 *ccp = prev_ccline;
3239 prev_ccline = ccline;
3240 ccline.cmdbuff = NULL;
3241 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003242 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003243}
3244
3245/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003246 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003247 */
3248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003249restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003250{
3251 ccline = prev_ccline;
3252 prev_ccline = *ccp;
3253}
3254
Bram Moolenaar5a305422006-04-28 22:38:25 +00003255#if defined(FEAT_EVAL) || defined(PROTO)
3256/*
3257 * Save the command line into allocated memory. Returns a pointer to be
3258 * passed to restore_cmdline_alloc() later.
3259 * Returns NULL when failed.
3260 */
3261 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003262save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003263{
3264 struct cmdline_info *p;
3265
3266 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3267 if (p != NULL)
3268 save_cmdline(p);
3269 return (char_u *)p;
3270}
3271
3272/*
3273 * Restore the command line from the return value of save_cmdline_alloc().
3274 */
3275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003276restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003277{
3278 if (p != NULL)
3279 {
3280 restore_cmdline((struct cmdline_info *)p);
3281 vim_free(p);
3282 }
3283}
3284#endif
3285
Bram Moolenaar8299df92004-07-10 09:47:34 +00003286/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003287 * Paste a yank register into the command line.
3288 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003289 * insert_reg() can't be used here, because special characters from the
3290 * register contents will be interpreted as commands.
3291 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003292 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003293 */
3294 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003295cmdline_paste(
3296 int regname,
3297 int literally, /* Insert text literally instead of "as typed" */
3298 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003299{
3300 long i;
3301 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003302 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003303 int allocated;
3304 struct cmdline_info save_ccline;
3305
3306 /* check for valid regname; also accept special characters for CTRL-R in
3307 * the command line */
3308 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3309 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3310 return FAIL;
3311
3312 /* A register containing CTRL-R can cause an endless loop. Allow using
3313 * CTRL-C to break the loop. */
3314 line_breakcheck();
3315 if (got_int)
3316 return FAIL;
3317
3318#ifdef FEAT_CLIPBOARD
3319 regname = may_get_selection(regname);
3320#endif
3321
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003322 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003323 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003324 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003325 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003326 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003327 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003328 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003329
3330 if (i)
3331 {
3332 /* Got the value of a special register in "arg". */
3333 if (arg == NULL)
3334 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003335
3336 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3337 * part of the word. */
3338 p = arg;
3339 if (p_is && regname == Ctrl_W)
3340 {
3341 char_u *w;
3342 int len;
3343
3344 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003345 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003346 {
3347#ifdef FEAT_MBYTE
3348 if (has_mbyte)
3349 {
3350 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3351 if (!vim_iswordc(mb_ptr2char(w - len)))
3352 break;
3353 w -= len;
3354 }
3355 else
3356#endif
3357 {
3358 if (!vim_iswordc(w[-1]))
3359 break;
3360 --w;
3361 }
3362 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003363 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003364 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3365 p += len;
3366 }
3367
3368 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003369 if (allocated)
3370 vim_free(arg);
3371 return OK;
3372 }
3373
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003374 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003375}
3376
3377/*
3378 * Put a string on the command line.
3379 * When "literally" is TRUE, insert literally.
3380 * When "literally" is FALSE, insert as typed, but don't leave the command
3381 * line.
3382 */
3383 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003384cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003385{
3386 int c, cv;
3387
3388 if (literally)
3389 put_on_cmdline(s, -1, TRUE);
3390 else
3391 while (*s != NUL)
3392 {
3393 cv = *s;
3394 if (cv == Ctrl_V && s[1])
3395 ++s;
3396#ifdef FEAT_MBYTE
3397 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003398 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003399 else
3400#endif
3401 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003402 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3403 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003404#ifdef UNIX
3405 || c == intr_char
3406#endif
3407 || (c == Ctrl_BSL && *s == Ctrl_N))
3408 stuffcharReadbuff(Ctrl_V);
3409 stuffcharReadbuff(c);
3410 }
3411}
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#ifdef FEAT_WILDMENU
3414/*
3415 * Delete characters on the command line, from "from" to the current
3416 * position.
3417 */
3418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003419cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3422 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3423 ccline.cmdlen -= ccline.cmdpos - from;
3424 ccline.cmdpos = from;
3425}
3426#endif
3427
3428/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003429 * This function is called when the screen size changes and with incremental
3430 * search and in other situations where the command line may have been
3431 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003434redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003436 redrawcmdline_ex(TRUE);
3437}
3438
3439 void
3440redrawcmdline_ex(int do_compute_cmdrow)
3441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (cmd_silent)
3443 return;
3444 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003445 if (do_compute_cmdrow)
3446 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 redrawcmd();
3448 cursorcmd();
3449}
3450
3451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 int i;
3455
3456 if (cmd_silent)
3457 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003458 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 msg_putchar(ccline.cmdfirstc);
3460 if (ccline.cmdprompt != NULL)
3461 {
3462 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3463 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3464 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003465 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 --ccline.cmdindent;
3467 }
3468 else
3469 for (i = ccline.cmdindent; i > 0; --i)
3470 msg_putchar(' ');
3471}
3472
3473/*
3474 * Redraw what is currently on the command line.
3475 */
3476 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003477redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 if (cmd_silent)
3480 return;
3481
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003482 /* when 'incsearch' is set there may be no command line while redrawing */
3483 if (ccline.cmdbuff == NULL)
3484 {
3485 windgoto(cmdline_row, 0);
3486 msg_clr_eos();
3487 return;
3488 }
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 msg_start();
3491 redrawcmdprompt();
3492
3493 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3494 msg_no_more = TRUE;
3495 draw_cmdline(0, ccline.cmdlen);
3496 msg_clr_eos();
3497 msg_no_more = FALSE;
3498
3499 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003500 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003501 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 /*
3504 * An emsg() before may have set msg_scroll. This is used in normal mode,
3505 * in cmdline mode we can reset them now.
3506 */
3507 msg_scroll = FALSE; /* next message overwrites cmdline */
3508
3509 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3510 * in cmdline mode */
3511 skip_redraw = FALSE;
3512}
3513
3514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003515compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003517 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 cmdline_row = Rows - 1;
3519 else
3520 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003521 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 if (cmd_silent)
3528 return;
3529
3530#ifdef FEAT_RIGHTLEFT
3531 if (cmdmsg_rl)
3532 {
3533 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3534 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3535 if (msg_row <= 0)
3536 msg_row = Rows - 1;
3537 }
3538 else
3539#endif
3540 {
3541 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3542 msg_col = ccline.cmdspos % (int)Columns;
3543 if (msg_row >= Rows)
3544 msg_row = Rows - 1;
3545 }
3546
3547 windgoto(msg_row, msg_col);
3548#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003549 if (p_imst == IM_ON_THE_SPOT)
3550 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#endif
3552#ifdef MCH_CURSOR_SHAPE
3553 mch_update_cursor();
3554#endif
3555}
3556
3557 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003558gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560 msg_start();
3561#ifdef FEAT_RIGHTLEFT
3562 if (cmdmsg_rl)
3563 msg_col = Columns - 1;
3564 else
3565#endif
3566 msg_col = 0; /* always start in column 0 */
3567 if (clr) /* clear the bottom line(s) */
3568 msg_clr_eos(); /* will reset clear_cmdline */
3569 windgoto(cmdline_row, 0);
3570}
3571
3572/*
3573 * Check the word in front of the cursor for an abbreviation.
3574 * Called when the non-id character "c" has been entered.
3575 * When an abbreviation is recognized it is removed from the text with
3576 * backspaces and the replacement string is inserted, followed by "c".
3577 */
3578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003579ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
3581 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3582 return FALSE;
3583
3584 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3585}
3586
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003587#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3588 static int
3589#ifdef __BORLANDC__
3590_RTLENTRYF
3591#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003592sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003593{
3594 char_u *p1 = *(char_u **)s1;
3595 char_u *p2 = *(char_u **)s2;
3596
3597 if (*p1 != '<' && *p2 == '<') return -1;
3598 if (*p1 == '<' && *p2 != '<') return 1;
3599 return STRCMP(p1, p2);
3600}
3601#endif
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603/*
3604 * Return FAIL if this is not an appropriate context in which to do
3605 * completion of anything, return OK if it is (even if there are no matches).
3606 * For the caller, this means that the character is just passed through like a
3607 * normal character (instead of being expanded). This allows :s/^I^D etc.
3608 */
3609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003610nextwild(
3611 expand_T *xp,
3612 int type,
3613 int options, /* extra options for ExpandOne() */
3614 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 int i, j;
3617 char_u *p1;
3618 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 int difflen;
3620 int v;
3621
3622 if (xp->xp_numfiles == -1)
3623 {
3624 set_expand_context(xp);
3625 cmd_showtail = expand_showtail(xp);
3626 }
3627
3628 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3629 {
3630 beep_flush();
3631 return OK; /* Something illegal on command line */
3632 }
3633 if (xp->xp_context == EXPAND_NOTHING)
3634 {
3635 /* Caller can use the character as a normal char instead */
3636 return FAIL;
3637 }
3638
3639 MSG_PUTS("..."); /* show that we are busy */
3640 out_flush();
3641
3642 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003643 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 if (type == WILD_NEXT || type == WILD_PREV)
3646 {
3647 /*
3648 * Get next/previous match for a previous expanded pattern.
3649 */
3650 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3651 }
3652 else
3653 {
3654 /*
3655 * Translate string into pattern and expand it.
3656 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003657 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3658 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 p2 = NULL;
3660 else
3661 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003662 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003663 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3664 if (escape)
3665 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003666
3667 if (p_wic)
3668 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003669 p2 = ExpandOne(xp, p1,
3670 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003671 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003673 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (p2 != NULL && type == WILD_LONGEST)
3675 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003676 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (ccline.cmdbuff[i + j] == '*'
3678 || ccline.cmdbuff[i + j] == '?')
3679 break;
3680 if ((int)STRLEN(p2) < j)
3681 {
3682 vim_free(p2);
3683 p2 = NULL;
3684 }
3685 }
3686 }
3687 }
3688
3689 if (p2 != NULL && !got_int)
3690 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003691 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003692 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003694 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 xp->xp_pattern = ccline.cmdbuff + i;
3696 }
3697 else
3698 v = OK;
3699 if (v == OK)
3700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003701 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3702 &ccline.cmdbuff[ccline.cmdpos],
3703 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3704 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 ccline.cmdlen += difflen;
3706 ccline.cmdpos += difflen;
3707 }
3708 }
3709 vim_free(p2);
3710
3711 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003712 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
3714 /* When expanding a ":map" command and no matches are found, assume that
3715 * the key is supposed to be inserted literally */
3716 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3717 return FAIL;
3718
3719 if (xp->xp_numfiles <= 0 && p2 == NULL)
3720 beep_flush();
3721 else if (xp->xp_numfiles == 1)
3722 /* free expanded pattern */
3723 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3724
3725 return OK;
3726}
3727
3728/*
3729 * Do wildcard expansion on the string 'str'.
3730 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003731 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 * Return NULL for failure.
3733 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003734 * "orig" is the originally expanded string, copied to allocated memory. It
3735 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3736 * WILD_PREV "orig" should be NULL.
3737 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003738 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3739 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 *
3741 * mode = WILD_FREE: just free previously expanded matches
3742 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3743 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3744 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3745 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3746 * mode = WILD_ALL: return all matches concatenated
3747 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003748 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 *
3750 * options = WILD_LIST_NOTFOUND: list entries without a match
3751 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3752 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3753 * options = WILD_NO_BEEP: Don't beep for multiple matches
3754 * options = WILD_ADD_SLASH: add a slash after directory names
3755 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3756 * options = WILD_SILENT: don't print warning messages
3757 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003758 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 *
3760 * The variables xp->xp_context and xp->xp_backslash must have been set!
3761 */
3762 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003763ExpandOne(
3764 expand_T *xp,
3765 char_u *str,
3766 char_u *orig, /* allocated copy of original of expanded string */
3767 int options,
3768 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 char_u *ss = NULL;
3771 static int findex;
3772 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003773 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 int i;
3775 long_u len;
3776 int non_suf_match; /* number without matching suffix */
3777
3778 /*
3779 * first handle the case of using an old match
3780 */
3781 if (mode == WILD_NEXT || mode == WILD_PREV)
3782 {
3783 if (xp->xp_numfiles > 0)
3784 {
3785 if (mode == WILD_PREV)
3786 {
3787 if (findex == -1)
3788 findex = xp->xp_numfiles;
3789 --findex;
3790 }
3791 else /* mode == WILD_NEXT */
3792 ++findex;
3793
3794 /*
3795 * When wrapping around, return the original string, set findex to
3796 * -1.
3797 */
3798 if (findex < 0)
3799 {
3800 if (orig_save == NULL)
3801 findex = xp->xp_numfiles - 1;
3802 else
3803 findex = -1;
3804 }
3805 if (findex >= xp->xp_numfiles)
3806 {
3807 if (orig_save == NULL)
3808 findex = 0;
3809 else
3810 findex = -1;
3811 }
3812#ifdef FEAT_WILDMENU
3813 if (p_wmnu)
3814 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3815 findex, cmd_showtail);
3816#endif
3817 if (findex == -1)
3818 return vim_strsave(orig_save);
3819 return vim_strsave(xp->xp_files[findex]);
3820 }
3821 else
3822 return NULL;
3823 }
3824
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003825 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3827 {
3828 FreeWild(xp->xp_numfiles, xp->xp_files);
3829 xp->xp_numfiles = -1;
3830 vim_free(orig_save);
3831 orig_save = NULL;
3832 }
3833 findex = 0;
3834
3835 if (mode == WILD_FREE) /* only release file name */
3836 return NULL;
3837
3838 if (xp->xp_numfiles == -1)
3839 {
3840 vim_free(orig_save);
3841 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003842 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 /*
3845 * Do the expansion.
3846 */
3847 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3848 options) == FAIL)
3849 {
3850#ifdef FNAME_ILLEGAL
3851 /* Illegal file name has been silently skipped. But when there
3852 * are wildcards, the real problem is that there was no match,
3853 * causing the pattern to be added, which has illegal characters.
3854 */
3855 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3856 EMSG2(_(e_nomatch2), str);
3857#endif
3858 }
3859 else if (xp->xp_numfiles == 0)
3860 {
3861 if (!(options & WILD_SILENT))
3862 EMSG2(_(e_nomatch2), str);
3863 }
3864 else
3865 {
3866 /* Escape the matches for use on the command line. */
3867 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3868
3869 /*
3870 * Check for matching suffixes in file names.
3871 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003872 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3873 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 if (xp->xp_numfiles)
3876 non_suf_match = xp->xp_numfiles;
3877 else
3878 non_suf_match = 1;
3879 if ((xp->xp_context == EXPAND_FILES
3880 || xp->xp_context == EXPAND_DIRECTORIES)
3881 && xp->xp_numfiles > 1)
3882 {
3883 /*
3884 * More than one match; check suffix.
3885 * The files will have been sorted on matching suffix in
3886 * expand_wildcards, only need to check the first two.
3887 */
3888 non_suf_match = 0;
3889 for (i = 0; i < 2; ++i)
3890 if (match_suffix(xp->xp_files[i]))
3891 ++non_suf_match;
3892 }
3893 if (non_suf_match != 1)
3894 {
3895 /* Can we ever get here unless it's while expanding
3896 * interactively? If not, we can get rid of this all
3897 * together. Don't really want to wait for this message
3898 * (and possibly have to hit return to continue!).
3899 */
3900 if (!(options & WILD_SILENT))
3901 EMSG(_(e_toomany));
3902 else if (!(options & WILD_NO_BEEP))
3903 beep_flush();
3904 }
3905 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3906 ss = vim_strsave(xp->xp_files[0]);
3907 }
3908 }
3909 }
3910
3911 /* Find longest common part */
3912 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3913 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003914 int mb_len = 1;
3915 int c0, ci;
3916
3917 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003919#ifdef FEAT_MBYTE
3920 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003922 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3923 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3924 }
3925 else
3926#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003927 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003928 for (i = 1; i < xp->xp_numfiles; ++i)
3929 {
3930#ifdef FEAT_MBYTE
3931 if (has_mbyte)
3932 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3933 else
3934#endif
3935 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003936 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003938 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003939 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003941 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 break;
3943 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003944 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 break;
3946 }
3947 if (i < xp->xp_numfiles)
3948 {
3949 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003950 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 break;
3952 }
3953 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 ss = alloc((unsigned)len + 1);
3956 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003957 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 findex = -1; /* next p_wc gets first one */
3959 }
3960
3961 /* Concatenate all matching names */
3962 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3963 {
3964 len = 0;
3965 for (i = 0; i < xp->xp_numfiles; ++i)
3966 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3967 ss = lalloc(len, TRUE);
3968 if (ss != NULL)
3969 {
3970 *ss = NUL;
3971 for (i = 0; i < xp->xp_numfiles; ++i)
3972 {
3973 STRCAT(ss, xp->xp_files[i]);
3974 if (i != xp->xp_numfiles - 1)
3975 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3976 }
3977 }
3978 }
3979
3980 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3981 ExpandCleanup(xp);
3982
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003983 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003984 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003985 vim_free(orig);
3986
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return ss;
3988}
3989
3990/*
3991 * Prepare an expand structure for use.
3992 */
3993 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003994ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003996 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003997 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003999#ifndef BACKSLASH_IN_FILENAME
4000 xp->xp_shell = FALSE;
4001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 xp->xp_numfiles = -1;
4003 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004004#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4005 xp->xp_arg = NULL;
4006#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004007 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008}
4009
4010/*
4011 * Cleanup an expand structure after use.
4012 */
4013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004014ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 if (xp->xp_numfiles >= 0)
4017 {
4018 FreeWild(xp->xp_numfiles, xp->xp_files);
4019 xp->xp_numfiles = -1;
4020 }
4021}
4022
4023 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004024ExpandEscape(
4025 expand_T *xp,
4026 char_u *str,
4027 int numfiles,
4028 char_u **files,
4029 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
4031 int i;
4032 char_u *p;
4033
4034 /*
4035 * May change home directory back to "~"
4036 */
4037 if (options & WILD_HOME_REPLACE)
4038 tilde_replace(str, numfiles, files);
4039
4040 if (options & WILD_ESCAPE)
4041 {
4042 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004043 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004044 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 || xp->xp_context == EXPAND_BUFFERS
4046 || xp->xp_context == EXPAND_DIRECTORIES)
4047 {
4048 /*
4049 * Insert a backslash into a file name before a space, \, %, #
4050 * and wildmatch characters, except '~'.
4051 */
4052 for (i = 0; i < numfiles; ++i)
4053 {
4054 /* for ":set path=" we need to escape spaces twice */
4055 if (xp->xp_backslash == XP_BS_THREE)
4056 {
4057 p = vim_strsave_escaped(files[i], (char_u *)" ");
4058 if (p != NULL)
4059 {
4060 vim_free(files[i]);
4061 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004062#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 p = vim_strsave_escaped(files[i], (char_u *)" ");
4064 if (p != NULL)
4065 {
4066 vim_free(files[i]);
4067 files[i] = p;
4068 }
4069#endif
4070 }
4071 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004072#ifdef BACKSLASH_IN_FILENAME
4073 p = vim_strsave_fnameescape(files[i], FALSE);
4074#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004075 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (p != NULL)
4078 {
4079 vim_free(files[i]);
4080 files[i] = p;
4081 }
4082
4083 /* If 'str' starts with "\~", replace "~" at start of
4084 * files[i] with "\~". */
4085 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004086 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004089
4090 /* If the first file starts with a '+' escape it. Otherwise it
4091 * could be seen as "+cmd". */
4092 if (*files[0] == '+')
4093 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 else if (xp->xp_context == EXPAND_TAGS)
4096 {
4097 /*
4098 * Insert a backslash before characters in a tag name that
4099 * would terminate the ":tag" command.
4100 */
4101 for (i = 0; i < numfiles; ++i)
4102 {
4103 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4104 if (p != NULL)
4105 {
4106 vim_free(files[i]);
4107 files[i] = p;
4108 }
4109 }
4110 }
4111 }
4112}
4113
4114/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004115 * Escape special characters in "fname" for when used as a file name argument
4116 * after a Vim command, or, when "shell" is non-zero, a shell command.
4117 * Returns the result in allocated memory.
4118 */
4119 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004120vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004121{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004122 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004123#ifdef BACKSLASH_IN_FILENAME
4124 char_u buf[20];
4125 int j = 0;
4126
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004127 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004128 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004129 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004130 buf[j++] = *p;
4131 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004132 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004133#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004134 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4135 if (shell && csh_like_shell() && p != NULL)
4136 {
4137 char_u *s;
4138
4139 /* For csh and similar shells need to put two backslashes before '!'.
4140 * One is taken by Vim, one by the shell. */
4141 s = vim_strsave_escaped(p, (char_u *)"!");
4142 vim_free(p);
4143 p = s;
4144 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004145#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004146
4147 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4148 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004149 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004150 escape_fname(&p);
4151
4152 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004153}
4154
4155/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004156 * Put a backslash before the file name in "pp", which is in allocated memory.
4157 */
4158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004159escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004160{
4161 char_u *p;
4162
4163 p = alloc((unsigned)(STRLEN(*pp) + 2));
4164 if (p != NULL)
4165 {
4166 p[0] = '\\';
4167 STRCPY(p + 1, *pp);
4168 vim_free(*pp);
4169 *pp = p;
4170 }
4171}
4172
4173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * For each file name in files[num_files]:
4175 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4176 */
4177 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004178tilde_replace(
4179 char_u *orig_pat,
4180 int num_files,
4181 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
4183 int i;
4184 char_u *p;
4185
4186 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4187 {
4188 for (i = 0; i < num_files; ++i)
4189 {
4190 p = home_replace_save(NULL, files[i]);
4191 if (p != NULL)
4192 {
4193 vim_free(files[i]);
4194 files[i] = p;
4195 }
4196 }
4197 }
4198}
4199
4200/*
4201 * Show all matches for completion on the command line.
4202 * Returns EXPAND_NOTHING when the character that triggered expansion should
4203 * be inserted like a normal character.
4204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004206showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4209 int num_files;
4210 char_u **files_found;
4211 int i, j, k;
4212 int maxlen;
4213 int lines;
4214 int columns;
4215 char_u *p;
4216 int lastlen;
4217 int attr;
4218 int showtail;
4219
4220 if (xp->xp_numfiles == -1)
4221 {
4222 set_expand_context(xp);
4223 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4224 &num_files, &files_found);
4225 showtail = expand_showtail(xp);
4226 if (i != EXPAND_OK)
4227 return i;
4228
4229 }
4230 else
4231 {
4232 num_files = xp->xp_numfiles;
4233 files_found = xp->xp_files;
4234 showtail = cmd_showtail;
4235 }
4236
4237#ifdef FEAT_WILDMENU
4238 if (!wildmenu)
4239 {
4240#endif
4241 msg_didany = FALSE; /* lines_left will be set */
4242 msg_start(); /* prepare for paging */
4243 msg_putchar('\n');
4244 out_flush();
4245 cmdline_row = msg_row;
4246 msg_didany = FALSE; /* lines_left will be set again */
4247 msg_start(); /* prepare for paging */
4248#ifdef FEAT_WILDMENU
4249 }
4250#endif
4251
4252 if (got_int)
4253 got_int = FALSE; /* only int. the completion, not the cmd line */
4254#ifdef FEAT_WILDMENU
4255 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004256 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257#endif
4258 else
4259 {
4260 /* find the length of the longest file name */
4261 maxlen = 0;
4262 for (i = 0; i < num_files; ++i)
4263 {
4264 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004265 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 || xp->xp_context == EXPAND_BUFFERS))
4267 {
4268 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4269 j = vim_strsize(NameBuff);
4270 }
4271 else
4272 j = vim_strsize(L_SHOWFILE(i));
4273 if (j > maxlen)
4274 maxlen = j;
4275 }
4276
4277 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4278 lines = num_files;
4279 else
4280 {
4281 /* compute the number of columns and lines for the listing */
4282 maxlen += 2; /* two spaces between file names */
4283 columns = ((int)Columns + 2) / maxlen;
4284 if (columns < 1)
4285 columns = 1;
4286 lines = (num_files + columns - 1) / columns;
4287 }
4288
Bram Moolenaar8820b482017-03-16 17:23:31 +01004289 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4292 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004293 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 msg_clr_eos();
4295 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004296 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298
4299 /* list the files line by line */
4300 for (i = 0; i < lines; ++i)
4301 {
4302 lastlen = 999;
4303 for (k = i; k < num_files; k += lines)
4304 {
4305 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4306 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004307 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 p = files_found[k] + STRLEN(files_found[k]) + 1;
4309 msg_advance(maxlen + 1);
4310 msg_puts(p);
4311 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004312 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 break;
4314 }
4315 for (j = maxlen - lastlen; --j >= 0; )
4316 msg_putchar(' ');
4317 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004318 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 || xp->xp_context == EXPAND_BUFFERS)
4320 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004321 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004322 if (xp->xp_numfiles != -1)
4323 {
4324 char_u *halved_slash;
4325 char_u *exp_path;
4326
4327 /* Expansion was done before and special characters
4328 * were escaped, need to halve backslashes. Also
4329 * $HOME has been replaced with ~/. */
4330 exp_path = expand_env_save_opt(files_found[k], TRUE);
4331 halved_slash = backslash_halve_save(
4332 exp_path != NULL ? exp_path : files_found[k]);
4333 j = mch_isdir(halved_slash != NULL ? halved_slash
4334 : files_found[k]);
4335 vim_free(exp_path);
4336 vim_free(halved_slash);
4337 }
4338 else
4339 /* Expansion was done here, file names are literal. */
4340 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 if (showtail)
4342 p = L_SHOWFILE(k);
4343 else
4344 {
4345 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4346 TRUE);
4347 p = NameBuff;
4348 }
4349 }
4350 else
4351 {
4352 j = FALSE;
4353 p = L_SHOWFILE(k);
4354 }
4355 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4356 }
4357 if (msg_col > 0) /* when not wrapped around */
4358 {
4359 msg_clr_eos();
4360 msg_putchar('\n');
4361 }
4362 out_flush(); /* show one line at a time */
4363 if (got_int)
4364 {
4365 got_int = FALSE;
4366 break;
4367 }
4368 }
4369
4370 /*
4371 * we redraw the command below the lines that we have just listed
4372 * This is a bit tricky, but it saves a lot of screen updating.
4373 */
4374 cmdline_row = msg_row; /* will put it back later */
4375 }
4376
4377 if (xp->xp_numfiles == -1)
4378 FreeWild(num_files, files_found);
4379
4380 return EXPAND_OK;
4381}
4382
4383/*
4384 * Private gettail for showmatches() (and win_redr_status_matches()):
4385 * Find tail of file name path, but ignore trailing "/".
4386 */
4387 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004388sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 char_u *p;
4391 char_u *t = s;
4392 int had_sep = FALSE;
4393
4394 for (p = s; *p != NUL; )
4395 {
4396 if (vim_ispathsep(*p)
4397#ifdef BACKSLASH_IN_FILENAME
4398 && !rem_backslash(p)
4399#endif
4400 )
4401 had_sep = TRUE;
4402 else if (had_sep)
4403 {
4404 t = p;
4405 had_sep = FALSE;
4406 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004407 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409 return t;
4410}
4411
4412/*
4413 * Return TRUE if we only need to show the tail of completion matches.
4414 * When not completing file names or there is a wildcard in the path FALSE is
4415 * returned.
4416 */
4417 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004418expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
4420 char_u *s;
4421 char_u *end;
4422
4423 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004424 if (xp->xp_context != EXPAND_FILES
4425 && xp->xp_context != EXPAND_SHELLCMD
4426 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 return FALSE;
4428
4429 end = gettail(xp->xp_pattern);
4430 if (end == xp->xp_pattern) /* there is no path separator */
4431 return FALSE;
4432
4433 for (s = xp->xp_pattern; s < end; s++)
4434 {
4435 /* Skip escaped wildcards. Only when the backslash is not a path
4436 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4437 if (rem_backslash(s))
4438 ++s;
4439 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4440 return FALSE;
4441 }
4442 return TRUE;
4443}
4444
4445/*
4446 * Prepare a string for expansion.
4447 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004448 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 * When expanding other names: The string will be used with regcomp(). Copy
4450 * the name into allocated memory and prepend "^".
4451 */
4452 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004453addstar(
4454 char_u *fname,
4455 int len,
4456 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
4458 char_u *retval;
4459 int i, j;
4460 int new_len;
4461 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004462 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004464 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004465 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004466 && context != EXPAND_SHELLCMD
4467 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
4469 /*
4470 * Matching will be done internally (on something other than files).
4471 * So we convert the file-matching-type wildcards into our kind for
4472 * use with vim_regcomp(). First work out how long it will be:
4473 */
4474
4475 /* For help tags the translation is done in find_help_tags().
4476 * For a tag pattern starting with "/" no translation is needed. */
4477 if (context == EXPAND_HELP
4478 || context == EXPAND_COLORS
4479 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004480 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004481 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004482 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004483 || ((context == EXPAND_TAGS_LISTFILES
4484 || context == EXPAND_TAGS)
4485 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 retval = vim_strnsave(fname, len);
4487 else
4488 {
4489 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4490 for (i = 0; i < len; i++)
4491 {
4492 if (fname[i] == '*' || fname[i] == '~')
4493 new_len++; /* '*' needs to be replaced by ".*"
4494 '~' needs to be replaced by "\~" */
4495
4496 /* Buffer names are like file names. "." should be literal */
4497 if (context == EXPAND_BUFFERS && fname[i] == '.')
4498 new_len++; /* "." becomes "\." */
4499
4500 /* Custom expansion takes care of special things, match
4501 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004502 if ((context == EXPAND_USER_DEFINED
4503 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 new_len++; /* '\' becomes "\\" */
4505 }
4506 retval = alloc(new_len);
4507 if (retval != NULL)
4508 {
4509 retval[0] = '^';
4510 j = 1;
4511 for (i = 0; i < len; i++, j++)
4512 {
4513 /* Skip backslash. But why? At least keep it for custom
4514 * expansion. */
4515 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004516 && context != EXPAND_USER_LIST
4517 && fname[i] == '\\'
4518 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 break;
4520
4521 switch (fname[i])
4522 {
4523 case '*': retval[j++] = '.';
4524 break;
4525 case '~': retval[j++] = '\\';
4526 break;
4527 case '?': retval[j] = '.';
4528 continue;
4529 case '.': if (context == EXPAND_BUFFERS)
4530 retval[j++] = '\\';
4531 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004532 case '\\': if (context == EXPAND_USER_DEFINED
4533 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 retval[j++] = '\\';
4535 break;
4536 }
4537 retval[j] = fname[i];
4538 }
4539 retval[j] = NUL;
4540 }
4541 }
4542 }
4543 else
4544 {
4545 retval = alloc(len + 4);
4546 if (retval != NULL)
4547 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004548 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004551 * Don't add a star to *, ~, ~user, $var or `cmd`.
4552 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 * ~ would be at the start of the file name, but not the tail.
4554 * $ could be anywhere in the tail.
4555 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004556 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 */
4558 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004559 ends_in_star = (len > 0 && retval[len - 1] == '*');
4560#ifndef BACKSLASH_IN_FILENAME
4561 for (i = len - 2; i >= 0; --i)
4562 {
4563 if (retval[i] != '\\')
4564 break;
4565 ends_in_star = !ends_in_star;
4566 }
4567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004569 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 && vim_strchr(tail, '$') == NULL
4571 && vim_strchr(retval, '`') == NULL)
4572 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004573 else if (len > 0 && retval[len - 1] == '$')
4574 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 retval[len] = NUL;
4576 }
4577 }
4578 return retval;
4579}
4580
4581/*
4582 * Must parse the command line so far to work out what context we are in.
4583 * Completion can then be done based on that context.
4584 * This routine sets the variables:
4585 * xp->xp_pattern The start of the pattern to be expanded within
4586 * the command line (ends at the cursor).
4587 * xp->xp_context The type of thing to expand. Will be one of:
4588 *
4589 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4590 * the command line, like an unknown command. Caller
4591 * should beep.
4592 * EXPAND_NOTHING Unrecognised context for completion, use char like
4593 * a normal char, rather than for completion. eg
4594 * :s/^I/
4595 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4596 * it.
4597 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4598 * EXPAND_FILES After command with XFILE set, or after setting
4599 * with P_EXPAND set. eg :e ^I, :w>>^I
4600 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4601 * when we know only directories are of interest. eg
4602 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004603 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4605 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4606 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4607 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4608 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4609 * EXPAND_EVENTS Complete event names
4610 * EXPAND_SYNTAX Complete :syntax command arguments
4611 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4612 * EXPAND_AUGROUP Complete autocommand group names
4613 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4614 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4615 * eg :unmap a^I , :cunab x^I
4616 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4617 * eg :call sub^I
4618 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4619 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4620 * names in expressions, eg :while s^I
4621 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004622 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 */
4624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004625set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004627 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (ccline.cmdfirstc != ':'
4629#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004630 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004631 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
4633 )
4634 {
4635 xp->xp_context = EXPAND_NOTHING;
4636 return;
4637 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004638 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639}
4640
4641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004642set_cmd_context(
4643 expand_T *xp,
4644 char_u *str, /* start of command line */
4645 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004646 int col, /* position of cursor */
4647 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
4649 int old_char = NUL;
4650 char_u *nextcomm;
4651
4652 /*
4653 * Avoid a UMR warning from Purify, only save the character if it has been
4654 * written before.
4655 */
4656 if (col < len)
4657 old_char = str[col];
4658 str[col] = NUL;
4659 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004660
4661#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004662 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004663 {
4664# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004665 /* pass CMD_SIZE because there is no real command */
4666 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004667# endif
4668 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004669 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004670 {
4671 xp->xp_context = ccline.xp_context;
4672 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004673# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004674 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004675# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004676 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004677 else
4678#endif
4679 while (nextcomm != NULL)
4680 nextcomm = set_one_cmd_context(xp, nextcomm);
4681
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004682 /* Store the string here so that call_user_expand_func() can get to them
4683 * easily. */
4684 xp->xp_line = str;
4685 xp->xp_col = col;
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 str[col] = old_char;
4688}
4689
4690/*
4691 * Expand the command line "str" from context "xp".
4692 * "xp" must have been set by set_cmd_context().
4693 * xp->xp_pattern points into "str", to where the text that is to be expanded
4694 * starts.
4695 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4696 * cursor.
4697 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4698 * key that triggered expansion literally.
4699 * Returns EXPAND_OK otherwise.
4700 */
4701 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004702expand_cmdline(
4703 expand_T *xp,
4704 char_u *str, /* start of command line */
4705 int col, /* position of cursor */
4706 int *matchcount, /* return: nr of matches */
4707 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004710 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
4712 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4713 {
4714 beep_flush();
4715 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4716 }
4717 if (xp->xp_context == EXPAND_NOTHING)
4718 {
4719 /* Caller can use the character as a normal char instead */
4720 return EXPAND_NOTHING;
4721 }
4722
4723 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004724 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4725 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (file_str == NULL)
4727 return EXPAND_UNSUCCESSFUL;
4728
Bram Moolenaar94950a92010-12-02 16:01:29 +01004729 if (p_wic)
4730 options += WILD_ICASE;
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004733 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 *matchcount = 0;
4736 *matches = NULL;
4737 }
4738 vim_free(file_str);
4739
4740 return EXPAND_OK;
4741}
4742
4743#ifdef FEAT_MULTI_LANG
4744/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004745 * Cleanup matches for help tags:
4746 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4747 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004749static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004752cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753{
4754 int i, j;
4755 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004756 char_u buf[4];
4757 char_u *p = buf;
4758
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004759 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004760 {
4761 *p++ = '@';
4762 *p++ = p_hlg[0];
4763 *p++ = p_hlg[1];
4764 }
4765 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766
4767 for (i = 0; i < num_file; ++i)
4768 {
4769 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004770 if (len <= 0)
4771 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004772 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 /* Sorting on priority means the same item in another language may
4775 * be anywhere. Search all items for a match up to the "@en". */
4776 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004777 if (j != i && (int)STRLEN(file[j]) == len + 3
4778 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 break;
4780 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004781 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 file[i][len] = NUL;
4783 }
4784 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004785
4786 if (*buf != NUL)
4787 for (i = 0; i < num_file; ++i)
4788 {
4789 len = (int)STRLEN(file[i]) - 3;
4790 if (len <= 0)
4791 continue;
4792 if (STRCMP(file[i] + len, buf) == 0)
4793 {
4794 /* remove the default language */
4795 file[i][len] = NUL;
4796 }
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798}
4799#endif
4800
4801/*
4802 * Do the expansion based on xp->xp_context and "pat".
4803 */
4804 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004805ExpandFromContext(
4806 expand_T *xp,
4807 char_u *pat,
4808 int *num_file,
4809 char_u ***file,
4810 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812#ifdef FEAT_CMDL_COMPL
4813 regmatch_T regmatch;
4814#endif
4815 int ret;
4816 int flags;
4817
4818 flags = EW_DIR; /* include directories */
4819 if (options & WILD_LIST_NOTFOUND)
4820 flags |= EW_NOTFOUND;
4821 if (options & WILD_ADD_SLASH)
4822 flags |= EW_ADDSLASH;
4823 if (options & WILD_KEEP_ALL)
4824 flags |= EW_KEEPALL;
4825 if (options & WILD_SILENT)
4826 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004827 if (options & WILD_ALLLINKS)
4828 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004830 if (xp->xp_context == EXPAND_FILES
4831 || xp->xp_context == EXPAND_DIRECTORIES
4832 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 /*
4835 * Expand file or directory names.
4836 */
4837 int free_pat = FALSE;
4838 int i;
4839
4840 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4841 * space */
4842 if (xp->xp_backslash != XP_BS_NONE)
4843 {
4844 free_pat = TRUE;
4845 pat = vim_strsave(pat);
4846 for (i = 0; pat[i]; ++i)
4847 if (pat[i] == '\\')
4848 {
4849 if (xp->xp_backslash == XP_BS_THREE
4850 && pat[i + 1] == '\\'
4851 && pat[i + 2] == '\\'
4852 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004853 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (xp->xp_backslash == XP_BS_ONE
4855 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004856 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 }
4859
4860 if (xp->xp_context == EXPAND_FILES)
4861 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004862 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4863 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 else
4865 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004866 if (options & WILD_ICASE)
4867 flags |= EW_ICASE;
4868
Bram Moolenaard7834d32009-12-02 16:14:36 +00004869 /* Expand wildcards, supporting %:h and the like. */
4870 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (free_pat)
4872 vim_free(pat);
4873 return ret;
4874 }
4875
4876 *file = (char_u **)"";
4877 *num_file = 0;
4878 if (xp->xp_context == EXPAND_HELP)
4879 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004880 /* With an empty argument we would get all the help tags, which is
4881 * very slow. Get matches for "help" instead. */
4882 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4883 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
4885#ifdef FEAT_MULTI_LANG
4886 cleanup_help_tags(*num_file, *file);
4887#endif
4888 return OK;
4889 }
4890 return FAIL;
4891 }
4892
4893#ifndef FEAT_CMDL_COMPL
4894 return FAIL;
4895#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004896 if (xp->xp_context == EXPAND_SHELLCMD)
4897 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (xp->xp_context == EXPAND_OLD_SETTING)
4899 return ExpandOldSetting(num_file, file);
4900 if (xp->xp_context == EXPAND_BUFFERS)
4901 return ExpandBufnames(pat, num_file, file, options);
4902 if (xp->xp_context == EXPAND_TAGS
4903 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4904 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4905 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004906 {
4907 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004908 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4909 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004912 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004913 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004914 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004915 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004916 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004917 {
4918 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004919 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004920 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004921 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004922 {
4923 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004924 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004925 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004926# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4927 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004928 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004929# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004930 if (xp->xp_context == EXPAND_PACKADD)
4931 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4934 if (regmatch.regprog == NULL)
4935 return FAIL;
4936
4937 /* set ignore-case according to p_ic, p_scs and pat */
4938 regmatch.rm_ic = ignorecase(pat);
4939
4940 if (xp->xp_context == EXPAND_SETTINGS
4941 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4942 ret = ExpandSettings(xp, &regmatch, num_file, file);
4943 else if (xp->xp_context == EXPAND_MAPPINGS)
4944 ret = ExpandMappings(&regmatch, num_file, file);
4945# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4946 else if (xp->xp_context == EXPAND_USER_DEFINED)
4947 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4948# endif
4949 else
4950 {
4951 static struct expgen
4952 {
4953 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004954 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004956 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 } tab[] =
4958 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004959 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4960 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004961 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004962 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004963#ifdef FEAT_CMDHIST
4964 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004967 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004968 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004969 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4970 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4971 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#endif
4973#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004974 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4975 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4976 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4977 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978#endif
4979#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004980 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4981 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
4983#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004984 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004986#ifdef FEAT_PROFILE
4987 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4988#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004989 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004991 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4992 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004994#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004995 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004996#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004997#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004998 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004999#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005000#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005001 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5004 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005005 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5006 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005008 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005009 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 };
5011 int i;
5012
5013 /*
5014 * Find a context in the table and call the ExpandGeneric() with the
5015 * right function to do the expansion.
5016 */
5017 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005018 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (xp->xp_context == tab[i].context)
5020 {
5021 if (tab[i].ic)
5022 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005023 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005024 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026 }
5027 }
5028
Bram Moolenaar473de612013-06-08 18:19:48 +02005029 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
5031 return ret;
5032#endif /* FEAT_CMDL_COMPL */
5033}
5034
5035#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5036/*
5037 * Expand a list of names.
5038 *
5039 * Generic function for command line completion. It calls a function to
5040 * obtain strings, one by one. The strings are matched against a regexp
5041 * program. Matching strings are copied into an array, which is returned.
5042 *
5043 * Returns OK when no problems encountered, FAIL for error (out of memory).
5044 */
5045 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005046ExpandGeneric(
5047 expand_T *xp,
5048 regmatch_T *regmatch,
5049 int *num_file,
5050 char_u ***file,
5051 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005053 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 int i;
5056 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005057 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 char_u *str;
5059
5060 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005061 * round == 0: count the number of matching names
5062 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005064 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 for (i = 0; ; ++i)
5067 {
5068 str = (*func)(xp, i);
5069 if (str == NULL) /* end of list */
5070 break;
5071 if (*str == NUL) /* skip empty strings */
5072 continue;
5073
5074 if (vim_regexec(regmatch, str, (colnr_T)0))
5075 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005076 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005078 if (escaped)
5079 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5080 else
5081 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 (*file)[count] = str;
5083#ifdef FEAT_MENU
5084 if (func == get_menu_names && str != NULL)
5085 {
5086 /* test for separator added by get_menu_names() */
5087 str += STRLEN(str) - 1;
5088 if (*str == '\001')
5089 *str = '.';
5090 }
5091#endif
5092 }
5093 ++count;
5094 }
5095 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005096 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 if (count == 0)
5099 return OK;
5100 *num_file = count;
5101 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5102 if (*file == NULL)
5103 {
5104 *file = (char_u **)"";
5105 return FAIL;
5106 }
5107 count = 0;
5108 }
5109 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005110
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005111 /* Sort the results. Keep menu's in the specified order. */
5112 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005113 {
5114 if (xp->xp_context == EXPAND_EXPRESSION
5115 || xp->xp_context == EXPAND_FUNCTIONS
5116 || xp->xp_context == EXPAND_USER_FUNC)
5117 /* <SNR> functions should be sorted to the end. */
5118 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5119 sort_func_compare);
5120 else
5121 sort_strings(*file, *num_file);
5122 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005123
Bram Moolenaar4f688582007-07-24 12:34:30 +00005124#ifdef FEAT_CMDL_COMPL
5125 /* Reset the variables used for special highlight names expansion, so that
5126 * they don't show up when getting normal highlight names by ID. */
5127 reset_expand_highlight();
5128#endif
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 return OK;
5131}
5132
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005133/*
5134 * Complete a shell command.
5135 * Returns FAIL or OK;
5136 */
5137 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005138expand_shellcmd(
5139 char_u *filepat, /* pattern to match with command names */
5140 int *num_file, /* return: number of matches */
5141 char_u ***file, /* return: array with matches */
5142 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005143{
5144 char_u *pat;
5145 int i;
5146 char_u *path;
5147 int mustfree = FALSE;
5148 garray_T ga;
5149 char_u *buf = alloc(MAXPATHL);
5150 size_t l;
5151 char_u *s, *e;
5152 int flags = flagsarg;
5153 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005154 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005155
5156 if (buf == NULL)
5157 return FAIL;
5158
5159 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5160 * space */
5161 pat = vim_strsave(filepat);
5162 for (i = 0; pat[i]; ++i)
5163 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005164 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005165
Bram Moolenaarb5971142015-03-21 17:32:19 +01005166 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005167
5168 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005169 if (mch_isFullName(pat))
5170 path = (char_u *)" ";
5171 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005172 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5173 path = (char_u *)".";
5174 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005175 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005176 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005177 if (path == NULL)
5178 path = (char_u *)"";
5179 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005180
5181 /*
5182 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005183 * collect them in "ga". When "." is not in $PATH also expand for the
5184 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005185 */
5186 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005187 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005188 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005189 if (*s == NUL)
5190 {
5191 if (did_curdir)
5192 break;
5193 /* Find directories in the current directory, path is empty. */
5194 did_curdir = TRUE;
5195 }
5196 else if (*s == '.')
5197 did_curdir = TRUE;
5198
Bram Moolenaar68c31742006-09-02 15:54:18 +00005199 if (*s == ' ')
5200 ++s; /* Skip space used for absolute path name. */
5201
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005202#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005203 e = vim_strchr(s, ';');
5204#else
5205 e = vim_strchr(s, ':');
5206#endif
5207 if (e == NULL)
5208 e = s + STRLEN(s);
5209
5210 l = e - s;
5211 if (l > MAXPATHL - 5)
5212 break;
5213 vim_strncpy(buf, s, l);
5214 add_pathsep(buf);
5215 l = STRLEN(buf);
5216 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5217
5218 /* Expand matches in one directory of $PATH. */
5219 ret = expand_wildcards(1, &buf, num_file, file, flags);
5220 if (ret == OK)
5221 {
5222 if (ga_grow(&ga, *num_file) == FAIL)
5223 FreeWild(*num_file, *file);
5224 else
5225 {
5226 for (i = 0; i < *num_file; ++i)
5227 {
5228 s = (*file)[i];
5229 if (STRLEN(s) > l)
5230 {
5231 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005232 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005233 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5234 }
5235 else
5236 vim_free(s);
5237 }
5238 vim_free(*file);
5239 }
5240 }
5241 if (*e != NUL)
5242 ++e;
5243 }
5244 *file = ga.ga_data;
5245 *num_file = ga.ga_len;
5246
5247 vim_free(buf);
5248 vim_free(pat);
5249 if (mustfree)
5250 vim_free(path);
5251 return OK;
5252}
5253
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005256static 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 +00005257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005259 * Call "user_expand_func()" to invoke a user defined Vim script function and
5260 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005262 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263call_user_expand_func(
5264 void *(*user_expand_func)(char_u *, int, char_u **, int),
5265 expand_T *xp,
5266 int *num_file,
5267 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005269 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005270 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005273 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005274 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
Bram Moolenaarb7515462013-06-29 12:58:33 +02005276 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005277 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 *num_file = 0;
5279 *file = NULL;
5280
Bram Moolenaarb7515462013-06-29 12:58:33 +02005281 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005282 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005283 keep = ccline.cmdbuff[ccline.cmdlen];
5284 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005285 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005286
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005287 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005288 args[1] = xp->xp_line;
5289 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 args[2] = num;
5291
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005292 /* Save the cmdline, we don't know what the function may do. */
5293 save_ccline = ccline;
5294 ccline.cmdbuff = NULL;
5295 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005297
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005298 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005299
5300 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005302 if (ccline.cmdbuff != NULL)
5303 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005304
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005305 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005306 return ret;
5307}
5308
5309/*
5310 * Expand names with a function defined by the user.
5311 */
5312 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005313ExpandUserDefined(
5314 expand_T *xp,
5315 regmatch_T *regmatch,
5316 int *num_file,
5317 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005318{
5319 char_u *retstr;
5320 char_u *s;
5321 char_u *e;
5322 char_u keep;
5323 garray_T ga;
5324
5325 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5326 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 return FAIL;
5328
5329 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005330 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
5332 e = vim_strchr(s, '\n');
5333 if (e == NULL)
5334 e = s + STRLEN(s);
5335 keep = *e;
5336 *e = 0;
5337
5338 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5339 {
5340 *e = keep;
5341 if (*e != NUL)
5342 ++e;
5343 continue;
5344 }
5345
5346 if (ga_grow(&ga, 1) == FAIL)
5347 break;
5348
5349 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5350 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 *e = keep;
5353 if (*e != NUL)
5354 ++e;
5355 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005356 vim_free(retstr);
5357 *file = ga.ga_data;
5358 *num_file = ga.ga_len;
5359 return OK;
5360}
5361
5362/*
5363 * Expand names with a list returned by a function defined by the user.
5364 */
5365 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005366ExpandUserList(
5367 expand_T *xp,
5368 int *num_file,
5369 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005370{
5371 list_T *retlist;
5372 listitem_T *li;
5373 garray_T ga;
5374
5375 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5376 if (retlist == NULL)
5377 return FAIL;
5378
5379 ga_init2(&ga, (int)sizeof(char *), 3);
5380 /* Loop over the items in the list. */
5381 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5382 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005383 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5384 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005385
5386 if (ga_grow(&ga, 1) == FAIL)
5387 break;
5388
5389 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005390 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005391 ++ga.ga_len;
5392 }
5393 list_unref(retlist);
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 *file = ga.ga_data;
5396 *num_file = ga.ga_len;
5397 return OK;
5398}
5399#endif
5400
5401/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005402 * Expand color scheme, compiler or filetype names.
5403 * Search from 'runtimepath':
5404 * 'runtimepath'/{dirnames}/{pat}.vim
5405 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5406 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5407 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5408 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005409 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 */
5411 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412ExpandRTDir(
5413 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005414 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415 int *num_file,
5416 char_u ***file,
5417 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 char_u *s;
5420 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005421 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005423 int i;
5424 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
5426 *num_file = 0;
5427 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005428 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005429 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005431 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005433 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5434 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005436 ga_clear_strings(&ga);
5437 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005439 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005440 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005441 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005443
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005444 if (flags & DIP_START) {
5445 for (i = 0; dirnames[i] != NULL; ++i)
5446 {
5447 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5448 if (s == NULL)
5449 {
5450 ga_clear_strings(&ga);
5451 return FAIL;
5452 }
5453 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5454 globpath(p_pp, s, &ga, 0);
5455 vim_free(s);
5456 }
5457 }
5458
5459 if (flags & DIP_OPT) {
5460 for (i = 0; dirnames[i] != NULL; ++i)
5461 {
5462 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5463 if (s == NULL)
5464 {
5465 ga_clear_strings(&ga);
5466 return FAIL;
5467 }
5468 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5469 globpath(p_pp, s, &ga, 0);
5470 vim_free(s);
5471 }
5472 }
5473
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005474 for (i = 0; i < ga.ga_len; ++i)
5475 {
5476 match = ((char_u **)ga.ga_data)[i];
5477 s = match;
5478 e = s + STRLEN(s);
5479 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5480 {
5481 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005482 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005483 if (s < match || vim_ispathsep(*s))
5484 break;
5485 ++s;
5486 *e = NUL;
5487 mch_memmove(match, s, e - s + 1);
5488 }
5489 }
5490
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005491 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005492 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005493
5494 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005495 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005496 remove_duplicates(&ga);
5497
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 *file = ga.ga_data;
5499 *num_file = ga.ga_len;
5500 return OK;
5501}
5502
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005503/*
5504 * Expand loadplugin names:
5505 * 'packpath'/pack/ * /opt/{pat}
5506 */
5507 static int
5508ExpandPackAddDir(
5509 char_u *pat,
5510 int *num_file,
5511 char_u ***file)
5512{
5513 char_u *s;
5514 char_u *e;
5515 char_u *match;
5516 garray_T ga;
5517 int i;
5518 int pat_len;
5519
5520 *num_file = 0;
5521 *file = NULL;
5522 pat_len = (int)STRLEN(pat);
5523 ga_init2(&ga, (int)sizeof(char *), 10);
5524
5525 s = alloc((unsigned)(pat_len + 26));
5526 if (s == NULL)
5527 {
5528 ga_clear_strings(&ga);
5529 return FAIL;
5530 }
5531 sprintf((char *)s, "pack/*/opt/%s*", pat);
5532 globpath(p_pp, s, &ga, 0);
5533 vim_free(s);
5534
5535 for (i = 0; i < ga.ga_len; ++i)
5536 {
5537 match = ((char_u **)ga.ga_data)[i];
5538 s = gettail(match);
5539 e = s + STRLEN(s);
5540 mch_memmove(match, s, e - s + 1);
5541 }
5542
5543 if (ga.ga_len == 0)
5544 return FAIL;
5545
5546 /* Sort and remove duplicates which can happen when specifying multiple
5547 * directories in dirnames. */
5548 remove_duplicates(&ga);
5549
5550 *file = ga.ga_data;
5551 *num_file = ga.ga_len;
5552 return OK;
5553}
5554
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555#endif
5556
5557#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5558/*
5559 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005560 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005562 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563globpath(
5564 char_u *path,
5565 char_u *file,
5566 garray_T *ga,
5567 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 expand_T xpc;
5570 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 int num_p;
5573 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 buf = alloc(MAXPATHL);
5576 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005577 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005579 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 /* Loop over all entries in {path}. */
5583 while (*path != NUL)
5584 {
5585 /* Copy one item of the path to buf[] and concatenate the file name. */
5586 copy_option_part(&path, buf, MAXPATHL, ",");
5587 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5588 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005589# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005590 /* Using the platform's path separator (\) makes vim incorrectly
5591 * treat it as an escape character, use '/' instead. */
5592 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5593 STRCAT(buf, "/");
5594# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005598 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5599 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005601 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005603 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 for (i = 0; i < num_p; ++i)
5606 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005607 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005608 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005609 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 FreeWild(num_p, p);
5614 }
5615 }
5616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619}
5620
5621#endif
5622
5623#if defined(FEAT_CMDHIST) || defined(PROTO)
5624
5625/*********************************
5626 * Command line history stuff *
5627 *********************************/
5628
5629/*
5630 * Translate a history character to the associated type number.
5631 */
5632 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 if (c == ':')
5636 return HIST_CMD;
5637 if (c == '=')
5638 return HIST_EXPR;
5639 if (c == '@')
5640 return HIST_INPUT;
5641 if (c == '>')
5642 return HIST_DEBUG;
5643 return HIST_SEARCH; /* must be '?' or '/' */
5644}
5645
5646/*
5647 * Table of history names.
5648 * These names are used in :history and various hist...() functions.
5649 * It is sufficient to give the significant prefix of a history name.
5650 */
5651
5652static char *(history_names[]) =
5653{
5654 "cmd",
5655 "search",
5656 "expr",
5657 "input",
5658 "debug",
5659 NULL
5660};
5661
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005662#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5663/*
5664 * Function given to ExpandGeneric() to obtain the possible first
5665 * arguments of the ":history command.
5666 */
5667 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005669{
5670 static char_u compl[2] = { NUL, NUL };
5671 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005672 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005673 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5674
5675 if (idx < short_names_count)
5676 {
5677 compl[0] = (char_u)short_names[idx];
5678 return compl;
5679 }
5680 if (idx < short_names_count + history_name_count)
5681 return (char_u *)history_names[idx - short_names_count];
5682 if (idx == short_names_count + history_name_count)
5683 return (char_u *)"all";
5684 return NULL;
5685}
5686#endif
5687
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688/*
5689 * init_history() - Initialize the command line history.
5690 * Also used to re-allocate the history when the size changes.
5691 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005692 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 int newlen; /* new length of history table */
5696 histentry_T *temp;
5697 int i;
5698 int j;
5699 int type;
5700
5701 /*
5702 * If size of history table changed, reallocate it
5703 */
5704 newlen = (int)p_hi;
5705 if (newlen != hislen) /* history length changed */
5706 {
5707 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5708 {
5709 if (newlen)
5710 {
5711 temp = (histentry_T *)lalloc(
5712 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5713 if (temp == NULL) /* out of memory! */
5714 {
5715 if (type == 0) /* first one: just keep the old length */
5716 {
5717 newlen = hislen;
5718 break;
5719 }
5720 /* Already changed one table, now we can only have zero
5721 * length for all tables. */
5722 newlen = 0;
5723 type = -1;
5724 continue;
5725 }
5726 }
5727 else
5728 temp = NULL;
5729 if (newlen == 0 || temp != NULL)
5730 {
5731 if (hisidx[type] < 0) /* there are no entries yet */
5732 {
5733 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005734 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
5736 else if (newlen > hislen) /* array becomes bigger */
5737 {
5738 for (i = 0; i <= hisidx[type]; ++i)
5739 temp[i] = history[type][i];
5740 j = i;
5741 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005742 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 for ( ; j < hislen; ++i, ++j)
5744 temp[i] = history[type][j];
5745 }
5746 else /* array becomes smaller or 0 */
5747 {
5748 j = hisidx[type];
5749 for (i = newlen - 1; ; --i)
5750 {
5751 if (i >= 0) /* copy newest entries */
5752 temp[i] = history[type][j];
5753 else /* remove older entries */
5754 vim_free(history[type][j].hisstr);
5755 if (--j < 0)
5756 j = hislen - 1;
5757 if (j == hisidx[type])
5758 break;
5759 }
5760 hisidx[type] = newlen - 1;
5761 }
5762 vim_free(history[type]);
5763 history[type] = temp;
5764 }
5765 }
5766 hislen = newlen;
5767 }
5768}
5769
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005771clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005772{
5773 hisptr->hisnum = 0;
5774 hisptr->viminfo = FALSE;
5775 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005776 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005777}
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779/*
5780 * Check if command line 'str' is already in history.
5781 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5782 */
5783 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005784in_history(
5785 int type,
5786 char_u *str,
5787 int move_to_front, /* Move the entry to the front if it exists */
5788 int sep,
5789 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 int i;
5792 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005793 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 if (hisidx[type] < 0)
5796 return FALSE;
5797 i = hisidx[type];
5798 do
5799 {
5800 if (history[type][i].hisstr == NULL)
5801 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005802
5803 /* For search history, check that the separator character matches as
5804 * well. */
5805 p = history[type][i].hisstr;
5806 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005807 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005808 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
5810 if (!move_to_front)
5811 return TRUE;
5812 last_i = i;
5813 break;
5814 }
5815 if (--i < 0)
5816 i = hislen - 1;
5817 } while (i != hisidx[type]);
5818
5819 if (last_i >= 0)
5820 {
5821 str = history[type][i].hisstr;
5822 while (i != hisidx[type])
5823 {
5824 if (++i >= hislen)
5825 i = 0;
5826 history[type][last_i] = history[type][i];
5827 last_i = i;
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005830 history[type][i].viminfo = FALSE;
5831 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005832 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 return TRUE;
5834 }
5835 return FALSE;
5836}
5837
5838/*
5839 * Convert history name (from table above) to its HIST_ equivalent.
5840 * When "name" is empty, return "cmd" history.
5841 * Returns -1 for unknown history name.
5842 */
5843 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005844get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 int i;
5847 int len = (int)STRLEN(name);
5848
5849 /* No argument: use current history. */
5850 if (len == 0)
5851 return hist_char2type(ccline.cmdfirstc);
5852
5853 for (i = 0; history_names[i] != NULL; ++i)
5854 if (STRNICMP(name, history_names[i], len) == 0)
5855 return i;
5856
5857 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5858 return hist_char2type(name[0]);
5859
5860 return -1;
5861}
5862
5863static int last_maptick = -1; /* last seen maptick */
5864
5865/*
5866 * Add the given string to the given history. If the string is already in the
5867 * history then it is moved to the front. "histype" may be one of he HIST_
5868 * values.
5869 */
5870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005871add_to_history(
5872 int histype,
5873 char_u *new_entry,
5874 int in_map, /* consider maptick when inside a mapping */
5875 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 histentry_T *hisptr;
5878 int len;
5879
5880 if (hislen == 0) /* no history */
5881 return;
5882
Bram Moolenaara939e432013-11-09 05:30:26 +01005883 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5884 return;
5885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 /*
5887 * Searches inside the same mapping overwrite each other, so that only
5888 * the last line is kept. Be careful not to remove a line that was moved
5889 * down, only lines that were added.
5890 */
5891 if (histype == HIST_SEARCH && in_map)
5892 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005893 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {
5895 /* Current line is from the same mapping, remove it */
5896 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5897 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005898 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 --hisnum[histype];
5900 if (--hisidx[HIST_SEARCH] < 0)
5901 hisidx[HIST_SEARCH] = hislen - 1;
5902 }
5903 last_maptick = -1;
5904 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005905 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
5907 if (++hisidx[histype] == hislen)
5908 hisidx[histype] = 0;
5909 hisptr = &history[histype][hisidx[histype]];
5910 vim_free(hisptr->hisstr);
5911
5912 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005913 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5915 if (hisptr->hisstr != NULL)
5916 hisptr->hisstr[len + 1] = sep;
5917
5918 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005919 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005920 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 if (histype == HIST_SEARCH && in_map)
5922 last_maptick = maptick;
5923 }
5924}
5925
5926#if defined(FEAT_EVAL) || defined(PROTO)
5927
5928/*
5929 * Get identifier of newest history entry.
5930 * "histype" may be one of the HIST_ values.
5931 */
5932 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005933get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5936 || hisidx[histype] < 0)
5937 return -1;
5938
5939 return history[histype][hisidx[histype]].hisnum;
5940}
5941
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 * Calculate history index from a number:
5944 * num > 0: seen as identifying number of a history entry
5945 * num < 0: relative position in history wrt newest entry
5946 * "histype" may be one of the HIST_ values.
5947 */
5948 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 int i;
5952 histentry_T *hist;
5953 int wrapped = FALSE;
5954
5955 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5956 || (i = hisidx[histype]) < 0 || num == 0)
5957 return -1;
5958
5959 hist = history[histype];
5960 if (num > 0)
5961 {
5962 while (hist[i].hisnum > num)
5963 if (--i < 0)
5964 {
5965 if (wrapped)
5966 break;
5967 i += hislen;
5968 wrapped = TRUE;
5969 }
5970 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5971 return i;
5972 }
5973 else if (-num <= hislen)
5974 {
5975 i += num + 1;
5976 if (i < 0)
5977 i += hislen;
5978 if (hist[i].hisstr != NULL)
5979 return i;
5980 }
5981 return -1;
5982}
5983
5984/*
5985 * Get a history entry by its index.
5986 * "histype" may be one of the HIST_ values.
5987 */
5988 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005989get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 idx = calc_hist_idx(histype, idx);
5992 if (idx >= 0)
5993 return history[histype][idx].hisstr;
5994 else
5995 return (char_u *)"";
5996}
5997
5998/*
5999 * Clear all entries of a history.
6000 * "histype" may be one of the HIST_ values.
6001 */
6002 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006003clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005 int i;
6006 histentry_T *hisptr;
6007
6008 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6009 {
6010 hisptr = history[histype];
6011 for (i = hislen; i--;)
6012 {
6013 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006014 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006015 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 }
6017 hisidx[histype] = -1; /* mark history as cleared */
6018 hisnum[histype] = 0; /* reset identifier counter */
6019 return OK;
6020 }
6021 return FAIL;
6022}
6023
6024/*
6025 * Remove all entries matching {str} from a history.
6026 * "histype" may be one of the HIST_ values.
6027 */
6028 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006029del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
6031 regmatch_T regmatch;
6032 histentry_T *hisptr;
6033 int idx;
6034 int i;
6035 int last;
6036 int found = FALSE;
6037
6038 regmatch.regprog = NULL;
6039 regmatch.rm_ic = FALSE; /* always match case */
6040 if (hislen != 0
6041 && histype >= 0
6042 && histype < HIST_COUNT
6043 && *str != NUL
6044 && (idx = hisidx[histype]) >= 0
6045 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6046 != NULL)
6047 {
6048 i = last = idx;
6049 do
6050 {
6051 hisptr = &history[histype][i];
6052 if (hisptr->hisstr == NULL)
6053 break;
6054 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6055 {
6056 found = TRUE;
6057 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006058 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
6060 else
6061 {
6062 if (i != last)
6063 {
6064 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006065 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 }
6067 if (--last < 0)
6068 last += hislen;
6069 }
6070 if (--i < 0)
6071 i += hislen;
6072 } while (i != idx);
6073 if (history[histype][idx].hisstr == NULL)
6074 hisidx[histype] = -1;
6075 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006076 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 return found;
6078}
6079
6080/*
6081 * Remove an indexed entry from a history.
6082 * "histype" may be one of the HIST_ values.
6083 */
6084 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006085del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 int i, j;
6088
6089 i = calc_hist_idx(histype, idx);
6090 if (i < 0)
6091 return FALSE;
6092 idx = hisidx[histype];
6093 vim_free(history[histype][i].hisstr);
6094
6095 /* When deleting the last added search string in a mapping, reset
6096 * last_maptick, so that the last added search string isn't deleted again.
6097 */
6098 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6099 last_maptick = -1;
6100
6101 while (i != idx)
6102 {
6103 j = (i + 1) % hislen;
6104 history[histype][i] = history[histype][j];
6105 i = j;
6106 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006107 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (--i < 0)
6109 i += hislen;
6110 hisidx[histype] = i;
6111 return TRUE;
6112}
6113
6114#endif /* FEAT_EVAL */
6115
6116#if defined(FEAT_CRYPT) || defined(PROTO)
6117/*
6118 * Very specific function to remove the value in ":set key=val" from the
6119 * history.
6120 */
6121 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006122remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124 char_u *p;
6125 int i;
6126
6127 i = hisidx[HIST_CMD];
6128 if (i < 0)
6129 return;
6130 p = history[HIST_CMD][i].hisstr;
6131 if (p != NULL)
6132 for ( ; *p; ++p)
6133 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6134 {
6135 p = vim_strchr(p + 3, '=');
6136 if (p == NULL)
6137 break;
6138 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006139 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (p[i] == '\\' && p[i + 1])
6141 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006142 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 --p;
6144 }
6145}
6146#endif
6147
6148#endif /* FEAT_CMDHIST */
6149
Bram Moolenaar064154c2016-03-19 22:50:43 +01006150#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006151/*
6152 * Get pointer to the command line info to use. cmdline_paste() may clear
6153 * ccline and put the previous value in prev_ccline.
6154 */
6155 static struct cmdline_info *
6156get_ccline_ptr(void)
6157{
6158 if ((State & CMDLINE) == 0)
6159 return NULL;
6160 if (ccline.cmdbuff != NULL)
6161 return &ccline;
6162 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6163 return &prev_ccline;
6164 return NULL;
6165}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006166#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006167
Bram Moolenaar064154c2016-03-19 22:50:43 +01006168#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006169/*
6170 * Get the current command line in allocated memory.
6171 * Only works when the command line is being edited.
6172 * Returns NULL when something is wrong.
6173 */
6174 char_u *
6175get_cmdline_str(void)
6176{
6177 struct cmdline_info *p = get_ccline_ptr();
6178
6179 if (p == NULL)
6180 return NULL;
6181 return vim_strnsave(p->cmdbuff, p->cmdlen);
6182}
6183
6184/*
6185 * Get the current command line position, counted in bytes.
6186 * Zero is the first position.
6187 * Only works when the command line is being edited.
6188 * Returns -1 when something is wrong.
6189 */
6190 int
6191get_cmdline_pos(void)
6192{
6193 struct cmdline_info *p = get_ccline_ptr();
6194
6195 if (p == NULL)
6196 return -1;
6197 return p->cmdpos;
6198}
6199
6200/*
6201 * Set the command line byte position to "pos". Zero is the first position.
6202 * Only works when the command line is being edited.
6203 * Returns 1 when failed, 0 when OK.
6204 */
6205 int
6206set_cmdline_pos(
6207 int pos)
6208{
6209 struct cmdline_info *p = get_ccline_ptr();
6210
6211 if (p == NULL)
6212 return 1;
6213
6214 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6215 * changed the command line. */
6216 if (pos < 0)
6217 new_cmdpos = 0;
6218 else
6219 new_cmdpos = pos;
6220 return 0;
6221}
6222#endif
6223
6224#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6225/*
6226 * Get the current command-line type.
6227 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6228 * Only works when the command line is being edited.
6229 * Returns NUL when something is wrong.
6230 */
6231 int
6232get_cmdline_type(void)
6233{
6234 struct cmdline_info *p = get_ccline_ptr();
6235
6236 if (p == NULL)
6237 return NUL;
6238 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006239 return
6240# ifdef FEAT_EVAL
6241 (p->input_fn) ? '@' :
6242# endif
6243 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006244 return p->cmdfirstc;
6245}
6246#endif
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6249/*
6250 * Get indices "num1,num2" that specify a range within a list (not a range of
6251 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6252 * Returns OK if parsed successfully, otherwise FAIL.
6253 */
6254 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006255get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 int len;
6258 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006259 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 *str = skipwhite(*str);
6262 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6263 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006264 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 *str += len;
6266 *num1 = (int)num;
6267 first = TRUE;
6268 }
6269 *str = skipwhite(*str);
6270 if (**str == ',') /* parse "to" part of range */
6271 {
6272 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006273 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (len > 0)
6275 {
6276 *num2 = (int)num;
6277 *str = skipwhite(*str + len);
6278 }
6279 else if (!first) /* no number given at all */
6280 return FAIL;
6281 }
6282 else if (first) /* only one number given */
6283 *num2 = *num1;
6284 return OK;
6285}
6286#endif
6287
6288#if defined(FEAT_CMDHIST) || defined(PROTO)
6289/*
6290 * :history command - print a history
6291 */
6292 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006293ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 histentry_T *hist;
6296 int histype1 = HIST_CMD;
6297 int histype2 = HIST_CMD;
6298 int hisidx1 = 1;
6299 int hisidx2 = -1;
6300 int idx;
6301 int i, j, k;
6302 char_u *end;
6303 char_u *arg = eap->arg;
6304
6305 if (hislen == 0)
6306 {
6307 MSG(_("'history' option is zero"));
6308 return;
6309 }
6310
6311 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6312 {
6313 end = arg;
6314 while (ASCII_ISALPHA(*end)
6315 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6316 end++;
6317 i = *end;
6318 *end = NUL;
6319 histype1 = get_histtype(arg);
6320 if (histype1 == -1)
6321 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006322 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 {
6324 histype1 = 0;
6325 histype2 = HIST_COUNT-1;
6326 }
6327 else
6328 {
6329 *end = i;
6330 EMSG(_(e_trailing));
6331 return;
6332 }
6333 }
6334 else
6335 histype2 = histype1;
6336 *end = i;
6337 }
6338 else
6339 end = arg;
6340 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6341 {
6342 EMSG(_(e_trailing));
6343 return;
6344 }
6345
6346 for (; !got_int && histype1 <= histype2; ++histype1)
6347 {
6348 STRCPY(IObuff, "\n # ");
6349 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6350 MSG_PUTS_TITLE(IObuff);
6351 idx = hisidx[histype1];
6352 hist = history[histype1];
6353 j = hisidx1;
6354 k = hisidx2;
6355 if (j < 0)
6356 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6357 if (k < 0)
6358 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6359 if (idx >= 0 && j <= k)
6360 for (i = idx + 1; !got_int; ++i)
6361 {
6362 if (i == hislen)
6363 i = 0;
6364 if (hist[i].hisstr != NULL
6365 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6366 {
6367 msg_putchar('\n');
6368 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6369 hist[i].hisnum);
6370 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6371 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006372 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 else
6374 STRCAT(IObuff, hist[i].hisstr);
6375 msg_outtrans(IObuff);
6376 out_flush();
6377 }
6378 if (i == idx)
6379 break;
6380 }
6381 }
6382}
6383#endif
6384
6385#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006386/*
6387 * Buffers for history read from a viminfo file. Only valid while reading.
6388 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006389static histentry_T *viminfo_history[HIST_COUNT] =
6390 {NULL, NULL, NULL, NULL, NULL};
6391static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6392static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393static int viminfo_add_at_front = FALSE;
6394
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006395static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397/*
6398 * Translate a history type number to the associated character.
6399 */
6400 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006401hist_type2char(
6402 int type,
6403 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 if (type == HIST_CMD)
6406 return ':';
6407 if (type == HIST_SEARCH)
6408 {
6409 if (use_question)
6410 return '?';
6411 else
6412 return '/';
6413 }
6414 if (type == HIST_EXPR)
6415 return '=';
6416 return '@';
6417}
6418
6419/*
6420 * Prepare for reading the history from the viminfo file.
6421 * This allocates history arrays to store the read history lines.
6422 */
6423 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006424prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 int i;
6427 int num;
6428 int type;
6429 int len;
6430
6431 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006432 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (asklen > hislen)
6434 asklen = hislen;
6435
6436 for (type = 0; type < HIST_COUNT; ++type)
6437 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006438 /* Count the number of empty spaces in the history list. Entries read
6439 * from viminfo previously are also considered empty. If there are
6440 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006442 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 num++;
6444 len = asklen;
6445 if (num > len)
6446 len = num;
6447 if (len <= 0)
6448 viminfo_history[type] = NULL;
6449 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006450 viminfo_history[type] = (histentry_T *)lalloc(
6451 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (viminfo_history[type] == NULL)
6453 len = 0;
6454 viminfo_hislen[type] = len;
6455 viminfo_hisidx[type] = 0;
6456 }
6457}
6458
6459/*
6460 * Accept a line from the viminfo, store it in the history array when it's
6461 * new.
6462 */
6463 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006464read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 int type;
6467 long_u len;
6468 char_u *val;
6469 char_u *p;
6470
6471 type = hist_char2type(virp->vir_line[0]);
6472 if (viminfo_hisidx[type] < viminfo_hislen[type])
6473 {
6474 val = viminfo_readstring(virp, 1, TRUE);
6475 if (val != NULL && *val != NUL)
6476 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006477 int sep = (*val == ' ' ? NUL : *val);
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006480 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 {
6482 /* Need to re-allocate to append the separator byte. */
6483 len = STRLEN(val);
6484 p = lalloc(len + 2, TRUE);
6485 if (p != NULL)
6486 {
6487 if (type == HIST_SEARCH)
6488 {
6489 /* Search entry: Move the separator from the first
6490 * column to after the NUL. */
6491 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006492 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 }
6494 else
6495 {
6496 /* Not a search entry: No separator in the viminfo
6497 * file, add a NUL separator. */
6498 mch_memmove(p, val, (size_t)len + 1);
6499 p[len + 1] = NUL;
6500 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006501 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6502 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006503 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6504 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006505 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 }
6507 }
6508 }
6509 vim_free(val);
6510 }
6511 return viminfo_readline(virp);
6512}
6513
Bram Moolenaar07219f92013-04-14 23:19:36 +02006514/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006515 * Accept a new style history line from the viminfo, store it in the history
6516 * array when it's new.
6517 */
6518 void
6519handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006520 garray_T *values,
6521 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006522{
6523 int type;
6524 long_u len;
6525 char_u *val;
6526 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006527 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006528
6529 /* Check the format:
6530 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006531 if (values->ga_len < 4
6532 || vp[0].bv_type != BVAL_NR
6533 || vp[1].bv_type != BVAL_NR
6534 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6535 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006536 return;
6537
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006538 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006539 if (type >= HIST_COUNT)
6540 return;
6541 if (viminfo_hisidx[type] < viminfo_hislen[type])
6542 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006543 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006544 if (val != NULL && *val != NUL)
6545 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006546 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6547 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006548 int idx;
6549 int overwrite = FALSE;
6550
6551 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6552 {
6553 /* If lines were written by an older Vim we need to avoid
6554 * getting duplicates. See if the entry already exists. */
6555 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6556 {
6557 p = viminfo_history[type][idx].hisstr;
6558 if (STRCMP(val, p) == 0
6559 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6560 {
6561 overwrite = TRUE;
6562 break;
6563 }
6564 }
6565
6566 if (!overwrite)
6567 {
6568 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006569 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006570 p = lalloc(len + 2, TRUE);
6571 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006572 else
6573 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006574 if (p != NULL)
6575 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006576 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006577 if (!overwrite)
6578 {
6579 mch_memmove(p, val, (size_t)len + 1);
6580 /* Put the separator after the NUL. */
6581 p[len + 1] = sep;
6582 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006583 viminfo_history[type][idx].hisnum = 0;
6584 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006585 viminfo_hisidx[type]++;
6586 }
6587 }
6588 }
6589 }
6590 }
6591}
6592
6593/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006594 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006595 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006596 static void
6597concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 int idx;
6600 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006601
6602 idx = hisidx[type] + viminfo_hisidx[type];
6603 if (idx >= hislen)
6604 idx -= hislen;
6605 else if (idx < 0)
6606 idx = hislen - 1;
6607 if (viminfo_add_at_front)
6608 hisidx[type] = idx;
6609 else
6610 {
6611 if (hisidx[type] == -1)
6612 hisidx[type] = hislen - 1;
6613 do
6614 {
6615 if (history[type][idx].hisstr != NULL
6616 || history[type][idx].viminfo)
6617 break;
6618 if (++idx == hislen)
6619 idx = 0;
6620 } while (idx != hisidx[type]);
6621 if (idx != hisidx[type] && --idx < 0)
6622 idx = hislen - 1;
6623 }
6624 for (i = 0; i < viminfo_hisidx[type]; i++)
6625 {
6626 vim_free(history[type][idx].hisstr);
6627 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6628 history[type][idx].viminfo = TRUE;
6629 history[type][idx].time_set = viminfo_history[type][i].time_set;
6630 if (--idx < 0)
6631 idx = hislen - 1;
6632 }
6633 idx += 1;
6634 idx %= hislen;
6635 for (i = 0; i < viminfo_hisidx[type]; i++)
6636 {
6637 history[type][idx++].hisnum = ++hisnum[type];
6638 idx %= hislen;
6639 }
6640}
6641
6642#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6643 static int
6644#ifdef __BORLANDC__
6645_RTLENTRYF
6646#endif
6647sort_hist(const void *s1, const void *s2)
6648{
6649 histentry_T *p1 = *(histentry_T **)s1;
6650 histentry_T *p2 = *(histentry_T **)s2;
6651
6652 if (p1->time_set < p2->time_set) return -1;
6653 if (p1->time_set > p2->time_set) return 1;
6654 return 0;
6655}
6656#endif
6657
6658/*
6659 * Merge history lines from viminfo and lines typed in this Vim based on the
6660 * timestamp;
6661 */
6662 static void
6663merge_history(int type)
6664{
6665 int max_len;
6666 histentry_T **tot_hist;
6667 histentry_T *new_hist;
6668 int i;
6669 int len;
6670
6671 /* Make one long list with all entries. */
6672 max_len = hislen + viminfo_hisidx[type];
6673 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6674 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6675 if (tot_hist == NULL || new_hist == NULL)
6676 {
6677 vim_free(tot_hist);
6678 vim_free(new_hist);
6679 return;
6680 }
6681 for (i = 0; i < viminfo_hisidx[type]; i++)
6682 tot_hist[i] = &viminfo_history[type][i];
6683 len = i;
6684 for (i = 0; i < hislen; i++)
6685 if (history[type][i].hisstr != NULL)
6686 tot_hist[len++] = &history[type][i];
6687
6688 /* Sort the list on timestamp. */
6689 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6690
6691 /* Keep the newest ones. */
6692 for (i = 0; i < hislen; i++)
6693 {
6694 if (i < len)
6695 {
6696 new_hist[i] = *tot_hist[i];
6697 tot_hist[i]->hisstr = NULL;
6698 if (new_hist[i].hisnum == 0)
6699 new_hist[i].hisnum = ++hisnum[type];
6700 }
6701 else
6702 clear_hist_entry(&new_hist[i]);
6703 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006704 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006705
6706 /* Free what is not kept. */
6707 for (i = 0; i < viminfo_hisidx[type]; i++)
6708 vim_free(viminfo_history[type][i].hisstr);
6709 for (i = 0; i < hislen; i++)
6710 vim_free(history[type][i].hisstr);
6711 vim_free(history[type]);
6712 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006713 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006714}
6715
6716/*
6717 * Finish reading history lines from viminfo. Not used when writing viminfo.
6718 */
6719 void
6720finish_viminfo_history(vir_T *virp)
6721{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006723 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725 for (type = 0; type < HIST_COUNT; ++type)
6726 {
6727 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006728 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006729
6730 if (merge)
6731 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006733 concat_history(type);
6734
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 vim_free(viminfo_history[type]);
6736 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006737 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739}
6740
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006741/*
6742 * Write history to viminfo file in "fp".
6743 * When "merge" is TRUE merge history lines with a previously read viminfo
6744 * file, data is in viminfo_history[].
6745 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006748write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 int i;
6751 int type;
6752 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006753 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
6755 init_history();
6756 if (hislen == 0)
6757 return;
6758 for (type = 0; type < HIST_COUNT; ++type)
6759 {
6760 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6761 if (num_saved == 0)
6762 continue;
6763 if (num_saved < 0) /* Use default */
6764 num_saved = hislen;
6765 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6766 type == HIST_CMD ? _("Command Line") :
6767 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006768 type == HIST_EXPR ? _("Expression") :
6769 type == HIST_INPUT ? _("Input Line") :
6770 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 if (num_saved > hislen)
6772 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006773
6774 /*
6775 * Merge typed and viminfo history:
6776 * round 1: history of typed commands.
6777 * round 2: history from recently read viminfo.
6778 */
6779 for (round = 1; round <= 2; ++round)
6780 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006781 if (round == 1)
6782 /* start at newest entry, somewhere in the list */
6783 i = hisidx[type];
6784 else if (viminfo_hisidx[type] > 0)
6785 /* start at newest entry, first in the list */
6786 i = 0;
6787 else
6788 /* empty list */
6789 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006790 if (i >= 0)
6791 while (num_saved > 0
6792 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006794 char_u *p;
6795 time_t timestamp;
6796 int c = NUL;
6797
6798 if (round == 1)
6799 {
6800 p = history[type][i].hisstr;
6801 timestamp = history[type][i].time_set;
6802 }
6803 else
6804 {
6805 p = viminfo_history[type] == NULL ? NULL
6806 : viminfo_history[type][i].hisstr;
6807 timestamp = viminfo_history[type] == NULL ? 0
6808 : viminfo_history[type][i].time_set;
6809 }
6810
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006811 if (p != NULL && (round == 2
6812 || !merge
6813 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006815 --num_saved;
6816 fputc(hist_type2char(type, TRUE), fp);
6817 /* For the search history: put the separator in the
6818 * second column; use a space if there isn't one. */
6819 if (type == HIST_SEARCH)
6820 {
6821 c = p[STRLEN(p) + 1];
6822 putc(c == NUL ? ' ' : c, fp);
6823 }
6824 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006825
6826 {
6827 char cbuf[NUMBUFLEN];
6828
6829 /* New style history with a bar line. Format:
6830 * |{bartype},{histtype},{timestamp},{separator},"text" */
6831 if (c == NUL)
6832 cbuf[0] = NUL;
6833 else
6834 sprintf(cbuf, "%d", c);
6835 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6836 type, (long)timestamp, cbuf);
6837 barline_writestring(fp, p, LSIZE - 20);
6838 putc('\n', fp);
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006841 if (round == 1)
6842 {
6843 /* Decrement index, loop around and stop when back at
6844 * the start. */
6845 if (--i < 0)
6846 i = hislen - 1;
6847 if (i == hisidx[type])
6848 break;
6849 }
6850 else
6851 {
6852 /* Increment index. Stop at the end in the while. */
6853 ++i;
6854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006856 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006857 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006858 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006859 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006860 vim_free(viminfo_history[type]);
6861 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006862 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864}
6865#endif /* FEAT_VIMINFO */
6866
6867#if defined(FEAT_FKMAP) || defined(PROTO)
6868/*
6869 * Write a character at the current cursor+offset position.
6870 * It is directly written into the command buffer block.
6871 */
6872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006873cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
6875 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6876 {
6877 EMSG(_("E198: cmd_pchar beyond the command length"));
6878 return;
6879 }
6880 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6881 ccline.cmdbuff[ccline.cmdlen] = NUL;
6882}
6883
6884 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006885cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6888 {
6889 /* EMSG(_("cmd_gchar beyond the command length")); */
6890 return NUL;
6891 }
6892 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6893}
6894#endif
6895
6896#if defined(FEAT_CMDWIN) || defined(PROTO)
6897/*
6898 * Open a window on the current command line and history. Allow editing in
6899 * the window. Returns when the window is closed.
6900 * Returns:
6901 * CR if the command is to be executed
6902 * Ctrl_C if it is to be abandoned
6903 * K_IGNORE if editing continues
6904 */
6905 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006906open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
6908 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006909 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006911 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 win_T *wp;
6913 int i;
6914 linenr_T lnum;
6915 int histtype;
6916 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 int save_restart_edit = restart_edit;
6918 int save_State = State;
6919 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006920#ifdef FEAT_RIGHTLEFT
6921 int save_cmdmsg_rl = cmdmsg_rl;
6922#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006923#ifdef FEAT_FOLDING
6924 int save_KeyTyped;
6925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 /* Can't do this recursively. Can't do it when typing a password. */
6928 if (cmdwin_type != 0
6929# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6930 || cmdline_star > 0
6931# endif
6932 )
6933 {
6934 beep_flush();
6935 return K_IGNORE;
6936 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006937 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939 /* Save current window sizes. */
6940 win_size_save(&winsizes);
6941
6942# ifdef FEAT_AUTOCMD
6943 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006944 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006946 /* don't use a new tab page */
6947 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006948 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 /* Create a window for the command-line buffer. */
6951 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6952 {
6953 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006954# ifdef FEAT_AUTOCMD
6955 unblock_autocmds();
6956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 return K_IGNORE;
6958 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006959 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006962 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006963 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006966#ifdef FEAT_FOLDING
6967 curwin->w_p_fen = FALSE;
6968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006970 curwin->w_p_rl = cmdmsg_rl;
6971 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006973 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
6975# ifdef FEAT_AUTOCMD
6976 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006977 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006978 /* But don't allow switching to another buffer. */
6979 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980# endif
6981
Bram Moolenaar46152342005-09-07 21:18:43 +00006982 /* Showing the prompt may have set need_wait_return, reset it. */
6983 need_wait_return = FALSE;
6984
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006985 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6987 {
6988 if (p_wc == TAB)
6989 {
6990 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6991 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6992 }
6993 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6994 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006995# ifdef FEAT_AUTOCMD
6996 --curbuf_lock;
6997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006999 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7000 * sets 'textwidth' to 78). */
7001 curbuf->b_p_tw = 0;
7002
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 /* Fill the buffer with the history. */
7004 init_history();
7005 if (hislen > 0)
7006 {
7007 i = hisidx[histtype];
7008 if (i >= 0)
7009 {
7010 lnum = 0;
7011 do
7012 {
7013 if (++i == hislen)
7014 i = 0;
7015 if (history[histtype][i].hisstr != NULL)
7016 ml_append(lnum++, history[histtype][i].hisstr,
7017 (colnr_T)0, FALSE);
7018 }
7019 while (i != hisidx[histtype]);
7020 }
7021 }
7022
7023 /* Replace the empty last line with the current command-line and put the
7024 * cursor there. */
7025 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7026 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7027 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007028 changed_line_abv_curs();
7029 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007030 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031
7032 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007033 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 /* No Ex mode here! */
7036 exmode_active = 0;
7037
7038 State = NORMAL;
7039# ifdef FEAT_MOUSE
7040 setmouse();
7041# endif
7042
7043# ifdef FEAT_AUTOCMD
7044 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007045 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007046 if (restart_edit != 0) /* autocmd with ":startinsert" */
7047 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048# endif
7049
7050 i = RedrawingDisabled;
7051 RedrawingDisabled = 0;
7052
7053 /*
7054 * Call the main loop until <CR> or CTRL-C is typed.
7055 */
7056 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007057 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058
7059 RedrawingDisabled = i;
7060
7061# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007062
7063# ifdef FEAT_FOLDING
7064 save_KeyTyped = KeyTyped;
7065# endif
7066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007068 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007069
7070# ifdef FEAT_FOLDING
7071 /* Restore KeyTyped in case it is modified by autocommands */
7072 KeyTyped = save_KeyTyped;
7073# endif
7074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075# endif
7076
Bram Moolenaarccc18222007-05-10 18:25:20 +00007077 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007078 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 cmdwin_type = 0;
7080
7081 exmode_active = save_exmode;
7082
Bram Moolenaarf9821062008-06-20 16:31:07 +00007083 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007085 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
7087 cmdwin_result = Ctrl_C;
7088 EMSG(_("E199: Active window or buffer deleted"));
7089 }
7090 else
7091 {
7092# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7093 /* autocmds may abort script processing */
7094 if (aborting() && cmdwin_result != K_IGNORE)
7095 cmdwin_result = Ctrl_C;
7096# endif
7097 /* Set the new command line from the cmdline buffer. */
7098 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007099 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007101 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7102
7103 if (histtype == HIST_CMD)
7104 {
7105 /* Execute the command directly. */
7106 ccline.cmdbuff = vim_strsave((char_u *)p);
7107 cmdwin_result = CAR;
7108 }
7109 else
7110 {
7111 /* First need to cancel what we were doing. */
7112 ccline.cmdbuff = NULL;
7113 stuffcharReadbuff(':');
7114 stuffReadbuff((char_u *)p);
7115 stuffcharReadbuff(CAR);
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 }
7118 else if (cmdwin_result == K_XF2) /* :qa typed */
7119 {
7120 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7121 cmdwin_result = CAR;
7122 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007123 else if (cmdwin_result == Ctrl_C)
7124 {
7125 /* :q or :close, don't execute any command
7126 * and don't modify the cmd window. */
7127 ccline.cmdbuff = NULL;
7128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 else
7130 ccline.cmdbuff = vim_strsave(ml_get_curline());
7131 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007132 {
7133 ccline.cmdbuff = vim_strsave((char_u *)"");
7134 ccline.cmdlen = 0;
7135 ccline.cmdbufflen = 1;
7136 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 else
7140 {
7141 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7142 ccline.cmdbufflen = ccline.cmdlen + 1;
7143 ccline.cmdpos = curwin->w_cursor.col;
7144 if (ccline.cmdpos > ccline.cmdlen)
7145 ccline.cmdpos = ccline.cmdlen;
7146 if (cmdwin_result == K_IGNORE)
7147 {
7148 set_cmdspos_cursor();
7149 redrawcmd();
7150 }
7151 }
7152
7153# ifdef FEAT_AUTOCMD
7154 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007155 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007157# ifdef FEAT_CONCEAL
7158 /* Avoid command-line window first character being concealed. */
7159 curwin->w_p_cole = 0;
7160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007162 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 win_goto(old_curwin);
7164 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007165
7166 /* win_close() may have already wiped the buffer when 'bh' is
7167 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007168 if (bufref_valid(&bufref))
7169 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
7171 /* Restore window sizes. */
7172 win_size_restore(&winsizes);
7173
7174# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007175 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176# endif
7177 }
7178
7179 ga_clear(&winsizes);
7180 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007181# ifdef FEAT_RIGHTLEFT
7182 cmdmsg_rl = save_cmdmsg_rl;
7183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
7185 State = save_State;
7186# ifdef FEAT_MOUSE
7187 setmouse();
7188# endif
7189
7190 return cmdwin_result;
7191}
7192#endif /* FEAT_CMDWIN */
7193
7194/*
7195 * Used for commands that either take a simple command string argument, or:
7196 * cmd << endmarker
7197 * {script}
7198 * endmarker
7199 * Returns a pointer to allocated memory with {script} or NULL.
7200 */
7201 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007202script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 char_u *theline;
7205 char *end_pattern = NULL;
7206 char dot[] = ".";
7207 garray_T ga;
7208
7209 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7210 return NULL;
7211
7212 ga_init2(&ga, 1, 0x400);
7213
7214 if (cmd[2] != NUL)
7215 end_pattern = (char *)skipwhite(cmd + 2);
7216 else
7217 end_pattern = dot;
7218
7219 for (;;)
7220 {
7221 theline = eap->getline(
7222#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007223 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224#endif
7225 NUL, eap->cookie, 0);
7226
7227 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007228 {
7229 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233 ga_concat(&ga, theline);
7234 ga_append(&ga, '\n');
7235 vim_free(theline);
7236 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007237 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
7239 return (char_u *)ga.ga_data;
7240}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007241
7242#ifdef FEAT_SEARCH_EXTRA
7243 static void
7244set_search_match(pos_T *t)
7245{
7246 /*
7247 * First move cursor to end of match, then to the start. This
7248 * moves the whole match onto the screen when 'nowrap' is set.
7249 */
7250 t->lnum += search_match_lines;
7251 t->col = search_match_endcol;
7252 if (t->lnum > curbuf->b_ml.ml_line_count)
7253 {
7254 t->lnum = curbuf->b_ml.ml_line_count;
7255 coladvance((colnr_T)MAXCOL);
7256 }
7257}
7258#endif