blob: ceeeaf89f068e40de073ed530910978327c917b2 [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/*
162 * getcmdline() - accept a command line starting with firstc.
163 *
164 * firstc == ':' get ":" command line.
165 * firstc == '/' or '?' get search pattern
166 * firstc == '=' get expression
167 * firstc == '@' get text for input() function
168 * firstc == '>' get text for debug mode
169 * firstc == NUL get text for :insert command
170 * firstc == -1 like NUL, and break on CTRL-C
171 *
172 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
173 * command line.
174 *
175 * Careful: getcmdline() can be called recursively!
176 *
177 * Return pointer to allocated string if there is a commandline, NULL
178 * otherwise.
179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100181getcmdline(
182 int firstc,
183 long count UNUSED, /* only used for incremental search */
184 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185{
186 int c;
187 int i;
188 int j;
189 int gotesc = FALSE; /* TRUE when <ESC> just typed */
190 int do_abbr; /* when TRUE check for abbr. */
191#ifdef FEAT_CMDHIST
192 char_u *lookfor = NULL; /* string to match */
193 int hiscnt; /* current history line in use */
194 int histype; /* history type to be used */
195#endif
196#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200197 pos_T search_start; /* where 'incsearch' starts searching */
198 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200200 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200202 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200204 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200205 pos_T match_start = curwin->w_cursor;
206 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207# ifdef FEAT_DIFF
208 int old_topfill;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200209 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
211 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200212 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 int did_incsearch = FALSE;
214 int incsearch_postponed = FALSE;
215#endif
216 int did_wild_list = FALSE; /* did wild_list() recently */
217 int wim_index = 0; /* index in wim_flags[] */
218 int res;
219 int save_msg_scroll = msg_scroll;
220 int save_State = State; /* remember State when called */
221 int some_key_typed = FALSE; /* one of the keys was typed */
222#ifdef FEAT_MOUSE
223 /* mouse drag and release events are ignored, unless they are
224 * preceded with a mouse down event */
225 int ignore_drag_release = TRUE;
226#endif
227#ifdef FEAT_EVAL
228 int break_ctrl_c = FALSE;
229#endif
230 expand_T xpc;
231 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100232#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000233 /* Everything that may work recursively should save and restore the
234 * current command line in save_ccline. That includes update_screen(), a
235 * custom status line may invoke ":normal". */
236 struct cmdline_info save_ccline;
237#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200238#ifdef FEAT_AUTOCMD
239 int cmdline_type;
240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#ifdef FEAT_EVAL
243 if (firstc == -1)
244 {
245 firstc = NUL;
246 break_ctrl_c = TRUE;
247 }
248#endif
249#ifdef FEAT_RIGHTLEFT
250 /* start without Hebrew mapping for a command line */
251 if (firstc == ':' || firstc == '=' || firstc == '>')
252 cmd_hkmap = 0;
253#endif
254
255 ccline.overstrike = FALSE; /* always start in insert mode */
256#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100257 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200258 save_cursor = curwin->w_cursor; /* may be restored later */
259 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 old_curswant = curwin->w_curswant;
261 old_leftcol = curwin->w_leftcol;
262 old_topline = curwin->w_topline;
263# ifdef FEAT_DIFF
264 old_topfill = curwin->w_topfill;
265# endif
266 old_botline = curwin->w_botline;
267#endif
268
269 /*
270 * set some variables for redrawcmd()
271 */
272 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000273 ccline.cmdindent = (firstc > 0 ? indent : 0);
274
275 /* alloc initial ccline.cmdbuff */
276 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 if (ccline.cmdbuff == NULL)
278 return NULL; /* out of memory */
279 ccline.cmdlen = ccline.cmdpos = 0;
280 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100281 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000283 /* autoindent for :insert and :append */
284 if (firstc <= 0)
285 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200286 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000287 ccline.cmdbuff[indent] = NUL;
288 ccline.cmdpos = indent;
289 ccline.cmdspos = indent;
290 ccline.cmdlen = indent;
291 }
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000294 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295
296#ifdef FEAT_RIGHTLEFT
297 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
298 && (firstc == '/' || firstc == '?'))
299 cmdmsg_rl = TRUE;
300 else
301 cmdmsg_rl = FALSE;
302#endif
303
304 redir_off = TRUE; /* don't redirect the typed command */
305 if (!cmd_silent)
306 {
307 i = msg_scrolled;
308 msg_scrolled = 0; /* avoid wait_return message */
309 gotocmdline(TRUE);
310 msg_scrolled += i;
311 redrawcmdprompt(); /* draw prompt or indent */
312 set_cmdspos();
313 }
314 xpc.xp_context = EXPAND_NOTHING;
315 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000316#ifndef BACKSLASH_IN_FILENAME
317 xpc.xp_shell = FALSE;
318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000320#if defined(FEAT_EVAL)
321 if (ccline.input_fn)
322 {
323 xpc.xp_context = ccline.xp_context;
324 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000325# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000326 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000327# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000328 }
329#endif
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /*
332 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
333 * doing ":@0" when register 0 doesn't contain a CR.
334 */
335 msg_scroll = FALSE;
336
337 State = CMDLINE;
338
339 if (firstc == '/' || firstc == '?' || firstc == '@')
340 {
341 /* Use ":lmap" mappings for search pattern and input(). */
342 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
343 b_im_ptr = &curbuf->b_p_iminsert;
344 else
345 b_im_ptr = &curbuf->b_p_imsearch;
346 if (*b_im_ptr == B_IMODE_LMAP)
347 State |= LANGMAP;
348#ifdef USE_IM_CONTROL
349 im_set_active(*b_im_ptr == B_IMODE_IM);
350#endif
351 }
352#ifdef USE_IM_CONTROL
353 else if (p_imcmdline)
354 im_set_active(TRUE);
355#endif
356
357#ifdef FEAT_MOUSE
358 setmouse();
359#endif
360#ifdef CURSOR_SHAPE
361 ui_cursor_shape(); /* may show different cursor shape */
362#endif
363
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000364 /* When inside an autocommand for writing "exiting" may be set and
365 * terminal mode set to cooked. Need to set raw mode here then. */
366 settmode(TMODE_RAW);
367
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200368#ifdef FEAT_AUTOCMD
369 /* Trigger CmdlineEnter autocommands. */
370 cmdline_type = firstc == NUL ? '-' : firstc;
371 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
372#endif
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#ifdef FEAT_CMDHIST
375 init_history();
376 hiscnt = hislen; /* set hiscnt to impossible history value */
377 histype = hist_char2type(firstc);
378#endif
379
380#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000381 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#endif
383
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200384 /* If something above caused an error, reset the flags, we do want to type
385 * and execute commands. Display may be messed up a bit. */
386 if (did_emsg)
387 redrawcmd();
388 did_emsg = FALSE;
389 got_int = FALSE;
390
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 /*
392 * Collect the command string, handling editing keys.
393 */
394 for (;;)
395 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000396 redir_off = TRUE; /* Don't redirect the typed command.
397 Repeated, because a ":redir" inside
398 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#ifdef USE_ON_FLY_SCROLL
400 dont_scroll = FALSE; /* allow scrolling here */
401#endif
402 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
403
404 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000405
406 /* Get a character. Ignore K_IGNORE, it should not do anything, such
407 * as stop completion. */
408 do
409 {
410 c = safe_vgetc();
411 } while (c == K_IGNORE);
412
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (KeyTyped)
414 {
415 some_key_typed = TRUE;
416#ifdef FEAT_RIGHTLEFT
417 if (cmd_hkmap)
418 c = hkmap(c);
419# ifdef FEAT_FKMAP
420 if (cmd_fkmap)
421 c = cmdl_fkmap(c);
422# endif
423 if (cmdmsg_rl && !KeyStuffed)
424 {
425 /* Invert horizontal movements and operations. Only when
426 * typed by the user directly, not when the result of a
427 * mapping. */
428 switch (c)
429 {
430 case K_RIGHT: c = K_LEFT; break;
431 case K_S_RIGHT: c = K_S_LEFT; break;
432 case K_C_RIGHT: c = K_C_LEFT; break;
433 case K_LEFT: c = K_RIGHT; break;
434 case K_S_LEFT: c = K_S_RIGHT; break;
435 case K_C_LEFT: c = K_C_RIGHT; break;
436 }
437 }
438#endif
439 }
440
441 /*
442 * Ignore got_int when CTRL-C was typed here.
443 * Don't ignore it in :global, we really need to break then, e.g., for
444 * ":g/pat/normal /pat" (without the <CR>).
445 * Don't ignore it for the input() function.
446 */
447 if ((c == Ctrl_C
448#ifdef UNIX
449 || c == intr_char
450#endif
451 )
452#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
453 && firstc != '@'
454#endif
455#ifdef FEAT_EVAL
456 && !break_ctrl_c
457#endif
458 && !global_busy)
459 got_int = FALSE;
460
461#ifdef FEAT_CMDHIST
462 /* free old command line when finished moving around in the history
463 * list */
464 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000465 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000466 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 && c != K_PAGEDOWN && c != K_PAGEUP
468 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000469 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
471 {
472 vim_free(lookfor);
473 lookfor = NULL;
474 }
475#endif
476
477 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000478 * When there are matching completions to select <S-Tab> works like
479 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000481 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 c = Ctrl_P;
483
484#ifdef FEAT_WILDMENU
485 /* Special translations for 'wildmenu' */
486 if (did_wild_list && p_wmnu)
487 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000488 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000490 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 c = Ctrl_N;
492 }
493 /* Hitting CR after "emenu Name.": complete submenu */
494 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
495 && ccline.cmdpos > 1
496 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
497 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
498 && (c == '\n' || c == '\r' || c == K_KENTER))
499 c = K_DOWN;
500#endif
501
502 /* free expanded names when finished walking through matches */
503 if (xpc.xp_numfiles != -1
504 && !(c == p_wc && KeyTyped) && c != p_wcm
505 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
506 && c != Ctrl_L)
507 {
508 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
509 did_wild_list = FALSE;
510#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000511 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512#endif
513 xpc.xp_context = EXPAND_NOTHING;
514 wim_index = 0;
515#ifdef FEAT_WILDMENU
516 if (p_wmnu && wild_menu_showing != 0)
517 {
518 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000519 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000520
521 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000522 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
524 if (wild_menu_showing == WM_SCROLLED)
525 {
526 /* Entered command line, move it up */
527 cmdline_row--;
528 redrawcmd();
529 }
530 else if (save_p_ls != -1)
531 {
532 /* restore 'laststatus' and 'winminheight' */
533 p_ls = save_p_ls;
534 p_wmh = save_p_wmh;
535 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000536 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000538 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 redrawcmd();
540 save_p_ls = -1;
541 }
542 else
543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 redraw_statuslines();
546 }
547 KeyTyped = skt;
548 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000549 if (ccline.input_fn)
550 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
552#endif
553 }
554
555#ifdef FEAT_WILDMENU
556 /* Special translations for 'wildmenu' */
557 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
558 {
559 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000560 if (c == K_DOWN && ccline.cmdpos > 0
561 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000563 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
565 /* Hitting <Up>: Remove one submenu name in front of the
566 * cursor */
567 int found = FALSE;
568
569 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
570 i = 0;
571 while (--j > 0)
572 {
573 /* check for start of menu name */
574 if (ccline.cmdbuff[j] == ' '
575 && ccline.cmdbuff[j - 1] != '\\')
576 {
577 i = j + 1;
578 break;
579 }
580 /* check for start of submenu name */
581 if (ccline.cmdbuff[j] == '.'
582 && ccline.cmdbuff[j - 1] != '\\')
583 {
584 if (found)
585 {
586 i = j + 1;
587 break;
588 }
589 else
590 found = TRUE;
591 }
592 }
593 if (i > 0)
594 cmdline_del(i);
595 c = p_wc;
596 xpc.xp_context = EXPAND_NOTHING;
597 }
598 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000599 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000600 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000601 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
603 char_u upseg[5];
604
605 upseg[0] = PATHSEP;
606 upseg[1] = '.';
607 upseg[2] = '.';
608 upseg[3] = PATHSEP;
609 upseg[4] = NUL;
610
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000611 if (c == K_DOWN
612 && ccline.cmdpos > 0
613 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
614 && (ccline.cmdpos < 3
615 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
617 {
618 /* go down a directory */
619 c = p_wc;
620 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000621 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {
623 /* If in a direct ancestor, strip off one ../ to go down */
624 int found = FALSE;
625
626 j = ccline.cmdpos;
627 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
628 while (--j > i)
629 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000630#ifdef FEAT_MBYTE
631 if (has_mbyte)
632 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (vim_ispathsep(ccline.cmdbuff[j]))
635 {
636 found = TRUE;
637 break;
638 }
639 }
640 if (found
641 && ccline.cmdbuff[j - 1] == '.'
642 && ccline.cmdbuff[j - 2] == '.'
643 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
644 {
645 cmdline_del(j - 2);
646 c = p_wc;
647 }
648 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000649 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
651 /* go up a directory */
652 int found = FALSE;
653
654 j = ccline.cmdpos - 1;
655 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
656 while (--j > i)
657 {
658#ifdef FEAT_MBYTE
659 if (has_mbyte)
660 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
661#endif
662 if (vim_ispathsep(ccline.cmdbuff[j])
663#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100664 && vim_strchr((char_u *)" *?[{`$%#",
665 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
667 )
668 {
669 if (found)
670 {
671 i = j + 1;
672 break;
673 }
674 else
675 found = TRUE;
676 }
677 }
678
679 if (!found)
680 j = i;
681 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
682 j += 4;
683 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
684 && j == i)
685 j += 3;
686 else
687 j = 0;
688 if (j > 0)
689 {
690 /* TODO this is only for DOS/UNIX systems - need to put in
691 * machine-specific stuff here and in upseg init */
692 cmdline_del(j);
693 put_on_cmdline(upseg + 1, 3, FALSE);
694 }
695 else if (ccline.cmdpos > i)
696 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100697
698 /* Now complete in the new directory. Set KeyTyped in case the
699 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100701 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
705#endif /* FEAT_WILDMENU */
706
707 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
708 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
709 if (c == Ctrl_BSL)
710 {
711 ++no_mapping;
712 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000713 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 --no_mapping;
715 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200716 /* CTRL-\ e doesn't work when obtaining an expression, unless it
717 * is in a mapping. */
718 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
719 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
721 vungetc(c);
722 c = Ctrl_BSL;
723 }
724#ifdef FEAT_EVAL
725 else if (c == 'e')
726 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200727 char_u *p = NULL;
728 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 /*
731 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000732 * Need to save and restore the current command line, to be
733 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */
735 if (ccline.cmdpos == ccline.cmdlen)
736 new_cmdpos = 99999; /* keep it at the end */
737 else
738 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000739
740 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000742 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 if (c == '=')
744 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000745 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000746 * to avoid nasty things like going to another buffer when
747 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000748 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000749 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000751 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000752 restore_cmdline(&save_ccline);
753
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200754 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200756 len = (int)STRLEN(p);
757 if (realloc_cmdbuff(len + 1) == OK)
758 {
759 ccline.cmdlen = len;
760 STRCPY(ccline.cmdbuff, p);
761 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200763 /* Restore the cursor or use the position set with
764 * set_cmdline_pos(). */
765 if (new_cmdpos > ccline.cmdlen)
766 ccline.cmdpos = ccline.cmdlen;
767 else
768 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200770 KeyTyped = FALSE; /* Don't do p_wc completion. */
771 redrawcmd();
772 goto cmdline_changed;
773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
775 }
776 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100777 got_int = FALSE; /* don't abandon the command line */
778 did_emsg = FALSE;
779 emsg_on_display = FALSE;
780 redrawcmd();
781 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783#endif
784 else
785 {
786 if (c == Ctrl_G && p_im && restart_edit == 0)
787 restart_edit = 'a';
788 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
789 in history */
790 goto returncmd; /* back to Normal mode */
791 }
792 }
793
794#ifdef FEAT_CMDWIN
795 if (c == cedit_key || c == K_CMDWIN)
796 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200797 if (ex_normal_busy == 0 && got_int == FALSE)
798 {
799 /*
800 * Open a window to edit the command line (and history).
801 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200802 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200803 some_key_typed = TRUE;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806# ifdef FEAT_DIGRAPHS
807 else
808# endif
809#endif
810#ifdef FEAT_DIGRAPHS
811 c = do_digraph(c);
812#endif
813
814 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
815 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
816 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000817 /* In Ex mode a backslash escapes a newline. */
818 if (exmode_active
819 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000820 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000821 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000822 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000824 if (c == K_KENTER)
825 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000827 else
828 {
829 gotesc = FALSE; /* Might have typed ESC previously, don't
830 truncate the cmdline now. */
831 if (ccheck_abbr(c + ABBR_OFF))
832 goto cmdline_changed;
833 if (!cmd_silent)
834 {
835 windgoto(msg_row, 0);
836 out_flush();
837 }
838 break;
839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841
842 /*
843 * Completion for 'wildchar' or 'wildcharm' key.
844 * - hitting <ESC> twice means: abandon command line.
845 * - wildcard expansion is only done when the 'wildchar' key is really
846 * typed, not when it comes from a macro
847 */
848 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
849 {
850 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
851 {
852 /* if 'wildmode' contains "list" may still need to list */
853 if (xpc.xp_numfiles > 1
854 && !did_wild_list
855 && (wim_flags[wim_index] & WIM_LIST))
856 {
857 (void)showmatches(&xpc, FALSE);
858 redrawcmd();
859 did_wild_list = TRUE;
860 }
861 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100862 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
863 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100865 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
866 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 else
868 res = OK; /* don't insert 'wildchar' now */
869 }
870 else /* typed p_wc first time */
871 {
872 wim_index = 0;
873 j = ccline.cmdpos;
874 /* if 'wildmode' first contains "longest", get longest
875 * common part */
876 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100877 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
878 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100880 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
881 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
883 /* if interrupted while completing, behave like it failed */
884 if (got_int)
885 {
886 (void)vpeekc(); /* remove <C-C> from input stream */
887 got_int = FALSE; /* don't abandon the command line */
888 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
889#ifdef FEAT_WILDMENU
890 xpc.xp_context = EXPAND_NOTHING;
891#endif
892 goto cmdline_changed;
893 }
894
895 /* when more than one match, and 'wildmode' first contains
896 * "list", or no change and 'wildmode' contains "longest,list",
897 * list all matches */
898 if (res == OK && xpc.xp_numfiles > 1)
899 {
900 /* a "longest" that didn't do anything is skipped (but not
901 * "list:longest") */
902 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
903 wim_index = 1;
904 if ((wim_flags[wim_index] & WIM_LIST)
905#ifdef FEAT_WILDMENU
906 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
907#endif
908 )
909 {
910 if (!(wim_flags[0] & WIM_LONGEST))
911 {
912#ifdef FEAT_WILDMENU
913 int p_wmnu_save = p_wmnu;
914 p_wmnu = 0;
915#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100916 /* remove match */
917 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#ifdef FEAT_WILDMENU
919 p_wmnu = p_wmnu_save;
920#endif
921 }
922#ifdef FEAT_WILDMENU
923 (void)showmatches(&xpc, p_wmnu
924 && ((wim_flags[wim_index] & WIM_LIST) == 0));
925#else
926 (void)showmatches(&xpc, FALSE);
927#endif
928 redrawcmd();
929 did_wild_list = TRUE;
930 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100931 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
932 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100934 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
935 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200938 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940#ifdef FEAT_WILDMENU
941 else if (xpc.xp_numfiles == -1)
942 xpc.xp_context = EXPAND_NOTHING;
943#endif
944 }
945 if (wim_index < 3)
946 ++wim_index;
947 if (c == ESC)
948 gotesc = TRUE;
949 if (res == OK)
950 goto cmdline_changed;
951 }
952
953 gotesc = FALSE;
954
955 /* <S-Tab> goes to last match, in a clumsy way */
956 if (c == K_S_TAB && KeyTyped)
957 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100958 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
959 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
960 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 goto cmdline_changed;
962 }
963
964 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
965 c = NL;
966
967 do_abbr = TRUE; /* default: check for abbreviation */
968
969 /*
970 * Big switch for a typed command line character.
971 */
972 switch (c)
973 {
974 case K_BS:
975 case Ctrl_H:
976 case K_DEL:
977 case K_KDEL:
978 case Ctrl_W:
979#ifdef FEAT_FKMAP
980 if (cmd_fkmap && c == K_BS)
981 c = K_DEL;
982#endif
983 if (c == K_KDEL)
984 c = K_DEL;
985
986 /*
987 * delete current character is the same as backspace on next
988 * character, except at end of line
989 */
990 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
991 ++ccline.cmdpos;
992#ifdef FEAT_MBYTE
993 if (has_mbyte && c == K_DEL)
994 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
995 ccline.cmdbuff + ccline.cmdpos);
996#endif
997 if (ccline.cmdpos > 0)
998 {
999 char_u *p;
1000
1001 j = ccline.cmdpos;
1002 p = ccline.cmdbuff + j;
1003#ifdef FEAT_MBYTE
1004 if (has_mbyte)
1005 {
1006 p = mb_prevptr(ccline.cmdbuff, p);
1007 if (c == Ctrl_W)
1008 {
1009 while (p > ccline.cmdbuff && vim_isspace(*p))
1010 p = mb_prevptr(ccline.cmdbuff, p);
1011 i = mb_get_class(p);
1012 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1013 p = mb_prevptr(ccline.cmdbuff, p);
1014 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001015 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018 else
1019#endif
1020 if (c == Ctrl_W)
1021 {
1022 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1023 --p;
1024 i = vim_iswordc(p[-1]);
1025 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1026 && vim_iswordc(p[-1]) == i)
1027 --p;
1028 }
1029 else
1030 --p;
1031 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1032 ccline.cmdlen -= j - ccline.cmdpos;
1033 i = ccline.cmdpos;
1034 while (i < ccline.cmdlen)
1035 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1036
1037 /* Truncate at the end, required for multi-byte chars. */
1038 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001039#ifdef FEAT_SEARCH_EXTRA
1040 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001041 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001042 search_start = save_cursor;
1043 /* save view settings, so that the screen
1044 * won't be restored at the wrong position */
1045 old_curswant = init_curswant;
1046 old_leftcol = init_leftcol;
1047 old_topline = init_topline;
1048# ifdef FEAT_DIFF
1049 old_topfill = init_topfill;
1050# endif
1051 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001052 }
1053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 redrawcmd();
1055 }
1056 else if (ccline.cmdlen == 0 && c != Ctrl_W
1057 && ccline.cmdprompt == NULL && indent == 0)
1058 {
1059 /* In ex and debug mode it doesn't make sense to return. */
1060 if (exmode_active
1061#ifdef FEAT_EVAL
1062 || ccline.cmdfirstc == '>'
1063#endif
1064 )
1065 goto cmdline_not_changed;
1066
1067 vim_free(ccline.cmdbuff); /* no commandline to return */
1068 ccline.cmdbuff = NULL;
1069 if (!cmd_silent)
1070 {
1071#ifdef FEAT_RIGHTLEFT
1072 if (cmdmsg_rl)
1073 msg_col = Columns;
1074 else
1075#endif
1076 msg_col = 0;
1077 msg_putchar(' '); /* delete ':' */
1078 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001079#ifdef FEAT_SEARCH_EXTRA
1080 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001081 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 redraw_cmdline = TRUE;
1084 goto returncmd; /* back to cmd mode */
1085 }
1086 goto cmdline_changed;
1087
1088 case K_INS:
1089 case K_KINS:
1090#ifdef FEAT_FKMAP
1091 /* if Farsi mode set, we are in reverse insert mode -
1092 Do not change the mode */
1093 if (cmd_fkmap)
1094 beep_flush();
1095 else
1096#endif
1097 ccline.overstrike = !ccline.overstrike;
1098#ifdef CURSOR_SHAPE
1099 ui_cursor_shape(); /* may show different cursor shape */
1100#endif
1101 goto cmdline_not_changed;
1102
1103 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001104 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106 /* ":lmap" mappings exists, toggle use of mappings. */
1107 State ^= LANGMAP;
1108#ifdef USE_IM_CONTROL
1109 im_set_active(FALSE); /* Disable input method */
1110#endif
1111 if (b_im_ptr != NULL)
1112 {
1113 if (State & LANGMAP)
1114 *b_im_ptr = B_IMODE_LMAP;
1115 else
1116 *b_im_ptr = B_IMODE_NONE;
1117 }
1118 }
1119#ifdef USE_IM_CONTROL
1120 else
1121 {
1122 /* There are no ":lmap" mappings, toggle IM. When
1123 * 'imdisable' is set don't try getting the status, it's
1124 * always off. */
1125 if ((p_imdisable && b_im_ptr != NULL)
1126 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1127 {
1128 im_set_active(FALSE); /* Disable input method */
1129 if (b_im_ptr != NULL)
1130 *b_im_ptr = B_IMODE_NONE;
1131 }
1132 else
1133 {
1134 im_set_active(TRUE); /* Enable input method */
1135 if (b_im_ptr != NULL)
1136 *b_im_ptr = B_IMODE_IM;
1137 }
1138 }
1139#endif
1140 if (b_im_ptr != NULL)
1141 {
1142 if (b_im_ptr == &curbuf->b_p_iminsert)
1143 set_iminsert_global();
1144 else
1145 set_imsearch_global();
1146 }
1147#ifdef CURSOR_SHAPE
1148 ui_cursor_shape(); /* may show different cursor shape */
1149#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001150#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 /* Show/unshow value of 'keymap' in status lines later. */
1152 status_redraw_curbuf();
1153#endif
1154 goto cmdline_not_changed;
1155
1156/* case '@': only in very old vi */
1157 case Ctrl_U:
1158 /* delete all characters left of the cursor */
1159 j = ccline.cmdpos;
1160 ccline.cmdlen -= j;
1161 i = ccline.cmdpos = 0;
1162 while (i < ccline.cmdlen)
1163 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1164 /* Truncate at the end, required for multi-byte chars. */
1165 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001166#ifdef FEAT_SEARCH_EXTRA
1167 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001168 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 redrawcmd();
1171 goto cmdline_changed;
1172
1173#ifdef FEAT_CLIPBOARD
1174 case Ctrl_Y:
1175 /* Copy the modeless selection, if there is one. */
1176 if (clip_star.state != SELECT_CLEARED)
1177 {
1178 if (clip_star.state == SELECT_DONE)
1179 clip_copy_modeless_selection(TRUE);
1180 goto cmdline_not_changed;
1181 }
1182 break;
1183#endif
1184
1185 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1186 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001187 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001188 * ":normal" runs out of characters. */
1189 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001190 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 goto cmdline_not_changed;
1192
1193 gotesc = TRUE; /* will free ccline.cmdbuff after
1194 putting it in history */
1195 goto returncmd; /* back to cmd mode */
1196
1197 case Ctrl_R: /* insert register */
1198#ifdef USE_ON_FLY_SCROLL
1199 dont_scroll = TRUE; /* disallow scrolling here */
1200#endif
1201 putcmdline('"', TRUE);
1202 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001203 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (i == Ctrl_O)
1205 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1206 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001207 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001208 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 --no_mapping;
1210#ifdef FEAT_EVAL
1211 /*
1212 * Insert the result of an expression.
1213 * Need to save the current command line, to be able to enter
1214 * a new one...
1215 */
1216 new_cmdpos = -1;
1217 if (c == '=')
1218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1220 {
1221 beep_flush();
1222 c = ESC;
1223 }
1224 else
1225 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001226 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001228 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230 }
1231#endif
1232 if (c != ESC) /* use ESC to cancel inserting register */
1233 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001234 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001235
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001236#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001237 /* When there was a serious error abort getting the
1238 * command line. */
1239 if (aborting())
1240 {
1241 gotesc = TRUE; /* will free ccline.cmdbuff after
1242 putting it in history */
1243 goto returncmd; /* back to cmd mode */
1244 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 KeyTyped = FALSE; /* Don't do p_wc completion. */
1247#ifdef FEAT_EVAL
1248 if (new_cmdpos >= 0)
1249 {
1250 /* set_cmdline_pos() was used */
1251 if (new_cmdpos > ccline.cmdlen)
1252 ccline.cmdpos = ccline.cmdlen;
1253 else
1254 ccline.cmdpos = new_cmdpos;
1255 }
1256#endif
1257 }
1258 redrawcmd();
1259 goto cmdline_changed;
1260
1261 case Ctrl_D:
1262 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1263 break; /* Use ^D as normal char instead */
1264
1265 redrawcmd();
1266 continue; /* don't do incremental search now */
1267
1268 case K_RIGHT:
1269 case K_S_RIGHT:
1270 case K_C_RIGHT:
1271 do
1272 {
1273 if (ccline.cmdpos >= ccline.cmdlen)
1274 break;
1275 i = cmdline_charsize(ccline.cmdpos);
1276 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1277 break;
1278 ccline.cmdspos += i;
1279#ifdef FEAT_MBYTE
1280 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001281 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 + ccline.cmdpos);
1283 else
1284#endif
1285 ++ccline.cmdpos;
1286 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001287 while ((c == K_S_RIGHT || c == K_C_RIGHT
1288 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1290#ifdef FEAT_MBYTE
1291 if (has_mbyte)
1292 set_cmdspos_cursor();
1293#endif
1294 goto cmdline_not_changed;
1295
1296 case K_LEFT:
1297 case K_S_LEFT:
1298 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001299 if (ccline.cmdpos == 0)
1300 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 do
1302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 --ccline.cmdpos;
1304#ifdef FEAT_MBYTE
1305 if (has_mbyte) /* move to first byte of char */
1306 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1307 ccline.cmdbuff + ccline.cmdpos);
1308#endif
1309 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1310 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001311 while (ccline.cmdpos > 0
1312 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001313 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1315#ifdef FEAT_MBYTE
1316 if (has_mbyte)
1317 set_cmdspos_cursor();
1318#endif
1319 goto cmdline_not_changed;
1320
1321 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001322 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001323 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
Bram Moolenaar4770d092006-01-12 23:22:24 +00001325#ifdef FEAT_GUI_W32
1326 /* On Win32 ignore <M-F4>, we get it when closing the window was
1327 * cancelled. */
1328 case K_F4:
1329 if (mod_mask == MOD_MASK_ALT)
1330 {
1331 redrawcmd(); /* somehow the cmdline is cleared */
1332 goto cmdline_not_changed;
1333 }
1334 break;
1335#endif
1336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#ifdef FEAT_MOUSE
1338 case K_MIDDLEDRAG:
1339 case K_MIDDLERELEASE:
1340 goto cmdline_not_changed; /* Ignore mouse */
1341
1342 case K_MIDDLEMOUSE:
1343# ifdef FEAT_GUI
1344 /* When GUI is active, also paste when 'mouse' is empty */
1345 if (!gui.in_use)
1346# endif
1347 if (!mouse_has(MOUSE_COMMAND))
1348 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001349# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001351 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001353# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001354 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 redrawcmd();
1356 goto cmdline_changed;
1357
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001358# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001360 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 redrawcmd();
1362 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 case K_LEFTDRAG:
1366 case K_LEFTRELEASE:
1367 case K_RIGHTDRAG:
1368 case K_RIGHTRELEASE:
1369 /* Ignore drag and release events when the button-down wasn't
1370 * seen before. */
1371 if (ignore_drag_release)
1372 goto cmdline_not_changed;
1373 /* FALLTHROUGH */
1374 case K_LEFTMOUSE:
1375 case K_RIGHTMOUSE:
1376 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1377 ignore_drag_release = TRUE;
1378 else
1379 ignore_drag_release = FALSE;
1380# ifdef FEAT_GUI
1381 /* When GUI is active, also move when 'mouse' is empty */
1382 if (!gui.in_use)
1383# endif
1384 if (!mouse_has(MOUSE_COMMAND))
1385 goto cmdline_not_changed; /* Ignore mouse */
1386# ifdef FEAT_CLIPBOARD
1387 if (mouse_row < cmdline_row && clip_star.available)
1388 {
1389 int button, is_click, is_drag;
1390
1391 /*
1392 * Handle modeless selection.
1393 */
1394 button = get_mouse_button(KEY2TERMCAP1(c),
1395 &is_click, &is_drag);
1396 if (mouse_model_popup() && button == MOUSE_LEFT
1397 && (mod_mask & MOD_MASK_SHIFT))
1398 {
1399 /* Translate shift-left to right button. */
1400 button = MOUSE_RIGHT;
1401 mod_mask &= ~MOD_MASK_SHIFT;
1402 }
1403 clip_modeless(button, is_click, is_drag);
1404 goto cmdline_not_changed;
1405 }
1406# endif
1407
1408 set_cmdspos();
1409 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1410 ++ccline.cmdpos)
1411 {
1412 i = cmdline_charsize(ccline.cmdpos);
1413 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1414 && mouse_col < ccline.cmdspos % Columns + i)
1415 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001416# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (has_mbyte)
1418 {
1419 /* Count ">" for double-wide char that doesn't fit. */
1420 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001421 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 + ccline.cmdpos) - 1;
1423 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 ccline.cmdspos += i;
1426 }
1427 goto cmdline_not_changed;
1428
1429 /* Mouse scroll wheel: ignored here */
1430 case K_MOUSEDOWN:
1431 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001432 case K_MOUSELEFT:
1433 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 /* Alternate buttons ignored here */
1435 case K_X1MOUSE:
1436 case K_X1DRAG:
1437 case K_X1RELEASE:
1438 case K_X2MOUSE:
1439 case K_X2DRAG:
1440 case K_X2RELEASE:
1441 goto cmdline_not_changed;
1442
1443#endif /* FEAT_MOUSE */
1444
1445#ifdef FEAT_GUI
1446 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1447 case K_LEFTRELEASE_NM:
1448 goto cmdline_not_changed;
1449
1450 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001451 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
1453 gui_do_scroll();
1454 redrawcmd();
1455 }
1456 goto cmdline_not_changed;
1457
1458 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001459 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001461 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 redrawcmd();
1463 }
1464 goto cmdline_not_changed;
1465#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001466#ifdef FEAT_GUI_TABLINE
1467 case K_TABLINE:
1468 case K_TABMENU:
1469 /* Don't want to change any tabs here. Make sure the same tab
1470 * is still selected. */
1471 if (gui_use_tabline())
1472 gui_mch_set_curtab(tabpage_index(curtab));
1473 goto cmdline_not_changed;
1474#endif
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 case K_SELECT: /* end of Select mode mapping - ignore */
1477 goto cmdline_not_changed;
1478
1479 case Ctrl_B: /* begin of command line */
1480 case K_HOME:
1481 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 case K_S_HOME:
1483 case K_C_HOME:
1484 ccline.cmdpos = 0;
1485 set_cmdspos();
1486 goto cmdline_not_changed;
1487
1488 case Ctrl_E: /* end of command line */
1489 case K_END:
1490 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 case K_S_END:
1492 case K_C_END:
1493 ccline.cmdpos = ccline.cmdlen;
1494 set_cmdspos_cursor();
1495 goto cmdline_not_changed;
1496
1497 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001498 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 break;
1500 goto cmdline_changed;
1501
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001502 case Ctrl_L:
1503#ifdef FEAT_SEARCH_EXTRA
1504 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1505 {
1506 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001507 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001508 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001509 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001510 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001511 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001512 c = gchar_cursor();
1513 /* If 'ignorecase' and 'smartcase' are set and the
1514 * command line has no uppercase characters, convert
1515 * the character to lowercase */
1516 if (p_ic && p_scs
1517 && !pat_has_uppercase(ccline.cmdbuff))
1518 c = MB_TOLOWER(c);
1519 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001520 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001521 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001522 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001523 != NULL)
1524 {
1525 /* put a backslash before special
1526 * characters */
1527 stuffcharReadbuff(c);
1528 c = '\\';
1529 }
1530 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001531 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001532 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001533 }
1534 goto cmdline_not_changed;
1535 }
1536#endif
1537
1538 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001539 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 break;
1541 goto cmdline_changed;
1542
1543 case Ctrl_N: /* next match */
1544 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001545 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001547 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1548 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001550 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001552 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554#ifdef FEAT_CMDHIST
1555 case K_UP:
1556 case K_DOWN:
1557 case K_S_UP:
1558 case K_S_DOWN:
1559 case K_PAGEUP:
1560 case K_KPAGEUP:
1561 case K_PAGEDOWN:
1562 case K_KPAGEDOWN:
1563 if (hislen == 0 || firstc == NUL) /* no history */
1564 goto cmdline_not_changed;
1565
1566 i = hiscnt;
1567
1568 /* save current command string so it can be restored later */
1569 if (lookfor == NULL)
1570 {
1571 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1572 goto cmdline_not_changed;
1573 lookfor[ccline.cmdpos] = NUL;
1574 }
1575
1576 j = (int)STRLEN(lookfor);
1577 for (;;)
1578 {
1579 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001580 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001581 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 if (hiscnt == hislen) /* first time */
1584 hiscnt = hisidx[histype];
1585 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1586 hiscnt = hislen - 1;
1587 else if (hiscnt != hisidx[histype] + 1)
1588 --hiscnt;
1589 else /* at top of list */
1590 {
1591 hiscnt = i;
1592 break;
1593 }
1594 }
1595 else /* one step forwards */
1596 {
1597 /* on last entry, clear the line */
1598 if (hiscnt == hisidx[histype])
1599 {
1600 hiscnt = hislen;
1601 break;
1602 }
1603
1604 /* not on a history line, nothing to do */
1605 if (hiscnt == hislen)
1606 break;
1607 if (hiscnt == hislen - 1) /* wrap around */
1608 hiscnt = 0;
1609 else
1610 ++hiscnt;
1611 }
1612 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1613 {
1614 hiscnt = i;
1615 break;
1616 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001617 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001618 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 || STRNCMP(history[histype][hiscnt].hisstr,
1620 lookfor, (size_t)j) == 0)
1621 break;
1622 }
1623
1624 if (hiscnt != i) /* jumped to other entry */
1625 {
1626 char_u *p;
1627 int len;
1628 int old_firstc;
1629
1630 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001631 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (hiscnt == hislen)
1633 p = lookfor; /* back to the old one */
1634 else
1635 p = history[histype][hiscnt].hisstr;
1636
1637 if (histype == HIST_SEARCH
1638 && p != lookfor
1639 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1640 {
1641 /* Correct for the separator character used when
1642 * adding the history entry vs the one used now.
1643 * First loop: count length.
1644 * Second loop: copy the characters. */
1645 for (i = 0; i <= 1; ++i)
1646 {
1647 len = 0;
1648 for (j = 0; p[j] != NUL; ++j)
1649 {
1650 /* Replace old sep with new sep, unless it is
1651 * escaped. */
1652 if (p[j] == old_firstc
1653 && (j == 0 || p[j - 1] != '\\'))
1654 {
1655 if (i > 0)
1656 ccline.cmdbuff[len] = firstc;
1657 }
1658 else
1659 {
1660 /* Escape new sep, unless it is already
1661 * escaped. */
1662 if (p[j] == firstc
1663 && (j == 0 || p[j - 1] != '\\'))
1664 {
1665 if (i > 0)
1666 ccline.cmdbuff[len] = '\\';
1667 ++len;
1668 }
1669 if (i > 0)
1670 ccline.cmdbuff[len] = p[j];
1671 }
1672 ++len;
1673 }
1674 if (i == 0)
1675 {
1676 alloc_cmdbuff(len);
1677 if (ccline.cmdbuff == NULL)
1678 goto returncmd;
1679 }
1680 }
1681 ccline.cmdbuff[len] = NUL;
1682 }
1683 else
1684 {
1685 alloc_cmdbuff((int)STRLEN(p));
1686 if (ccline.cmdbuff == NULL)
1687 goto returncmd;
1688 STRCPY(ccline.cmdbuff, p);
1689 }
1690
1691 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1692 redrawcmd();
1693 goto cmdline_changed;
1694 }
1695 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001696#endif
1697 goto cmdline_not_changed;
1698
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001699#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001700 case Ctrl_G: /* next match */
1701 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001702 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1703 {
1704 pos_T t;
1705 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1706 + SEARCH_PEEK;
1707
1708 if (char_avail())
1709 continue;
1710 cursor_off();
1711 out_flush();
1712 if (c == Ctrl_G)
1713 {
1714 t = match_end;
1715 search_flags += SEARCH_COL;
1716 }
1717 else
1718 t = match_start;
1719 ++emsg_off;
1720 i = searchit(curwin, curbuf, &t,
1721 c == Ctrl_G ? FORWARD : BACKWARD,
1722 ccline.cmdbuff, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001723 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001724 --emsg_off;
1725 if (i)
1726 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001727 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001728 match_end = t;
1729 match_start = t;
1730 if (c == Ctrl_T && firstc == '/')
1731 {
1732 /* move just before the current match, so that
1733 * when nv_search finishes the cursor will be
1734 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001735 search_start = t;
1736 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001737 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001738 else if (c == Ctrl_G && firstc == '?')
1739 {
1740 /* move just after the current match, so that
1741 * when nv_search finishes the cursor will be
1742 * put back on the match */
1743 search_start = t;
1744 (void)incl(&search_start);
1745 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001746 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001747 {
1748 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001749 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001750 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001751 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001752 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001753 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001754 }
1755
1756 set_search_match(&match_end);
1757 curwin->w_cursor = match_start;
1758 changed_cline_bef_curs();
1759 update_topline();
1760 validate_cursor();
1761 highlight_match = TRUE;
1762 old_curswant = curwin->w_curswant;
1763 old_leftcol = curwin->w_leftcol;
1764 old_topline = curwin->w_topline;
1765# ifdef FEAT_DIFF
1766 old_topfill = curwin->w_topfill;
1767# endif
1768 old_botline = curwin->w_botline;
1769 update_screen(NOT_VALID);
1770 redrawcmdline();
1771 }
1772 else
1773 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001774 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001775 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001776 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#endif
1778
1779 case Ctrl_V:
1780 case Ctrl_Q:
1781#ifdef FEAT_MOUSE
1782 ignore_drag_release = TRUE;
1783#endif
1784 putcmdline('^', TRUE);
1785 c = get_literal(); /* get next (two) character(s) */
1786 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001787 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#ifdef FEAT_MBYTE
1789 /* may need to remove ^ when composing char was typed */
1790 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1791 {
1792 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1793 msg_putchar(' ');
1794 cursorcmd();
1795 }
1796#endif
1797 break;
1798
1799#ifdef FEAT_DIGRAPHS
1800 case Ctrl_K:
1801#ifdef FEAT_MOUSE
1802 ignore_drag_release = TRUE;
1803#endif
1804 putcmdline('?', TRUE);
1805#ifdef USE_ON_FLY_SCROLL
1806 dont_scroll = TRUE; /* disallow scrolling here */
1807#endif
1808 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001809 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (c != NUL)
1811 break;
1812
1813 redrawcmd();
1814 goto cmdline_not_changed;
1815#endif /* FEAT_DIGRAPHS */
1816
1817#ifdef FEAT_RIGHTLEFT
1818 case Ctrl__: /* CTRL-_: switch language mode */
1819 if (!p_ari)
1820 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001821# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (p_altkeymap)
1823 {
1824 cmd_fkmap = !cmd_fkmap;
1825 if (cmd_fkmap) /* in Farsi always in Insert mode */
1826 ccline.overstrike = FALSE;
1827 }
1828 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 cmd_hkmap = !cmd_hkmap;
1831 goto cmdline_not_changed;
1832#endif
1833
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001834 case K_PS:
1835 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1836 goto cmdline_changed;
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 default:
1839#ifdef UNIX
1840 if (c == intr_char)
1841 {
1842 gotesc = TRUE; /* will free ccline.cmdbuff after
1843 putting it in history */
1844 goto returncmd; /* back to Normal mode */
1845 }
1846#endif
1847 /*
1848 * Normal character with no special meaning. Just set mod_mask
1849 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1850 * the string <S-Space>. This should only happen after ^V.
1851 */
1852 if (!IS_SPECIAL(c))
1853 mod_mask = 0x0;
1854 break;
1855 }
1856 /*
1857 * End of switch on command line character.
1858 * We come here if we have a normal character.
1859 */
1860
Bram Moolenaarede3e632013-06-23 16:16:19 +02001861 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862#ifdef FEAT_MBYTE
1863 /* Add ABBR_OFF for characters above 0x100, this is
1864 * what check_abbr() expects. */
1865 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1866#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001867 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 goto cmdline_changed;
1869
1870 /*
1871 * put the character in the command line
1872 */
1873 if (IS_SPECIAL(c) || mod_mask != 0)
1874 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1875 else
1876 {
1877#ifdef FEAT_MBYTE
1878 if (has_mbyte)
1879 {
1880 j = (*mb_char2bytes)(c, IObuff);
1881 IObuff[j] = NUL; /* exclude composing chars */
1882 put_on_cmdline(IObuff, j, TRUE);
1883 }
1884 else
1885#endif
1886 {
1887 IObuff[0] = c;
1888 put_on_cmdline(IObuff, 1, TRUE);
1889 }
1890 }
1891 goto cmdline_changed;
1892
1893/*
1894 * This part implements incremental searches for "/" and "?"
1895 * Jump to cmdline_not_changed when a character has been read but the command
1896 * line did not change. Then we only search and redraw if something changed in
1897 * the past.
1898 * Jump to cmdline_changed when the command line did change.
1899 * (Sorry for the goto's, I know it is ugly).
1900 */
1901cmdline_not_changed:
1902#ifdef FEAT_SEARCH_EXTRA
1903 if (!incsearch_postponed)
1904 continue;
1905#endif
1906
1907cmdline_changed:
1908#ifdef FEAT_SEARCH_EXTRA
1909 /*
1910 * 'incsearch' highlighting.
1911 */
1912 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1913 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001914 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001915#ifdef FEAT_RELTIME
1916 proftime_T tm;
1917#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 /* if there is a character waiting, search and redraw later */
1920 if (char_avail())
1921 {
1922 incsearch_postponed = TRUE;
1923 continue;
1924 }
1925 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001926 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 /* If there is no command line, don't do anything */
1929 if (ccline.cmdlen == 0)
1930 i = 0;
1931 else
1932 {
1933 cursor_off(); /* so the user knows we're busy */
1934 out_flush();
1935 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001936#ifdef FEAT_RELTIME
1937 /* Set the time limit to half a second. */
1938 profile_setlimit(500L, &tm);
1939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001941 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1942#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001943 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001944#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001945 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001946#endif
1947 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 --emsg_off;
1949 /* if interrupted while searching, behave like it failed */
1950 if (got_int)
1951 {
1952 (void)vpeekc(); /* remove <C-C> from input stream */
1953 got_int = FALSE; /* don't abandon the command line */
1954 i = 0;
1955 }
1956 else if (char_avail())
1957 /* cancelled searching because a char was typed */
1958 incsearch_postponed = TRUE;
1959 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001960 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 highlight_match = TRUE; /* highlight position */
1962 else
1963 highlight_match = FALSE; /* remove highlight */
1964
1965 /* first restore the old curwin values, so the screen is
1966 * positioned in the same way as the actual search command */
1967 curwin->w_leftcol = old_leftcol;
1968 curwin->w_topline = old_topline;
1969# ifdef FEAT_DIFF
1970 curwin->w_topfill = old_topfill;
1971# endif
1972 curwin->w_botline = old_botline;
1973 changed_cline_bef_curs();
1974 update_topline();
1975
1976 if (i != 0)
1977 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001978 pos_T save_pos = curwin->w_cursor;
1979
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001980 match_start = curwin->w_cursor;
1981 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001983 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001984 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001985 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001987 else
1988 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001991 /* May redraw the status line to show the cursor position. */
1992 if (p_ru && curwin->w_status_height > 0)
1993 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001995 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001996 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001997 restore_cmdline(&save_ccline);
1998
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001999 /* Leave it at the end to make CTRL-R CTRL-W work. */
2000 if (i != 0)
2001 curwin->w_cursor = end_pos;
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 msg_starthere();
2004 redrawcmdline();
2005 did_incsearch = TRUE;
2006 }
2007#else /* FEAT_SEARCH_EXTRA */
2008 ;
2009#endif
2010
2011#ifdef FEAT_RIGHTLEFT
2012 if (cmdmsg_rl
2013# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002014 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015# endif
2016 )
2017 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002018 * right-left typing. Not efficient, but it works.
2019 * Do it only when there are no characters left to read
2020 * to avoid useless intermediate redraws. */
2021 if (vpeekc() == NUL)
2022 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#endif
2024 }
2025
2026returncmd:
2027
2028#ifdef FEAT_RIGHTLEFT
2029 cmdmsg_rl = FALSE;
2030#endif
2031
2032#ifdef FEAT_FKMAP
2033 cmd_fkmap = 0;
2034#endif
2035
2036 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002037 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
2039#ifdef FEAT_SEARCH_EXTRA
2040 if (did_incsearch)
2041 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002042 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002043 curwin->w_cursor = save_cursor;
2044 else
2045 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002046 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002047 {
2048 /* put the '" mark at the original position */
2049 curwin->w_cursor = save_cursor;
2050 setpcmark();
2051 }
2052 curwin->w_cursor = search_start;
2053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 curwin->w_curswant = old_curswant;
2055 curwin->w_leftcol = old_leftcol;
2056 curwin->w_topline = old_topline;
2057# ifdef FEAT_DIFF
2058 curwin->w_topfill = old_topfill;
2059# endif
2060 curwin->w_botline = old_botline;
2061 highlight_match = FALSE;
2062 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002063 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065#endif
2066
2067 if (ccline.cmdbuff != NULL)
2068 {
2069 /*
2070 * Put line in history buffer (":" and "=" only when it was typed).
2071 */
2072#ifdef FEAT_CMDHIST
2073 if (ccline.cmdlen && firstc != NUL
2074 && (some_key_typed || histype == HIST_SEARCH))
2075 {
2076 add_to_history(histype, ccline.cmdbuff, TRUE,
2077 histype == HIST_SEARCH ? firstc : NUL);
2078 if (firstc == ':')
2079 {
2080 vim_free(new_last_cmdline);
2081 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2082 }
2083 }
2084#endif
2085
2086 if (gotesc) /* abandon command line */
2087 {
2088 vim_free(ccline.cmdbuff);
2089 ccline.cmdbuff = NULL;
2090 if (msg_scrolled == 0)
2091 compute_cmdrow();
2092 MSG("");
2093 redraw_cmdline = TRUE;
2094 }
2095 }
2096
2097 /*
2098 * If the screen was shifted up, redraw the whole screen (later).
2099 * If the line is too long, clear it, so ruler and shown command do
2100 * not get printed in the middle of it.
2101 */
2102 msg_check();
2103 msg_scroll = save_msg_scroll;
2104 redir_off = FALSE;
2105
2106 /* When the command line was typed, no need for a wait-return prompt. */
2107 if (some_key_typed)
2108 need_wait_return = FALSE;
2109
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002110#ifdef FEAT_AUTOCMD
2111 /* Trigger CmdlineLeave autocommands. */
2112 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
2113#endif
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 State = save_State;
2116#ifdef USE_IM_CONTROL
2117 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2118 im_save_status(b_im_ptr);
2119 im_set_active(FALSE);
2120#endif
2121#ifdef FEAT_MOUSE
2122 setmouse();
2123#endif
2124#ifdef CURSOR_SHAPE
2125 ui_cursor_shape(); /* may show different cursor shape */
2126#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002127 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002129 {
2130 char_u *p = ccline.cmdbuff;
2131
2132 /* Make ccline empty, getcmdline() may try to use it. */
2133 ccline.cmdbuff = NULL;
2134 return p;
2135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136}
2137
2138#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2139/*
2140 * Get a command line with a prompt.
2141 * This is prepared to be called recursively from getcmdline() (e.g. by
2142 * f_input() when evaluating an expression from CTRL-R =).
2143 * Returns the command line in allocated memory, or NULL.
2144 */
2145 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002146getcmdline_prompt(
2147 int firstc,
2148 char_u *prompt, /* command line prompt */
2149 int attr, /* attributes for prompt */
2150 int xp_context, /* type of expansion */
2151 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 char_u *s;
2154 struct cmdline_info save_ccline;
2155 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002156 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002158 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 ccline.cmdprompt = prompt;
2160 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002161# ifdef FEAT_EVAL
2162 ccline.xp_context = xp_context;
2163 ccline.xp_arg = xp_arg;
2164 ccline.input_fn = (firstc == '@');
2165# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002166 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002168 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002169 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002170 /* Restore msg_col, the prompt from input() may have changed it.
2171 * But only if called recursively and the commandline is therefore being
2172 * restored to an old one; if not, the input() prompt stays on the screen,
2173 * so we need its modified msg_col left intact. */
2174 if (ccline.cmdbuff != NULL)
2175 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 return s;
2178}
2179#endif
2180
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002181/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002182 * Return TRUE when the text must not be changed and we can't switch to
2183 * another window or buffer. Used when editing the command line, evaluating
2184 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002185 */
2186 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002187text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002188{
2189#ifdef FEAT_CMDWIN
2190 if (cmdwin_type != 0)
2191 return TRUE;
2192#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002193 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002194}
2195
2196/*
2197 * Give an error message for a command that isn't allowed while the cmdline
2198 * window is open or editing the cmdline in another way.
2199 */
2200 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002201text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002202{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002203 EMSG(_(get_text_locked_msg()));
2204}
2205
2206 char_u *
2207get_text_locked_msg(void)
2208{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002209#ifdef FEAT_CMDWIN
2210 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002211 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002212#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002213 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002214}
2215
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002216#if defined(FEAT_AUTOCMD) || defined(PROTO)
2217/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002218 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2219 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220 */
2221 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002222curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223{
2224 if (curbuf_lock > 0)
2225 {
2226 EMSG(_("E788: Not allowed to edit another buffer now"));
2227 return TRUE;
2228 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002229 return allbuf_locked();
2230}
2231
2232/*
2233 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2234 * message.
2235 */
2236 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002237allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002238{
2239 if (allbuf_lock > 0)
2240 {
2241 EMSG(_("E811: Not allowed to change buffer information now"));
2242 return TRUE;
2243 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244 return FALSE;
2245}
2246#endif
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002249cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2252 if (cmdline_star > 0) /* showing '*', always 1 position */
2253 return 1;
2254#endif
2255 return ptr2cells(ccline.cmdbuff + idx);
2256}
2257
2258/*
2259 * Compute the offset of the cursor on the command line for the prompt and
2260 * indent.
2261 */
2262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002263set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002265 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 ccline.cmdspos = 1 + ccline.cmdindent;
2267 else
2268 ccline.cmdspos = 0 + ccline.cmdindent;
2269}
2270
2271/*
2272 * Compute the screen position for the cursor on the command line.
2273 */
2274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002275set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 int i, m, c;
2278
2279 set_cmdspos();
2280 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002283 if (m < 0) /* overflow, Columns or Rows at weird value */
2284 m = MAXCOL;
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 else
2287 m = MAXCOL;
2288 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2289 {
2290 c = cmdline_charsize(i);
2291#ifdef FEAT_MBYTE
2292 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2293 if (has_mbyte)
2294 correct_cmdspos(i, c);
2295#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002296 /* If the cmdline doesn't fit, show cursor on last visible char.
2297 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if ((ccline.cmdspos += c) >= m)
2299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 ccline.cmdspos -= c;
2301 break;
2302 }
2303#ifdef FEAT_MBYTE
2304 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002305 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#endif
2307 }
2308}
2309
2310#ifdef FEAT_MBYTE
2311/*
2312 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2313 * character that doesn't fit, so that a ">" must be displayed.
2314 */
2315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002316correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002318 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2320 && ccline.cmdspos % Columns + cells > Columns)
2321 ccline.cmdspos++;
2322}
2323#endif
2324
2325/*
2326 * Get an Ex command line for the ":" command.
2327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002329getexline(
2330 int c, /* normally ':', NUL for ":append" */
2331 void *cookie UNUSED,
2332 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 /* When executing a register, remove ':' that's in front of each line. */
2335 if (exec_from_reg && vpeekc() == ':')
2336 (void)vgetc();
2337 return getcmdline(c, 1L, indent);
2338}
2339
2340/*
2341 * Get an Ex command line for Ex mode.
2342 * In Ex mode we only use the OS supplied line editing features and no
2343 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002344 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002347getexmodeline(
2348 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002349 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002350 void *cookie UNUSED,
2351 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002353 garray_T line_ga;
2354 char_u *pend;
2355 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002356 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002357 int escaped = FALSE; /* CTRL-V typed */
2358 int vcol = 0;
2359 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002360 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002361 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363 /* Switch cursor on now. This avoids that it happens after the "\n", which
2364 * confuses the system function that computes tabstops. */
2365 cursor_on();
2366
2367 /* always start in column 0; write a newline if necessary */
2368 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002369 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002371 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002373 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002374 if (p_prompt)
2375 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 while (indent-- > 0)
2377 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
2380
2381 ga_init2(&line_ga, 1, 30);
2382
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002383 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002384 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002385 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002386 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002387 while (indent >= 8)
2388 {
2389 ga_append(&line_ga, TAB);
2390 msg_puts((char_u *)" ");
2391 indent -= 8;
2392 }
2393 while (indent-- > 0)
2394 {
2395 ga_append(&line_ga, ' ');
2396 msg_putchar(' ');
2397 }
2398 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002399 ++no_mapping;
2400 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 /*
2403 * Get the line, one character at a time.
2404 */
2405 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002406 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002408 long sw;
2409 char_u *s;
2410
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (ga_grow(&line_ga, 40) == FAIL)
2412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaarba748c82017-02-26 14:00:07 +01002414 /*
2415 * Get one character at a time.
2416 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002417 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002418
2419 /* Check for a ":normal" command and no more characters left. */
2420 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2421 c1 = '\n';
2422 else
2423 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002426 * Handle line editing.
2427 * Previously this was left to the system, putting the terminal in
2428 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002430 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002432 msg_putchar('\n');
2433 break;
2434 }
2435
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002436 if (c1 == K_PS)
2437 {
2438 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2439 goto redraw;
2440 }
2441
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002442 if (!escaped)
2443 {
2444 /* CR typed means "enter", which is NL */
2445 if (c1 == '\r')
2446 c1 = '\n';
2447
2448 if (c1 == BS || c1 == K_BS
2449 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002451 if (line_ga.ga_len > 0)
2452 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002453#ifdef FEAT_MBYTE
2454 if (has_mbyte)
2455 {
2456 p = (char_u *)line_ga.ga_data;
2457 p[line_ga.ga_len] = NUL;
2458 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2459 line_ga.ga_len -= len;
2460 }
2461 else
2462#endif
2463 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002464 goto redraw;
2465 }
2466 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002469 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002471 msg_col = startcol;
2472 msg_clr_eos();
2473 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002474 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002475 }
2476
2477 if (c1 == Ctrl_T)
2478 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002479 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002480 p = (char_u *)line_ga.ga_data;
2481 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002482 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002483 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002484add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002485 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002487 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002488 p = (char_u *)line_ga.ga_data;
2489 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002490 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2491 *s = ' ';
2492 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002494redraw:
2495 /* redraw the line */
2496 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002497 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002498 p = (char_u *)line_ga.ga_data;
2499 p[line_ga.ga_len] = NUL;
2500 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002502 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002504 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002506 msg_putchar(' ');
2507 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002508 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002510 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002512 len = MB_PTR2LEN(p);
2513 msg_outtrans_len(p, len);
2514 vcol += ptr2cells(p);
2515 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002518 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002519 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002520 continue;
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002523 if (c1 == Ctrl_D)
2524 {
2525 /* Delete one shiftwidth. */
2526 p = (char_u *)line_ga.ga_data;
2527 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002529 if (prev_char == '^')
2530 ex_keep_indent = TRUE;
2531 indent = 0;
2532 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
2534 else
2535 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002536 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002537 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002538 if (indent > 0)
2539 {
2540 --indent;
2541 indent -= indent % get_sw_value(curbuf);
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002544 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002545 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002546 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002547 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2548 --line_ga.ga_len;
2549 }
2550 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002552
2553 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2554 {
2555 escaped = TRUE;
2556 continue;
2557 }
2558
2559 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2560 if (IS_SPECIAL(c1))
2561 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563
2564 if (IS_SPECIAL(c1))
2565 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002566#ifdef FEAT_MBYTE
2567 if (has_mbyte)
2568 len = (*mb_char2bytes)(c1,
2569 (char_u *)line_ga.ga_data + line_ga.ga_len);
2570 else
2571#endif
2572 {
2573 len = 1;
2574 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2575 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 if (c1 == '\n')
2577 msg_putchar('\n');
2578 else if (c1 == TAB)
2579 {
2580 /* Don't use chartabsize(), 'ts' can be different */
2581 do
2582 {
2583 msg_putchar(' ');
2584 } while (++vcol % 8);
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002588 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002589 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002590 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002592 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002593 escaped = FALSE;
2594
2595 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002596 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002597
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002598 /* We are done when a NL is entered, but not when it comes after an
2599 * odd number of backslashes, that results in a NUL. */
2600 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002602 int bcount = 0;
2603
2604 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2605 ++bcount;
2606
2607 if (bcount > 0)
2608 {
2609 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2610 * "\NL", etc. */
2611 line_ga.ga_len -= (bcount + 1) / 2;
2612 pend -= (bcount + 1) / 2;
2613 pend[-1] = '\n';
2614 }
2615
2616 if ((bcount & 1) == 0)
2617 {
2618 --line_ga.ga_len;
2619 --pend;
2620 *pend = NUL;
2621 break;
2622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624 }
2625
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 --no_mapping;
2627 --allow_keys;
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /* make following messages go to the next line */
2630 msg_didout = FALSE;
2631 msg_col = 0;
2632 if (msg_row < Rows - 1)
2633 ++msg_row;
2634 emsg_on_display = FALSE; /* don't want ui_delay() */
2635
2636 if (got_int)
2637 ga_clear(&line_ga);
2638
2639 return (char_u *)line_ga.ga_data;
2640}
2641
Bram Moolenaare344bea2005-09-01 20:46:49 +00002642# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2643 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644/*
2645 * Return TRUE if ccline.overstrike is on.
2646 */
2647 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002648cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 return ccline.overstrike;
2651}
2652
2653/*
2654 * Return TRUE if the cursor is at the end of the cmdline.
2655 */
2656 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002657cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
2659 return (ccline.cmdpos >= ccline.cmdlen);
2660}
2661#endif
2662
Bram Moolenaar9372a112005-12-06 19:59:18 +00002663#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664/*
2665 * Return the virtual column number at the current cursor position.
2666 * This is used by the IM code to obtain the start of the preedit string.
2667 */
2668 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002669cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2672 return MAXCOL;
2673
2674# ifdef FEAT_MBYTE
2675 if (has_mbyte)
2676 {
2677 colnr_T col;
2678 int i = 0;
2679
2680 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002681 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 return col;
2684 }
2685 else
2686# endif
2687 return ccline.cmdpos;
2688}
2689#endif
2690
2691#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2692/*
2693 * If part of the command line is an IM preedit string, redraw it with
2694 * IM feedback attributes. The cursor position is restored after drawing.
2695 */
2696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002697redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 if ((State & CMDLINE)
2700 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002701 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 && !p_imdisable
2703 && im_is_preediting())
2704 {
2705 int cmdpos = 0;
2706 int cmdspos;
2707 int old_row;
2708 int old_col;
2709 colnr_T col;
2710
2711 old_row = msg_row;
2712 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002713 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715# ifdef FEAT_MBYTE
2716 if (has_mbyte)
2717 {
2718 for (col = 0; col < preedit_start_col
2719 && cmdpos < ccline.cmdlen; ++col)
2720 {
2721 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002722 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724 }
2725 else
2726# endif
2727 {
2728 cmdspos += preedit_start_col;
2729 cmdpos += preedit_start_col;
2730 }
2731
2732 msg_row = cmdline_row + (cmdspos / (int)Columns);
2733 msg_col = cmdspos % (int)Columns;
2734 if (msg_row >= Rows)
2735 msg_row = Rows - 1;
2736
2737 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2738 {
2739 int char_len;
2740 int char_attr;
2741
2742 char_attr = im_get_feedback_attr(col);
2743 if (char_attr < 0)
2744 break; /* end of preedit string */
2745
2746# ifdef FEAT_MBYTE
2747 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002748 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
2750# endif
2751 char_len = 1;
2752
2753 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2754 cmdpos += char_len;
2755 }
2756
2757 msg_row = old_row;
2758 msg_col = old_col;
2759 }
2760}
2761#endif /* FEAT_XIM && FEAT_GUI_GTK */
2762
2763/*
2764 * Allocate a new command line buffer.
2765 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2766 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2767 */
2768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002769alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 /*
2772 * give some extra space to avoid having to allocate all the time
2773 */
2774 if (len < 80)
2775 len = 100;
2776 else
2777 len += 20;
2778
2779 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2780 ccline.cmdbufflen = len;
2781}
2782
2783/*
2784 * Re-allocate the command line to length len + something extra.
2785 * return FAIL for failure, OK otherwise
2786 */
2787 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002788realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 char_u *p;
2791
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002792 if (len < ccline.cmdbufflen)
2793 return OK; /* no need to resize */
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 p = ccline.cmdbuff;
2796 alloc_cmdbuff(len); /* will get some more */
2797 if (ccline.cmdbuff == NULL) /* out of memory */
2798 {
2799 ccline.cmdbuff = p; /* keep the old one */
2800 return FAIL;
2801 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002802 /* There isn't always a NUL after the command, but it may need to be
2803 * there, thus copy up to the NUL and add a NUL. */
2804 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2805 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002807
2808 if (ccline.xpc != NULL
2809 && ccline.xpc->xp_pattern != NULL
2810 && ccline.xpc->xp_context != EXPAND_NOTHING
2811 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2812 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002813 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002814
2815 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2816 * to point into the newly allocated memory. */
2817 if (i >= 0 && i <= ccline.cmdlen)
2818 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2819 }
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return OK;
2822}
2823
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002824#if defined(FEAT_ARABIC) || defined(PROTO)
2825static char_u *arshape_buf = NULL;
2826
2827# if defined(EXITFREE) || defined(PROTO)
2828 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002829free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002830{
2831 vim_free(arshape_buf);
2832}
2833# endif
2834#endif
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836/*
2837 * Draw part of the cmdline at the current cursor position. But draw stars
2838 * when cmdline_star is TRUE.
2839 */
2840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002841draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2844 int i;
2845
2846 if (cmdline_star > 0)
2847 for (i = 0; i < len; ++i)
2848 {
2849 msg_putchar('*');
2850# ifdef FEAT_MBYTE
2851 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002852 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853# endif
2854 }
2855 else
2856#endif
2857#ifdef FEAT_ARABIC
2858 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 static int buflen = 0;
2861 char_u *p;
2862 int j;
2863 int newlen = 0;
2864 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002865 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 int prev_c = 0;
2867 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002868 int u8c;
2869 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 /*
2873 * Do arabic shaping into a temporary buffer. This is very
2874 * inefficient!
2875 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002876 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 {
2878 /* Re-allocate the buffer. We keep it around to avoid a lot of
2879 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002880 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002881 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002882 arshape_buf = alloc(buflen);
2883 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 return; /* out of memory */
2885 }
2886
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002887 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2888 {
2889 /* Prepend a space to draw the leading composing char on. */
2890 arshape_buf[0] = ' ';
2891 newlen = 1;
2892 }
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 for (j = start; j < start + len; j += mb_l)
2895 {
2896 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002897 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002898 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 if (ARABIC_CHAR(u8c))
2900 {
2901 /* Do Arabic shaping. */
2902 if (cmdmsg_rl)
2903 {
2904 /* displaying from right to left */
2905 pc = prev_c;
2906 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002907 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (j + mb_l >= start + len)
2909 nc = NUL;
2910 else
2911 nc = utf_ptr2char(p + mb_l);
2912 }
2913 else
2914 {
2915 /* displaying from left to right */
2916 if (j + mb_l >= start + len)
2917 pc = NUL;
2918 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002919 {
2920 int pcc[MAX_MCO];
2921
2922 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002924 pc1 = pcc[0];
2925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 nc = prev_c;
2927 }
2928 prev_c = u8c;
2929
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002930 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002932 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002933 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002935 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2936 if (u8cc[1] != 0)
2937 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002938 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940 }
2941 else
2942 {
2943 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002944 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 newlen += mb_l;
2946 }
2947 }
2948
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002949 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951 else
2952#endif
2953 msg_outtrans_len(ccline.cmdbuff + start, len);
2954}
2955
2956/*
2957 * Put a character on the command line. Shifts the following text to the
2958 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2959 * "c" must be printable (fit in one display cell)!
2960 */
2961 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002962putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963{
2964 if (cmd_silent)
2965 return;
2966 msg_no_more = TRUE;
2967 msg_putchar(c);
2968 if (shift)
2969 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2970 msg_no_more = FALSE;
2971 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002972 extra_char = c;
2973 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974}
2975
2976/*
2977 * Undo a putcmdline(c, FALSE).
2978 */
2979 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002980unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 if (cmd_silent)
2983 return;
2984 msg_no_more = TRUE;
2985 if (ccline.cmdlen == ccline.cmdpos)
2986 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002987#ifdef FEAT_MBYTE
2988 else if (has_mbyte)
2989 draw_cmdline(ccline.cmdpos,
2990 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 else
2993 draw_cmdline(ccline.cmdpos, 1);
2994 msg_no_more = FALSE;
2995 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002996 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997}
2998
2999/*
3000 * Put the given string, of the given length, onto the command line.
3001 * If len is -1, then STRLEN() is used to calculate the length.
3002 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3003 * part will be redrawn, otherwise it will not. If this function is called
3004 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3005 * called afterwards.
3006 */
3007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003008put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 int retval;
3011 int i;
3012 int m;
3013 int c;
3014
3015 if (len < 0)
3016 len = (int)STRLEN(str);
3017
3018 /* Check if ccline.cmdbuff needs to be longer */
3019 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003020 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 else
3022 retval = OK;
3023 if (retval == OK)
3024 {
3025 if (!ccline.overstrike)
3026 {
3027 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3028 ccline.cmdbuff + ccline.cmdpos,
3029 (size_t)(ccline.cmdlen - ccline.cmdpos));
3030 ccline.cmdlen += len;
3031 }
3032 else
3033 {
3034#ifdef FEAT_MBYTE
3035 if (has_mbyte)
3036 {
3037 /* Count nr of characters in the new string. */
3038 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003039 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 ++m;
3041 /* Count nr of bytes in cmdline that are overwritten by these
3042 * characters. */
3043 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003044 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 --m;
3046 if (i < ccline.cmdlen)
3047 {
3048 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3049 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3050 ccline.cmdlen += ccline.cmdpos + len - i;
3051 }
3052 else
3053 ccline.cmdlen = ccline.cmdpos + len;
3054 }
3055 else
3056#endif
3057 if (ccline.cmdpos + len > ccline.cmdlen)
3058 ccline.cmdlen = ccline.cmdpos + len;
3059 }
3060 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3061 ccline.cmdbuff[ccline.cmdlen] = NUL;
3062
3063#ifdef FEAT_MBYTE
3064 if (enc_utf8)
3065 {
3066 /* When the inserted text starts with a composing character,
3067 * backup to the character before it. There could be two of them.
3068 */
3069 i = 0;
3070 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3071 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3072 {
3073 i = (*mb_head_off)(ccline.cmdbuff,
3074 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3075 ccline.cmdpos -= i;
3076 len += i;
3077 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3078 }
3079# ifdef FEAT_ARABIC
3080 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3081 {
3082 /* Check the previous character for Arabic combining pair. */
3083 i = (*mb_head_off)(ccline.cmdbuff,
3084 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3085 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3086 + ccline.cmdpos - i), c))
3087 {
3088 ccline.cmdpos -= i;
3089 len += i;
3090 }
3091 else
3092 i = 0;
3093 }
3094# endif
3095 if (i != 0)
3096 {
3097 /* Also backup the cursor position. */
3098 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3099 ccline.cmdspos -= i;
3100 msg_col -= i;
3101 if (msg_col < 0)
3102 {
3103 msg_col += Columns;
3104 --msg_row;
3105 }
3106 }
3107 }
3108#endif
3109
3110 if (redraw && !cmd_silent)
3111 {
3112 msg_no_more = TRUE;
3113 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003114 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3116 /* Avoid clearing the rest of the line too often. */
3117 if (cmdline_row != i || ccline.overstrike)
3118 msg_clr_eos();
3119 msg_no_more = FALSE;
3120 }
3121#ifdef FEAT_FKMAP
3122 /*
3123 * If we are in Farsi command mode, the character input must be in
3124 * Insert mode. So do not advance the cmdpos.
3125 */
3126 if (!cmd_fkmap)
3127#endif
3128 {
3129 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003132 if (m < 0) /* overflow, Columns or Rows at weird value */
3133 m = MAXCOL;
3134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 else
3136 m = MAXCOL;
3137 for (i = 0; i < len; ++i)
3138 {
3139 c = cmdline_charsize(ccline.cmdpos);
3140#ifdef FEAT_MBYTE
3141 /* count ">" for a double-wide char that doesn't fit. */
3142 if (has_mbyte)
3143 correct_cmdspos(ccline.cmdpos, c);
3144#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003145 /* Stop cursor at the end of the screen, but do increment the
3146 * insert position, so that entering a very long command
3147 * works, even though you can't see it. */
3148 if (ccline.cmdspos + c < m)
3149 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#ifdef FEAT_MBYTE
3151 if (has_mbyte)
3152 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003153 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (c > len - i - 1)
3155 c = len - i - 1;
3156 ccline.cmdpos += c;
3157 i += c;
3158 }
3159#endif
3160 ++ccline.cmdpos;
3161 }
3162 }
3163 }
3164 if (redraw)
3165 msg_check();
3166 return retval;
3167}
3168
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003169static struct cmdline_info prev_ccline;
3170static int prev_ccline_used = FALSE;
3171
3172/*
3173 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3174 * and overwrite it. But get_cmdline_str() may need it, thus make it
3175 * available globally in prev_ccline.
3176 */
3177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003178save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003179{
3180 if (!prev_ccline_used)
3181 {
3182 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3183 prev_ccline_used = TRUE;
3184 }
3185 *ccp = prev_ccline;
3186 prev_ccline = ccline;
3187 ccline.cmdbuff = NULL;
3188 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003189 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003190}
3191
3192/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003193 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003194 */
3195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003197{
3198 ccline = prev_ccline;
3199 prev_ccline = *ccp;
3200}
3201
Bram Moolenaar5a305422006-04-28 22:38:25 +00003202#if defined(FEAT_EVAL) || defined(PROTO)
3203/*
3204 * Save the command line into allocated memory. Returns a pointer to be
3205 * passed to restore_cmdline_alloc() later.
3206 * Returns NULL when failed.
3207 */
3208 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003209save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003210{
3211 struct cmdline_info *p;
3212
3213 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3214 if (p != NULL)
3215 save_cmdline(p);
3216 return (char_u *)p;
3217}
3218
3219/*
3220 * Restore the command line from the return value of save_cmdline_alloc().
3221 */
3222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003223restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003224{
3225 if (p != NULL)
3226 {
3227 restore_cmdline((struct cmdline_info *)p);
3228 vim_free(p);
3229 }
3230}
3231#endif
3232
Bram Moolenaar8299df92004-07-10 09:47:34 +00003233/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003234 * Paste a yank register into the command line.
3235 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003236 * insert_reg() can't be used here, because special characters from the
3237 * register contents will be interpreted as commands.
3238 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003239 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003240 */
3241 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003242cmdline_paste(
3243 int regname,
3244 int literally, /* Insert text literally instead of "as typed" */
3245 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003246{
3247 long i;
3248 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003249 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003250 int allocated;
3251 struct cmdline_info save_ccline;
3252
3253 /* check for valid regname; also accept special characters for CTRL-R in
3254 * the command line */
3255 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3256 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3257 return FAIL;
3258
3259 /* A register containing CTRL-R can cause an endless loop. Allow using
3260 * CTRL-C to break the loop. */
3261 line_breakcheck();
3262 if (got_int)
3263 return FAIL;
3264
3265#ifdef FEAT_CLIPBOARD
3266 regname = may_get_selection(regname);
3267#endif
3268
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003269 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003270 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003271 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003272 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003273 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003274 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003275 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003276
3277 if (i)
3278 {
3279 /* Got the value of a special register in "arg". */
3280 if (arg == NULL)
3281 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003282
3283 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3284 * part of the word. */
3285 p = arg;
3286 if (p_is && regname == Ctrl_W)
3287 {
3288 char_u *w;
3289 int len;
3290
3291 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003292 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003293 {
3294#ifdef FEAT_MBYTE
3295 if (has_mbyte)
3296 {
3297 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3298 if (!vim_iswordc(mb_ptr2char(w - len)))
3299 break;
3300 w -= len;
3301 }
3302 else
3303#endif
3304 {
3305 if (!vim_iswordc(w[-1]))
3306 break;
3307 --w;
3308 }
3309 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003310 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003311 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3312 p += len;
3313 }
3314
3315 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003316 if (allocated)
3317 vim_free(arg);
3318 return OK;
3319 }
3320
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003321 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003322}
3323
3324/*
3325 * Put a string on the command line.
3326 * When "literally" is TRUE, insert literally.
3327 * When "literally" is FALSE, insert as typed, but don't leave the command
3328 * line.
3329 */
3330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003331cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003332{
3333 int c, cv;
3334
3335 if (literally)
3336 put_on_cmdline(s, -1, TRUE);
3337 else
3338 while (*s != NUL)
3339 {
3340 cv = *s;
3341 if (cv == Ctrl_V && s[1])
3342 ++s;
3343#ifdef FEAT_MBYTE
3344 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003345 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003346 else
3347#endif
3348 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003349 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3350 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003351#ifdef UNIX
3352 || c == intr_char
3353#endif
3354 || (c == Ctrl_BSL && *s == Ctrl_N))
3355 stuffcharReadbuff(Ctrl_V);
3356 stuffcharReadbuff(c);
3357 }
3358}
3359
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#ifdef FEAT_WILDMENU
3361/*
3362 * Delete characters on the command line, from "from" to the current
3363 * position.
3364 */
3365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003366cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3369 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3370 ccline.cmdlen -= ccline.cmdpos - from;
3371 ccline.cmdpos = from;
3372}
3373#endif
3374
3375/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003376 * This function is called when the screen size changes and with incremental
3377 * search and in other situations where the command line may have been
3378 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 */
3380 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003381redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003383 redrawcmdline_ex(TRUE);
3384}
3385
3386 void
3387redrawcmdline_ex(int do_compute_cmdrow)
3388{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 if (cmd_silent)
3390 return;
3391 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003392 if (do_compute_cmdrow)
3393 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 redrawcmd();
3395 cursorcmd();
3396}
3397
3398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003399redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 int i;
3402
3403 if (cmd_silent)
3404 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003405 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 msg_putchar(ccline.cmdfirstc);
3407 if (ccline.cmdprompt != NULL)
3408 {
3409 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3410 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3411 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003412 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 --ccline.cmdindent;
3414 }
3415 else
3416 for (i = ccline.cmdindent; i > 0; --i)
3417 msg_putchar(' ');
3418}
3419
3420/*
3421 * Redraw what is currently on the command line.
3422 */
3423 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003424redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
3426 if (cmd_silent)
3427 return;
3428
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003429 /* when 'incsearch' is set there may be no command line while redrawing */
3430 if (ccline.cmdbuff == NULL)
3431 {
3432 windgoto(cmdline_row, 0);
3433 msg_clr_eos();
3434 return;
3435 }
3436
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 msg_start();
3438 redrawcmdprompt();
3439
3440 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3441 msg_no_more = TRUE;
3442 draw_cmdline(0, ccline.cmdlen);
3443 msg_clr_eos();
3444 msg_no_more = FALSE;
3445
3446 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003447 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003448 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
3450 /*
3451 * An emsg() before may have set msg_scroll. This is used in normal mode,
3452 * in cmdline mode we can reset them now.
3453 */
3454 msg_scroll = FALSE; /* next message overwrites cmdline */
3455
3456 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3457 * in cmdline mode */
3458 skip_redraw = FALSE;
3459}
3460
3461 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003462compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003464 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 cmdline_row = Rows - 1;
3466 else
3467 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003468 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
3470
3471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003472cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 if (cmd_silent)
3475 return;
3476
3477#ifdef FEAT_RIGHTLEFT
3478 if (cmdmsg_rl)
3479 {
3480 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3481 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3482 if (msg_row <= 0)
3483 msg_row = Rows - 1;
3484 }
3485 else
3486#endif
3487 {
3488 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3489 msg_col = ccline.cmdspos % (int)Columns;
3490 if (msg_row >= Rows)
3491 msg_row = Rows - 1;
3492 }
3493
3494 windgoto(msg_row, msg_col);
3495#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003496 if (p_imst == IM_ON_THE_SPOT)
3497 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#endif
3499#ifdef MCH_CURSOR_SHAPE
3500 mch_update_cursor();
3501#endif
3502}
3503
3504 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003505gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506{
3507 msg_start();
3508#ifdef FEAT_RIGHTLEFT
3509 if (cmdmsg_rl)
3510 msg_col = Columns - 1;
3511 else
3512#endif
3513 msg_col = 0; /* always start in column 0 */
3514 if (clr) /* clear the bottom line(s) */
3515 msg_clr_eos(); /* will reset clear_cmdline */
3516 windgoto(cmdline_row, 0);
3517}
3518
3519/*
3520 * Check the word in front of the cursor for an abbreviation.
3521 * Called when the non-id character "c" has been entered.
3522 * When an abbreviation is recognized it is removed from the text with
3523 * backspaces and the replacement string is inserted, followed by "c".
3524 */
3525 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003526ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
3528 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3529 return FALSE;
3530
3531 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3532}
3533
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003534#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3535 static int
3536#ifdef __BORLANDC__
3537_RTLENTRYF
3538#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003539sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003540{
3541 char_u *p1 = *(char_u **)s1;
3542 char_u *p2 = *(char_u **)s2;
3543
3544 if (*p1 != '<' && *p2 == '<') return -1;
3545 if (*p1 == '<' && *p2 != '<') return 1;
3546 return STRCMP(p1, p2);
3547}
3548#endif
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550/*
3551 * Return FAIL if this is not an appropriate context in which to do
3552 * completion of anything, return OK if it is (even if there are no matches).
3553 * For the caller, this means that the character is just passed through like a
3554 * normal character (instead of being expanded). This allows :s/^I^D etc.
3555 */
3556 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003557nextwild(
3558 expand_T *xp,
3559 int type,
3560 int options, /* extra options for ExpandOne() */
3561 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
3563 int i, j;
3564 char_u *p1;
3565 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 int difflen;
3567 int v;
3568
3569 if (xp->xp_numfiles == -1)
3570 {
3571 set_expand_context(xp);
3572 cmd_showtail = expand_showtail(xp);
3573 }
3574
3575 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3576 {
3577 beep_flush();
3578 return OK; /* Something illegal on command line */
3579 }
3580 if (xp->xp_context == EXPAND_NOTHING)
3581 {
3582 /* Caller can use the character as a normal char instead */
3583 return FAIL;
3584 }
3585
3586 MSG_PUTS("..."); /* show that we are busy */
3587 out_flush();
3588
3589 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003590 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 if (type == WILD_NEXT || type == WILD_PREV)
3593 {
3594 /*
3595 * Get next/previous match for a previous expanded pattern.
3596 */
3597 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3598 }
3599 else
3600 {
3601 /*
3602 * Translate string into pattern and expand it.
3603 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003604 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3605 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 p2 = NULL;
3607 else
3608 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003609 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003610 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3611 if (escape)
3612 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003613
3614 if (p_wic)
3615 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003616 p2 = ExpandOne(xp, p1,
3617 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003618 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003620 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (p2 != NULL && type == WILD_LONGEST)
3622 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003623 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (ccline.cmdbuff[i + j] == '*'
3625 || ccline.cmdbuff[i + j] == '?')
3626 break;
3627 if ((int)STRLEN(p2) < j)
3628 {
3629 vim_free(p2);
3630 p2 = NULL;
3631 }
3632 }
3633 }
3634 }
3635
3636 if (p2 != NULL && !got_int)
3637 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003638 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003639 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003641 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 xp->xp_pattern = ccline.cmdbuff + i;
3643 }
3644 else
3645 v = OK;
3646 if (v == OK)
3647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003648 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3649 &ccline.cmdbuff[ccline.cmdpos],
3650 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3651 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 ccline.cmdlen += difflen;
3653 ccline.cmdpos += difflen;
3654 }
3655 }
3656 vim_free(p2);
3657
3658 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003659 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 /* When expanding a ":map" command and no matches are found, assume that
3662 * the key is supposed to be inserted literally */
3663 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3664 return FAIL;
3665
3666 if (xp->xp_numfiles <= 0 && p2 == NULL)
3667 beep_flush();
3668 else if (xp->xp_numfiles == 1)
3669 /* free expanded pattern */
3670 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3671
3672 return OK;
3673}
3674
3675/*
3676 * Do wildcard expansion on the string 'str'.
3677 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003678 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 * Return NULL for failure.
3680 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003681 * "orig" is the originally expanded string, copied to allocated memory. It
3682 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3683 * WILD_PREV "orig" should be NULL.
3684 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003685 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3686 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 *
3688 * mode = WILD_FREE: just free previously expanded matches
3689 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3690 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3691 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3692 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3693 * mode = WILD_ALL: return all matches concatenated
3694 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003695 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 *
3697 * options = WILD_LIST_NOTFOUND: list entries without a match
3698 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3699 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3700 * options = WILD_NO_BEEP: Don't beep for multiple matches
3701 * options = WILD_ADD_SLASH: add a slash after directory names
3702 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3703 * options = WILD_SILENT: don't print warning messages
3704 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003705 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 *
3707 * The variables xp->xp_context and xp->xp_backslash must have been set!
3708 */
3709 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003710ExpandOne(
3711 expand_T *xp,
3712 char_u *str,
3713 char_u *orig, /* allocated copy of original of expanded string */
3714 int options,
3715 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
3717 char_u *ss = NULL;
3718 static int findex;
3719 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003720 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 int i;
3722 long_u len;
3723 int non_suf_match; /* number without matching suffix */
3724
3725 /*
3726 * first handle the case of using an old match
3727 */
3728 if (mode == WILD_NEXT || mode == WILD_PREV)
3729 {
3730 if (xp->xp_numfiles > 0)
3731 {
3732 if (mode == WILD_PREV)
3733 {
3734 if (findex == -1)
3735 findex = xp->xp_numfiles;
3736 --findex;
3737 }
3738 else /* mode == WILD_NEXT */
3739 ++findex;
3740
3741 /*
3742 * When wrapping around, return the original string, set findex to
3743 * -1.
3744 */
3745 if (findex < 0)
3746 {
3747 if (orig_save == NULL)
3748 findex = xp->xp_numfiles - 1;
3749 else
3750 findex = -1;
3751 }
3752 if (findex >= xp->xp_numfiles)
3753 {
3754 if (orig_save == NULL)
3755 findex = 0;
3756 else
3757 findex = -1;
3758 }
3759#ifdef FEAT_WILDMENU
3760 if (p_wmnu)
3761 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3762 findex, cmd_showtail);
3763#endif
3764 if (findex == -1)
3765 return vim_strsave(orig_save);
3766 return vim_strsave(xp->xp_files[findex]);
3767 }
3768 else
3769 return NULL;
3770 }
3771
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003772 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3774 {
3775 FreeWild(xp->xp_numfiles, xp->xp_files);
3776 xp->xp_numfiles = -1;
3777 vim_free(orig_save);
3778 orig_save = NULL;
3779 }
3780 findex = 0;
3781
3782 if (mode == WILD_FREE) /* only release file name */
3783 return NULL;
3784
3785 if (xp->xp_numfiles == -1)
3786 {
3787 vim_free(orig_save);
3788 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003789 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 /*
3792 * Do the expansion.
3793 */
3794 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3795 options) == FAIL)
3796 {
3797#ifdef FNAME_ILLEGAL
3798 /* Illegal file name has been silently skipped. But when there
3799 * are wildcards, the real problem is that there was no match,
3800 * causing the pattern to be added, which has illegal characters.
3801 */
3802 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3803 EMSG2(_(e_nomatch2), str);
3804#endif
3805 }
3806 else if (xp->xp_numfiles == 0)
3807 {
3808 if (!(options & WILD_SILENT))
3809 EMSG2(_(e_nomatch2), str);
3810 }
3811 else
3812 {
3813 /* Escape the matches for use on the command line. */
3814 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3815
3816 /*
3817 * Check for matching suffixes in file names.
3818 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003819 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3820 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
3822 if (xp->xp_numfiles)
3823 non_suf_match = xp->xp_numfiles;
3824 else
3825 non_suf_match = 1;
3826 if ((xp->xp_context == EXPAND_FILES
3827 || xp->xp_context == EXPAND_DIRECTORIES)
3828 && xp->xp_numfiles > 1)
3829 {
3830 /*
3831 * More than one match; check suffix.
3832 * The files will have been sorted on matching suffix in
3833 * expand_wildcards, only need to check the first two.
3834 */
3835 non_suf_match = 0;
3836 for (i = 0; i < 2; ++i)
3837 if (match_suffix(xp->xp_files[i]))
3838 ++non_suf_match;
3839 }
3840 if (non_suf_match != 1)
3841 {
3842 /* Can we ever get here unless it's while expanding
3843 * interactively? If not, we can get rid of this all
3844 * together. Don't really want to wait for this message
3845 * (and possibly have to hit return to continue!).
3846 */
3847 if (!(options & WILD_SILENT))
3848 EMSG(_(e_toomany));
3849 else if (!(options & WILD_NO_BEEP))
3850 beep_flush();
3851 }
3852 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3853 ss = vim_strsave(xp->xp_files[0]);
3854 }
3855 }
3856 }
3857
3858 /* Find longest common part */
3859 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3860 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003861 int mb_len = 1;
3862 int c0, ci;
3863
3864 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003866#ifdef FEAT_MBYTE
3867 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003869 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3870 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3871 }
3872 else
3873#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003874 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003875 for (i = 1; i < xp->xp_numfiles; ++i)
3876 {
3877#ifdef FEAT_MBYTE
3878 if (has_mbyte)
3879 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3880 else
3881#endif
3882 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003883 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003885 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003886 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003888 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 break;
3890 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003891 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 break;
3893 }
3894 if (i < xp->xp_numfiles)
3895 {
3896 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003897 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 break;
3899 }
3900 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 ss = alloc((unsigned)len + 1);
3903 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003904 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 findex = -1; /* next p_wc gets first one */
3906 }
3907
3908 /* Concatenate all matching names */
3909 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3910 {
3911 len = 0;
3912 for (i = 0; i < xp->xp_numfiles; ++i)
3913 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3914 ss = lalloc(len, TRUE);
3915 if (ss != NULL)
3916 {
3917 *ss = NUL;
3918 for (i = 0; i < xp->xp_numfiles; ++i)
3919 {
3920 STRCAT(ss, xp->xp_files[i]);
3921 if (i != xp->xp_numfiles - 1)
3922 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3923 }
3924 }
3925 }
3926
3927 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3928 ExpandCleanup(xp);
3929
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003930 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003931 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003932 vim_free(orig);
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 return ss;
3935}
3936
3937/*
3938 * Prepare an expand structure for use.
3939 */
3940 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003941ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003943 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003944 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003946#ifndef BACKSLASH_IN_FILENAME
3947 xp->xp_shell = FALSE;
3948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 xp->xp_numfiles = -1;
3950 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003951#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3952 xp->xp_arg = NULL;
3953#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003954 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955}
3956
3957/*
3958 * Cleanup an expand structure after use.
3959 */
3960 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003961ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963 if (xp->xp_numfiles >= 0)
3964 {
3965 FreeWild(xp->xp_numfiles, xp->xp_files);
3966 xp->xp_numfiles = -1;
3967 }
3968}
3969
3970 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003971ExpandEscape(
3972 expand_T *xp,
3973 char_u *str,
3974 int numfiles,
3975 char_u **files,
3976 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 int i;
3979 char_u *p;
3980
3981 /*
3982 * May change home directory back to "~"
3983 */
3984 if (options & WILD_HOME_REPLACE)
3985 tilde_replace(str, numfiles, files);
3986
3987 if (options & WILD_ESCAPE)
3988 {
3989 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003990 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003991 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 || xp->xp_context == EXPAND_BUFFERS
3993 || xp->xp_context == EXPAND_DIRECTORIES)
3994 {
3995 /*
3996 * Insert a backslash into a file name before a space, \, %, #
3997 * and wildmatch characters, except '~'.
3998 */
3999 for (i = 0; i < numfiles; ++i)
4000 {
4001 /* for ":set path=" we need to escape spaces twice */
4002 if (xp->xp_backslash == XP_BS_THREE)
4003 {
4004 p = vim_strsave_escaped(files[i], (char_u *)" ");
4005 if (p != NULL)
4006 {
4007 vim_free(files[i]);
4008 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004009#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 p = vim_strsave_escaped(files[i], (char_u *)" ");
4011 if (p != NULL)
4012 {
4013 vim_free(files[i]);
4014 files[i] = p;
4015 }
4016#endif
4017 }
4018 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004019#ifdef BACKSLASH_IN_FILENAME
4020 p = vim_strsave_fnameescape(files[i], FALSE);
4021#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004022 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (p != NULL)
4025 {
4026 vim_free(files[i]);
4027 files[i] = p;
4028 }
4029
4030 /* If 'str' starts with "\~", replace "~" at start of
4031 * files[i] with "\~". */
4032 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004033 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004036
4037 /* If the first file starts with a '+' escape it. Otherwise it
4038 * could be seen as "+cmd". */
4039 if (*files[0] == '+')
4040 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 }
4042 else if (xp->xp_context == EXPAND_TAGS)
4043 {
4044 /*
4045 * Insert a backslash before characters in a tag name that
4046 * would terminate the ":tag" command.
4047 */
4048 for (i = 0; i < numfiles; ++i)
4049 {
4050 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4051 if (p != NULL)
4052 {
4053 vim_free(files[i]);
4054 files[i] = p;
4055 }
4056 }
4057 }
4058 }
4059}
4060
4061/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004062 * Escape special characters in "fname" for when used as a file name argument
4063 * after a Vim command, or, when "shell" is non-zero, a shell command.
4064 * Returns the result in allocated memory.
4065 */
4066 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004067vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004068{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004069 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004070#ifdef BACKSLASH_IN_FILENAME
4071 char_u buf[20];
4072 int j = 0;
4073
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004074 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004075 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004076 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004077 buf[j++] = *p;
4078 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004079 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004080#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004081 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4082 if (shell && csh_like_shell() && p != NULL)
4083 {
4084 char_u *s;
4085
4086 /* For csh and similar shells need to put two backslashes before '!'.
4087 * One is taken by Vim, one by the shell. */
4088 s = vim_strsave_escaped(p, (char_u *)"!");
4089 vim_free(p);
4090 p = s;
4091 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004092#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004093
4094 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4095 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004096 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004097 escape_fname(&p);
4098
4099 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004100}
4101
4102/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004103 * Put a backslash before the file name in "pp", which is in allocated memory.
4104 */
4105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004106escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004107{
4108 char_u *p;
4109
4110 p = alloc((unsigned)(STRLEN(*pp) + 2));
4111 if (p != NULL)
4112 {
4113 p[0] = '\\';
4114 STRCPY(p + 1, *pp);
4115 vim_free(*pp);
4116 *pp = p;
4117 }
4118}
4119
4120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * For each file name in files[num_files]:
4122 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4123 */
4124 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004125tilde_replace(
4126 char_u *orig_pat,
4127 int num_files,
4128 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 int i;
4131 char_u *p;
4132
4133 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4134 {
4135 for (i = 0; i < num_files; ++i)
4136 {
4137 p = home_replace_save(NULL, files[i]);
4138 if (p != NULL)
4139 {
4140 vim_free(files[i]);
4141 files[i] = p;
4142 }
4143 }
4144 }
4145}
4146
4147/*
4148 * Show all matches for completion on the command line.
4149 * Returns EXPAND_NOTHING when the character that triggered expansion should
4150 * be inserted like a normal character.
4151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004153showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
4155#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4156 int num_files;
4157 char_u **files_found;
4158 int i, j, k;
4159 int maxlen;
4160 int lines;
4161 int columns;
4162 char_u *p;
4163 int lastlen;
4164 int attr;
4165 int showtail;
4166
4167 if (xp->xp_numfiles == -1)
4168 {
4169 set_expand_context(xp);
4170 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4171 &num_files, &files_found);
4172 showtail = expand_showtail(xp);
4173 if (i != EXPAND_OK)
4174 return i;
4175
4176 }
4177 else
4178 {
4179 num_files = xp->xp_numfiles;
4180 files_found = xp->xp_files;
4181 showtail = cmd_showtail;
4182 }
4183
4184#ifdef FEAT_WILDMENU
4185 if (!wildmenu)
4186 {
4187#endif
4188 msg_didany = FALSE; /* lines_left will be set */
4189 msg_start(); /* prepare for paging */
4190 msg_putchar('\n');
4191 out_flush();
4192 cmdline_row = msg_row;
4193 msg_didany = FALSE; /* lines_left will be set again */
4194 msg_start(); /* prepare for paging */
4195#ifdef FEAT_WILDMENU
4196 }
4197#endif
4198
4199 if (got_int)
4200 got_int = FALSE; /* only int. the completion, not the cmd line */
4201#ifdef FEAT_WILDMENU
4202 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004203 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204#endif
4205 else
4206 {
4207 /* find the length of the longest file name */
4208 maxlen = 0;
4209 for (i = 0; i < num_files; ++i)
4210 {
4211 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004212 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 || xp->xp_context == EXPAND_BUFFERS))
4214 {
4215 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4216 j = vim_strsize(NameBuff);
4217 }
4218 else
4219 j = vim_strsize(L_SHOWFILE(i));
4220 if (j > maxlen)
4221 maxlen = j;
4222 }
4223
4224 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4225 lines = num_files;
4226 else
4227 {
4228 /* compute the number of columns and lines for the listing */
4229 maxlen += 2; /* two spaces between file names */
4230 columns = ((int)Columns + 2) / maxlen;
4231 if (columns < 1)
4232 columns = 1;
4233 lines = (num_files + columns - 1) / columns;
4234 }
4235
Bram Moolenaar8820b482017-03-16 17:23:31 +01004236 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4239 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004240 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 msg_clr_eos();
4242 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004243 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245
4246 /* list the files line by line */
4247 for (i = 0; i < lines; ++i)
4248 {
4249 lastlen = 999;
4250 for (k = i; k < num_files; k += lines)
4251 {
4252 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4253 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004254 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 p = files_found[k] + STRLEN(files_found[k]) + 1;
4256 msg_advance(maxlen + 1);
4257 msg_puts(p);
4258 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004259 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 break;
4261 }
4262 for (j = maxlen - lastlen; --j >= 0; )
4263 msg_putchar(' ');
4264 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004265 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 || xp->xp_context == EXPAND_BUFFERS)
4267 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004268 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004269 if (xp->xp_numfiles != -1)
4270 {
4271 char_u *halved_slash;
4272 char_u *exp_path;
4273
4274 /* Expansion was done before and special characters
4275 * were escaped, need to halve backslashes. Also
4276 * $HOME has been replaced with ~/. */
4277 exp_path = expand_env_save_opt(files_found[k], TRUE);
4278 halved_slash = backslash_halve_save(
4279 exp_path != NULL ? exp_path : files_found[k]);
4280 j = mch_isdir(halved_slash != NULL ? halved_slash
4281 : files_found[k]);
4282 vim_free(exp_path);
4283 vim_free(halved_slash);
4284 }
4285 else
4286 /* Expansion was done here, file names are literal. */
4287 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (showtail)
4289 p = L_SHOWFILE(k);
4290 else
4291 {
4292 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4293 TRUE);
4294 p = NameBuff;
4295 }
4296 }
4297 else
4298 {
4299 j = FALSE;
4300 p = L_SHOWFILE(k);
4301 }
4302 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4303 }
4304 if (msg_col > 0) /* when not wrapped around */
4305 {
4306 msg_clr_eos();
4307 msg_putchar('\n');
4308 }
4309 out_flush(); /* show one line at a time */
4310 if (got_int)
4311 {
4312 got_int = FALSE;
4313 break;
4314 }
4315 }
4316
4317 /*
4318 * we redraw the command below the lines that we have just listed
4319 * This is a bit tricky, but it saves a lot of screen updating.
4320 */
4321 cmdline_row = msg_row; /* will put it back later */
4322 }
4323
4324 if (xp->xp_numfiles == -1)
4325 FreeWild(num_files, files_found);
4326
4327 return EXPAND_OK;
4328}
4329
4330/*
4331 * Private gettail for showmatches() (and win_redr_status_matches()):
4332 * Find tail of file name path, but ignore trailing "/".
4333 */
4334 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004335sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 char_u *p;
4338 char_u *t = s;
4339 int had_sep = FALSE;
4340
4341 for (p = s; *p != NUL; )
4342 {
4343 if (vim_ispathsep(*p)
4344#ifdef BACKSLASH_IN_FILENAME
4345 && !rem_backslash(p)
4346#endif
4347 )
4348 had_sep = TRUE;
4349 else if (had_sep)
4350 {
4351 t = p;
4352 had_sep = FALSE;
4353 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004354 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 return t;
4357}
4358
4359/*
4360 * Return TRUE if we only need to show the tail of completion matches.
4361 * When not completing file names or there is a wildcard in the path FALSE is
4362 * returned.
4363 */
4364 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004365expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
4367 char_u *s;
4368 char_u *end;
4369
4370 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004371 if (xp->xp_context != EXPAND_FILES
4372 && xp->xp_context != EXPAND_SHELLCMD
4373 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 return FALSE;
4375
4376 end = gettail(xp->xp_pattern);
4377 if (end == xp->xp_pattern) /* there is no path separator */
4378 return FALSE;
4379
4380 for (s = xp->xp_pattern; s < end; s++)
4381 {
4382 /* Skip escaped wildcards. Only when the backslash is not a path
4383 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4384 if (rem_backslash(s))
4385 ++s;
4386 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4387 return FALSE;
4388 }
4389 return TRUE;
4390}
4391
4392/*
4393 * Prepare a string for expansion.
4394 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004395 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 * When expanding other names: The string will be used with regcomp(). Copy
4397 * the name into allocated memory and prepend "^".
4398 */
4399 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004400addstar(
4401 char_u *fname,
4402 int len,
4403 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 char_u *retval;
4406 int i, j;
4407 int new_len;
4408 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004409 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004411 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004412 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004413 && context != EXPAND_SHELLCMD
4414 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 /*
4417 * Matching will be done internally (on something other than files).
4418 * So we convert the file-matching-type wildcards into our kind for
4419 * use with vim_regcomp(). First work out how long it will be:
4420 */
4421
4422 /* For help tags the translation is done in find_help_tags().
4423 * For a tag pattern starting with "/" no translation is needed. */
4424 if (context == EXPAND_HELP
4425 || context == EXPAND_COLORS
4426 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004427 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004428 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004429 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004430 || ((context == EXPAND_TAGS_LISTFILES
4431 || context == EXPAND_TAGS)
4432 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 retval = vim_strnsave(fname, len);
4434 else
4435 {
4436 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4437 for (i = 0; i < len; i++)
4438 {
4439 if (fname[i] == '*' || fname[i] == '~')
4440 new_len++; /* '*' needs to be replaced by ".*"
4441 '~' needs to be replaced by "\~" */
4442
4443 /* Buffer names are like file names. "." should be literal */
4444 if (context == EXPAND_BUFFERS && fname[i] == '.')
4445 new_len++; /* "." becomes "\." */
4446
4447 /* Custom expansion takes care of special things, match
4448 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004449 if ((context == EXPAND_USER_DEFINED
4450 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 new_len++; /* '\' becomes "\\" */
4452 }
4453 retval = alloc(new_len);
4454 if (retval != NULL)
4455 {
4456 retval[0] = '^';
4457 j = 1;
4458 for (i = 0; i < len; i++, j++)
4459 {
4460 /* Skip backslash. But why? At least keep it for custom
4461 * expansion. */
4462 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004463 && context != EXPAND_USER_LIST
4464 && fname[i] == '\\'
4465 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 break;
4467
4468 switch (fname[i])
4469 {
4470 case '*': retval[j++] = '.';
4471 break;
4472 case '~': retval[j++] = '\\';
4473 break;
4474 case '?': retval[j] = '.';
4475 continue;
4476 case '.': if (context == EXPAND_BUFFERS)
4477 retval[j++] = '\\';
4478 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004479 case '\\': if (context == EXPAND_USER_DEFINED
4480 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 retval[j++] = '\\';
4482 break;
4483 }
4484 retval[j] = fname[i];
4485 }
4486 retval[j] = NUL;
4487 }
4488 }
4489 }
4490 else
4491 {
4492 retval = alloc(len + 4);
4493 if (retval != NULL)
4494 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004495 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004498 * Don't add a star to *, ~, ~user, $var or `cmd`.
4499 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 * ~ would be at the start of the file name, but not the tail.
4501 * $ could be anywhere in the tail.
4502 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004503 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 */
4505 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004506 ends_in_star = (len > 0 && retval[len - 1] == '*');
4507#ifndef BACKSLASH_IN_FILENAME
4508 for (i = len - 2; i >= 0; --i)
4509 {
4510 if (retval[i] != '\\')
4511 break;
4512 ends_in_star = !ends_in_star;
4513 }
4514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004516 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 && vim_strchr(tail, '$') == NULL
4518 && vim_strchr(retval, '`') == NULL)
4519 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004520 else if (len > 0 && retval[len - 1] == '$')
4521 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 retval[len] = NUL;
4523 }
4524 }
4525 return retval;
4526}
4527
4528/*
4529 * Must parse the command line so far to work out what context we are in.
4530 * Completion can then be done based on that context.
4531 * This routine sets the variables:
4532 * xp->xp_pattern The start of the pattern to be expanded within
4533 * the command line (ends at the cursor).
4534 * xp->xp_context The type of thing to expand. Will be one of:
4535 *
4536 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4537 * the command line, like an unknown command. Caller
4538 * should beep.
4539 * EXPAND_NOTHING Unrecognised context for completion, use char like
4540 * a normal char, rather than for completion. eg
4541 * :s/^I/
4542 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4543 * it.
4544 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4545 * EXPAND_FILES After command with XFILE set, or after setting
4546 * with P_EXPAND set. eg :e ^I, :w>>^I
4547 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4548 * when we know only directories are of interest. eg
4549 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004550 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4552 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4553 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4554 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4555 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4556 * EXPAND_EVENTS Complete event names
4557 * EXPAND_SYNTAX Complete :syntax command arguments
4558 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4559 * EXPAND_AUGROUP Complete autocommand group names
4560 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4561 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4562 * eg :unmap a^I , :cunab x^I
4563 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4564 * eg :call sub^I
4565 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4566 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4567 * names in expressions, eg :while s^I
4568 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004569 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 */
4571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004572set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004574 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (ccline.cmdfirstc != ':'
4576#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004577 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004578 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579#endif
4580 )
4581 {
4582 xp->xp_context = EXPAND_NOTHING;
4583 return;
4584 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004585 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586}
4587
4588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004589set_cmd_context(
4590 expand_T *xp,
4591 char_u *str, /* start of command line */
4592 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004593 int col, /* position of cursor */
4594 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595{
4596 int old_char = NUL;
4597 char_u *nextcomm;
4598
4599 /*
4600 * Avoid a UMR warning from Purify, only save the character if it has been
4601 * written before.
4602 */
4603 if (col < len)
4604 old_char = str[col];
4605 str[col] = NUL;
4606 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004607
4608#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004609 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004610 {
4611# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004612 /* pass CMD_SIZE because there is no real command */
4613 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004614# endif
4615 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004616 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004617 {
4618 xp->xp_context = ccline.xp_context;
4619 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004620# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004621 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004622# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004623 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004624 else
4625#endif
4626 while (nextcomm != NULL)
4627 nextcomm = set_one_cmd_context(xp, nextcomm);
4628
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004629 /* Store the string here so that call_user_expand_func() can get to them
4630 * easily. */
4631 xp->xp_line = str;
4632 xp->xp_col = col;
4633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 str[col] = old_char;
4635}
4636
4637/*
4638 * Expand the command line "str" from context "xp".
4639 * "xp" must have been set by set_cmd_context().
4640 * xp->xp_pattern points into "str", to where the text that is to be expanded
4641 * starts.
4642 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4643 * cursor.
4644 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4645 * key that triggered expansion literally.
4646 * Returns EXPAND_OK otherwise.
4647 */
4648 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004649expand_cmdline(
4650 expand_T *xp,
4651 char_u *str, /* start of command line */
4652 int col, /* position of cursor */
4653 int *matchcount, /* return: nr of matches */
4654 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
4656 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004657 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4660 {
4661 beep_flush();
4662 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4663 }
4664 if (xp->xp_context == EXPAND_NOTHING)
4665 {
4666 /* Caller can use the character as a normal char instead */
4667 return EXPAND_NOTHING;
4668 }
4669
4670 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004671 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4672 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (file_str == NULL)
4674 return EXPAND_UNSUCCESSFUL;
4675
Bram Moolenaar94950a92010-12-02 16:01:29 +01004676 if (p_wic)
4677 options += WILD_ICASE;
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004680 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
4682 *matchcount = 0;
4683 *matches = NULL;
4684 }
4685 vim_free(file_str);
4686
4687 return EXPAND_OK;
4688}
4689
4690#ifdef FEAT_MULTI_LANG
4691/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004692 * Cleanup matches for help tags:
4693 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4694 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004696static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
4698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004699cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
4701 int i, j;
4702 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004703 char_u buf[4];
4704 char_u *p = buf;
4705
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004706 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004707 {
4708 *p++ = '@';
4709 *p++ = p_hlg[0];
4710 *p++ = p_hlg[1];
4711 }
4712 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
4714 for (i = 0; i < num_file; ++i)
4715 {
4716 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004717 if (len <= 0)
4718 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004719 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 /* Sorting on priority means the same item in another language may
4722 * be anywhere. Search all items for a match up to the "@en". */
4723 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004724 if (j != i && (int)STRLEN(file[j]) == len + 3
4725 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 break;
4727 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004728 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 file[i][len] = NUL;
4730 }
4731 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004732
4733 if (*buf != NUL)
4734 for (i = 0; i < num_file; ++i)
4735 {
4736 len = (int)STRLEN(file[i]) - 3;
4737 if (len <= 0)
4738 continue;
4739 if (STRCMP(file[i] + len, buf) == 0)
4740 {
4741 /* remove the default language */
4742 file[i][len] = NUL;
4743 }
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745}
4746#endif
4747
4748/*
4749 * Do the expansion based on xp->xp_context and "pat".
4750 */
4751 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004752ExpandFromContext(
4753 expand_T *xp,
4754 char_u *pat,
4755 int *num_file,
4756 char_u ***file,
4757 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759#ifdef FEAT_CMDL_COMPL
4760 regmatch_T regmatch;
4761#endif
4762 int ret;
4763 int flags;
4764
4765 flags = EW_DIR; /* include directories */
4766 if (options & WILD_LIST_NOTFOUND)
4767 flags |= EW_NOTFOUND;
4768 if (options & WILD_ADD_SLASH)
4769 flags |= EW_ADDSLASH;
4770 if (options & WILD_KEEP_ALL)
4771 flags |= EW_KEEPALL;
4772 if (options & WILD_SILENT)
4773 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004774 if (options & WILD_ALLLINKS)
4775 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004777 if (xp->xp_context == EXPAND_FILES
4778 || xp->xp_context == EXPAND_DIRECTORIES
4779 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
4781 /*
4782 * Expand file or directory names.
4783 */
4784 int free_pat = FALSE;
4785 int i;
4786
4787 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4788 * space */
4789 if (xp->xp_backslash != XP_BS_NONE)
4790 {
4791 free_pat = TRUE;
4792 pat = vim_strsave(pat);
4793 for (i = 0; pat[i]; ++i)
4794 if (pat[i] == '\\')
4795 {
4796 if (xp->xp_backslash == XP_BS_THREE
4797 && pat[i + 1] == '\\'
4798 && pat[i + 2] == '\\'
4799 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004800 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (xp->xp_backslash == XP_BS_ONE
4802 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004803 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 }
4806
4807 if (xp->xp_context == EXPAND_FILES)
4808 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004809 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4810 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 else
4812 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004813 if (options & WILD_ICASE)
4814 flags |= EW_ICASE;
4815
Bram Moolenaard7834d32009-12-02 16:14:36 +00004816 /* Expand wildcards, supporting %:h and the like. */
4817 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (free_pat)
4819 vim_free(pat);
4820 return ret;
4821 }
4822
4823 *file = (char_u **)"";
4824 *num_file = 0;
4825 if (xp->xp_context == EXPAND_HELP)
4826 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004827 /* With an empty argument we would get all the help tags, which is
4828 * very slow. Get matches for "help" instead. */
4829 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4830 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
4832#ifdef FEAT_MULTI_LANG
4833 cleanup_help_tags(*num_file, *file);
4834#endif
4835 return OK;
4836 }
4837 return FAIL;
4838 }
4839
4840#ifndef FEAT_CMDL_COMPL
4841 return FAIL;
4842#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004843 if (xp->xp_context == EXPAND_SHELLCMD)
4844 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (xp->xp_context == EXPAND_OLD_SETTING)
4846 return ExpandOldSetting(num_file, file);
4847 if (xp->xp_context == EXPAND_BUFFERS)
4848 return ExpandBufnames(pat, num_file, file, options);
4849 if (xp->xp_context == EXPAND_TAGS
4850 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4851 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4852 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004853 {
4854 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004855 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4856 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004859 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004860 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004861 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004862 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004863 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004864 {
4865 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004866 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004867 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004868 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004869 {
4870 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004871 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004872 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004873# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4874 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004875 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004876# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004877 if (xp->xp_context == EXPAND_PACKADD)
4878 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4881 if (regmatch.regprog == NULL)
4882 return FAIL;
4883
4884 /* set ignore-case according to p_ic, p_scs and pat */
4885 regmatch.rm_ic = ignorecase(pat);
4886
4887 if (xp->xp_context == EXPAND_SETTINGS
4888 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4889 ret = ExpandSettings(xp, &regmatch, num_file, file);
4890 else if (xp->xp_context == EXPAND_MAPPINGS)
4891 ret = ExpandMappings(&regmatch, num_file, file);
4892# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4893 else if (xp->xp_context == EXPAND_USER_DEFINED)
4894 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4895# endif
4896 else
4897 {
4898 static struct expgen
4899 {
4900 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004901 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004903 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 } tab[] =
4905 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004906 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4907 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004908 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004909 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004910#ifdef FEAT_CMDHIST
4911 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004914 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004915 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004916 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4917 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4918 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919#endif
4920#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004921 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4922 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4923 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4924 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925#endif
4926#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004927 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4928 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
4930#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004931 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004933#ifdef FEAT_PROFILE
4934 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4935#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004936 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004938 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4939 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004941#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004942 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004943#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004944#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004945 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004946#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004947#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004948 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4951 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004952 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4953 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004955 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004956 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 };
4958 int i;
4959
4960 /*
4961 * Find a context in the table and call the ExpandGeneric() with the
4962 * right function to do the expansion.
4963 */
4964 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004965 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (xp->xp_context == tab[i].context)
4967 {
4968 if (tab[i].ic)
4969 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004970 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004971 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 break;
4973 }
4974 }
4975
Bram Moolenaar473de612013-06-08 18:19:48 +02004976 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 return ret;
4979#endif /* FEAT_CMDL_COMPL */
4980}
4981
4982#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4983/*
4984 * Expand a list of names.
4985 *
4986 * Generic function for command line completion. It calls a function to
4987 * obtain strings, one by one. The strings are matched against a regexp
4988 * program. Matching strings are copied into an array, which is returned.
4989 *
4990 * Returns OK when no problems encountered, FAIL for error (out of memory).
4991 */
4992 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004993ExpandGeneric(
4994 expand_T *xp,
4995 regmatch_T *regmatch,
4996 int *num_file,
4997 char_u ***file,
4998 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005000 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 int i;
5003 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005004 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 char_u *str;
5006
5007 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005008 * round == 0: count the number of matching names
5009 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005011 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 for (i = 0; ; ++i)
5014 {
5015 str = (*func)(xp, i);
5016 if (str == NULL) /* end of list */
5017 break;
5018 if (*str == NUL) /* skip empty strings */
5019 continue;
5020
5021 if (vim_regexec(regmatch, str, (colnr_T)0))
5022 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005023 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005025 if (escaped)
5026 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5027 else
5028 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 (*file)[count] = str;
5030#ifdef FEAT_MENU
5031 if (func == get_menu_names && str != NULL)
5032 {
5033 /* test for separator added by get_menu_names() */
5034 str += STRLEN(str) - 1;
5035 if (*str == '\001')
5036 *str = '.';
5037 }
5038#endif
5039 }
5040 ++count;
5041 }
5042 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005043 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 if (count == 0)
5046 return OK;
5047 *num_file = count;
5048 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5049 if (*file == NULL)
5050 {
5051 *file = (char_u **)"";
5052 return FAIL;
5053 }
5054 count = 0;
5055 }
5056 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005057
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005058 /* Sort the results. Keep menu's in the specified order. */
5059 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005060 {
5061 if (xp->xp_context == EXPAND_EXPRESSION
5062 || xp->xp_context == EXPAND_FUNCTIONS
5063 || xp->xp_context == EXPAND_USER_FUNC)
5064 /* <SNR> functions should be sorted to the end. */
5065 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5066 sort_func_compare);
5067 else
5068 sort_strings(*file, *num_file);
5069 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005070
Bram Moolenaar4f688582007-07-24 12:34:30 +00005071#ifdef FEAT_CMDL_COMPL
5072 /* Reset the variables used for special highlight names expansion, so that
5073 * they don't show up when getting normal highlight names by ID. */
5074 reset_expand_highlight();
5075#endif
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 return OK;
5078}
5079
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005080/*
5081 * Complete a shell command.
5082 * Returns FAIL or OK;
5083 */
5084 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005085expand_shellcmd(
5086 char_u *filepat, /* pattern to match with command names */
5087 int *num_file, /* return: number of matches */
5088 char_u ***file, /* return: array with matches */
5089 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005090{
5091 char_u *pat;
5092 int i;
5093 char_u *path;
5094 int mustfree = FALSE;
5095 garray_T ga;
5096 char_u *buf = alloc(MAXPATHL);
5097 size_t l;
5098 char_u *s, *e;
5099 int flags = flagsarg;
5100 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005101 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005102
5103 if (buf == NULL)
5104 return FAIL;
5105
5106 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5107 * space */
5108 pat = vim_strsave(filepat);
5109 for (i = 0; pat[i]; ++i)
5110 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005111 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005112
Bram Moolenaarb5971142015-03-21 17:32:19 +01005113 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005114
5115 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005116 if (mch_isFullName(pat))
5117 path = (char_u *)" ";
5118 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005119 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5120 path = (char_u *)".";
5121 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005122 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005123 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005124 if (path == NULL)
5125 path = (char_u *)"";
5126 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005127
5128 /*
5129 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005130 * collect them in "ga". When "." is not in $PATH also expand for the
5131 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005132 */
5133 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005134 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005135 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005136 if (*s == NUL)
5137 {
5138 if (did_curdir)
5139 break;
5140 /* Find directories in the current directory, path is empty. */
5141 did_curdir = TRUE;
5142 }
5143 else if (*s == '.')
5144 did_curdir = TRUE;
5145
Bram Moolenaar68c31742006-09-02 15:54:18 +00005146 if (*s == ' ')
5147 ++s; /* Skip space used for absolute path name. */
5148
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005149#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005150 e = vim_strchr(s, ';');
5151#else
5152 e = vim_strchr(s, ':');
5153#endif
5154 if (e == NULL)
5155 e = s + STRLEN(s);
5156
5157 l = e - s;
5158 if (l > MAXPATHL - 5)
5159 break;
5160 vim_strncpy(buf, s, l);
5161 add_pathsep(buf);
5162 l = STRLEN(buf);
5163 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5164
5165 /* Expand matches in one directory of $PATH. */
5166 ret = expand_wildcards(1, &buf, num_file, file, flags);
5167 if (ret == OK)
5168 {
5169 if (ga_grow(&ga, *num_file) == FAIL)
5170 FreeWild(*num_file, *file);
5171 else
5172 {
5173 for (i = 0; i < *num_file; ++i)
5174 {
5175 s = (*file)[i];
5176 if (STRLEN(s) > l)
5177 {
5178 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005179 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005180 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5181 }
5182 else
5183 vim_free(s);
5184 }
5185 vim_free(*file);
5186 }
5187 }
5188 if (*e != NUL)
5189 ++e;
5190 }
5191 *file = ga.ga_data;
5192 *num_file = ga.ga_len;
5193
5194 vim_free(buf);
5195 vim_free(pat);
5196 if (mustfree)
5197 vim_free(path);
5198 return OK;
5199}
5200
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005203static 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 +00005204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005206 * Call "user_expand_func()" to invoke a user defined Vim script function and
5207 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005209 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005210call_user_expand_func(
5211 void *(*user_expand_func)(char_u *, int, char_u **, int),
5212 expand_T *xp,
5213 int *num_file,
5214 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005216 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005217 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005220 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005221 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
Bram Moolenaarb7515462013-06-29 12:58:33 +02005223 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005224 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 *num_file = 0;
5226 *file = NULL;
5227
Bram Moolenaarb7515462013-06-29 12:58:33 +02005228 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005229 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005230 keep = ccline.cmdbuff[ccline.cmdlen];
5231 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005232 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005233
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005234 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005235 args[1] = xp->xp_line;
5236 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 args[2] = num;
5238
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005239 /* Save the cmdline, we don't know what the function may do. */
5240 save_ccline = ccline;
5241 ccline.cmdbuff = NULL;
5242 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005244
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005245 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005246
5247 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005249 if (ccline.cmdbuff != NULL)
5250 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005251
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005252 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005253 return ret;
5254}
5255
5256/*
5257 * Expand names with a function defined by the user.
5258 */
5259 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260ExpandUserDefined(
5261 expand_T *xp,
5262 regmatch_T *regmatch,
5263 int *num_file,
5264 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005265{
5266 char_u *retstr;
5267 char_u *s;
5268 char_u *e;
5269 char_u keep;
5270 garray_T ga;
5271
5272 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5273 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 return FAIL;
5275
5276 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005277 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279 e = vim_strchr(s, '\n');
5280 if (e == NULL)
5281 e = s + STRLEN(s);
5282 keep = *e;
5283 *e = 0;
5284
5285 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5286 {
5287 *e = keep;
5288 if (*e != NUL)
5289 ++e;
5290 continue;
5291 }
5292
5293 if (ga_grow(&ga, 1) == FAIL)
5294 break;
5295
5296 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5297 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 *e = keep;
5300 if (*e != NUL)
5301 ++e;
5302 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005303 vim_free(retstr);
5304 *file = ga.ga_data;
5305 *num_file = ga.ga_len;
5306 return OK;
5307}
5308
5309/*
5310 * Expand names with a list returned by a function defined by the user.
5311 */
5312 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005313ExpandUserList(
5314 expand_T *xp,
5315 int *num_file,
5316 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005317{
5318 list_T *retlist;
5319 listitem_T *li;
5320 garray_T ga;
5321
5322 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5323 if (retlist == NULL)
5324 return FAIL;
5325
5326 ga_init2(&ga, (int)sizeof(char *), 3);
5327 /* Loop over the items in the list. */
5328 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5329 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005330 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5331 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005332
5333 if (ga_grow(&ga, 1) == FAIL)
5334 break;
5335
5336 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005337 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005338 ++ga.ga_len;
5339 }
5340 list_unref(retlist);
5341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 *file = ga.ga_data;
5343 *num_file = ga.ga_len;
5344 return OK;
5345}
5346#endif
5347
5348/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005349 * Expand color scheme, compiler or filetype names.
5350 * Search from 'runtimepath':
5351 * 'runtimepath'/{dirnames}/{pat}.vim
5352 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5353 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5354 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5355 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005356 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 */
5358 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359ExpandRTDir(
5360 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005361 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362 int *num_file,
5363 char_u ***file,
5364 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 char_u *s;
5367 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005368 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005370 int i;
5371 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 *num_file = 0;
5374 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005375 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005376 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005378 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005380 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5381 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005383 ga_clear_strings(&ga);
5384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005386 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005387 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005388 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005390
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005391 if (flags & DIP_START) {
5392 for (i = 0; dirnames[i] != NULL; ++i)
5393 {
5394 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5395 if (s == NULL)
5396 {
5397 ga_clear_strings(&ga);
5398 return FAIL;
5399 }
5400 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5401 globpath(p_pp, s, &ga, 0);
5402 vim_free(s);
5403 }
5404 }
5405
5406 if (flags & DIP_OPT) {
5407 for (i = 0; dirnames[i] != NULL; ++i)
5408 {
5409 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5410 if (s == NULL)
5411 {
5412 ga_clear_strings(&ga);
5413 return FAIL;
5414 }
5415 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5416 globpath(p_pp, s, &ga, 0);
5417 vim_free(s);
5418 }
5419 }
5420
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005421 for (i = 0; i < ga.ga_len; ++i)
5422 {
5423 match = ((char_u **)ga.ga_data)[i];
5424 s = match;
5425 e = s + STRLEN(s);
5426 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5427 {
5428 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005429 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005430 if (s < match || vim_ispathsep(*s))
5431 break;
5432 ++s;
5433 *e = NUL;
5434 mch_memmove(match, s, e - s + 1);
5435 }
5436 }
5437
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005438 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005439 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005440
5441 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005442 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005443 remove_duplicates(&ga);
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 *file = ga.ga_data;
5446 *num_file = ga.ga_len;
5447 return OK;
5448}
5449
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005450/*
5451 * Expand loadplugin names:
5452 * 'packpath'/pack/ * /opt/{pat}
5453 */
5454 static int
5455ExpandPackAddDir(
5456 char_u *pat,
5457 int *num_file,
5458 char_u ***file)
5459{
5460 char_u *s;
5461 char_u *e;
5462 char_u *match;
5463 garray_T ga;
5464 int i;
5465 int pat_len;
5466
5467 *num_file = 0;
5468 *file = NULL;
5469 pat_len = (int)STRLEN(pat);
5470 ga_init2(&ga, (int)sizeof(char *), 10);
5471
5472 s = alloc((unsigned)(pat_len + 26));
5473 if (s == NULL)
5474 {
5475 ga_clear_strings(&ga);
5476 return FAIL;
5477 }
5478 sprintf((char *)s, "pack/*/opt/%s*", pat);
5479 globpath(p_pp, s, &ga, 0);
5480 vim_free(s);
5481
5482 for (i = 0; i < ga.ga_len; ++i)
5483 {
5484 match = ((char_u **)ga.ga_data)[i];
5485 s = gettail(match);
5486 e = s + STRLEN(s);
5487 mch_memmove(match, s, e - s + 1);
5488 }
5489
5490 if (ga.ga_len == 0)
5491 return FAIL;
5492
5493 /* Sort and remove duplicates which can happen when specifying multiple
5494 * directories in dirnames. */
5495 remove_duplicates(&ga);
5496
5497 *file = ga.ga_data;
5498 *num_file = ga.ga_len;
5499 return OK;
5500}
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#endif
5503
5504#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5505/*
5506 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005507 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510globpath(
5511 char_u *path,
5512 char_u *file,
5513 garray_T *ga,
5514 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 expand_T xpc;
5517 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 int num_p;
5520 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
5522 buf = alloc(MAXPATHL);
5523 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005524 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005526 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005528
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 /* Loop over all entries in {path}. */
5530 while (*path != NUL)
5531 {
5532 /* Copy one item of the path to buf[] and concatenate the file name. */
5533 copy_option_part(&path, buf, MAXPATHL, ",");
5534 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5535 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005536# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005537 /* Using the platform's path separator (\) makes vim incorrectly
5538 * treat it as an escape character, use '/' instead. */
5539 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5540 STRCAT(buf, "/");
5541# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005545 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5546 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005548 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005550 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 for (i = 0; i < num_p; ++i)
5553 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005554 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005555 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005556 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 FreeWild(num_p, p);
5561 }
5562 }
5563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566}
5567
5568#endif
5569
5570#if defined(FEAT_CMDHIST) || defined(PROTO)
5571
5572/*********************************
5573 * Command line history stuff *
5574 *********************************/
5575
5576/*
5577 * Translate a history character to the associated type number.
5578 */
5579 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 if (c == ':')
5583 return HIST_CMD;
5584 if (c == '=')
5585 return HIST_EXPR;
5586 if (c == '@')
5587 return HIST_INPUT;
5588 if (c == '>')
5589 return HIST_DEBUG;
5590 return HIST_SEARCH; /* must be '?' or '/' */
5591}
5592
5593/*
5594 * Table of history names.
5595 * These names are used in :history and various hist...() functions.
5596 * It is sufficient to give the significant prefix of a history name.
5597 */
5598
5599static char *(history_names[]) =
5600{
5601 "cmd",
5602 "search",
5603 "expr",
5604 "input",
5605 "debug",
5606 NULL
5607};
5608
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005609#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5610/*
5611 * Function given to ExpandGeneric() to obtain the possible first
5612 * arguments of the ":history command.
5613 */
5614 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005616{
5617 static char_u compl[2] = { NUL, NUL };
5618 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005619 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005620 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5621
5622 if (idx < short_names_count)
5623 {
5624 compl[0] = (char_u)short_names[idx];
5625 return compl;
5626 }
5627 if (idx < short_names_count + history_name_count)
5628 return (char_u *)history_names[idx - short_names_count];
5629 if (idx == short_names_count + history_name_count)
5630 return (char_u *)"all";
5631 return NULL;
5632}
5633#endif
5634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635/*
5636 * init_history() - Initialize the command line history.
5637 * Also used to re-allocate the history when the size changes.
5638 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005639 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 int newlen; /* new length of history table */
5643 histentry_T *temp;
5644 int i;
5645 int j;
5646 int type;
5647
5648 /*
5649 * If size of history table changed, reallocate it
5650 */
5651 newlen = (int)p_hi;
5652 if (newlen != hislen) /* history length changed */
5653 {
5654 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5655 {
5656 if (newlen)
5657 {
5658 temp = (histentry_T *)lalloc(
5659 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5660 if (temp == NULL) /* out of memory! */
5661 {
5662 if (type == 0) /* first one: just keep the old length */
5663 {
5664 newlen = hislen;
5665 break;
5666 }
5667 /* Already changed one table, now we can only have zero
5668 * length for all tables. */
5669 newlen = 0;
5670 type = -1;
5671 continue;
5672 }
5673 }
5674 else
5675 temp = NULL;
5676 if (newlen == 0 || temp != NULL)
5677 {
5678 if (hisidx[type] < 0) /* there are no entries yet */
5679 {
5680 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005681 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683 else if (newlen > hislen) /* array becomes bigger */
5684 {
5685 for (i = 0; i <= hisidx[type]; ++i)
5686 temp[i] = history[type][i];
5687 j = i;
5688 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005689 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 for ( ; j < hislen; ++i, ++j)
5691 temp[i] = history[type][j];
5692 }
5693 else /* array becomes smaller or 0 */
5694 {
5695 j = hisidx[type];
5696 for (i = newlen - 1; ; --i)
5697 {
5698 if (i >= 0) /* copy newest entries */
5699 temp[i] = history[type][j];
5700 else /* remove older entries */
5701 vim_free(history[type][j].hisstr);
5702 if (--j < 0)
5703 j = hislen - 1;
5704 if (j == hisidx[type])
5705 break;
5706 }
5707 hisidx[type] = newlen - 1;
5708 }
5709 vim_free(history[type]);
5710 history[type] = temp;
5711 }
5712 }
5713 hislen = newlen;
5714 }
5715}
5716
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005719{
5720 hisptr->hisnum = 0;
5721 hisptr->viminfo = FALSE;
5722 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005723 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005724}
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726/*
5727 * Check if command line 'str' is already in history.
5728 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5729 */
5730 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005731in_history(
5732 int type,
5733 char_u *str,
5734 int move_to_front, /* Move the entry to the front if it exists */
5735 int sep,
5736 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 int i;
5739 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005740 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 if (hisidx[type] < 0)
5743 return FALSE;
5744 i = hisidx[type];
5745 do
5746 {
5747 if (history[type][i].hisstr == NULL)
5748 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005749
5750 /* For search history, check that the separator character matches as
5751 * well. */
5752 p = history[type][i].hisstr;
5753 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005754 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005755 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
5757 if (!move_to_front)
5758 return TRUE;
5759 last_i = i;
5760 break;
5761 }
5762 if (--i < 0)
5763 i = hislen - 1;
5764 } while (i != hisidx[type]);
5765
5766 if (last_i >= 0)
5767 {
5768 str = history[type][i].hisstr;
5769 while (i != hisidx[type])
5770 {
5771 if (++i >= hislen)
5772 i = 0;
5773 history[type][last_i] = history[type][i];
5774 last_i = i;
5775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005777 history[type][i].viminfo = FALSE;
5778 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005779 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 return TRUE;
5781 }
5782 return FALSE;
5783}
5784
5785/*
5786 * Convert history name (from table above) to its HIST_ equivalent.
5787 * When "name" is empty, return "cmd" history.
5788 * Returns -1 for unknown history name.
5789 */
5790 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005791get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 int i;
5794 int len = (int)STRLEN(name);
5795
5796 /* No argument: use current history. */
5797 if (len == 0)
5798 return hist_char2type(ccline.cmdfirstc);
5799
5800 for (i = 0; history_names[i] != NULL; ++i)
5801 if (STRNICMP(name, history_names[i], len) == 0)
5802 return i;
5803
5804 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5805 return hist_char2type(name[0]);
5806
5807 return -1;
5808}
5809
5810static int last_maptick = -1; /* last seen maptick */
5811
5812/*
5813 * Add the given string to the given history. If the string is already in the
5814 * history then it is moved to the front. "histype" may be one of he HIST_
5815 * values.
5816 */
5817 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005818add_to_history(
5819 int histype,
5820 char_u *new_entry,
5821 int in_map, /* consider maptick when inside a mapping */
5822 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 histentry_T *hisptr;
5825 int len;
5826
5827 if (hislen == 0) /* no history */
5828 return;
5829
Bram Moolenaara939e432013-11-09 05:30:26 +01005830 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5831 return;
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 /*
5834 * Searches inside the same mapping overwrite each other, so that only
5835 * the last line is kept. Be careful not to remove a line that was moved
5836 * down, only lines that were added.
5837 */
5838 if (histype == HIST_SEARCH && in_map)
5839 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005840 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
5842 /* Current line is from the same mapping, remove it */
5843 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5844 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005845 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 --hisnum[histype];
5847 if (--hisidx[HIST_SEARCH] < 0)
5848 hisidx[HIST_SEARCH] = hislen - 1;
5849 }
5850 last_maptick = -1;
5851 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005852 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
5854 if (++hisidx[histype] == hislen)
5855 hisidx[histype] = 0;
5856 hisptr = &history[histype][hisidx[histype]];
5857 vim_free(hisptr->hisstr);
5858
5859 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005860 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5862 if (hisptr->hisstr != NULL)
5863 hisptr->hisstr[len + 1] = sep;
5864
5865 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005866 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005867 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 if (histype == HIST_SEARCH && in_map)
5869 last_maptick = maptick;
5870 }
5871}
5872
5873#if defined(FEAT_EVAL) || defined(PROTO)
5874
5875/*
5876 * Get identifier of newest history entry.
5877 * "histype" may be one of the HIST_ values.
5878 */
5879 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005880get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5883 || hisidx[histype] < 0)
5884 return -1;
5885
5886 return history[histype][hisidx[histype]].hisnum;
5887}
5888
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 * Calculate history index from a number:
5891 * num > 0: seen as identifying number of a history entry
5892 * num < 0: relative position in history wrt newest entry
5893 * "histype" may be one of the HIST_ values.
5894 */
5895 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005896calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 int i;
5899 histentry_T *hist;
5900 int wrapped = FALSE;
5901
5902 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5903 || (i = hisidx[histype]) < 0 || num == 0)
5904 return -1;
5905
5906 hist = history[histype];
5907 if (num > 0)
5908 {
5909 while (hist[i].hisnum > num)
5910 if (--i < 0)
5911 {
5912 if (wrapped)
5913 break;
5914 i += hislen;
5915 wrapped = TRUE;
5916 }
5917 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5918 return i;
5919 }
5920 else if (-num <= hislen)
5921 {
5922 i += num + 1;
5923 if (i < 0)
5924 i += hislen;
5925 if (hist[i].hisstr != NULL)
5926 return i;
5927 }
5928 return -1;
5929}
5930
5931/*
5932 * Get a history entry by its index.
5933 * "histype" may be one of the HIST_ values.
5934 */
5935 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005936get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 idx = calc_hist_idx(histype, idx);
5939 if (idx >= 0)
5940 return history[histype][idx].hisstr;
5941 else
5942 return (char_u *)"";
5943}
5944
5945/*
5946 * Clear all entries of a history.
5947 * "histype" may be one of the HIST_ values.
5948 */
5949 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005950clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952 int i;
5953 histentry_T *hisptr;
5954
5955 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5956 {
5957 hisptr = history[histype];
5958 for (i = hislen; i--;)
5959 {
5960 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005961 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005962 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 }
5964 hisidx[histype] = -1; /* mark history as cleared */
5965 hisnum[histype] = 0; /* reset identifier counter */
5966 return OK;
5967 }
5968 return FAIL;
5969}
5970
5971/*
5972 * Remove all entries matching {str} from a history.
5973 * "histype" may be one of the HIST_ values.
5974 */
5975 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005976del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 regmatch_T regmatch;
5979 histentry_T *hisptr;
5980 int idx;
5981 int i;
5982 int last;
5983 int found = FALSE;
5984
5985 regmatch.regprog = NULL;
5986 regmatch.rm_ic = FALSE; /* always match case */
5987 if (hislen != 0
5988 && histype >= 0
5989 && histype < HIST_COUNT
5990 && *str != NUL
5991 && (idx = hisidx[histype]) >= 0
5992 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5993 != NULL)
5994 {
5995 i = last = idx;
5996 do
5997 {
5998 hisptr = &history[histype][i];
5999 if (hisptr->hisstr == NULL)
6000 break;
6001 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6002 {
6003 found = TRUE;
6004 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006005 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
6007 else
6008 {
6009 if (i != last)
6010 {
6011 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006012 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 }
6014 if (--last < 0)
6015 last += hislen;
6016 }
6017 if (--i < 0)
6018 i += hislen;
6019 } while (i != idx);
6020 if (history[histype][idx].hisstr == NULL)
6021 hisidx[histype] = -1;
6022 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006023 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 return found;
6025}
6026
6027/*
6028 * Remove an indexed entry from a history.
6029 * "histype" may be one of the HIST_ values.
6030 */
6031 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006032del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 int i, j;
6035
6036 i = calc_hist_idx(histype, idx);
6037 if (i < 0)
6038 return FALSE;
6039 idx = hisidx[histype];
6040 vim_free(history[histype][i].hisstr);
6041
6042 /* When deleting the last added search string in a mapping, reset
6043 * last_maptick, so that the last added search string isn't deleted again.
6044 */
6045 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6046 last_maptick = -1;
6047
6048 while (i != idx)
6049 {
6050 j = (i + 1) % hislen;
6051 history[histype][i] = history[histype][j];
6052 i = j;
6053 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006054 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (--i < 0)
6056 i += hislen;
6057 hisidx[histype] = i;
6058 return TRUE;
6059}
6060
6061#endif /* FEAT_EVAL */
6062
6063#if defined(FEAT_CRYPT) || defined(PROTO)
6064/*
6065 * Very specific function to remove the value in ":set key=val" from the
6066 * history.
6067 */
6068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006069remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 char_u *p;
6072 int i;
6073
6074 i = hisidx[HIST_CMD];
6075 if (i < 0)
6076 return;
6077 p = history[HIST_CMD][i].hisstr;
6078 if (p != NULL)
6079 for ( ; *p; ++p)
6080 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6081 {
6082 p = vim_strchr(p + 3, '=');
6083 if (p == NULL)
6084 break;
6085 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006086 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 if (p[i] == '\\' && p[i + 1])
6088 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006089 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 --p;
6091 }
6092}
6093#endif
6094
6095#endif /* FEAT_CMDHIST */
6096
Bram Moolenaar064154c2016-03-19 22:50:43 +01006097#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006098/*
6099 * Get pointer to the command line info to use. cmdline_paste() may clear
6100 * ccline and put the previous value in prev_ccline.
6101 */
6102 static struct cmdline_info *
6103get_ccline_ptr(void)
6104{
6105 if ((State & CMDLINE) == 0)
6106 return NULL;
6107 if (ccline.cmdbuff != NULL)
6108 return &ccline;
6109 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6110 return &prev_ccline;
6111 return NULL;
6112}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006113#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006114
Bram Moolenaar064154c2016-03-19 22:50:43 +01006115#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006116/*
6117 * Get the current command line in allocated memory.
6118 * Only works when the command line is being edited.
6119 * Returns NULL when something is wrong.
6120 */
6121 char_u *
6122get_cmdline_str(void)
6123{
6124 struct cmdline_info *p = get_ccline_ptr();
6125
6126 if (p == NULL)
6127 return NULL;
6128 return vim_strnsave(p->cmdbuff, p->cmdlen);
6129}
6130
6131/*
6132 * Get the current command line position, counted in bytes.
6133 * Zero is the first position.
6134 * Only works when the command line is being edited.
6135 * Returns -1 when something is wrong.
6136 */
6137 int
6138get_cmdline_pos(void)
6139{
6140 struct cmdline_info *p = get_ccline_ptr();
6141
6142 if (p == NULL)
6143 return -1;
6144 return p->cmdpos;
6145}
6146
6147/*
6148 * Set the command line byte position to "pos". Zero is the first position.
6149 * Only works when the command line is being edited.
6150 * Returns 1 when failed, 0 when OK.
6151 */
6152 int
6153set_cmdline_pos(
6154 int pos)
6155{
6156 struct cmdline_info *p = get_ccline_ptr();
6157
6158 if (p == NULL)
6159 return 1;
6160
6161 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6162 * changed the command line. */
6163 if (pos < 0)
6164 new_cmdpos = 0;
6165 else
6166 new_cmdpos = pos;
6167 return 0;
6168}
6169#endif
6170
6171#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6172/*
6173 * Get the current command-line type.
6174 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6175 * Only works when the command line is being edited.
6176 * Returns NUL when something is wrong.
6177 */
6178 int
6179get_cmdline_type(void)
6180{
6181 struct cmdline_info *p = get_ccline_ptr();
6182
6183 if (p == NULL)
6184 return NUL;
6185 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006186 return
6187# ifdef FEAT_EVAL
6188 (p->input_fn) ? '@' :
6189# endif
6190 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006191 return p->cmdfirstc;
6192}
6193#endif
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6196/*
6197 * Get indices "num1,num2" that specify a range within a list (not a range of
6198 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6199 * Returns OK if parsed successfully, otherwise FAIL.
6200 */
6201 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006202get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 int len;
6205 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006206 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
6208 *str = skipwhite(*str);
6209 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6210 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006211 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 *str += len;
6213 *num1 = (int)num;
6214 first = TRUE;
6215 }
6216 *str = skipwhite(*str);
6217 if (**str == ',') /* parse "to" part of range */
6218 {
6219 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006220 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (len > 0)
6222 {
6223 *num2 = (int)num;
6224 *str = skipwhite(*str + len);
6225 }
6226 else if (!first) /* no number given at all */
6227 return FAIL;
6228 }
6229 else if (first) /* only one number given */
6230 *num2 = *num1;
6231 return OK;
6232}
6233#endif
6234
6235#if defined(FEAT_CMDHIST) || defined(PROTO)
6236/*
6237 * :history command - print a history
6238 */
6239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006240ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 histentry_T *hist;
6243 int histype1 = HIST_CMD;
6244 int histype2 = HIST_CMD;
6245 int hisidx1 = 1;
6246 int hisidx2 = -1;
6247 int idx;
6248 int i, j, k;
6249 char_u *end;
6250 char_u *arg = eap->arg;
6251
6252 if (hislen == 0)
6253 {
6254 MSG(_("'history' option is zero"));
6255 return;
6256 }
6257
6258 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6259 {
6260 end = arg;
6261 while (ASCII_ISALPHA(*end)
6262 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6263 end++;
6264 i = *end;
6265 *end = NUL;
6266 histype1 = get_histtype(arg);
6267 if (histype1 == -1)
6268 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006269 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 histype1 = 0;
6272 histype2 = HIST_COUNT-1;
6273 }
6274 else
6275 {
6276 *end = i;
6277 EMSG(_(e_trailing));
6278 return;
6279 }
6280 }
6281 else
6282 histype2 = histype1;
6283 *end = i;
6284 }
6285 else
6286 end = arg;
6287 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6288 {
6289 EMSG(_(e_trailing));
6290 return;
6291 }
6292
6293 for (; !got_int && histype1 <= histype2; ++histype1)
6294 {
6295 STRCPY(IObuff, "\n # ");
6296 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6297 MSG_PUTS_TITLE(IObuff);
6298 idx = hisidx[histype1];
6299 hist = history[histype1];
6300 j = hisidx1;
6301 k = hisidx2;
6302 if (j < 0)
6303 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6304 if (k < 0)
6305 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6306 if (idx >= 0 && j <= k)
6307 for (i = idx + 1; !got_int; ++i)
6308 {
6309 if (i == hislen)
6310 i = 0;
6311 if (hist[i].hisstr != NULL
6312 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6313 {
6314 msg_putchar('\n');
6315 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6316 hist[i].hisnum);
6317 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6318 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006319 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 else
6321 STRCAT(IObuff, hist[i].hisstr);
6322 msg_outtrans(IObuff);
6323 out_flush();
6324 }
6325 if (i == idx)
6326 break;
6327 }
6328 }
6329}
6330#endif
6331
6332#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006333/*
6334 * Buffers for history read from a viminfo file. Only valid while reading.
6335 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006336static histentry_T *viminfo_history[HIST_COUNT] =
6337 {NULL, NULL, NULL, NULL, NULL};
6338static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6339static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340static int viminfo_add_at_front = FALSE;
6341
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006342static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344/*
6345 * Translate a history type number to the associated character.
6346 */
6347 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006348hist_type2char(
6349 int type,
6350 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 if (type == HIST_CMD)
6353 return ':';
6354 if (type == HIST_SEARCH)
6355 {
6356 if (use_question)
6357 return '?';
6358 else
6359 return '/';
6360 }
6361 if (type == HIST_EXPR)
6362 return '=';
6363 return '@';
6364}
6365
6366/*
6367 * Prepare for reading the history from the viminfo file.
6368 * This allocates history arrays to store the read history lines.
6369 */
6370 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006371prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 int i;
6374 int num;
6375 int type;
6376 int len;
6377
6378 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006379 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (asklen > hislen)
6381 asklen = hislen;
6382
6383 for (type = 0; type < HIST_COUNT; ++type)
6384 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006385 /* Count the number of empty spaces in the history list. Entries read
6386 * from viminfo previously are also considered empty. If there are
6387 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006389 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 num++;
6391 len = asklen;
6392 if (num > len)
6393 len = num;
6394 if (len <= 0)
6395 viminfo_history[type] = NULL;
6396 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006397 viminfo_history[type] = (histentry_T *)lalloc(
6398 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (viminfo_history[type] == NULL)
6400 len = 0;
6401 viminfo_hislen[type] = len;
6402 viminfo_hisidx[type] = 0;
6403 }
6404}
6405
6406/*
6407 * Accept a line from the viminfo, store it in the history array when it's
6408 * new.
6409 */
6410 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006411read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 int type;
6414 long_u len;
6415 char_u *val;
6416 char_u *p;
6417
6418 type = hist_char2type(virp->vir_line[0]);
6419 if (viminfo_hisidx[type] < viminfo_hislen[type])
6420 {
6421 val = viminfo_readstring(virp, 1, TRUE);
6422 if (val != NULL && *val != NUL)
6423 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006424 int sep = (*val == ' ' ? NUL : *val);
6425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006427 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
6429 /* Need to re-allocate to append the separator byte. */
6430 len = STRLEN(val);
6431 p = lalloc(len + 2, TRUE);
6432 if (p != NULL)
6433 {
6434 if (type == HIST_SEARCH)
6435 {
6436 /* Search entry: Move the separator from the first
6437 * column to after the NUL. */
6438 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006439 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
6441 else
6442 {
6443 /* Not a search entry: No separator in the viminfo
6444 * file, add a NUL separator. */
6445 mch_memmove(p, val, (size_t)len + 1);
6446 p[len + 1] = NUL;
6447 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006448 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6449 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006450 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6451 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006452 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 }
6454 }
6455 }
6456 vim_free(val);
6457 }
6458 return viminfo_readline(virp);
6459}
6460
Bram Moolenaar07219f92013-04-14 23:19:36 +02006461/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006462 * Accept a new style history line from the viminfo, store it in the history
6463 * array when it's new.
6464 */
6465 void
6466handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006467 garray_T *values,
6468 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006469{
6470 int type;
6471 long_u len;
6472 char_u *val;
6473 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006474 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006475
6476 /* Check the format:
6477 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006478 if (values->ga_len < 4
6479 || vp[0].bv_type != BVAL_NR
6480 || vp[1].bv_type != BVAL_NR
6481 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6482 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006483 return;
6484
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006485 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006486 if (type >= HIST_COUNT)
6487 return;
6488 if (viminfo_hisidx[type] < viminfo_hislen[type])
6489 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006490 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006491 if (val != NULL && *val != NUL)
6492 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006493 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6494 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006495 int idx;
6496 int overwrite = FALSE;
6497
6498 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6499 {
6500 /* If lines were written by an older Vim we need to avoid
6501 * getting duplicates. See if the entry already exists. */
6502 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6503 {
6504 p = viminfo_history[type][idx].hisstr;
6505 if (STRCMP(val, p) == 0
6506 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6507 {
6508 overwrite = TRUE;
6509 break;
6510 }
6511 }
6512
6513 if (!overwrite)
6514 {
6515 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006516 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006517 p = lalloc(len + 2, TRUE);
6518 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006519 else
6520 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006521 if (p != NULL)
6522 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006523 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006524 if (!overwrite)
6525 {
6526 mch_memmove(p, val, (size_t)len + 1);
6527 /* Put the separator after the NUL. */
6528 p[len + 1] = sep;
6529 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006530 viminfo_history[type][idx].hisnum = 0;
6531 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006532 viminfo_hisidx[type]++;
6533 }
6534 }
6535 }
6536 }
6537 }
6538}
6539
6540/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006541 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006542 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006543 static void
6544concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 int idx;
6547 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006548
6549 idx = hisidx[type] + viminfo_hisidx[type];
6550 if (idx >= hislen)
6551 idx -= hislen;
6552 else if (idx < 0)
6553 idx = hislen - 1;
6554 if (viminfo_add_at_front)
6555 hisidx[type] = idx;
6556 else
6557 {
6558 if (hisidx[type] == -1)
6559 hisidx[type] = hislen - 1;
6560 do
6561 {
6562 if (history[type][idx].hisstr != NULL
6563 || history[type][idx].viminfo)
6564 break;
6565 if (++idx == hislen)
6566 idx = 0;
6567 } while (idx != hisidx[type]);
6568 if (idx != hisidx[type] && --idx < 0)
6569 idx = hislen - 1;
6570 }
6571 for (i = 0; i < viminfo_hisidx[type]; i++)
6572 {
6573 vim_free(history[type][idx].hisstr);
6574 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6575 history[type][idx].viminfo = TRUE;
6576 history[type][idx].time_set = viminfo_history[type][i].time_set;
6577 if (--idx < 0)
6578 idx = hislen - 1;
6579 }
6580 idx += 1;
6581 idx %= hislen;
6582 for (i = 0; i < viminfo_hisidx[type]; i++)
6583 {
6584 history[type][idx++].hisnum = ++hisnum[type];
6585 idx %= hislen;
6586 }
6587}
6588
6589#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6590 static int
6591#ifdef __BORLANDC__
6592_RTLENTRYF
6593#endif
6594sort_hist(const void *s1, const void *s2)
6595{
6596 histentry_T *p1 = *(histentry_T **)s1;
6597 histentry_T *p2 = *(histentry_T **)s2;
6598
6599 if (p1->time_set < p2->time_set) return -1;
6600 if (p1->time_set > p2->time_set) return 1;
6601 return 0;
6602}
6603#endif
6604
6605/*
6606 * Merge history lines from viminfo and lines typed in this Vim based on the
6607 * timestamp;
6608 */
6609 static void
6610merge_history(int type)
6611{
6612 int max_len;
6613 histentry_T **tot_hist;
6614 histentry_T *new_hist;
6615 int i;
6616 int len;
6617
6618 /* Make one long list with all entries. */
6619 max_len = hislen + viminfo_hisidx[type];
6620 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6621 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6622 if (tot_hist == NULL || new_hist == NULL)
6623 {
6624 vim_free(tot_hist);
6625 vim_free(new_hist);
6626 return;
6627 }
6628 for (i = 0; i < viminfo_hisidx[type]; i++)
6629 tot_hist[i] = &viminfo_history[type][i];
6630 len = i;
6631 for (i = 0; i < hislen; i++)
6632 if (history[type][i].hisstr != NULL)
6633 tot_hist[len++] = &history[type][i];
6634
6635 /* Sort the list on timestamp. */
6636 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6637
6638 /* Keep the newest ones. */
6639 for (i = 0; i < hislen; i++)
6640 {
6641 if (i < len)
6642 {
6643 new_hist[i] = *tot_hist[i];
6644 tot_hist[i]->hisstr = NULL;
6645 if (new_hist[i].hisnum == 0)
6646 new_hist[i].hisnum = ++hisnum[type];
6647 }
6648 else
6649 clear_hist_entry(&new_hist[i]);
6650 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006651 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006652
6653 /* Free what is not kept. */
6654 for (i = 0; i < viminfo_hisidx[type]; i++)
6655 vim_free(viminfo_history[type][i].hisstr);
6656 for (i = 0; i < hislen; i++)
6657 vim_free(history[type][i].hisstr);
6658 vim_free(history[type]);
6659 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006660 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006661}
6662
6663/*
6664 * Finish reading history lines from viminfo. Not used when writing viminfo.
6665 */
6666 void
6667finish_viminfo_history(vir_T *virp)
6668{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006670 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 for (type = 0; type < HIST_COUNT; ++type)
6673 {
6674 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006675 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006676
6677 if (merge)
6678 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006680 concat_history(type);
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 vim_free(viminfo_history[type]);
6683 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006684 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 }
6686}
6687
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006688/*
6689 * Write history to viminfo file in "fp".
6690 * When "merge" is TRUE merge history lines with a previously read viminfo
6691 * file, data is in viminfo_history[].
6692 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006695write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 int i;
6698 int type;
6699 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006700 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702 init_history();
6703 if (hislen == 0)
6704 return;
6705 for (type = 0; type < HIST_COUNT; ++type)
6706 {
6707 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6708 if (num_saved == 0)
6709 continue;
6710 if (num_saved < 0) /* Use default */
6711 num_saved = hislen;
6712 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6713 type == HIST_CMD ? _("Command Line") :
6714 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006715 type == HIST_EXPR ? _("Expression") :
6716 type == HIST_INPUT ? _("Input Line") :
6717 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (num_saved > hislen)
6719 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006720
6721 /*
6722 * Merge typed and viminfo history:
6723 * round 1: history of typed commands.
6724 * round 2: history from recently read viminfo.
6725 */
6726 for (round = 1; round <= 2; ++round)
6727 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006728 if (round == 1)
6729 /* start at newest entry, somewhere in the list */
6730 i = hisidx[type];
6731 else if (viminfo_hisidx[type] > 0)
6732 /* start at newest entry, first in the list */
6733 i = 0;
6734 else
6735 /* empty list */
6736 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006737 if (i >= 0)
6738 while (num_saved > 0
6739 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006741 char_u *p;
6742 time_t timestamp;
6743 int c = NUL;
6744
6745 if (round == 1)
6746 {
6747 p = history[type][i].hisstr;
6748 timestamp = history[type][i].time_set;
6749 }
6750 else
6751 {
6752 p = viminfo_history[type] == NULL ? NULL
6753 : viminfo_history[type][i].hisstr;
6754 timestamp = viminfo_history[type] == NULL ? 0
6755 : viminfo_history[type][i].time_set;
6756 }
6757
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006758 if (p != NULL && (round == 2
6759 || !merge
6760 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006762 --num_saved;
6763 fputc(hist_type2char(type, TRUE), fp);
6764 /* For the search history: put the separator in the
6765 * second column; use a space if there isn't one. */
6766 if (type == HIST_SEARCH)
6767 {
6768 c = p[STRLEN(p) + 1];
6769 putc(c == NUL ? ' ' : c, fp);
6770 }
6771 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006772
6773 {
6774 char cbuf[NUMBUFLEN];
6775
6776 /* New style history with a bar line. Format:
6777 * |{bartype},{histtype},{timestamp},{separator},"text" */
6778 if (c == NUL)
6779 cbuf[0] = NUL;
6780 else
6781 sprintf(cbuf, "%d", c);
6782 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6783 type, (long)timestamp, cbuf);
6784 barline_writestring(fp, p, LSIZE - 20);
6785 putc('\n', fp);
6786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006788 if (round == 1)
6789 {
6790 /* Decrement index, loop around and stop when back at
6791 * the start. */
6792 if (--i < 0)
6793 i = hislen - 1;
6794 if (i == hisidx[type])
6795 break;
6796 }
6797 else
6798 {
6799 /* Increment index. Stop at the end in the while. */
6800 ++i;
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006803 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006804 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006805 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006806 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006807 vim_free(viminfo_history[type]);
6808 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006809 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 }
6811}
6812#endif /* FEAT_VIMINFO */
6813
6814#if defined(FEAT_FKMAP) || defined(PROTO)
6815/*
6816 * Write a character at the current cursor+offset position.
6817 * It is directly written into the command buffer block.
6818 */
6819 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006820cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6823 {
6824 EMSG(_("E198: cmd_pchar beyond the command length"));
6825 return;
6826 }
6827 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6828 ccline.cmdbuff[ccline.cmdlen] = NUL;
6829}
6830
6831 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006832cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
6834 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6835 {
6836 /* EMSG(_("cmd_gchar beyond the command length")); */
6837 return NUL;
6838 }
6839 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6840}
6841#endif
6842
6843#if defined(FEAT_CMDWIN) || defined(PROTO)
6844/*
6845 * Open a window on the current command line and history. Allow editing in
6846 * the window. Returns when the window is closed.
6847 * Returns:
6848 * CR if the command is to be executed
6849 * Ctrl_C if it is to be abandoned
6850 * K_IGNORE if editing continues
6851 */
6852 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006853open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006856 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006858 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 win_T *wp;
6860 int i;
6861 linenr_T lnum;
6862 int histtype;
6863 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 int save_restart_edit = restart_edit;
6865 int save_State = State;
6866 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006867#ifdef FEAT_RIGHTLEFT
6868 int save_cmdmsg_rl = cmdmsg_rl;
6869#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006870#ifdef FEAT_FOLDING
6871 int save_KeyTyped;
6872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874 /* Can't do this recursively. Can't do it when typing a password. */
6875 if (cmdwin_type != 0
6876# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6877 || cmdline_star > 0
6878# endif
6879 )
6880 {
6881 beep_flush();
6882 return K_IGNORE;
6883 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006884 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
6886 /* Save current window sizes. */
6887 win_size_save(&winsizes);
6888
6889# ifdef FEAT_AUTOCMD
6890 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006891 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006893 /* don't use a new tab page */
6894 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006895 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006896
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 /* Create a window for the command-line buffer. */
6898 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6899 {
6900 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006901# ifdef FEAT_AUTOCMD
6902 unblock_autocmds();
6903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 return K_IGNORE;
6905 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006906 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006909 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006910 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006913#ifdef FEAT_FOLDING
6914 curwin->w_p_fen = FALSE;
6915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006917 curwin->w_p_rl = cmdmsg_rl;
6918 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006920 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922# ifdef FEAT_AUTOCMD
6923 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006924 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006925 /* But don't allow switching to another buffer. */
6926 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927# endif
6928
Bram Moolenaar46152342005-09-07 21:18:43 +00006929 /* Showing the prompt may have set need_wait_return, reset it. */
6930 need_wait_return = FALSE;
6931
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006932 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6934 {
6935 if (p_wc == TAB)
6936 {
6937 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6938 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6939 }
6940 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6941 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006942# ifdef FEAT_AUTOCMD
6943 --curbuf_lock;
6944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006946 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6947 * sets 'textwidth' to 78). */
6948 curbuf->b_p_tw = 0;
6949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 /* Fill the buffer with the history. */
6951 init_history();
6952 if (hislen > 0)
6953 {
6954 i = hisidx[histtype];
6955 if (i >= 0)
6956 {
6957 lnum = 0;
6958 do
6959 {
6960 if (++i == hislen)
6961 i = 0;
6962 if (history[histtype][i].hisstr != NULL)
6963 ml_append(lnum++, history[histtype][i].hisstr,
6964 (colnr_T)0, FALSE);
6965 }
6966 while (i != hisidx[histtype]);
6967 }
6968 }
6969
6970 /* Replace the empty last line with the current command-line and put the
6971 * cursor there. */
6972 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6973 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6974 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006975 changed_line_abv_curs();
6976 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006977 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
6979 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006980 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 /* No Ex mode here! */
6983 exmode_active = 0;
6984
6985 State = NORMAL;
6986# ifdef FEAT_MOUSE
6987 setmouse();
6988# endif
6989
6990# ifdef FEAT_AUTOCMD
6991 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02006992 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006993 if (restart_edit != 0) /* autocmd with ":startinsert" */
6994 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995# endif
6996
6997 i = RedrawingDisabled;
6998 RedrawingDisabled = 0;
6999
7000 /*
7001 * Call the main loop until <CR> or CTRL-C is typed.
7002 */
7003 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007004 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 RedrawingDisabled = i;
7007
7008# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007009
7010# ifdef FEAT_FOLDING
7011 save_KeyTyped = KeyTyped;
7012# endif
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007015 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007016
7017# ifdef FEAT_FOLDING
7018 /* Restore KeyTyped in case it is modified by autocommands */
7019 KeyTyped = save_KeyTyped;
7020# endif
7021
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022# endif
7023
Bram Moolenaarccc18222007-05-10 18:25:20 +00007024 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007025 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 cmdwin_type = 0;
7027
7028 exmode_active = save_exmode;
7029
Bram Moolenaarf9821062008-06-20 16:31:07 +00007030 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007032 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
7034 cmdwin_result = Ctrl_C;
7035 EMSG(_("E199: Active window or buffer deleted"));
7036 }
7037 else
7038 {
7039# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7040 /* autocmds may abort script processing */
7041 if (aborting() && cmdwin_result != K_IGNORE)
7042 cmdwin_result = Ctrl_C;
7043# endif
7044 /* Set the new command line from the cmdline buffer. */
7045 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007046 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007048 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7049
7050 if (histtype == HIST_CMD)
7051 {
7052 /* Execute the command directly. */
7053 ccline.cmdbuff = vim_strsave((char_u *)p);
7054 cmdwin_result = CAR;
7055 }
7056 else
7057 {
7058 /* First need to cancel what we were doing. */
7059 ccline.cmdbuff = NULL;
7060 stuffcharReadbuff(':');
7061 stuffReadbuff((char_u *)p);
7062 stuffcharReadbuff(CAR);
7063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065 else if (cmdwin_result == K_XF2) /* :qa typed */
7066 {
7067 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7068 cmdwin_result = CAR;
7069 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007070 else if (cmdwin_result == Ctrl_C)
7071 {
7072 /* :q or :close, don't execute any command
7073 * and don't modify the cmd window. */
7074 ccline.cmdbuff = NULL;
7075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 else
7077 ccline.cmdbuff = vim_strsave(ml_get_curline());
7078 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007079 {
7080 ccline.cmdbuff = vim_strsave((char_u *)"");
7081 ccline.cmdlen = 0;
7082 ccline.cmdbufflen = 1;
7083 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 else
7087 {
7088 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7089 ccline.cmdbufflen = ccline.cmdlen + 1;
7090 ccline.cmdpos = curwin->w_cursor.col;
7091 if (ccline.cmdpos > ccline.cmdlen)
7092 ccline.cmdpos = ccline.cmdlen;
7093 if (cmdwin_result == K_IGNORE)
7094 {
7095 set_cmdspos_cursor();
7096 redrawcmd();
7097 }
7098 }
7099
7100# ifdef FEAT_AUTOCMD
7101 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007102 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007104# ifdef FEAT_CONCEAL
7105 /* Avoid command-line window first character being concealed. */
7106 curwin->w_p_cole = 0;
7107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007109 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 win_goto(old_curwin);
7111 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007112
7113 /* win_close() may have already wiped the buffer when 'bh' is
7114 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007115 if (bufref_valid(&bufref))
7116 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118 /* Restore window sizes. */
7119 win_size_restore(&winsizes);
7120
7121# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007122 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123# endif
7124 }
7125
7126 ga_clear(&winsizes);
7127 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007128# ifdef FEAT_RIGHTLEFT
7129 cmdmsg_rl = save_cmdmsg_rl;
7130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
7132 State = save_State;
7133# ifdef FEAT_MOUSE
7134 setmouse();
7135# endif
7136
7137 return cmdwin_result;
7138}
7139#endif /* FEAT_CMDWIN */
7140
7141/*
7142 * Used for commands that either take a simple command string argument, or:
7143 * cmd << endmarker
7144 * {script}
7145 * endmarker
7146 * Returns a pointer to allocated memory with {script} or NULL.
7147 */
7148 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007149script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 char_u *theline;
7152 char *end_pattern = NULL;
7153 char dot[] = ".";
7154 garray_T ga;
7155
7156 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7157 return NULL;
7158
7159 ga_init2(&ga, 1, 0x400);
7160
7161 if (cmd[2] != NUL)
7162 end_pattern = (char *)skipwhite(cmd + 2);
7163 else
7164 end_pattern = dot;
7165
7166 for (;;)
7167 {
7168 theline = eap->getline(
7169#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007170 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171#endif
7172 NUL, eap->cookie, 0);
7173
7174 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007175 {
7176 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
7180 ga_concat(&ga, theline);
7181 ga_append(&ga, '\n');
7182 vim_free(theline);
7183 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007184 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
7186 return (char_u *)ga.ga_data;
7187}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007188
7189#ifdef FEAT_SEARCH_EXTRA
7190 static void
7191set_search_match(pos_T *t)
7192{
7193 /*
7194 * First move cursor to end of match, then to the start. This
7195 * moves the whole match onto the screen when 'nowrap' is set.
7196 */
7197 t->lnum += search_match_lines;
7198 t->col = search_match_endcol;
7199 if (t->lnum > curbuf->b_ml.ml_line_count)
7200 {
7201 t->lnum = curbuf->b_ml.ml_line_count;
7202 coladvance((colnr_T)MAXCOL);
7203 }
7204}
7205#endif