blob: 837d29998fcff0bdee000c834a90243cd4a92b29 [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:
Bram Moolenaar153b7042018-01-31 15:48:32 +01001954#ifdef FEAT_AUTOCMD
1955 /* Trigger CmdlineChanged autocommands. */
1956 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
1957#endif
1958
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959#ifdef FEAT_SEARCH_EXTRA
1960 /*
1961 * 'incsearch' highlighting.
1962 */
1963 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1964 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001965 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001966#ifdef FEAT_RELTIME
1967 proftime_T tm;
1968#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 /* if there is a character waiting, search and redraw later */
1971 if (char_avail())
1972 {
1973 incsearch_postponed = TRUE;
1974 continue;
1975 }
1976 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001977 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001978 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 /* If there is no command line, don't do anything */
1981 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 i = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001984 SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001985 redraw_all_later(SOME_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 else
1988 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001989 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 cursor_off(); /* so the user knows we're busy */
1991 out_flush();
1992 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001993#ifdef FEAT_RELTIME
1994 /* Set the time limit to half a second. */
1995 profile_setlimit(500L, &tm);
1996#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001997 if (!p_hls)
1998 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002000 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002001#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002002 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002003#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002004 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002005#endif
2006 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 --emsg_off;
2008 /* if interrupted while searching, behave like it failed */
2009 if (got_int)
2010 {
2011 (void)vpeekc(); /* remove <C-C> from input stream */
2012 got_int = FALSE; /* don't abandon the command line */
2013 i = 0;
2014 }
2015 else if (char_avail())
2016 /* cancelled searching because a char was typed */
2017 incsearch_postponed = TRUE;
2018 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002019 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 highlight_match = TRUE; /* highlight position */
2021 else
2022 highlight_match = FALSE; /* remove highlight */
2023
2024 /* first restore the old curwin values, so the screen is
2025 * positioned in the same way as the actual search command */
2026 curwin->w_leftcol = old_leftcol;
2027 curwin->w_topline = old_topline;
2028# ifdef FEAT_DIFF
2029 curwin->w_topfill = old_topfill;
2030# endif
2031 curwin->w_botline = old_botline;
2032 changed_cline_bef_curs();
2033 update_topline();
2034
2035 if (i != 0)
2036 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002037 pos_T save_pos = curwin->w_cursor;
2038
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002039 match_start = curwin->w_cursor;
2040 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002042 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002043 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002044 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002046 else
2047 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002048
Bram Moolenaar66216052017-12-16 16:33:44 +01002049 /* Disable 'hlsearch' highlighting if the pattern matches
2050 * everything. Avoids a flash when typing "foo\|". */
2051 if (empty_pattern(ccline.cmdbuff))
2052 SET_NO_HLSEARCH(TRUE);
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002055 /* May redraw the status line to show the cursor position. */
2056 if (p_ru && curwin->w_status_height > 0)
2057 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002059 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002060 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002061 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002062 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002063
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002064 /* Leave it at the end to make CTRL-R CTRL-W work. */
2065 if (i != 0)
2066 curwin->w_cursor = end_pos;
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 msg_starthere();
2069 redrawcmdline();
2070 did_incsearch = TRUE;
2071 }
2072#else /* FEAT_SEARCH_EXTRA */
2073 ;
2074#endif
2075
2076#ifdef FEAT_RIGHTLEFT
2077 if (cmdmsg_rl
2078# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002079 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080# endif
2081 )
2082 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002083 * right-left typing. Not efficient, but it works.
2084 * Do it only when there are no characters left to read
2085 * to avoid useless intermediate redraws. */
2086 if (vpeekc() == NUL)
2087 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#endif
2089 }
2090
2091returncmd:
2092
2093#ifdef FEAT_RIGHTLEFT
2094 cmdmsg_rl = FALSE;
2095#endif
2096
2097#ifdef FEAT_FKMAP
2098 cmd_fkmap = 0;
2099#endif
2100
2101 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002102 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
2104#ifdef FEAT_SEARCH_EXTRA
2105 if (did_incsearch)
2106 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002107 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002108 curwin->w_cursor = save_cursor;
2109 else
2110 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002111 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002112 {
2113 /* put the '" mark at the original position */
2114 curwin->w_cursor = save_cursor;
2115 setpcmark();
2116 }
2117 curwin->w_cursor = search_start;
2118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 curwin->w_curswant = old_curswant;
2120 curwin->w_leftcol = old_leftcol;
2121 curwin->w_topline = old_topline;
2122# ifdef FEAT_DIFF
2123 curwin->w_topfill = old_topfill;
2124# endif
2125 curwin->w_botline = old_botline;
2126 highlight_match = FALSE;
2127 validate_cursor(); /* needed for TAB */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01002128 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130#endif
2131
2132 if (ccline.cmdbuff != NULL)
2133 {
2134 /*
2135 * Put line in history buffer (":" and "=" only when it was typed).
2136 */
2137#ifdef FEAT_CMDHIST
2138 if (ccline.cmdlen && firstc != NUL
2139 && (some_key_typed || histype == HIST_SEARCH))
2140 {
2141 add_to_history(histype, ccline.cmdbuff, TRUE,
2142 histype == HIST_SEARCH ? firstc : NUL);
2143 if (firstc == ':')
2144 {
2145 vim_free(new_last_cmdline);
2146 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2147 }
2148 }
2149#endif
2150
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002151 if (gotesc)
2152 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154
2155 /*
2156 * If the screen was shifted up, redraw the whole screen (later).
2157 * If the line is too long, clear it, so ruler and shown command do
2158 * not get printed in the middle of it.
2159 */
2160 msg_check();
2161 msg_scroll = save_msg_scroll;
2162 redir_off = FALSE;
2163
2164 /* When the command line was typed, no need for a wait-return prompt. */
2165 if (some_key_typed)
2166 need_wait_return = FALSE;
2167
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002168#ifdef FEAT_AUTOCMD
2169 /* Trigger CmdlineLeave autocommands. */
2170 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
2171#endif
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 State = save_State;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01002174#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2176 im_save_status(b_im_ptr);
2177 im_set_active(FALSE);
2178#endif
2179#ifdef FEAT_MOUSE
2180 setmouse();
2181#endif
2182#ifdef CURSOR_SHAPE
2183 ui_cursor_shape(); /* may show different cursor shape */
2184#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002185 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002187 {
2188 char_u *p = ccline.cmdbuff;
2189
2190 /* Make ccline empty, getcmdline() may try to use it. */
2191 ccline.cmdbuff = NULL;
2192 return p;
2193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194}
2195
2196#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2197/*
2198 * Get a command line with a prompt.
2199 * This is prepared to be called recursively from getcmdline() (e.g. by
2200 * f_input() when evaluating an expression from CTRL-R =).
2201 * Returns the command line in allocated memory, or NULL.
2202 */
2203 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002204getcmdline_prompt(
2205 int firstc,
2206 char_u *prompt, /* command line prompt */
2207 int attr, /* attributes for prompt */
2208 int xp_context, /* type of expansion */
2209 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211 char_u *s;
2212 struct cmdline_info save_ccline;
2213 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002214 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002216 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 ccline.cmdprompt = prompt;
2218 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002219# ifdef FEAT_EVAL
2220 ccline.xp_context = xp_context;
2221 ccline.xp_arg = xp_arg;
2222 ccline.input_fn = (firstc == '@');
2223# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002224 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002226 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002227 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002228 /* Restore msg_col, the prompt from input() may have changed it.
2229 * But only if called recursively and the commandline is therefore being
2230 * restored to an old one; if not, the input() prompt stays on the screen,
2231 * so we need its modified msg_col left intact. */
2232 if (ccline.cmdbuff != NULL)
2233 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 return s;
2236}
2237#endif
2238
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002239/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002240 * Return TRUE when the text must not be changed and we can't switch to
2241 * another window or buffer. Used when editing the command line, evaluating
2242 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002243 */
2244 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002245text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002246{
2247#ifdef FEAT_CMDWIN
2248 if (cmdwin_type != 0)
2249 return TRUE;
2250#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002251 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002252}
2253
2254/*
2255 * Give an error message for a command that isn't allowed while the cmdline
2256 * window is open or editing the cmdline in another way.
2257 */
2258 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002259text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002260{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002261 EMSG(_(get_text_locked_msg()));
2262}
2263
2264 char_u *
2265get_text_locked_msg(void)
2266{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002267#ifdef FEAT_CMDWIN
2268 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002269 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002270#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002271 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002272}
2273
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002274#if defined(FEAT_AUTOCMD) || defined(PROTO)
2275/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002276 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2277 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002278 */
2279 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002280curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002281{
2282 if (curbuf_lock > 0)
2283 {
2284 EMSG(_("E788: Not allowed to edit another buffer now"));
2285 return TRUE;
2286 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002287 return allbuf_locked();
2288}
2289
2290/*
2291 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2292 * message.
2293 */
2294 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002295allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002296{
2297 if (allbuf_lock > 0)
2298 {
2299 EMSG(_("E811: Not allowed to change buffer information now"));
2300 return TRUE;
2301 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002302 return FALSE;
2303}
2304#endif
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002307cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2310 if (cmdline_star > 0) /* showing '*', always 1 position */
2311 return 1;
2312#endif
2313 return ptr2cells(ccline.cmdbuff + idx);
2314}
2315
2316/*
2317 * Compute the offset of the cursor on the command line for the prompt and
2318 * indent.
2319 */
2320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002321set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002323 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 ccline.cmdspos = 1 + ccline.cmdindent;
2325 else
2326 ccline.cmdspos = 0 + ccline.cmdindent;
2327}
2328
2329/*
2330 * Compute the screen position for the cursor on the command line.
2331 */
2332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002333set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 int i, m, c;
2336
2337 set_cmdspos();
2338 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002341 if (m < 0) /* overflow, Columns or Rows at weird value */
2342 m = MAXCOL;
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 else
2345 m = MAXCOL;
2346 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2347 {
2348 c = cmdline_charsize(i);
2349#ifdef FEAT_MBYTE
2350 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2351 if (has_mbyte)
2352 correct_cmdspos(i, c);
2353#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002354 /* If the cmdline doesn't fit, show cursor on last visible char.
2355 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if ((ccline.cmdspos += c) >= m)
2357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 ccline.cmdspos -= c;
2359 break;
2360 }
2361#ifdef FEAT_MBYTE
2362 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002363 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#endif
2365 }
2366}
2367
2368#ifdef FEAT_MBYTE
2369/*
2370 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2371 * character that doesn't fit, so that a ">" must be displayed.
2372 */
2373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002374correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002376 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2378 && ccline.cmdspos % Columns + cells > Columns)
2379 ccline.cmdspos++;
2380}
2381#endif
2382
2383/*
2384 * Get an Ex command line for the ":" command.
2385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002387getexline(
2388 int c, /* normally ':', NUL for ":append" */
2389 void *cookie UNUSED,
2390 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392 /* When executing a register, remove ':' that's in front of each line. */
2393 if (exec_from_reg && vpeekc() == ':')
2394 (void)vgetc();
2395 return getcmdline(c, 1L, indent);
2396}
2397
2398/*
2399 * Get an Ex command line for Ex mode.
2400 * In Ex mode we only use the OS supplied line editing features and no
2401 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002405getexmodeline(
2406 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002407 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002408 void *cookie UNUSED,
2409 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002411 garray_T line_ga;
2412 char_u *pend;
2413 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002414 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002415 int escaped = FALSE; /* CTRL-V typed */
2416 int vcol = 0;
2417 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002418 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002419 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421 /* Switch cursor on now. This avoids that it happens after the "\n", which
2422 * confuses the system function that computes tabstops. */
2423 cursor_on();
2424
2425 /* always start in column 0; write a newline if necessary */
2426 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002427 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002429 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002431 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002432 if (p_prompt)
2433 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 while (indent-- > 0)
2435 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438
2439 ga_init2(&line_ga, 1, 30);
2440
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002441 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002442 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002443 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002444 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002445 while (indent >= 8)
2446 {
2447 ga_append(&line_ga, TAB);
2448 msg_puts((char_u *)" ");
2449 indent -= 8;
2450 }
2451 while (indent-- > 0)
2452 {
2453 ga_append(&line_ga, ' ');
2454 msg_putchar(' ');
2455 }
2456 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002457 ++no_mapping;
2458 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 /*
2461 * Get the line, one character at a time.
2462 */
2463 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002464 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002466 long sw;
2467 char_u *s;
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (ga_grow(&line_ga, 40) == FAIL)
2470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaarba748c82017-02-26 14:00:07 +01002472 /*
2473 * Get one character at a time.
2474 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002475 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002476
2477 /* Check for a ":normal" command and no more characters left. */
2478 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2479 c1 = '\n';
2480 else
2481 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002484 * Handle line editing.
2485 * Previously this was left to the system, putting the terminal in
2486 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002488 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002490 msg_putchar('\n');
2491 break;
2492 }
2493
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002494 if (c1 == K_PS)
2495 {
2496 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2497 goto redraw;
2498 }
2499
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002500 if (!escaped)
2501 {
2502 /* CR typed means "enter", which is NL */
2503 if (c1 == '\r')
2504 c1 = '\n';
2505
2506 if (c1 == BS || c1 == K_BS
2507 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002509 if (line_ga.ga_len > 0)
2510 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002511#ifdef FEAT_MBYTE
2512 if (has_mbyte)
2513 {
2514 p = (char_u *)line_ga.ga_data;
2515 p[line_ga.ga_len] = NUL;
2516 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2517 line_ga.ga_len -= len;
2518 }
2519 else
2520#endif
2521 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002522 goto redraw;
2523 }
2524 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002527 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002529 msg_col = startcol;
2530 msg_clr_eos();
2531 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002532 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002533 }
2534
2535 if (c1 == Ctrl_T)
2536 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002537 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002538 p = (char_u *)line_ga.ga_data;
2539 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002540 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002541 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002542add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002543 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002545 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002546 p = (char_u *)line_ga.ga_data;
2547 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002548 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2549 *s = ' ';
2550 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002552redraw:
2553 /* redraw the line */
2554 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002556 p = (char_u *)line_ga.ga_data;
2557 p[line_ga.ga_len] = NUL;
2558 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002560 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002562 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002564 msg_putchar(' ');
2565 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002566 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002570 len = MB_PTR2LEN(p);
2571 msg_outtrans_len(p, len);
2572 vcol += ptr2cells(p);
2573 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002577 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002578 continue;
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002581 if (c1 == Ctrl_D)
2582 {
2583 /* Delete one shiftwidth. */
2584 p = (char_u *)line_ga.ga_data;
2585 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002587 if (prev_char == '^')
2588 ex_keep_indent = TRUE;
2589 indent = 0;
2590 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592 else
2593 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002594 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002595 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002596 if (indent > 0)
2597 {
2598 --indent;
2599 indent -= indent % get_sw_value(curbuf);
2600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002602 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002603 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002604 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002605 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2606 --line_ga.ga_len;
2607 }
2608 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002610
2611 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2612 {
2613 escaped = TRUE;
2614 continue;
2615 }
2616
2617 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2618 if (IS_SPECIAL(c1))
2619 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002621
2622 if (IS_SPECIAL(c1))
2623 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002624#ifdef FEAT_MBYTE
2625 if (has_mbyte)
2626 len = (*mb_char2bytes)(c1,
2627 (char_u *)line_ga.ga_data + line_ga.ga_len);
2628 else
2629#endif
2630 {
2631 len = 1;
2632 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2633 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002634 if (c1 == '\n')
2635 msg_putchar('\n');
2636 else if (c1 == TAB)
2637 {
2638 /* Don't use chartabsize(), 'ts' can be different */
2639 do
2640 {
2641 msg_putchar(' ');
2642 } while (++vcol % 8);
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002647 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002648 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002650 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002651 escaped = FALSE;
2652
2653 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002654 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002655
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002656 /* We are done when a NL is entered, but not when it comes after an
2657 * odd number of backslashes, that results in a NUL. */
2658 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002660 int bcount = 0;
2661
2662 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2663 ++bcount;
2664
2665 if (bcount > 0)
2666 {
2667 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2668 * "\NL", etc. */
2669 line_ga.ga_len -= (bcount + 1) / 2;
2670 pend -= (bcount + 1) / 2;
2671 pend[-1] = '\n';
2672 }
2673
2674 if ((bcount & 1) == 0)
2675 {
2676 --line_ga.ga_len;
2677 --pend;
2678 *pend = NUL;
2679 break;
2680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 }
2683
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002684 --no_mapping;
2685 --allow_keys;
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 /* make following messages go to the next line */
2688 msg_didout = FALSE;
2689 msg_col = 0;
2690 if (msg_row < Rows - 1)
2691 ++msg_row;
2692 emsg_on_display = FALSE; /* don't want ui_delay() */
2693
2694 if (got_int)
2695 ga_clear(&line_ga);
2696
2697 return (char_u *)line_ga.ga_data;
2698}
2699
Bram Moolenaare344bea2005-09-01 20:46:49 +00002700# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2701 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702/*
2703 * Return TRUE if ccline.overstrike is on.
2704 */
2705 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002706cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 return ccline.overstrike;
2709}
2710
2711/*
2712 * Return TRUE if the cursor is at the end of the cmdline.
2713 */
2714 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002715cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
2717 return (ccline.cmdpos >= ccline.cmdlen);
2718}
2719#endif
2720
Bram Moolenaar9372a112005-12-06 19:59:18 +00002721#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722/*
2723 * Return the virtual column number at the current cursor position.
2724 * This is used by the IM code to obtain the start of the preedit string.
2725 */
2726 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002727cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2730 return MAXCOL;
2731
2732# ifdef FEAT_MBYTE
2733 if (has_mbyte)
2734 {
2735 colnr_T col;
2736 int i = 0;
2737
2738 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002739 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 return col;
2742 }
2743 else
2744# endif
2745 return ccline.cmdpos;
2746}
2747#endif
2748
2749#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2750/*
2751 * If part of the command line is an IM preedit string, redraw it with
2752 * IM feedback attributes. The cursor position is restored after drawing.
2753 */
2754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002755redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 if ((State & CMDLINE)
2758 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002759 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 && !p_imdisable
2761 && im_is_preediting())
2762 {
2763 int cmdpos = 0;
2764 int cmdspos;
2765 int old_row;
2766 int old_col;
2767 colnr_T col;
2768
2769 old_row = msg_row;
2770 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002771 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773# ifdef FEAT_MBYTE
2774 if (has_mbyte)
2775 {
2776 for (col = 0; col < preedit_start_col
2777 && cmdpos < ccline.cmdlen; ++col)
2778 {
2779 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002780 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
2782 }
2783 else
2784# endif
2785 {
2786 cmdspos += preedit_start_col;
2787 cmdpos += preedit_start_col;
2788 }
2789
2790 msg_row = cmdline_row + (cmdspos / (int)Columns);
2791 msg_col = cmdspos % (int)Columns;
2792 if (msg_row >= Rows)
2793 msg_row = Rows - 1;
2794
2795 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2796 {
2797 int char_len;
2798 int char_attr;
2799
2800 char_attr = im_get_feedback_attr(col);
2801 if (char_attr < 0)
2802 break; /* end of preedit string */
2803
2804# ifdef FEAT_MBYTE
2805 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002806 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else
2808# endif
2809 char_len = 1;
2810
2811 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2812 cmdpos += char_len;
2813 }
2814
2815 msg_row = old_row;
2816 msg_col = old_col;
2817 }
2818}
2819#endif /* FEAT_XIM && FEAT_GUI_GTK */
2820
2821/*
2822 * Allocate a new command line buffer.
2823 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2824 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2825 */
2826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002827alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
2829 /*
2830 * give some extra space to avoid having to allocate all the time
2831 */
2832 if (len < 80)
2833 len = 100;
2834 else
2835 len += 20;
2836
2837 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2838 ccline.cmdbufflen = len;
2839}
2840
2841/*
2842 * Re-allocate the command line to length len + something extra.
2843 * return FAIL for failure, OK otherwise
2844 */
2845 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002846realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
2848 char_u *p;
2849
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002850 if (len < ccline.cmdbufflen)
2851 return OK; /* no need to resize */
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 p = ccline.cmdbuff;
2854 alloc_cmdbuff(len); /* will get some more */
2855 if (ccline.cmdbuff == NULL) /* out of memory */
2856 {
2857 ccline.cmdbuff = p; /* keep the old one */
2858 return FAIL;
2859 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002860 /* There isn't always a NUL after the command, but it may need to be
2861 * there, thus copy up to the NUL and add a NUL. */
2862 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2863 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002865
2866 if (ccline.xpc != NULL
2867 && ccline.xpc->xp_pattern != NULL
2868 && ccline.xpc->xp_context != EXPAND_NOTHING
2869 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2870 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002871 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002872
2873 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2874 * to point into the newly allocated memory. */
2875 if (i >= 0 && i <= ccline.cmdlen)
2876 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2877 }
2878
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 return OK;
2880}
2881
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002882#if defined(FEAT_ARABIC) || defined(PROTO)
2883static char_u *arshape_buf = NULL;
2884
2885# if defined(EXITFREE) || defined(PROTO)
2886 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002887free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888{
2889 vim_free(arshape_buf);
2890}
2891# endif
2892#endif
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894/*
2895 * Draw part of the cmdline at the current cursor position. But draw stars
2896 * when cmdline_star is TRUE.
2897 */
2898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002899draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2902 int i;
2903
2904 if (cmdline_star > 0)
2905 for (i = 0; i < len; ++i)
2906 {
2907 msg_putchar('*');
2908# ifdef FEAT_MBYTE
2909 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002910 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911# endif
2912 }
2913 else
2914#endif
2915#ifdef FEAT_ARABIC
2916 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 static int buflen = 0;
2919 char_u *p;
2920 int j;
2921 int newlen = 0;
2922 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002923 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 int prev_c = 0;
2925 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002926 int u8c;
2927 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 /*
2931 * Do arabic shaping into a temporary buffer. This is very
2932 * inefficient!
2933 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002934 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
2936 /* Re-allocate the buffer. We keep it around to avoid a lot of
2937 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002938 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002939 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002940 arshape_buf = alloc(buflen);
2941 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 return; /* out of memory */
2943 }
2944
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002945 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2946 {
2947 /* Prepend a space to draw the leading composing char on. */
2948 arshape_buf[0] = ' ';
2949 newlen = 1;
2950 }
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 for (j = start; j < start + len; j += mb_l)
2953 {
2954 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002955 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002956 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (ARABIC_CHAR(u8c))
2958 {
2959 /* Do Arabic shaping. */
2960 if (cmdmsg_rl)
2961 {
2962 /* displaying from right to left */
2963 pc = prev_c;
2964 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002965 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 if (j + mb_l >= start + len)
2967 nc = NUL;
2968 else
2969 nc = utf_ptr2char(p + mb_l);
2970 }
2971 else
2972 {
2973 /* displaying from left to right */
2974 if (j + mb_l >= start + len)
2975 pc = NUL;
2976 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002977 {
2978 int pcc[MAX_MCO];
2979
2980 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002982 pc1 = pcc[0];
2983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 nc = prev_c;
2985 }
2986 prev_c = u8c;
2987
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002988 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002990 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002991 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002993 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2994 if (u8cc[1] != 0)
2995 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002996 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998 }
2999 else
3000 {
3001 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003002 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 newlen += mb_l;
3004 }
3005 }
3006
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003007 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 }
3009 else
3010#endif
3011 msg_outtrans_len(ccline.cmdbuff + start, len);
3012}
3013
3014/*
3015 * Put a character on the command line. Shifts the following text to the
3016 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3017 * "c" must be printable (fit in one display cell)!
3018 */
3019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 if (cmd_silent)
3023 return;
3024 msg_no_more = TRUE;
3025 msg_putchar(c);
3026 if (shift)
3027 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3028 msg_no_more = FALSE;
3029 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003030 extra_char = c;
3031 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032}
3033
3034/*
3035 * Undo a putcmdline(c, FALSE).
3036 */
3037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003038unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 if (cmd_silent)
3041 return;
3042 msg_no_more = TRUE;
3043 if (ccline.cmdlen == ccline.cmdpos)
3044 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003045#ifdef FEAT_MBYTE
3046 else if (has_mbyte)
3047 draw_cmdline(ccline.cmdpos,
3048 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 else
3051 draw_cmdline(ccline.cmdpos, 1);
3052 msg_no_more = FALSE;
3053 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003054 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055}
3056
3057/*
3058 * Put the given string, of the given length, onto the command line.
3059 * If len is -1, then STRLEN() is used to calculate the length.
3060 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3061 * part will be redrawn, otherwise it will not. If this function is called
3062 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3063 * called afterwards.
3064 */
3065 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003066put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 int retval;
3069 int i;
3070 int m;
3071 int c;
3072
3073 if (len < 0)
3074 len = (int)STRLEN(str);
3075
3076 /* Check if ccline.cmdbuff needs to be longer */
3077 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003078 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 else
3080 retval = OK;
3081 if (retval == OK)
3082 {
3083 if (!ccline.overstrike)
3084 {
3085 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3086 ccline.cmdbuff + ccline.cmdpos,
3087 (size_t)(ccline.cmdlen - ccline.cmdpos));
3088 ccline.cmdlen += len;
3089 }
3090 else
3091 {
3092#ifdef FEAT_MBYTE
3093 if (has_mbyte)
3094 {
3095 /* Count nr of characters in the new string. */
3096 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003097 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 ++m;
3099 /* Count nr of bytes in cmdline that are overwritten by these
3100 * characters. */
3101 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003102 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 --m;
3104 if (i < ccline.cmdlen)
3105 {
3106 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3107 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3108 ccline.cmdlen += ccline.cmdpos + len - i;
3109 }
3110 else
3111 ccline.cmdlen = ccline.cmdpos + len;
3112 }
3113 else
3114#endif
3115 if (ccline.cmdpos + len > ccline.cmdlen)
3116 ccline.cmdlen = ccline.cmdpos + len;
3117 }
3118 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3119 ccline.cmdbuff[ccline.cmdlen] = NUL;
3120
3121#ifdef FEAT_MBYTE
3122 if (enc_utf8)
3123 {
3124 /* When the inserted text starts with a composing character,
3125 * backup to the character before it. There could be two of them.
3126 */
3127 i = 0;
3128 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3129 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3130 {
3131 i = (*mb_head_off)(ccline.cmdbuff,
3132 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3133 ccline.cmdpos -= i;
3134 len += i;
3135 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3136 }
3137# ifdef FEAT_ARABIC
3138 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3139 {
3140 /* Check the previous character for Arabic combining pair. */
3141 i = (*mb_head_off)(ccline.cmdbuff,
3142 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3143 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3144 + ccline.cmdpos - i), c))
3145 {
3146 ccline.cmdpos -= i;
3147 len += i;
3148 }
3149 else
3150 i = 0;
3151 }
3152# endif
3153 if (i != 0)
3154 {
3155 /* Also backup the cursor position. */
3156 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3157 ccline.cmdspos -= i;
3158 msg_col -= i;
3159 if (msg_col < 0)
3160 {
3161 msg_col += Columns;
3162 --msg_row;
3163 }
3164 }
3165 }
3166#endif
3167
3168 if (redraw && !cmd_silent)
3169 {
3170 msg_no_more = TRUE;
3171 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003172 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3174 /* Avoid clearing the rest of the line too often. */
3175 if (cmdline_row != i || ccline.overstrike)
3176 msg_clr_eos();
3177 msg_no_more = FALSE;
3178 }
3179#ifdef FEAT_FKMAP
3180 /*
3181 * If we are in Farsi command mode, the character input must be in
3182 * Insert mode. So do not advance the cmdpos.
3183 */
3184 if (!cmd_fkmap)
3185#endif
3186 {
3187 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003190 if (m < 0) /* overflow, Columns or Rows at weird value */
3191 m = MAXCOL;
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 else
3194 m = MAXCOL;
3195 for (i = 0; i < len; ++i)
3196 {
3197 c = cmdline_charsize(ccline.cmdpos);
3198#ifdef FEAT_MBYTE
3199 /* count ">" for a double-wide char that doesn't fit. */
3200 if (has_mbyte)
3201 correct_cmdspos(ccline.cmdpos, c);
3202#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003203 /* Stop cursor at the end of the screen, but do increment the
3204 * insert position, so that entering a very long command
3205 * works, even though you can't see it. */
3206 if (ccline.cmdspos + c < m)
3207 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#ifdef FEAT_MBYTE
3209 if (has_mbyte)
3210 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003211 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (c > len - i - 1)
3213 c = len - i - 1;
3214 ccline.cmdpos += c;
3215 i += c;
3216 }
3217#endif
3218 ++ccline.cmdpos;
3219 }
3220 }
3221 }
3222 if (redraw)
3223 msg_check();
3224 return retval;
3225}
3226
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003227static struct cmdline_info prev_ccline;
3228static int prev_ccline_used = FALSE;
3229
3230/*
3231 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3232 * and overwrite it. But get_cmdline_str() may need it, thus make it
3233 * available globally in prev_ccline.
3234 */
3235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003236save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003237{
3238 if (!prev_ccline_used)
3239 {
3240 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3241 prev_ccline_used = TRUE;
3242 }
3243 *ccp = prev_ccline;
3244 prev_ccline = ccline;
3245 ccline.cmdbuff = NULL;
3246 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003247 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003248}
3249
3250/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003251 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003252 */
3253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003254restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003255{
3256 ccline = prev_ccline;
3257 prev_ccline = *ccp;
3258}
3259
Bram Moolenaar5a305422006-04-28 22:38:25 +00003260#if defined(FEAT_EVAL) || defined(PROTO)
3261/*
3262 * Save the command line into allocated memory. Returns a pointer to be
3263 * passed to restore_cmdline_alloc() later.
3264 * Returns NULL when failed.
3265 */
3266 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003267save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003268{
3269 struct cmdline_info *p;
3270
3271 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3272 if (p != NULL)
3273 save_cmdline(p);
3274 return (char_u *)p;
3275}
3276
3277/*
3278 * Restore the command line from the return value of save_cmdline_alloc().
3279 */
3280 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003281restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003282{
3283 if (p != NULL)
3284 {
3285 restore_cmdline((struct cmdline_info *)p);
3286 vim_free(p);
3287 }
3288}
3289#endif
3290
Bram Moolenaar8299df92004-07-10 09:47:34 +00003291/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003292 * Paste a yank register into the command line.
3293 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003294 * insert_reg() can't be used here, because special characters from the
3295 * register contents will be interpreted as commands.
3296 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003297 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003298 */
3299 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003300cmdline_paste(
3301 int regname,
3302 int literally, /* Insert text literally instead of "as typed" */
3303 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003304{
3305 long i;
3306 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003307 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003308 int allocated;
3309 struct cmdline_info save_ccline;
3310
3311 /* check for valid regname; also accept special characters for CTRL-R in
3312 * the command line */
3313 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3314 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3315 return FAIL;
3316
3317 /* A register containing CTRL-R can cause an endless loop. Allow using
3318 * CTRL-C to break the loop. */
3319 line_breakcheck();
3320 if (got_int)
3321 return FAIL;
3322
3323#ifdef FEAT_CLIPBOARD
3324 regname = may_get_selection(regname);
3325#endif
3326
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003327 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003328 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003329 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003330 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003331 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003332 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003333 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003334
3335 if (i)
3336 {
3337 /* Got the value of a special register in "arg". */
3338 if (arg == NULL)
3339 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003340
3341 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3342 * part of the word. */
3343 p = arg;
3344 if (p_is && regname == Ctrl_W)
3345 {
3346 char_u *w;
3347 int len;
3348
3349 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003350 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003351 {
3352#ifdef FEAT_MBYTE
3353 if (has_mbyte)
3354 {
3355 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3356 if (!vim_iswordc(mb_ptr2char(w - len)))
3357 break;
3358 w -= len;
3359 }
3360 else
3361#endif
3362 {
3363 if (!vim_iswordc(w[-1]))
3364 break;
3365 --w;
3366 }
3367 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003368 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003369 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3370 p += len;
3371 }
3372
3373 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003374 if (allocated)
3375 vim_free(arg);
3376 return OK;
3377 }
3378
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003379 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003380}
3381
3382/*
3383 * Put a string on the command line.
3384 * When "literally" is TRUE, insert literally.
3385 * When "literally" is FALSE, insert as typed, but don't leave the command
3386 * line.
3387 */
3388 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003389cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003390{
3391 int c, cv;
3392
3393 if (literally)
3394 put_on_cmdline(s, -1, TRUE);
3395 else
3396 while (*s != NUL)
3397 {
3398 cv = *s;
3399 if (cv == Ctrl_V && s[1])
3400 ++s;
3401#ifdef FEAT_MBYTE
3402 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003403 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003404 else
3405#endif
3406 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003407 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3408 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003409#ifdef UNIX
3410 || c == intr_char
3411#endif
3412 || (c == Ctrl_BSL && *s == Ctrl_N))
3413 stuffcharReadbuff(Ctrl_V);
3414 stuffcharReadbuff(c);
3415 }
3416}
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#ifdef FEAT_WILDMENU
3419/*
3420 * Delete characters on the command line, from "from" to the current
3421 * position.
3422 */
3423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003424cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
3426 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3427 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3428 ccline.cmdlen -= ccline.cmdpos - from;
3429 ccline.cmdpos = from;
3430}
3431#endif
3432
3433/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003434 * This function is called when the screen size changes and with incremental
3435 * search and in other situations where the command line may have been
3436 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
3438 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003439redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003441 redrawcmdline_ex(TRUE);
3442}
3443
3444 void
3445redrawcmdline_ex(int do_compute_cmdrow)
3446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (cmd_silent)
3448 return;
3449 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003450 if (do_compute_cmdrow)
3451 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 redrawcmd();
3453 cursorcmd();
3454}
3455
3456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003457redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 int i;
3460
3461 if (cmd_silent)
3462 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003463 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 msg_putchar(ccline.cmdfirstc);
3465 if (ccline.cmdprompt != NULL)
3466 {
3467 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3468 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3469 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003470 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 --ccline.cmdindent;
3472 }
3473 else
3474 for (i = ccline.cmdindent; i > 0; --i)
3475 msg_putchar(' ');
3476}
3477
3478/*
3479 * Redraw what is currently on the command line.
3480 */
3481 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003482redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 if (cmd_silent)
3485 return;
3486
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003487 /* when 'incsearch' is set there may be no command line while redrawing */
3488 if (ccline.cmdbuff == NULL)
3489 {
3490 windgoto(cmdline_row, 0);
3491 msg_clr_eos();
3492 return;
3493 }
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 msg_start();
3496 redrawcmdprompt();
3497
3498 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3499 msg_no_more = TRUE;
3500 draw_cmdline(0, ccline.cmdlen);
3501 msg_clr_eos();
3502 msg_no_more = FALSE;
3503
3504 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003505 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003506 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 /*
3509 * An emsg() before may have set msg_scroll. This is used in normal mode,
3510 * in cmdline mode we can reset them now.
3511 */
3512 msg_scroll = FALSE; /* next message overwrites cmdline */
3513
3514 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3515 * in cmdline mode */
3516 skip_redraw = FALSE;
3517}
3518
3519 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003520compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003522 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 cmdline_row = Rows - 1;
3524 else
3525 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003526 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527}
3528
3529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003530cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 if (cmd_silent)
3533 return;
3534
3535#ifdef FEAT_RIGHTLEFT
3536 if (cmdmsg_rl)
3537 {
3538 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3539 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3540 if (msg_row <= 0)
3541 msg_row = Rows - 1;
3542 }
3543 else
3544#endif
3545 {
3546 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3547 msg_col = ccline.cmdspos % (int)Columns;
3548 if (msg_row >= Rows)
3549 msg_row = Rows - 1;
3550 }
3551
3552 windgoto(msg_row, msg_col);
3553#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003554 if (p_imst == IM_ON_THE_SPOT)
3555 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
3557#ifdef MCH_CURSOR_SHAPE
3558 mch_update_cursor();
3559#endif
3560}
3561
3562 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003563gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
3565 msg_start();
3566#ifdef FEAT_RIGHTLEFT
3567 if (cmdmsg_rl)
3568 msg_col = Columns - 1;
3569 else
3570#endif
3571 msg_col = 0; /* always start in column 0 */
3572 if (clr) /* clear the bottom line(s) */
3573 msg_clr_eos(); /* will reset clear_cmdline */
3574 windgoto(cmdline_row, 0);
3575}
3576
3577/*
3578 * Check the word in front of the cursor for an abbreviation.
3579 * Called when the non-id character "c" has been entered.
3580 * When an abbreviation is recognized it is removed from the text with
3581 * backspaces and the replacement string is inserted, followed by "c".
3582 */
3583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003584ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
3586 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3587 return FALSE;
3588
3589 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3590}
3591
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003592#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3593 static int
3594#ifdef __BORLANDC__
3595_RTLENTRYF
3596#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003597sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003598{
3599 char_u *p1 = *(char_u **)s1;
3600 char_u *p2 = *(char_u **)s2;
3601
3602 if (*p1 != '<' && *p2 == '<') return -1;
3603 if (*p1 == '<' && *p2 != '<') return 1;
3604 return STRCMP(p1, p2);
3605}
3606#endif
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608/*
3609 * Return FAIL if this is not an appropriate context in which to do
3610 * completion of anything, return OK if it is (even if there are no matches).
3611 * For the caller, this means that the character is just passed through like a
3612 * normal character (instead of being expanded). This allows :s/^I^D etc.
3613 */
3614 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003615nextwild(
3616 expand_T *xp,
3617 int type,
3618 int options, /* extra options for ExpandOne() */
3619 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 int i, j;
3622 char_u *p1;
3623 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 int difflen;
3625 int v;
3626
3627 if (xp->xp_numfiles == -1)
3628 {
3629 set_expand_context(xp);
3630 cmd_showtail = expand_showtail(xp);
3631 }
3632
3633 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3634 {
3635 beep_flush();
3636 return OK; /* Something illegal on command line */
3637 }
3638 if (xp->xp_context == EXPAND_NOTHING)
3639 {
3640 /* Caller can use the character as a normal char instead */
3641 return FAIL;
3642 }
3643
3644 MSG_PUTS("..."); /* show that we are busy */
3645 out_flush();
3646
3647 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003648 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 if (type == WILD_NEXT || type == WILD_PREV)
3651 {
3652 /*
3653 * Get next/previous match for a previous expanded pattern.
3654 */
3655 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3656 }
3657 else
3658 {
3659 /*
3660 * Translate string into pattern and expand it.
3661 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003662 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3663 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 p2 = NULL;
3665 else
3666 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003667 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003668 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3669 if (escape)
3670 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003671
3672 if (p_wic)
3673 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003674 p2 = ExpandOne(xp, p1,
3675 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003676 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003678 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (p2 != NULL && type == WILD_LONGEST)
3680 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003681 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (ccline.cmdbuff[i + j] == '*'
3683 || ccline.cmdbuff[i + j] == '?')
3684 break;
3685 if ((int)STRLEN(p2) < j)
3686 {
3687 vim_free(p2);
3688 p2 = NULL;
3689 }
3690 }
3691 }
3692 }
3693
3694 if (p2 != NULL && !got_int)
3695 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003696 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003697 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003699 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 xp->xp_pattern = ccline.cmdbuff + i;
3701 }
3702 else
3703 v = OK;
3704 if (v == OK)
3705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003706 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3707 &ccline.cmdbuff[ccline.cmdpos],
3708 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3709 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 ccline.cmdlen += difflen;
3711 ccline.cmdpos += difflen;
3712 }
3713 }
3714 vim_free(p2);
3715
3716 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003717 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 /* When expanding a ":map" command and no matches are found, assume that
3720 * the key is supposed to be inserted literally */
3721 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3722 return FAIL;
3723
3724 if (xp->xp_numfiles <= 0 && p2 == NULL)
3725 beep_flush();
3726 else if (xp->xp_numfiles == 1)
3727 /* free expanded pattern */
3728 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3729
3730 return OK;
3731}
3732
3733/*
3734 * Do wildcard expansion on the string 'str'.
3735 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003736 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 * Return NULL for failure.
3738 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003739 * "orig" is the originally expanded string, copied to allocated memory. It
3740 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3741 * WILD_PREV "orig" should be NULL.
3742 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003743 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3744 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 *
3746 * mode = WILD_FREE: just free previously expanded matches
3747 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3748 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3749 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3750 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3751 * mode = WILD_ALL: return all matches concatenated
3752 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003753 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 *
3755 * options = WILD_LIST_NOTFOUND: list entries without a match
3756 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3757 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3758 * options = WILD_NO_BEEP: Don't beep for multiple matches
3759 * options = WILD_ADD_SLASH: add a slash after directory names
3760 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3761 * options = WILD_SILENT: don't print warning messages
3762 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003763 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 *
3765 * The variables xp->xp_context and xp->xp_backslash must have been set!
3766 */
3767 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003768ExpandOne(
3769 expand_T *xp,
3770 char_u *str,
3771 char_u *orig, /* allocated copy of original of expanded string */
3772 int options,
3773 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 char_u *ss = NULL;
3776 static int findex;
3777 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003778 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 int i;
3780 long_u len;
3781 int non_suf_match; /* number without matching suffix */
3782
3783 /*
3784 * first handle the case of using an old match
3785 */
3786 if (mode == WILD_NEXT || mode == WILD_PREV)
3787 {
3788 if (xp->xp_numfiles > 0)
3789 {
3790 if (mode == WILD_PREV)
3791 {
3792 if (findex == -1)
3793 findex = xp->xp_numfiles;
3794 --findex;
3795 }
3796 else /* mode == WILD_NEXT */
3797 ++findex;
3798
3799 /*
3800 * When wrapping around, return the original string, set findex to
3801 * -1.
3802 */
3803 if (findex < 0)
3804 {
3805 if (orig_save == NULL)
3806 findex = xp->xp_numfiles - 1;
3807 else
3808 findex = -1;
3809 }
3810 if (findex >= xp->xp_numfiles)
3811 {
3812 if (orig_save == NULL)
3813 findex = 0;
3814 else
3815 findex = -1;
3816 }
3817#ifdef FEAT_WILDMENU
3818 if (p_wmnu)
3819 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3820 findex, cmd_showtail);
3821#endif
3822 if (findex == -1)
3823 return vim_strsave(orig_save);
3824 return vim_strsave(xp->xp_files[findex]);
3825 }
3826 else
3827 return NULL;
3828 }
3829
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003830 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3832 {
3833 FreeWild(xp->xp_numfiles, xp->xp_files);
3834 xp->xp_numfiles = -1;
3835 vim_free(orig_save);
3836 orig_save = NULL;
3837 }
3838 findex = 0;
3839
3840 if (mode == WILD_FREE) /* only release file name */
3841 return NULL;
3842
3843 if (xp->xp_numfiles == -1)
3844 {
3845 vim_free(orig_save);
3846 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003847 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 /*
3850 * Do the expansion.
3851 */
3852 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3853 options) == FAIL)
3854 {
3855#ifdef FNAME_ILLEGAL
3856 /* Illegal file name has been silently skipped. But when there
3857 * are wildcards, the real problem is that there was no match,
3858 * causing the pattern to be added, which has illegal characters.
3859 */
3860 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3861 EMSG2(_(e_nomatch2), str);
3862#endif
3863 }
3864 else if (xp->xp_numfiles == 0)
3865 {
3866 if (!(options & WILD_SILENT))
3867 EMSG2(_(e_nomatch2), str);
3868 }
3869 else
3870 {
3871 /* Escape the matches for use on the command line. */
3872 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3873
3874 /*
3875 * Check for matching suffixes in file names.
3876 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003877 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3878 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
3880 if (xp->xp_numfiles)
3881 non_suf_match = xp->xp_numfiles;
3882 else
3883 non_suf_match = 1;
3884 if ((xp->xp_context == EXPAND_FILES
3885 || xp->xp_context == EXPAND_DIRECTORIES)
3886 && xp->xp_numfiles > 1)
3887 {
3888 /*
3889 * More than one match; check suffix.
3890 * The files will have been sorted on matching suffix in
3891 * expand_wildcards, only need to check the first two.
3892 */
3893 non_suf_match = 0;
3894 for (i = 0; i < 2; ++i)
3895 if (match_suffix(xp->xp_files[i]))
3896 ++non_suf_match;
3897 }
3898 if (non_suf_match != 1)
3899 {
3900 /* Can we ever get here unless it's while expanding
3901 * interactively? If not, we can get rid of this all
3902 * together. Don't really want to wait for this message
3903 * (and possibly have to hit return to continue!).
3904 */
3905 if (!(options & WILD_SILENT))
3906 EMSG(_(e_toomany));
3907 else if (!(options & WILD_NO_BEEP))
3908 beep_flush();
3909 }
3910 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3911 ss = vim_strsave(xp->xp_files[0]);
3912 }
3913 }
3914 }
3915
3916 /* Find longest common part */
3917 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3918 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003919 int mb_len = 1;
3920 int c0, ci;
3921
3922 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003924#ifdef FEAT_MBYTE
3925 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003927 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3928 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3929 }
3930 else
3931#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003932 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003933 for (i = 1; i < xp->xp_numfiles; ++i)
3934 {
3935#ifdef FEAT_MBYTE
3936 if (has_mbyte)
3937 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3938 else
3939#endif
3940 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003941 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003943 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003944 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003946 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003949 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 break;
3951 }
3952 if (i < xp->xp_numfiles)
3953 {
3954 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003955 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 break;
3957 }
3958 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 ss = alloc((unsigned)len + 1);
3961 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003962 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 findex = -1; /* next p_wc gets first one */
3964 }
3965
3966 /* Concatenate all matching names */
3967 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3968 {
3969 len = 0;
3970 for (i = 0; i < xp->xp_numfiles; ++i)
3971 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3972 ss = lalloc(len, TRUE);
3973 if (ss != NULL)
3974 {
3975 *ss = NUL;
3976 for (i = 0; i < xp->xp_numfiles; ++i)
3977 {
3978 STRCAT(ss, xp->xp_files[i]);
3979 if (i != xp->xp_numfiles - 1)
3980 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3981 }
3982 }
3983 }
3984
3985 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3986 ExpandCleanup(xp);
3987
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003988 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003989 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003990 vim_free(orig);
3991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 return ss;
3993}
3994
3995/*
3996 * Prepare an expand structure for use.
3997 */
3998 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003999ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004001 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004002 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004004#ifndef BACKSLASH_IN_FILENAME
4005 xp->xp_shell = FALSE;
4006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 xp->xp_numfiles = -1;
4008 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004009#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4010 xp->xp_arg = NULL;
4011#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004012 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013}
4014
4015/*
4016 * Cleanup an expand structure after use.
4017 */
4018 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004019ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
4021 if (xp->xp_numfiles >= 0)
4022 {
4023 FreeWild(xp->xp_numfiles, xp->xp_files);
4024 xp->xp_numfiles = -1;
4025 }
4026}
4027
4028 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004029ExpandEscape(
4030 expand_T *xp,
4031 char_u *str,
4032 int numfiles,
4033 char_u **files,
4034 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036 int i;
4037 char_u *p;
4038
4039 /*
4040 * May change home directory back to "~"
4041 */
4042 if (options & WILD_HOME_REPLACE)
4043 tilde_replace(str, numfiles, files);
4044
4045 if (options & WILD_ESCAPE)
4046 {
4047 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004048 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004049 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 || xp->xp_context == EXPAND_BUFFERS
4051 || xp->xp_context == EXPAND_DIRECTORIES)
4052 {
4053 /*
4054 * Insert a backslash into a file name before a space, \, %, #
4055 * and wildmatch characters, except '~'.
4056 */
4057 for (i = 0; i < numfiles; ++i)
4058 {
4059 /* for ":set path=" we need to escape spaces twice */
4060 if (xp->xp_backslash == XP_BS_THREE)
4061 {
4062 p = vim_strsave_escaped(files[i], (char_u *)" ");
4063 if (p != NULL)
4064 {
4065 vim_free(files[i]);
4066 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004067#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 p = vim_strsave_escaped(files[i], (char_u *)" ");
4069 if (p != NULL)
4070 {
4071 vim_free(files[i]);
4072 files[i] = p;
4073 }
4074#endif
4075 }
4076 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004077#ifdef BACKSLASH_IN_FILENAME
4078 p = vim_strsave_fnameescape(files[i], FALSE);
4079#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004080 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (p != NULL)
4083 {
4084 vim_free(files[i]);
4085 files[i] = p;
4086 }
4087
4088 /* If 'str' starts with "\~", replace "~" at start of
4089 * files[i] with "\~". */
4090 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004091 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004094
4095 /* If the first file starts with a '+' escape it. Otherwise it
4096 * could be seen as "+cmd". */
4097 if (*files[0] == '+')
4098 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 else if (xp->xp_context == EXPAND_TAGS)
4101 {
4102 /*
4103 * Insert a backslash before characters in a tag name that
4104 * would terminate the ":tag" command.
4105 */
4106 for (i = 0; i < numfiles; ++i)
4107 {
4108 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4109 if (p != NULL)
4110 {
4111 vim_free(files[i]);
4112 files[i] = p;
4113 }
4114 }
4115 }
4116 }
4117}
4118
4119/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004120 * Escape special characters in "fname" for when used as a file name argument
4121 * after a Vim command, or, when "shell" is non-zero, a shell command.
4122 * Returns the result in allocated memory.
4123 */
4124 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004125vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004126{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004127 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004128#ifdef BACKSLASH_IN_FILENAME
4129 char_u buf[20];
4130 int j = 0;
4131
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004132 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004133 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004134 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004135 buf[j++] = *p;
4136 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004137 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004138#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004139 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4140 if (shell && csh_like_shell() && p != NULL)
4141 {
4142 char_u *s;
4143
4144 /* For csh and similar shells need to put two backslashes before '!'.
4145 * One is taken by Vim, one by the shell. */
4146 s = vim_strsave_escaped(p, (char_u *)"!");
4147 vim_free(p);
4148 p = s;
4149 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004150#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004151
4152 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4153 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004154 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004155 escape_fname(&p);
4156
4157 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004158}
4159
4160/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004161 * Put a backslash before the file name in "pp", which is in allocated memory.
4162 */
4163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004164escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004165{
4166 char_u *p;
4167
4168 p = alloc((unsigned)(STRLEN(*pp) + 2));
4169 if (p != NULL)
4170 {
4171 p[0] = '\\';
4172 STRCPY(p + 1, *pp);
4173 vim_free(*pp);
4174 *pp = p;
4175 }
4176}
4177
4178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 * For each file name in files[num_files]:
4180 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4181 */
4182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004183tilde_replace(
4184 char_u *orig_pat,
4185 int num_files,
4186 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188 int i;
4189 char_u *p;
4190
4191 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4192 {
4193 for (i = 0; i < num_files; ++i)
4194 {
4195 p = home_replace_save(NULL, files[i]);
4196 if (p != NULL)
4197 {
4198 vim_free(files[i]);
4199 files[i] = p;
4200 }
4201 }
4202 }
4203}
4204
4205/*
4206 * Show all matches for completion on the command line.
4207 * Returns EXPAND_NOTHING when the character that triggered expansion should
4208 * be inserted like a normal character.
4209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004211showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
4213#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4214 int num_files;
4215 char_u **files_found;
4216 int i, j, k;
4217 int maxlen;
4218 int lines;
4219 int columns;
4220 char_u *p;
4221 int lastlen;
4222 int attr;
4223 int showtail;
4224
4225 if (xp->xp_numfiles == -1)
4226 {
4227 set_expand_context(xp);
4228 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4229 &num_files, &files_found);
4230 showtail = expand_showtail(xp);
4231 if (i != EXPAND_OK)
4232 return i;
4233
4234 }
4235 else
4236 {
4237 num_files = xp->xp_numfiles;
4238 files_found = xp->xp_files;
4239 showtail = cmd_showtail;
4240 }
4241
4242#ifdef FEAT_WILDMENU
4243 if (!wildmenu)
4244 {
4245#endif
4246 msg_didany = FALSE; /* lines_left will be set */
4247 msg_start(); /* prepare for paging */
4248 msg_putchar('\n');
4249 out_flush();
4250 cmdline_row = msg_row;
4251 msg_didany = FALSE; /* lines_left will be set again */
4252 msg_start(); /* prepare for paging */
4253#ifdef FEAT_WILDMENU
4254 }
4255#endif
4256
4257 if (got_int)
4258 got_int = FALSE; /* only int. the completion, not the cmd line */
4259#ifdef FEAT_WILDMENU
4260 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004261 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#endif
4263 else
4264 {
4265 /* find the length of the longest file name */
4266 maxlen = 0;
4267 for (i = 0; i < num_files; ++i)
4268 {
4269 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004270 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 || xp->xp_context == EXPAND_BUFFERS))
4272 {
4273 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4274 j = vim_strsize(NameBuff);
4275 }
4276 else
4277 j = vim_strsize(L_SHOWFILE(i));
4278 if (j > maxlen)
4279 maxlen = j;
4280 }
4281
4282 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4283 lines = num_files;
4284 else
4285 {
4286 /* compute the number of columns and lines for the listing */
4287 maxlen += 2; /* two spaces between file names */
4288 columns = ((int)Columns + 2) / maxlen;
4289 if (columns < 1)
4290 columns = 1;
4291 lines = (num_files + columns - 1) / columns;
4292 }
4293
Bram Moolenaar8820b482017-03-16 17:23:31 +01004294 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4297 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004298 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 msg_clr_eos();
4300 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004301 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303
4304 /* list the files line by line */
4305 for (i = 0; i < lines; ++i)
4306 {
4307 lastlen = 999;
4308 for (k = i; k < num_files; k += lines)
4309 {
4310 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4311 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004312 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 p = files_found[k] + STRLEN(files_found[k]) + 1;
4314 msg_advance(maxlen + 1);
4315 msg_puts(p);
4316 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004317 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 break;
4319 }
4320 for (j = maxlen - lastlen; --j >= 0; )
4321 msg_putchar(' ');
4322 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004323 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 || xp->xp_context == EXPAND_BUFFERS)
4325 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004326 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004327 if (xp->xp_numfiles != -1)
4328 {
4329 char_u *halved_slash;
4330 char_u *exp_path;
4331
4332 /* Expansion was done before and special characters
4333 * were escaped, need to halve backslashes. Also
4334 * $HOME has been replaced with ~/. */
4335 exp_path = expand_env_save_opt(files_found[k], TRUE);
4336 halved_slash = backslash_halve_save(
4337 exp_path != NULL ? exp_path : files_found[k]);
4338 j = mch_isdir(halved_slash != NULL ? halved_slash
4339 : files_found[k]);
4340 vim_free(exp_path);
4341 vim_free(halved_slash);
4342 }
4343 else
4344 /* Expansion was done here, file names are literal. */
4345 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (showtail)
4347 p = L_SHOWFILE(k);
4348 else
4349 {
4350 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4351 TRUE);
4352 p = NameBuff;
4353 }
4354 }
4355 else
4356 {
4357 j = FALSE;
4358 p = L_SHOWFILE(k);
4359 }
4360 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4361 }
4362 if (msg_col > 0) /* when not wrapped around */
4363 {
4364 msg_clr_eos();
4365 msg_putchar('\n');
4366 }
4367 out_flush(); /* show one line at a time */
4368 if (got_int)
4369 {
4370 got_int = FALSE;
4371 break;
4372 }
4373 }
4374
4375 /*
4376 * we redraw the command below the lines that we have just listed
4377 * This is a bit tricky, but it saves a lot of screen updating.
4378 */
4379 cmdline_row = msg_row; /* will put it back later */
4380 }
4381
4382 if (xp->xp_numfiles == -1)
4383 FreeWild(num_files, files_found);
4384
4385 return EXPAND_OK;
4386}
4387
4388/*
4389 * Private gettail for showmatches() (and win_redr_status_matches()):
4390 * Find tail of file name path, but ignore trailing "/".
4391 */
4392 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004393sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
4395 char_u *p;
4396 char_u *t = s;
4397 int had_sep = FALSE;
4398
4399 for (p = s; *p != NUL; )
4400 {
4401 if (vim_ispathsep(*p)
4402#ifdef BACKSLASH_IN_FILENAME
4403 && !rem_backslash(p)
4404#endif
4405 )
4406 had_sep = TRUE;
4407 else if (had_sep)
4408 {
4409 t = p;
4410 had_sep = FALSE;
4411 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004412 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 return t;
4415}
4416
4417/*
4418 * Return TRUE if we only need to show the tail of completion matches.
4419 * When not completing file names or there is a wildcard in the path FALSE is
4420 * returned.
4421 */
4422 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004423expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 char_u *s;
4426 char_u *end;
4427
4428 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004429 if (xp->xp_context != EXPAND_FILES
4430 && xp->xp_context != EXPAND_SHELLCMD
4431 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 return FALSE;
4433
4434 end = gettail(xp->xp_pattern);
4435 if (end == xp->xp_pattern) /* there is no path separator */
4436 return FALSE;
4437
4438 for (s = xp->xp_pattern; s < end; s++)
4439 {
4440 /* Skip escaped wildcards. Only when the backslash is not a path
4441 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4442 if (rem_backslash(s))
4443 ++s;
4444 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4445 return FALSE;
4446 }
4447 return TRUE;
4448}
4449
4450/*
4451 * Prepare a string for expansion.
4452 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004453 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 * When expanding other names: The string will be used with regcomp(). Copy
4455 * the name into allocated memory and prepend "^".
4456 */
4457 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004458addstar(
4459 char_u *fname,
4460 int len,
4461 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 char_u *retval;
4464 int i, j;
4465 int new_len;
4466 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004467 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004469 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004470 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004471 && context != EXPAND_SHELLCMD
4472 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
4474 /*
4475 * Matching will be done internally (on something other than files).
4476 * So we convert the file-matching-type wildcards into our kind for
4477 * use with vim_regcomp(). First work out how long it will be:
4478 */
4479
4480 /* For help tags the translation is done in find_help_tags().
4481 * For a tag pattern starting with "/" no translation is needed. */
4482 if (context == EXPAND_HELP
4483 || context == EXPAND_COLORS
4484 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004485 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004486 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004487 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004488 || ((context == EXPAND_TAGS_LISTFILES
4489 || context == EXPAND_TAGS)
4490 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 retval = vim_strnsave(fname, len);
4492 else
4493 {
4494 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4495 for (i = 0; i < len; i++)
4496 {
4497 if (fname[i] == '*' || fname[i] == '~')
4498 new_len++; /* '*' needs to be replaced by ".*"
4499 '~' needs to be replaced by "\~" */
4500
4501 /* Buffer names are like file names. "." should be literal */
4502 if (context == EXPAND_BUFFERS && fname[i] == '.')
4503 new_len++; /* "." becomes "\." */
4504
4505 /* Custom expansion takes care of special things, match
4506 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004507 if ((context == EXPAND_USER_DEFINED
4508 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 new_len++; /* '\' becomes "\\" */
4510 }
4511 retval = alloc(new_len);
4512 if (retval != NULL)
4513 {
4514 retval[0] = '^';
4515 j = 1;
4516 for (i = 0; i < len; i++, j++)
4517 {
4518 /* Skip backslash. But why? At least keep it for custom
4519 * expansion. */
4520 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004521 && context != EXPAND_USER_LIST
4522 && fname[i] == '\\'
4523 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 break;
4525
4526 switch (fname[i])
4527 {
4528 case '*': retval[j++] = '.';
4529 break;
4530 case '~': retval[j++] = '\\';
4531 break;
4532 case '?': retval[j] = '.';
4533 continue;
4534 case '.': if (context == EXPAND_BUFFERS)
4535 retval[j++] = '\\';
4536 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004537 case '\\': if (context == EXPAND_USER_DEFINED
4538 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 retval[j++] = '\\';
4540 break;
4541 }
4542 retval[j] = fname[i];
4543 }
4544 retval[j] = NUL;
4545 }
4546 }
4547 }
4548 else
4549 {
4550 retval = alloc(len + 4);
4551 if (retval != NULL)
4552 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004553 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004556 * Don't add a star to *, ~, ~user, $var or `cmd`.
4557 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 * ~ would be at the start of the file name, but not the tail.
4559 * $ could be anywhere in the tail.
4560 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004561 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 */
4563 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004564 ends_in_star = (len > 0 && retval[len - 1] == '*');
4565#ifndef BACKSLASH_IN_FILENAME
4566 for (i = len - 2; i >= 0; --i)
4567 {
4568 if (retval[i] != '\\')
4569 break;
4570 ends_in_star = !ends_in_star;
4571 }
4572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004574 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 && vim_strchr(tail, '$') == NULL
4576 && vim_strchr(retval, '`') == NULL)
4577 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004578 else if (len > 0 && retval[len - 1] == '$')
4579 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 retval[len] = NUL;
4581 }
4582 }
4583 return retval;
4584}
4585
4586/*
4587 * Must parse the command line so far to work out what context we are in.
4588 * Completion can then be done based on that context.
4589 * This routine sets the variables:
4590 * xp->xp_pattern The start of the pattern to be expanded within
4591 * the command line (ends at the cursor).
4592 * xp->xp_context The type of thing to expand. Will be one of:
4593 *
4594 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4595 * the command line, like an unknown command. Caller
4596 * should beep.
4597 * EXPAND_NOTHING Unrecognised context for completion, use char like
4598 * a normal char, rather than for completion. eg
4599 * :s/^I/
4600 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4601 * it.
4602 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4603 * EXPAND_FILES After command with XFILE set, or after setting
4604 * with P_EXPAND set. eg :e ^I, :w>>^I
4605 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4606 * when we know only directories are of interest. eg
4607 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004608 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4610 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4611 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4612 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4613 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4614 * EXPAND_EVENTS Complete event names
4615 * EXPAND_SYNTAX Complete :syntax command arguments
4616 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4617 * EXPAND_AUGROUP Complete autocommand group names
4618 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4619 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4620 * eg :unmap a^I , :cunab x^I
4621 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4622 * eg :call sub^I
4623 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4624 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4625 * names in expressions, eg :while s^I
4626 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004627 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 */
4629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004632 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (ccline.cmdfirstc != ':'
4634#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004635 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004636 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638 )
4639 {
4640 xp->xp_context = EXPAND_NOTHING;
4641 return;
4642 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004643 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644}
4645
4646 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004647set_cmd_context(
4648 expand_T *xp,
4649 char_u *str, /* start of command line */
4650 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004651 int col, /* position of cursor */
4652 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
4654 int old_char = NUL;
4655 char_u *nextcomm;
4656
4657 /*
4658 * Avoid a UMR warning from Purify, only save the character if it has been
4659 * written before.
4660 */
4661 if (col < len)
4662 old_char = str[col];
4663 str[col] = NUL;
4664 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004665
4666#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004667 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004668 {
4669# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004670 /* pass CMD_SIZE because there is no real command */
4671 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004672# endif
4673 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004674 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004675 {
4676 xp->xp_context = ccline.xp_context;
4677 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004678# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004679 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004680# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004681 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004682 else
4683#endif
4684 while (nextcomm != NULL)
4685 nextcomm = set_one_cmd_context(xp, nextcomm);
4686
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004687 /* Store the string here so that call_user_expand_func() can get to them
4688 * easily. */
4689 xp->xp_line = str;
4690 xp->xp_col = col;
4691
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 str[col] = old_char;
4693}
4694
4695/*
4696 * Expand the command line "str" from context "xp".
4697 * "xp" must have been set by set_cmd_context().
4698 * xp->xp_pattern points into "str", to where the text that is to be expanded
4699 * starts.
4700 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4701 * cursor.
4702 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4703 * key that triggered expansion literally.
4704 * Returns EXPAND_OK otherwise.
4705 */
4706 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004707expand_cmdline(
4708 expand_T *xp,
4709 char_u *str, /* start of command line */
4710 int col, /* position of cursor */
4711 int *matchcount, /* return: nr of matches */
4712 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
4714 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004715 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4718 {
4719 beep_flush();
4720 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4721 }
4722 if (xp->xp_context == EXPAND_NOTHING)
4723 {
4724 /* Caller can use the character as a normal char instead */
4725 return EXPAND_NOTHING;
4726 }
4727
4728 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004729 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4730 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (file_str == NULL)
4732 return EXPAND_UNSUCCESSFUL;
4733
Bram Moolenaar94950a92010-12-02 16:01:29 +01004734 if (p_wic)
4735 options += WILD_ICASE;
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004738 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 *matchcount = 0;
4741 *matches = NULL;
4742 }
4743 vim_free(file_str);
4744
4745 return EXPAND_OK;
4746}
4747
4748#ifdef FEAT_MULTI_LANG
4749/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004750 * Cleanup matches for help tags:
4751 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4752 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004754static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004757cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 int i, j;
4760 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004761 char_u buf[4];
4762 char_u *p = buf;
4763
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004764 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004765 {
4766 *p++ = '@';
4767 *p++ = p_hlg[0];
4768 *p++ = p_hlg[1];
4769 }
4770 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 for (i = 0; i < num_file; ++i)
4773 {
4774 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004775 if (len <= 0)
4776 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004777 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 /* Sorting on priority means the same item in another language may
4780 * be anywhere. Search all items for a match up to the "@en". */
4781 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004782 if (j != i && (int)STRLEN(file[j]) == len + 3
4783 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 break;
4785 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004786 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 file[i][len] = NUL;
4788 }
4789 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004790
4791 if (*buf != NUL)
4792 for (i = 0; i < num_file; ++i)
4793 {
4794 len = (int)STRLEN(file[i]) - 3;
4795 if (len <= 0)
4796 continue;
4797 if (STRCMP(file[i] + len, buf) == 0)
4798 {
4799 /* remove the default language */
4800 file[i][len] = NUL;
4801 }
4802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803}
4804#endif
4805
4806/*
4807 * Do the expansion based on xp->xp_context and "pat".
4808 */
4809 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810ExpandFromContext(
4811 expand_T *xp,
4812 char_u *pat,
4813 int *num_file,
4814 char_u ***file,
4815 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817#ifdef FEAT_CMDL_COMPL
4818 regmatch_T regmatch;
4819#endif
4820 int ret;
4821 int flags;
4822
4823 flags = EW_DIR; /* include directories */
4824 if (options & WILD_LIST_NOTFOUND)
4825 flags |= EW_NOTFOUND;
4826 if (options & WILD_ADD_SLASH)
4827 flags |= EW_ADDSLASH;
4828 if (options & WILD_KEEP_ALL)
4829 flags |= EW_KEEPALL;
4830 if (options & WILD_SILENT)
4831 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004832 if (options & WILD_ALLLINKS)
4833 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004835 if (xp->xp_context == EXPAND_FILES
4836 || xp->xp_context == EXPAND_DIRECTORIES
4837 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 /*
4840 * Expand file or directory names.
4841 */
4842 int free_pat = FALSE;
4843 int i;
4844
4845 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4846 * space */
4847 if (xp->xp_backslash != XP_BS_NONE)
4848 {
4849 free_pat = TRUE;
4850 pat = vim_strsave(pat);
4851 for (i = 0; pat[i]; ++i)
4852 if (pat[i] == '\\')
4853 {
4854 if (xp->xp_backslash == XP_BS_THREE
4855 && pat[i + 1] == '\\'
4856 && pat[i + 2] == '\\'
4857 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004858 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (xp->xp_backslash == XP_BS_ONE
4860 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004861 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863 }
4864
4865 if (xp->xp_context == EXPAND_FILES)
4866 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004867 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4868 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 else
4870 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004871 if (options & WILD_ICASE)
4872 flags |= EW_ICASE;
4873
Bram Moolenaard7834d32009-12-02 16:14:36 +00004874 /* Expand wildcards, supporting %:h and the like. */
4875 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (free_pat)
4877 vim_free(pat);
4878 return ret;
4879 }
4880
4881 *file = (char_u **)"";
4882 *num_file = 0;
4883 if (xp->xp_context == EXPAND_HELP)
4884 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004885 /* With an empty argument we would get all the help tags, which is
4886 * very slow. Get matches for "help" instead. */
4887 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4888 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
4890#ifdef FEAT_MULTI_LANG
4891 cleanup_help_tags(*num_file, *file);
4892#endif
4893 return OK;
4894 }
4895 return FAIL;
4896 }
4897
4898#ifndef FEAT_CMDL_COMPL
4899 return FAIL;
4900#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004901 if (xp->xp_context == EXPAND_SHELLCMD)
4902 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (xp->xp_context == EXPAND_OLD_SETTING)
4904 return ExpandOldSetting(num_file, file);
4905 if (xp->xp_context == EXPAND_BUFFERS)
4906 return ExpandBufnames(pat, num_file, file, options);
4907 if (xp->xp_context == EXPAND_TAGS
4908 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4909 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4910 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004911 {
4912 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004913 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4914 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004917 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004918 char *directories[] = {"compiler", 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_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004922 {
4923 char *directories[] = {"syntax", 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 Moolenaar1587a1e2010-07-29 20:59:59 +02004926 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004927 {
4928 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004929 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004930 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004931# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4932 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004933 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004934# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004935 if (xp->xp_context == EXPAND_PACKADD)
4936 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4939 if (regmatch.regprog == NULL)
4940 return FAIL;
4941
4942 /* set ignore-case according to p_ic, p_scs and pat */
4943 regmatch.rm_ic = ignorecase(pat);
4944
4945 if (xp->xp_context == EXPAND_SETTINGS
4946 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4947 ret = ExpandSettings(xp, &regmatch, num_file, file);
4948 else if (xp->xp_context == EXPAND_MAPPINGS)
4949 ret = ExpandMappings(&regmatch, num_file, file);
4950# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4951 else if (xp->xp_context == EXPAND_USER_DEFINED)
4952 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4953# endif
4954 else
4955 {
4956 static struct expgen
4957 {
4958 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004959 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004961 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 } tab[] =
4963 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004964 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4965 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004966 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004967 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004968#ifdef FEAT_CMDHIST
4969 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004972 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004973 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004974 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4975 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4976 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977#endif
4978#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004979 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4980 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4981 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4982 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983#endif
4984#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004985 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4986 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987#endif
4988#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004989 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004991#ifdef FEAT_PROFILE
4992 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4993#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004994 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004996 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4997 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004999#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005000 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005001#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005002#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005003 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005004#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005005#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005006 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5009 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005010 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5011 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005013 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005014 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 };
5016 int i;
5017
5018 /*
5019 * Find a context in the table and call the ExpandGeneric() with the
5020 * right function to do the expansion.
5021 */
5022 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005023 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (xp->xp_context == tab[i].context)
5025 {
5026 if (tab[i].ic)
5027 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005028 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005029 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031 }
5032 }
5033
Bram Moolenaar473de612013-06-08 18:19:48 +02005034 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
5036 return ret;
5037#endif /* FEAT_CMDL_COMPL */
5038}
5039
5040#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5041/*
5042 * Expand a list of names.
5043 *
5044 * Generic function for command line completion. It calls a function to
5045 * obtain strings, one by one. The strings are matched against a regexp
5046 * program. Matching strings are copied into an array, which is returned.
5047 *
5048 * Returns OK when no problems encountered, FAIL for error (out of memory).
5049 */
5050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005051ExpandGeneric(
5052 expand_T *xp,
5053 regmatch_T *regmatch,
5054 int *num_file,
5055 char_u ***file,
5056 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005058 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 int i;
5061 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005062 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *str;
5064
5065 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005066 * round == 0: count the number of matching names
5067 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005069 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 for (i = 0; ; ++i)
5072 {
5073 str = (*func)(xp, i);
5074 if (str == NULL) /* end of list */
5075 break;
5076 if (*str == NUL) /* skip empty strings */
5077 continue;
5078
5079 if (vim_regexec(regmatch, str, (colnr_T)0))
5080 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005081 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005083 if (escaped)
5084 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5085 else
5086 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 (*file)[count] = str;
5088#ifdef FEAT_MENU
5089 if (func == get_menu_names && str != NULL)
5090 {
5091 /* test for separator added by get_menu_names() */
5092 str += STRLEN(str) - 1;
5093 if (*str == '\001')
5094 *str = '.';
5095 }
5096#endif
5097 }
5098 ++count;
5099 }
5100 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005101 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
5103 if (count == 0)
5104 return OK;
5105 *num_file = count;
5106 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5107 if (*file == NULL)
5108 {
5109 *file = (char_u **)"";
5110 return FAIL;
5111 }
5112 count = 0;
5113 }
5114 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005115
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005116 /* Sort the results. Keep menu's in the specified order. */
5117 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005118 {
5119 if (xp->xp_context == EXPAND_EXPRESSION
5120 || xp->xp_context == EXPAND_FUNCTIONS
5121 || xp->xp_context == EXPAND_USER_FUNC)
5122 /* <SNR> functions should be sorted to the end. */
5123 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5124 sort_func_compare);
5125 else
5126 sort_strings(*file, *num_file);
5127 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005128
Bram Moolenaar4f688582007-07-24 12:34:30 +00005129#ifdef FEAT_CMDL_COMPL
5130 /* Reset the variables used for special highlight names expansion, so that
5131 * they don't show up when getting normal highlight names by ID. */
5132 reset_expand_highlight();
5133#endif
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 return OK;
5136}
5137
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005138/*
5139 * Complete a shell command.
5140 * Returns FAIL or OK;
5141 */
5142 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005143expand_shellcmd(
5144 char_u *filepat, /* pattern to match with command names */
5145 int *num_file, /* return: number of matches */
5146 char_u ***file, /* return: array with matches */
5147 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005148{
5149 char_u *pat;
5150 int i;
5151 char_u *path;
5152 int mustfree = FALSE;
5153 garray_T ga;
5154 char_u *buf = alloc(MAXPATHL);
5155 size_t l;
5156 char_u *s, *e;
5157 int flags = flagsarg;
5158 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005159 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005160
5161 if (buf == NULL)
5162 return FAIL;
5163
5164 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5165 * space */
5166 pat = vim_strsave(filepat);
5167 for (i = 0; pat[i]; ++i)
5168 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005169 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005170
Bram Moolenaarb5971142015-03-21 17:32:19 +01005171 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005172
5173 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005174 if (mch_isFullName(pat))
5175 path = (char_u *)" ";
5176 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005177 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5178 path = (char_u *)".";
5179 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005180 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005181 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005182 if (path == NULL)
5183 path = (char_u *)"";
5184 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005185
5186 /*
5187 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005188 * collect them in "ga". When "." is not in $PATH also expand for the
5189 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005190 */
5191 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005192 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005193 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005194 if (*s == NUL)
5195 {
5196 if (did_curdir)
5197 break;
5198 /* Find directories in the current directory, path is empty. */
5199 did_curdir = TRUE;
5200 }
5201 else if (*s == '.')
5202 did_curdir = TRUE;
5203
Bram Moolenaar68c31742006-09-02 15:54:18 +00005204 if (*s == ' ')
5205 ++s; /* Skip space used for absolute path name. */
5206
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005207#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005208 e = vim_strchr(s, ';');
5209#else
5210 e = vim_strchr(s, ':');
5211#endif
5212 if (e == NULL)
5213 e = s + STRLEN(s);
5214
5215 l = e - s;
5216 if (l > MAXPATHL - 5)
5217 break;
5218 vim_strncpy(buf, s, l);
5219 add_pathsep(buf);
5220 l = STRLEN(buf);
5221 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5222
5223 /* Expand matches in one directory of $PATH. */
5224 ret = expand_wildcards(1, &buf, num_file, file, flags);
5225 if (ret == OK)
5226 {
5227 if (ga_grow(&ga, *num_file) == FAIL)
5228 FreeWild(*num_file, *file);
5229 else
5230 {
5231 for (i = 0; i < *num_file; ++i)
5232 {
5233 s = (*file)[i];
5234 if (STRLEN(s) > l)
5235 {
5236 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005237 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005238 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5239 }
5240 else
5241 vim_free(s);
5242 }
5243 vim_free(*file);
5244 }
5245 }
5246 if (*e != NUL)
5247 ++e;
5248 }
5249 *file = ga.ga_data;
5250 *num_file = ga.ga_len;
5251
5252 vim_free(buf);
5253 vim_free(pat);
5254 if (mustfree)
5255 vim_free(path);
5256 return OK;
5257}
5258
5259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005261static 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 +00005262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005264 * Call "user_expand_func()" to invoke a user defined Vim script function and
5265 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005267 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005268call_user_expand_func(
5269 void *(*user_expand_func)(char_u *, int, char_u **, int),
5270 expand_T *xp,
5271 int *num_file,
5272 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005274 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005275 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005278 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005279 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaarb7515462013-06-29 12:58:33 +02005281 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005282 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 *num_file = 0;
5284 *file = NULL;
5285
Bram Moolenaarb7515462013-06-29 12:58:33 +02005286 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005287 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005288 keep = ccline.cmdbuff[ccline.cmdlen];
5289 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005290 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005291
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005292 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005293 args[1] = xp->xp_line;
5294 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 args[2] = num;
5296
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005297 /* Save the cmdline, we don't know what the function may do. */
5298 save_ccline = ccline;
5299 ccline.cmdbuff = NULL;
5300 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005302
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005303 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005304
5305 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005307 if (ccline.cmdbuff != NULL)
5308 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005309
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005310 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005311 return ret;
5312}
5313
5314/*
5315 * Expand names with a function defined by the user.
5316 */
5317 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318ExpandUserDefined(
5319 expand_T *xp,
5320 regmatch_T *regmatch,
5321 int *num_file,
5322 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005323{
5324 char_u *retstr;
5325 char_u *s;
5326 char_u *e;
5327 char_u keep;
5328 garray_T ga;
5329
5330 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5331 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 return FAIL;
5333
5334 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005335 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
5337 e = vim_strchr(s, '\n');
5338 if (e == NULL)
5339 e = s + STRLEN(s);
5340 keep = *e;
5341 *e = 0;
5342
5343 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5344 {
5345 *e = keep;
5346 if (*e != NUL)
5347 ++e;
5348 continue;
5349 }
5350
5351 if (ga_grow(&ga, 1) == FAIL)
5352 break;
5353
5354 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5355 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 *e = keep;
5358 if (*e != NUL)
5359 ++e;
5360 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005361 vim_free(retstr);
5362 *file = ga.ga_data;
5363 *num_file = ga.ga_len;
5364 return OK;
5365}
5366
5367/*
5368 * Expand names with a list returned by a function defined by the user.
5369 */
5370 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371ExpandUserList(
5372 expand_T *xp,
5373 int *num_file,
5374 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005375{
5376 list_T *retlist;
5377 listitem_T *li;
5378 garray_T ga;
5379
5380 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5381 if (retlist == NULL)
5382 return FAIL;
5383
5384 ga_init2(&ga, (int)sizeof(char *), 3);
5385 /* Loop over the items in the list. */
5386 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5387 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005388 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5389 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005390
5391 if (ga_grow(&ga, 1) == FAIL)
5392 break;
5393
5394 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005395 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005396 ++ga.ga_len;
5397 }
5398 list_unref(retlist);
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 *file = ga.ga_data;
5401 *num_file = ga.ga_len;
5402 return OK;
5403}
5404#endif
5405
5406/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005407 * Expand color scheme, compiler or filetype names.
5408 * Search from 'runtimepath':
5409 * 'runtimepath'/{dirnames}/{pat}.vim
5410 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5411 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5412 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5413 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005414 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 */
5416 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417ExpandRTDir(
5418 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005419 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005420 int *num_file,
5421 char_u ***file,
5422 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 char_u *s;
5425 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005426 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005428 int i;
5429 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
5431 *num_file = 0;
5432 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005433 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005434 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005436 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005438 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5439 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005441 ga_clear_strings(&ga);
5442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005444 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005445 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005446 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005448
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005449 if (flags & DIP_START) {
5450 for (i = 0; dirnames[i] != NULL; ++i)
5451 {
5452 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5453 if (s == NULL)
5454 {
5455 ga_clear_strings(&ga);
5456 return FAIL;
5457 }
5458 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5459 globpath(p_pp, s, &ga, 0);
5460 vim_free(s);
5461 }
5462 }
5463
5464 if (flags & DIP_OPT) {
5465 for (i = 0; dirnames[i] != NULL; ++i)
5466 {
5467 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5468 if (s == NULL)
5469 {
5470 ga_clear_strings(&ga);
5471 return FAIL;
5472 }
5473 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5474 globpath(p_pp, s, &ga, 0);
5475 vim_free(s);
5476 }
5477 }
5478
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005479 for (i = 0; i < ga.ga_len; ++i)
5480 {
5481 match = ((char_u **)ga.ga_data)[i];
5482 s = match;
5483 e = s + STRLEN(s);
5484 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5485 {
5486 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005487 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005488 if (s < match || vim_ispathsep(*s))
5489 break;
5490 ++s;
5491 *e = NUL;
5492 mch_memmove(match, s, e - s + 1);
5493 }
5494 }
5495
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005496 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005497 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005498
5499 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005500 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005501 remove_duplicates(&ga);
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 *file = ga.ga_data;
5504 *num_file = ga.ga_len;
5505 return OK;
5506}
5507
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005508/*
5509 * Expand loadplugin names:
5510 * 'packpath'/pack/ * /opt/{pat}
5511 */
5512 static int
5513ExpandPackAddDir(
5514 char_u *pat,
5515 int *num_file,
5516 char_u ***file)
5517{
5518 char_u *s;
5519 char_u *e;
5520 char_u *match;
5521 garray_T ga;
5522 int i;
5523 int pat_len;
5524
5525 *num_file = 0;
5526 *file = NULL;
5527 pat_len = (int)STRLEN(pat);
5528 ga_init2(&ga, (int)sizeof(char *), 10);
5529
5530 s = alloc((unsigned)(pat_len + 26));
5531 if (s == NULL)
5532 {
5533 ga_clear_strings(&ga);
5534 return FAIL;
5535 }
5536 sprintf((char *)s, "pack/*/opt/%s*", pat);
5537 globpath(p_pp, s, &ga, 0);
5538 vim_free(s);
5539
5540 for (i = 0; i < ga.ga_len; ++i)
5541 {
5542 match = ((char_u **)ga.ga_data)[i];
5543 s = gettail(match);
5544 e = s + STRLEN(s);
5545 mch_memmove(match, s, e - s + 1);
5546 }
5547
5548 if (ga.ga_len == 0)
5549 return FAIL;
5550
5551 /* Sort and remove duplicates which can happen when specifying multiple
5552 * directories in dirnames. */
5553 remove_duplicates(&ga);
5554
5555 *file = ga.ga_data;
5556 *num_file = ga.ga_len;
5557 return OK;
5558}
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560#endif
5561
5562#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5563/*
5564 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005565 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005567 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568globpath(
5569 char_u *path,
5570 char_u *file,
5571 garray_T *ga,
5572 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
5574 expand_T xpc;
5575 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 int num_p;
5578 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 buf = alloc(MAXPATHL);
5581 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005582 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005584 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 /* Loop over all entries in {path}. */
5588 while (*path != NUL)
5589 {
5590 /* Copy one item of the path to buf[] and concatenate the file name. */
5591 copy_option_part(&path, buf, MAXPATHL, ",");
5592 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5593 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005594# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005595 /* Using the platform's path separator (\) makes vim incorrectly
5596 * treat it as an escape character, use '/' instead. */
5597 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5598 STRCAT(buf, "/");
5599# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005603 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5604 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005606 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005608 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 for (i = 0; i < num_p; ++i)
5611 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005612 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005613 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005614 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 FreeWild(num_p, p);
5619 }
5620 }
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624}
5625
5626#endif
5627
5628#if defined(FEAT_CMDHIST) || defined(PROTO)
5629
5630/*********************************
5631 * Command line history stuff *
5632 *********************************/
5633
5634/*
5635 * Translate a history character to the associated type number.
5636 */
5637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 if (c == ':')
5641 return HIST_CMD;
5642 if (c == '=')
5643 return HIST_EXPR;
5644 if (c == '@')
5645 return HIST_INPUT;
5646 if (c == '>')
5647 return HIST_DEBUG;
5648 return HIST_SEARCH; /* must be '?' or '/' */
5649}
5650
5651/*
5652 * Table of history names.
5653 * These names are used in :history and various hist...() functions.
5654 * It is sufficient to give the significant prefix of a history name.
5655 */
5656
5657static char *(history_names[]) =
5658{
5659 "cmd",
5660 "search",
5661 "expr",
5662 "input",
5663 "debug",
5664 NULL
5665};
5666
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005667#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5668/*
5669 * Function given to ExpandGeneric() to obtain the possible first
5670 * arguments of the ":history command.
5671 */
5672 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005674{
5675 static char_u compl[2] = { NUL, NUL };
5676 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005677 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005678 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5679
5680 if (idx < short_names_count)
5681 {
5682 compl[0] = (char_u)short_names[idx];
5683 return compl;
5684 }
5685 if (idx < short_names_count + history_name_count)
5686 return (char_u *)history_names[idx - short_names_count];
5687 if (idx == short_names_count + history_name_count)
5688 return (char_u *)"all";
5689 return NULL;
5690}
5691#endif
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693/*
5694 * init_history() - Initialize the command line history.
5695 * Also used to re-allocate the history when the size changes.
5696 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 int newlen; /* new length of history table */
5701 histentry_T *temp;
5702 int i;
5703 int j;
5704 int type;
5705
5706 /*
5707 * If size of history table changed, reallocate it
5708 */
5709 newlen = (int)p_hi;
5710 if (newlen != hislen) /* history length changed */
5711 {
5712 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5713 {
5714 if (newlen)
5715 {
5716 temp = (histentry_T *)lalloc(
5717 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5718 if (temp == NULL) /* out of memory! */
5719 {
5720 if (type == 0) /* first one: just keep the old length */
5721 {
5722 newlen = hislen;
5723 break;
5724 }
5725 /* Already changed one table, now we can only have zero
5726 * length for all tables. */
5727 newlen = 0;
5728 type = -1;
5729 continue;
5730 }
5731 }
5732 else
5733 temp = NULL;
5734 if (newlen == 0 || temp != NULL)
5735 {
5736 if (hisidx[type] < 0) /* there are no entries yet */
5737 {
5738 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005739 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
5741 else if (newlen > hislen) /* array becomes bigger */
5742 {
5743 for (i = 0; i <= hisidx[type]; ++i)
5744 temp[i] = history[type][i];
5745 j = i;
5746 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005747 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 for ( ; j < hislen; ++i, ++j)
5749 temp[i] = history[type][j];
5750 }
5751 else /* array becomes smaller or 0 */
5752 {
5753 j = hisidx[type];
5754 for (i = newlen - 1; ; --i)
5755 {
5756 if (i >= 0) /* copy newest entries */
5757 temp[i] = history[type][j];
5758 else /* remove older entries */
5759 vim_free(history[type][j].hisstr);
5760 if (--j < 0)
5761 j = hislen - 1;
5762 if (j == hisidx[type])
5763 break;
5764 }
5765 hisidx[type] = newlen - 1;
5766 }
5767 vim_free(history[type]);
5768 history[type] = temp;
5769 }
5770 }
5771 hislen = newlen;
5772 }
5773}
5774
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005776clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005777{
5778 hisptr->hisnum = 0;
5779 hisptr->viminfo = FALSE;
5780 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005781 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005782}
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784/*
5785 * Check if command line 'str' is already in history.
5786 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5787 */
5788 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789in_history(
5790 int type,
5791 char_u *str,
5792 int move_to_front, /* Move the entry to the front if it exists */
5793 int sep,
5794 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 int i;
5797 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005798 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
5800 if (hisidx[type] < 0)
5801 return FALSE;
5802 i = hisidx[type];
5803 do
5804 {
5805 if (history[type][i].hisstr == NULL)
5806 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005807
5808 /* For search history, check that the separator character matches as
5809 * well. */
5810 p = history[type][i].hisstr;
5811 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005812 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005813 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
5815 if (!move_to_front)
5816 return TRUE;
5817 last_i = i;
5818 break;
5819 }
5820 if (--i < 0)
5821 i = hislen - 1;
5822 } while (i != hisidx[type]);
5823
5824 if (last_i >= 0)
5825 {
5826 str = history[type][i].hisstr;
5827 while (i != hisidx[type])
5828 {
5829 if (++i >= hislen)
5830 i = 0;
5831 history[type][last_i] = history[type][i];
5832 last_i = i;
5833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005835 history[type][i].viminfo = FALSE;
5836 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005837 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 return TRUE;
5839 }
5840 return FALSE;
5841}
5842
5843/*
5844 * Convert history name (from table above) to its HIST_ equivalent.
5845 * When "name" is empty, return "cmd" history.
5846 * Returns -1 for unknown history name.
5847 */
5848 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005849get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 int i;
5852 int len = (int)STRLEN(name);
5853
5854 /* No argument: use current history. */
5855 if (len == 0)
5856 return hist_char2type(ccline.cmdfirstc);
5857
5858 for (i = 0; history_names[i] != NULL; ++i)
5859 if (STRNICMP(name, history_names[i], len) == 0)
5860 return i;
5861
5862 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5863 return hist_char2type(name[0]);
5864
5865 return -1;
5866}
5867
5868static int last_maptick = -1; /* last seen maptick */
5869
5870/*
5871 * Add the given string to the given history. If the string is already in the
5872 * history then it is moved to the front. "histype" may be one of he HIST_
5873 * values.
5874 */
5875 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005876add_to_history(
5877 int histype,
5878 char_u *new_entry,
5879 int in_map, /* consider maptick when inside a mapping */
5880 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 histentry_T *hisptr;
5883 int len;
5884
5885 if (hislen == 0) /* no history */
5886 return;
5887
Bram Moolenaara939e432013-11-09 05:30:26 +01005888 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5889 return;
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 /*
5892 * Searches inside the same mapping overwrite each other, so that only
5893 * the last line is kept. Be careful not to remove a line that was moved
5894 * down, only lines that were added.
5895 */
5896 if (histype == HIST_SEARCH && in_map)
5897 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005898 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 /* Current line is from the same mapping, remove it */
5901 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5902 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005903 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 --hisnum[histype];
5905 if (--hisidx[HIST_SEARCH] < 0)
5906 hisidx[HIST_SEARCH] = hislen - 1;
5907 }
5908 last_maptick = -1;
5909 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005910 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {
5912 if (++hisidx[histype] == hislen)
5913 hisidx[histype] = 0;
5914 hisptr = &history[histype][hisidx[histype]];
5915 vim_free(hisptr->hisstr);
5916
5917 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005918 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5920 if (hisptr->hisstr != NULL)
5921 hisptr->hisstr[len + 1] = sep;
5922
5923 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005924 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005925 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 if (histype == HIST_SEARCH && in_map)
5927 last_maptick = maptick;
5928 }
5929}
5930
5931#if defined(FEAT_EVAL) || defined(PROTO)
5932
5933/*
5934 * Get identifier of newest history entry.
5935 * "histype" may be one of the HIST_ values.
5936 */
5937 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005938get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5941 || hisidx[histype] < 0)
5942 return -1;
5943
5944 return history[histype][hisidx[histype]].hisnum;
5945}
5946
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 * Calculate history index from a number:
5949 * num > 0: seen as identifying number of a history entry
5950 * num < 0: relative position in history wrt newest entry
5951 * "histype" may be one of the HIST_ values.
5952 */
5953 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 int i;
5957 histentry_T *hist;
5958 int wrapped = FALSE;
5959
5960 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5961 || (i = hisidx[histype]) < 0 || num == 0)
5962 return -1;
5963
5964 hist = history[histype];
5965 if (num > 0)
5966 {
5967 while (hist[i].hisnum > num)
5968 if (--i < 0)
5969 {
5970 if (wrapped)
5971 break;
5972 i += hislen;
5973 wrapped = TRUE;
5974 }
5975 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5976 return i;
5977 }
5978 else if (-num <= hislen)
5979 {
5980 i += num + 1;
5981 if (i < 0)
5982 i += hislen;
5983 if (hist[i].hisstr != NULL)
5984 return i;
5985 }
5986 return -1;
5987}
5988
5989/*
5990 * Get a history entry by its index.
5991 * "histype" may be one of the HIST_ values.
5992 */
5993 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005994get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
5996 idx = calc_hist_idx(histype, idx);
5997 if (idx >= 0)
5998 return history[histype][idx].hisstr;
5999 else
6000 return (char_u *)"";
6001}
6002
6003/*
6004 * Clear all entries of a history.
6005 * "histype" may be one of the HIST_ values.
6006 */
6007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006008clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 int i;
6011 histentry_T *hisptr;
6012
6013 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6014 {
6015 hisptr = history[histype];
6016 for (i = hislen; i--;)
6017 {
6018 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006019 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006020 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022 hisidx[histype] = -1; /* mark history as cleared */
6023 hisnum[histype] = 0; /* reset identifier counter */
6024 return OK;
6025 }
6026 return FAIL;
6027}
6028
6029/*
6030 * Remove all entries matching {str} from a history.
6031 * "histype" may be one of the HIST_ values.
6032 */
6033 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006034del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 regmatch_T regmatch;
6037 histentry_T *hisptr;
6038 int idx;
6039 int i;
6040 int last;
6041 int found = FALSE;
6042
6043 regmatch.regprog = NULL;
6044 regmatch.rm_ic = FALSE; /* always match case */
6045 if (hislen != 0
6046 && histype >= 0
6047 && histype < HIST_COUNT
6048 && *str != NUL
6049 && (idx = hisidx[histype]) >= 0
6050 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6051 != NULL)
6052 {
6053 i = last = idx;
6054 do
6055 {
6056 hisptr = &history[histype][i];
6057 if (hisptr->hisstr == NULL)
6058 break;
6059 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6060 {
6061 found = TRUE;
6062 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006063 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 }
6065 else
6066 {
6067 if (i != last)
6068 {
6069 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006070 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 }
6072 if (--last < 0)
6073 last += hislen;
6074 }
6075 if (--i < 0)
6076 i += hislen;
6077 } while (i != idx);
6078 if (history[histype][idx].hisstr == NULL)
6079 hisidx[histype] = -1;
6080 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006081 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 return found;
6083}
6084
6085/*
6086 * Remove an indexed entry from a history.
6087 * "histype" may be one of the HIST_ values.
6088 */
6089 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006090del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 int i, j;
6093
6094 i = calc_hist_idx(histype, idx);
6095 if (i < 0)
6096 return FALSE;
6097 idx = hisidx[histype];
6098 vim_free(history[histype][i].hisstr);
6099
6100 /* When deleting the last added search string in a mapping, reset
6101 * last_maptick, so that the last added search string isn't deleted again.
6102 */
6103 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6104 last_maptick = -1;
6105
6106 while (i != idx)
6107 {
6108 j = (i + 1) % hislen;
6109 history[histype][i] = history[histype][j];
6110 i = j;
6111 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006112 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (--i < 0)
6114 i += hislen;
6115 hisidx[histype] = i;
6116 return TRUE;
6117}
6118
6119#endif /* FEAT_EVAL */
6120
6121#if defined(FEAT_CRYPT) || defined(PROTO)
6122/*
6123 * Very specific function to remove the value in ":set key=val" from the
6124 * history.
6125 */
6126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006127remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 char_u *p;
6130 int i;
6131
6132 i = hisidx[HIST_CMD];
6133 if (i < 0)
6134 return;
6135 p = history[HIST_CMD][i].hisstr;
6136 if (p != NULL)
6137 for ( ; *p; ++p)
6138 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6139 {
6140 p = vim_strchr(p + 3, '=');
6141 if (p == NULL)
6142 break;
6143 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006144 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (p[i] == '\\' && p[i + 1])
6146 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006147 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 --p;
6149 }
6150}
6151#endif
6152
6153#endif /* FEAT_CMDHIST */
6154
Bram Moolenaar064154c2016-03-19 22:50:43 +01006155#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006156/*
6157 * Get pointer to the command line info to use. cmdline_paste() may clear
6158 * ccline and put the previous value in prev_ccline.
6159 */
6160 static struct cmdline_info *
6161get_ccline_ptr(void)
6162{
6163 if ((State & CMDLINE) == 0)
6164 return NULL;
6165 if (ccline.cmdbuff != NULL)
6166 return &ccline;
6167 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6168 return &prev_ccline;
6169 return NULL;
6170}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006171#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006172
Bram Moolenaar064154c2016-03-19 22:50:43 +01006173#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006174/*
6175 * Get the current command line in allocated memory.
6176 * Only works when the command line is being edited.
6177 * Returns NULL when something is wrong.
6178 */
6179 char_u *
6180get_cmdline_str(void)
6181{
6182 struct cmdline_info *p = get_ccline_ptr();
6183
6184 if (p == NULL)
6185 return NULL;
6186 return vim_strnsave(p->cmdbuff, p->cmdlen);
6187}
6188
6189/*
6190 * Get the current command line position, counted in bytes.
6191 * Zero is the first position.
6192 * Only works when the command line is being edited.
6193 * Returns -1 when something is wrong.
6194 */
6195 int
6196get_cmdline_pos(void)
6197{
6198 struct cmdline_info *p = get_ccline_ptr();
6199
6200 if (p == NULL)
6201 return -1;
6202 return p->cmdpos;
6203}
6204
6205/*
6206 * Set the command line byte position to "pos". Zero is the first position.
6207 * Only works when the command line is being edited.
6208 * Returns 1 when failed, 0 when OK.
6209 */
6210 int
6211set_cmdline_pos(
6212 int pos)
6213{
6214 struct cmdline_info *p = get_ccline_ptr();
6215
6216 if (p == NULL)
6217 return 1;
6218
6219 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6220 * changed the command line. */
6221 if (pos < 0)
6222 new_cmdpos = 0;
6223 else
6224 new_cmdpos = pos;
6225 return 0;
6226}
6227#endif
6228
6229#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6230/*
6231 * Get the current command-line type.
6232 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6233 * Only works when the command line is being edited.
6234 * Returns NUL when something is wrong.
6235 */
6236 int
6237get_cmdline_type(void)
6238{
6239 struct cmdline_info *p = get_ccline_ptr();
6240
6241 if (p == NULL)
6242 return NUL;
6243 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006244 return
6245# ifdef FEAT_EVAL
6246 (p->input_fn) ? '@' :
6247# endif
6248 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006249 return p->cmdfirstc;
6250}
6251#endif
6252
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6254/*
6255 * Get indices "num1,num2" that specify a range within a list (not a range of
6256 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6257 * Returns OK if parsed successfully, otherwise FAIL.
6258 */
6259 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 int len;
6263 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006264 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266 *str = skipwhite(*str);
6267 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6268 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006269 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 *str += len;
6271 *num1 = (int)num;
6272 first = TRUE;
6273 }
6274 *str = skipwhite(*str);
6275 if (**str == ',') /* parse "to" part of range */
6276 {
6277 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006278 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (len > 0)
6280 {
6281 *num2 = (int)num;
6282 *str = skipwhite(*str + len);
6283 }
6284 else if (!first) /* no number given at all */
6285 return FAIL;
6286 }
6287 else if (first) /* only one number given */
6288 *num2 = *num1;
6289 return OK;
6290}
6291#endif
6292
6293#if defined(FEAT_CMDHIST) || defined(PROTO)
6294/*
6295 * :history command - print a history
6296 */
6297 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006298ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 histentry_T *hist;
6301 int histype1 = HIST_CMD;
6302 int histype2 = HIST_CMD;
6303 int hisidx1 = 1;
6304 int hisidx2 = -1;
6305 int idx;
6306 int i, j, k;
6307 char_u *end;
6308 char_u *arg = eap->arg;
6309
6310 if (hislen == 0)
6311 {
6312 MSG(_("'history' option is zero"));
6313 return;
6314 }
6315
6316 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6317 {
6318 end = arg;
6319 while (ASCII_ISALPHA(*end)
6320 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6321 end++;
6322 i = *end;
6323 *end = NUL;
6324 histype1 = get_histtype(arg);
6325 if (histype1 == -1)
6326 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006327 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 {
6329 histype1 = 0;
6330 histype2 = HIST_COUNT-1;
6331 }
6332 else
6333 {
6334 *end = i;
6335 EMSG(_(e_trailing));
6336 return;
6337 }
6338 }
6339 else
6340 histype2 = histype1;
6341 *end = i;
6342 }
6343 else
6344 end = arg;
6345 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6346 {
6347 EMSG(_(e_trailing));
6348 return;
6349 }
6350
6351 for (; !got_int && histype1 <= histype2; ++histype1)
6352 {
6353 STRCPY(IObuff, "\n # ");
6354 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6355 MSG_PUTS_TITLE(IObuff);
6356 idx = hisidx[histype1];
6357 hist = history[histype1];
6358 j = hisidx1;
6359 k = hisidx2;
6360 if (j < 0)
6361 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6362 if (k < 0)
6363 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6364 if (idx >= 0 && j <= k)
6365 for (i = idx + 1; !got_int; ++i)
6366 {
6367 if (i == hislen)
6368 i = 0;
6369 if (hist[i].hisstr != NULL
6370 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6371 {
6372 msg_putchar('\n');
6373 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6374 hist[i].hisnum);
6375 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6376 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006377 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 else
6379 STRCAT(IObuff, hist[i].hisstr);
6380 msg_outtrans(IObuff);
6381 out_flush();
6382 }
6383 if (i == idx)
6384 break;
6385 }
6386 }
6387}
6388#endif
6389
6390#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006391/*
6392 * Buffers for history read from a viminfo file. Only valid while reading.
6393 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006394static histentry_T *viminfo_history[HIST_COUNT] =
6395 {NULL, NULL, NULL, NULL, NULL};
6396static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6397static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398static int viminfo_add_at_front = FALSE;
6399
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006400static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402/*
6403 * Translate a history type number to the associated character.
6404 */
6405 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006406hist_type2char(
6407 int type,
6408 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 if (type == HIST_CMD)
6411 return ':';
6412 if (type == HIST_SEARCH)
6413 {
6414 if (use_question)
6415 return '?';
6416 else
6417 return '/';
6418 }
6419 if (type == HIST_EXPR)
6420 return '=';
6421 return '@';
6422}
6423
6424/*
6425 * Prepare for reading the history from the viminfo file.
6426 * This allocates history arrays to store the read history lines.
6427 */
6428 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006429prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 int i;
6432 int num;
6433 int type;
6434 int len;
6435
6436 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006437 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (asklen > hislen)
6439 asklen = hislen;
6440
6441 for (type = 0; type < HIST_COUNT; ++type)
6442 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006443 /* Count the number of empty spaces in the history list. Entries read
6444 * from viminfo previously are also considered empty. If there are
6445 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006447 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 num++;
6449 len = asklen;
6450 if (num > len)
6451 len = num;
6452 if (len <= 0)
6453 viminfo_history[type] = NULL;
6454 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006455 viminfo_history[type] = (histentry_T *)lalloc(
6456 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (viminfo_history[type] == NULL)
6458 len = 0;
6459 viminfo_hislen[type] = len;
6460 viminfo_hisidx[type] = 0;
6461 }
6462}
6463
6464/*
6465 * Accept a line from the viminfo, store it in the history array when it's
6466 * new.
6467 */
6468 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006469read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 int type;
6472 long_u len;
6473 char_u *val;
6474 char_u *p;
6475
6476 type = hist_char2type(virp->vir_line[0]);
6477 if (viminfo_hisidx[type] < viminfo_hislen[type])
6478 {
6479 val = viminfo_readstring(virp, 1, TRUE);
6480 if (val != NULL && *val != NUL)
6481 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006482 int sep = (*val == ' ' ? NUL : *val);
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006485 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
6487 /* Need to re-allocate to append the separator byte. */
6488 len = STRLEN(val);
6489 p = lalloc(len + 2, TRUE);
6490 if (p != NULL)
6491 {
6492 if (type == HIST_SEARCH)
6493 {
6494 /* Search entry: Move the separator from the first
6495 * column to after the NUL. */
6496 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006497 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
6499 else
6500 {
6501 /* Not a search entry: No separator in the viminfo
6502 * file, add a NUL separator. */
6503 mch_memmove(p, val, (size_t)len + 1);
6504 p[len + 1] = NUL;
6505 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006506 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6507 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006508 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6509 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006510 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 }
6512 }
6513 }
6514 vim_free(val);
6515 }
6516 return viminfo_readline(virp);
6517}
6518
Bram Moolenaar07219f92013-04-14 23:19:36 +02006519/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006520 * Accept a new style history line from the viminfo, store it in the history
6521 * array when it's new.
6522 */
6523 void
6524handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006525 garray_T *values,
6526 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006527{
6528 int type;
6529 long_u len;
6530 char_u *val;
6531 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006532 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006533
6534 /* Check the format:
6535 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006536 if (values->ga_len < 4
6537 || vp[0].bv_type != BVAL_NR
6538 || vp[1].bv_type != BVAL_NR
6539 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6540 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006541 return;
6542
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006543 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006544 if (type >= HIST_COUNT)
6545 return;
6546 if (viminfo_hisidx[type] < viminfo_hislen[type])
6547 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006548 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006549 if (val != NULL && *val != NUL)
6550 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006551 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6552 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006553 int idx;
6554 int overwrite = FALSE;
6555
6556 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6557 {
6558 /* If lines were written by an older Vim we need to avoid
6559 * getting duplicates. See if the entry already exists. */
6560 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6561 {
6562 p = viminfo_history[type][idx].hisstr;
6563 if (STRCMP(val, p) == 0
6564 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6565 {
6566 overwrite = TRUE;
6567 break;
6568 }
6569 }
6570
6571 if (!overwrite)
6572 {
6573 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006574 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006575 p = lalloc(len + 2, TRUE);
6576 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006577 else
6578 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006579 if (p != NULL)
6580 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006581 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006582 if (!overwrite)
6583 {
6584 mch_memmove(p, val, (size_t)len + 1);
6585 /* Put the separator after the NUL. */
6586 p[len + 1] = sep;
6587 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006588 viminfo_history[type][idx].hisnum = 0;
6589 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006590 viminfo_hisidx[type]++;
6591 }
6592 }
6593 }
6594 }
6595 }
6596}
6597
6598/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006599 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006600 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006601 static void
6602concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 int idx;
6605 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006606
6607 idx = hisidx[type] + viminfo_hisidx[type];
6608 if (idx >= hislen)
6609 idx -= hislen;
6610 else if (idx < 0)
6611 idx = hislen - 1;
6612 if (viminfo_add_at_front)
6613 hisidx[type] = idx;
6614 else
6615 {
6616 if (hisidx[type] == -1)
6617 hisidx[type] = hislen - 1;
6618 do
6619 {
6620 if (history[type][idx].hisstr != NULL
6621 || history[type][idx].viminfo)
6622 break;
6623 if (++idx == hislen)
6624 idx = 0;
6625 } while (idx != hisidx[type]);
6626 if (idx != hisidx[type] && --idx < 0)
6627 idx = hislen - 1;
6628 }
6629 for (i = 0; i < viminfo_hisidx[type]; i++)
6630 {
6631 vim_free(history[type][idx].hisstr);
6632 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6633 history[type][idx].viminfo = TRUE;
6634 history[type][idx].time_set = viminfo_history[type][i].time_set;
6635 if (--idx < 0)
6636 idx = hislen - 1;
6637 }
6638 idx += 1;
6639 idx %= hislen;
6640 for (i = 0; i < viminfo_hisidx[type]; i++)
6641 {
6642 history[type][idx++].hisnum = ++hisnum[type];
6643 idx %= hislen;
6644 }
6645}
6646
6647#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6648 static int
6649#ifdef __BORLANDC__
6650_RTLENTRYF
6651#endif
6652sort_hist(const void *s1, const void *s2)
6653{
6654 histentry_T *p1 = *(histentry_T **)s1;
6655 histentry_T *p2 = *(histentry_T **)s2;
6656
6657 if (p1->time_set < p2->time_set) return -1;
6658 if (p1->time_set > p2->time_set) return 1;
6659 return 0;
6660}
6661#endif
6662
6663/*
6664 * Merge history lines from viminfo and lines typed in this Vim based on the
6665 * timestamp;
6666 */
6667 static void
6668merge_history(int type)
6669{
6670 int max_len;
6671 histentry_T **tot_hist;
6672 histentry_T *new_hist;
6673 int i;
6674 int len;
6675
6676 /* Make one long list with all entries. */
6677 max_len = hislen + viminfo_hisidx[type];
6678 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6679 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6680 if (tot_hist == NULL || new_hist == NULL)
6681 {
6682 vim_free(tot_hist);
6683 vim_free(new_hist);
6684 return;
6685 }
6686 for (i = 0; i < viminfo_hisidx[type]; i++)
6687 tot_hist[i] = &viminfo_history[type][i];
6688 len = i;
6689 for (i = 0; i < hislen; i++)
6690 if (history[type][i].hisstr != NULL)
6691 tot_hist[len++] = &history[type][i];
6692
6693 /* Sort the list on timestamp. */
6694 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6695
6696 /* Keep the newest ones. */
6697 for (i = 0; i < hislen; i++)
6698 {
6699 if (i < len)
6700 {
6701 new_hist[i] = *tot_hist[i];
6702 tot_hist[i]->hisstr = NULL;
6703 if (new_hist[i].hisnum == 0)
6704 new_hist[i].hisnum = ++hisnum[type];
6705 }
6706 else
6707 clear_hist_entry(&new_hist[i]);
6708 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006709 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006710
6711 /* Free what is not kept. */
6712 for (i = 0; i < viminfo_hisidx[type]; i++)
6713 vim_free(viminfo_history[type][i].hisstr);
6714 for (i = 0; i < hislen; i++)
6715 vim_free(history[type][i].hisstr);
6716 vim_free(history[type]);
6717 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006718 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006719}
6720
6721/*
6722 * Finish reading history lines from viminfo. Not used when writing viminfo.
6723 */
6724 void
6725finish_viminfo_history(vir_T *virp)
6726{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006728 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 for (type = 0; type < HIST_COUNT; ++type)
6731 {
6732 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006733 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006734
6735 if (merge)
6736 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006738 concat_history(type);
6739
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 vim_free(viminfo_history[type]);
6741 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006742 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744}
6745
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006746/*
6747 * Write history to viminfo file in "fp".
6748 * When "merge" is TRUE merge history lines with a previously read viminfo
6749 * file, data is in viminfo_history[].
6750 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006753write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 int i;
6756 int type;
6757 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006758 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760 init_history();
6761 if (hislen == 0)
6762 return;
6763 for (type = 0; type < HIST_COUNT; ++type)
6764 {
6765 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6766 if (num_saved == 0)
6767 continue;
6768 if (num_saved < 0) /* Use default */
6769 num_saved = hislen;
6770 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6771 type == HIST_CMD ? _("Command Line") :
6772 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006773 type == HIST_EXPR ? _("Expression") :
6774 type == HIST_INPUT ? _("Input Line") :
6775 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 if (num_saved > hislen)
6777 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006778
6779 /*
6780 * Merge typed and viminfo history:
6781 * round 1: history of typed commands.
6782 * round 2: history from recently read viminfo.
6783 */
6784 for (round = 1; round <= 2; ++round)
6785 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006786 if (round == 1)
6787 /* start at newest entry, somewhere in the list */
6788 i = hisidx[type];
6789 else if (viminfo_hisidx[type] > 0)
6790 /* start at newest entry, first in the list */
6791 i = 0;
6792 else
6793 /* empty list */
6794 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006795 if (i >= 0)
6796 while (num_saved > 0
6797 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006799 char_u *p;
6800 time_t timestamp;
6801 int c = NUL;
6802
6803 if (round == 1)
6804 {
6805 p = history[type][i].hisstr;
6806 timestamp = history[type][i].time_set;
6807 }
6808 else
6809 {
6810 p = viminfo_history[type] == NULL ? NULL
6811 : viminfo_history[type][i].hisstr;
6812 timestamp = viminfo_history[type] == NULL ? 0
6813 : viminfo_history[type][i].time_set;
6814 }
6815
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006816 if (p != NULL && (round == 2
6817 || !merge
6818 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006820 --num_saved;
6821 fputc(hist_type2char(type, TRUE), fp);
6822 /* For the search history: put the separator in the
6823 * second column; use a space if there isn't one. */
6824 if (type == HIST_SEARCH)
6825 {
6826 c = p[STRLEN(p) + 1];
6827 putc(c == NUL ? ' ' : c, fp);
6828 }
6829 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006830
6831 {
6832 char cbuf[NUMBUFLEN];
6833
6834 /* New style history with a bar line. Format:
6835 * |{bartype},{histtype},{timestamp},{separator},"text" */
6836 if (c == NUL)
6837 cbuf[0] = NUL;
6838 else
6839 sprintf(cbuf, "%d", c);
6840 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6841 type, (long)timestamp, cbuf);
6842 barline_writestring(fp, p, LSIZE - 20);
6843 putc('\n', fp);
6844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006846 if (round == 1)
6847 {
6848 /* Decrement index, loop around and stop when back at
6849 * the start. */
6850 if (--i < 0)
6851 i = hislen - 1;
6852 if (i == hisidx[type])
6853 break;
6854 }
6855 else
6856 {
6857 /* Increment index. Stop at the end in the while. */
6858 ++i;
6859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006861 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006862 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006863 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006864 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006865 vim_free(viminfo_history[type]);
6866 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006867 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869}
6870#endif /* FEAT_VIMINFO */
6871
6872#if defined(FEAT_FKMAP) || defined(PROTO)
6873/*
6874 * Write a character at the current cursor+offset position.
6875 * It is directly written into the command buffer block.
6876 */
6877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006878cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6881 {
6882 EMSG(_("E198: cmd_pchar beyond the command length"));
6883 return;
6884 }
6885 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6886 ccline.cmdbuff[ccline.cmdlen] = NUL;
6887}
6888
6889 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006890cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6893 {
6894 /* EMSG(_("cmd_gchar beyond the command length")); */
6895 return NUL;
6896 }
6897 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6898}
6899#endif
6900
6901#if defined(FEAT_CMDWIN) || defined(PROTO)
6902/*
6903 * Open a window on the current command line and history. Allow editing in
6904 * the window. Returns when the window is closed.
6905 * Returns:
6906 * CR if the command is to be executed
6907 * Ctrl_C if it is to be abandoned
6908 * K_IGNORE if editing continues
6909 */
6910 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006911open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
6913 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006914 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006916 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 win_T *wp;
6918 int i;
6919 linenr_T lnum;
6920 int histtype;
6921 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 int save_restart_edit = restart_edit;
6923 int save_State = State;
6924 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006925#ifdef FEAT_RIGHTLEFT
6926 int save_cmdmsg_rl = cmdmsg_rl;
6927#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006928#ifdef FEAT_FOLDING
6929 int save_KeyTyped;
6930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
6932 /* Can't do this recursively. Can't do it when typing a password. */
6933 if (cmdwin_type != 0
6934# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6935 || cmdline_star > 0
6936# endif
6937 )
6938 {
6939 beep_flush();
6940 return K_IGNORE;
6941 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006942 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
6944 /* Save current window sizes. */
6945 win_size_save(&winsizes);
6946
6947# ifdef FEAT_AUTOCMD
6948 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006949 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006951 /* don't use a new tab page */
6952 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006953 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 /* Create a window for the command-line buffer. */
6956 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6957 {
6958 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006959# ifdef FEAT_AUTOCMD
6960 unblock_autocmds();
6961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 return K_IGNORE;
6963 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006964 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
6966 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006967 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006968 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006971#ifdef FEAT_FOLDING
6972 curwin->w_p_fen = FALSE;
6973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006975 curwin->w_p_rl = cmdmsg_rl;
6976 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006978 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
6980# ifdef FEAT_AUTOCMD
6981 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006982 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006983 /* But don't allow switching to another buffer. */
6984 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985# endif
6986
Bram Moolenaar46152342005-09-07 21:18:43 +00006987 /* Showing the prompt may have set need_wait_return, reset it. */
6988 need_wait_return = FALSE;
6989
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006990 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6992 {
6993 if (p_wc == TAB)
6994 {
6995 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6996 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6997 }
6998 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6999 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007000# ifdef FEAT_AUTOCMD
7001 --curbuf_lock;
7002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007004 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7005 * sets 'textwidth' to 78). */
7006 curbuf->b_p_tw = 0;
7007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 /* Fill the buffer with the history. */
7009 init_history();
7010 if (hislen > 0)
7011 {
7012 i = hisidx[histtype];
7013 if (i >= 0)
7014 {
7015 lnum = 0;
7016 do
7017 {
7018 if (++i == hislen)
7019 i = 0;
7020 if (history[histtype][i].hisstr != NULL)
7021 ml_append(lnum++, history[histtype][i].hisstr,
7022 (colnr_T)0, FALSE);
7023 }
7024 while (i != hisidx[histtype]);
7025 }
7026 }
7027
7028 /* Replace the empty last line with the current command-line and put the
7029 * cursor there. */
7030 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7031 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7032 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007033 changed_line_abv_curs();
7034 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007035 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
7037 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007038 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
7040 /* No Ex mode here! */
7041 exmode_active = 0;
7042
7043 State = NORMAL;
7044# ifdef FEAT_MOUSE
7045 setmouse();
7046# endif
7047
7048# ifdef FEAT_AUTOCMD
7049 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007050 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007051 if (restart_edit != 0) /* autocmd with ":startinsert" */
7052 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053# endif
7054
7055 i = RedrawingDisabled;
7056 RedrawingDisabled = 0;
7057
7058 /*
7059 * Call the main loop until <CR> or CTRL-C is typed.
7060 */
7061 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007062 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064 RedrawingDisabled = i;
7065
7066# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007067
7068# ifdef FEAT_FOLDING
7069 save_KeyTyped = KeyTyped;
7070# endif
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007073 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007074
7075# ifdef FEAT_FOLDING
7076 /* Restore KeyTyped in case it is modified by autocommands */
7077 KeyTyped = save_KeyTyped;
7078# endif
7079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080# endif
7081
Bram Moolenaarccc18222007-05-10 18:25:20 +00007082 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007083 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 cmdwin_type = 0;
7085
7086 exmode_active = save_exmode;
7087
Bram Moolenaarf9821062008-06-20 16:31:07 +00007088 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007090 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {
7092 cmdwin_result = Ctrl_C;
7093 EMSG(_("E199: Active window or buffer deleted"));
7094 }
7095 else
7096 {
7097# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7098 /* autocmds may abort script processing */
7099 if (aborting() && cmdwin_result != K_IGNORE)
7100 cmdwin_result = Ctrl_C;
7101# endif
7102 /* Set the new command line from the cmdline buffer. */
7103 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007104 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007106 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7107
7108 if (histtype == HIST_CMD)
7109 {
7110 /* Execute the command directly. */
7111 ccline.cmdbuff = vim_strsave((char_u *)p);
7112 cmdwin_result = CAR;
7113 }
7114 else
7115 {
7116 /* First need to cancel what we were doing. */
7117 ccline.cmdbuff = NULL;
7118 stuffcharReadbuff(':');
7119 stuffReadbuff((char_u *)p);
7120 stuffcharReadbuff(CAR);
7121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123 else if (cmdwin_result == K_XF2) /* :qa typed */
7124 {
7125 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7126 cmdwin_result = CAR;
7127 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007128 else if (cmdwin_result == Ctrl_C)
7129 {
7130 /* :q or :close, don't execute any command
7131 * and don't modify the cmd window. */
7132 ccline.cmdbuff = NULL;
7133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 else
7135 ccline.cmdbuff = vim_strsave(ml_get_curline());
7136 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007137 {
7138 ccline.cmdbuff = vim_strsave((char_u *)"");
7139 ccline.cmdlen = 0;
7140 ccline.cmdbufflen = 1;
7141 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 else
7145 {
7146 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7147 ccline.cmdbufflen = ccline.cmdlen + 1;
7148 ccline.cmdpos = curwin->w_cursor.col;
7149 if (ccline.cmdpos > ccline.cmdlen)
7150 ccline.cmdpos = ccline.cmdlen;
7151 if (cmdwin_result == K_IGNORE)
7152 {
7153 set_cmdspos_cursor();
7154 redrawcmd();
7155 }
7156 }
7157
7158# ifdef FEAT_AUTOCMD
7159 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007160 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007162# ifdef FEAT_CONCEAL
7163 /* Avoid command-line window first character being concealed. */
7164 curwin->w_p_cole = 0;
7165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007167 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 win_goto(old_curwin);
7169 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007170
7171 /* win_close() may have already wiped the buffer when 'bh' is
7172 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007173 if (bufref_valid(&bufref))
7174 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
7176 /* Restore window sizes. */
7177 win_size_restore(&winsizes);
7178
7179# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007180 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181# endif
7182 }
7183
7184 ga_clear(&winsizes);
7185 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007186# ifdef FEAT_RIGHTLEFT
7187 cmdmsg_rl = save_cmdmsg_rl;
7188# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190 State = save_State;
7191# ifdef FEAT_MOUSE
7192 setmouse();
7193# endif
7194
7195 return cmdwin_result;
7196}
7197#endif /* FEAT_CMDWIN */
7198
7199/*
7200 * Used for commands that either take a simple command string argument, or:
7201 * cmd << endmarker
7202 * {script}
7203 * endmarker
7204 * Returns a pointer to allocated memory with {script} or NULL.
7205 */
7206 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007207script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
7209 char_u *theline;
7210 char *end_pattern = NULL;
7211 char dot[] = ".";
7212 garray_T ga;
7213
7214 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7215 return NULL;
7216
7217 ga_init2(&ga, 1, 0x400);
7218
7219 if (cmd[2] != NUL)
7220 end_pattern = (char *)skipwhite(cmd + 2);
7221 else
7222 end_pattern = dot;
7223
7224 for (;;)
7225 {
7226 theline = eap->getline(
7227#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007228 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229#endif
7230 NUL, eap->cookie, 0);
7231
7232 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007233 {
7234 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238 ga_concat(&ga, theline);
7239 ga_append(&ga, '\n');
7240 vim_free(theline);
7241 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007242 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
7244 return (char_u *)ga.ga_data;
7245}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007246
7247#ifdef FEAT_SEARCH_EXTRA
7248 static void
7249set_search_match(pos_T *t)
7250{
7251 /*
7252 * First move cursor to end of match, then to the start. This
7253 * moves the whole match onto the screen when 'nowrap' is set.
7254 */
7255 t->lnum += search_match_lines;
7256 t->col = search_match_endcol;
7257 if (t->lnum > curbuf->b_ml.ml_line_count)
7258 {
7259 t->lnum = curbuf->b_ml.ml_line_count;
7260 coladvance((colnr_T)MAXCOL);
7261 }
7262}
7263#endif