blob: d0f2bfe1d6f1374a6924ee85bfc619baa4a544e0 [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 Moolenaar071d4272004-06-13 20:20:40 +0000148/*
149 * getcmdline() - accept a command line starting with firstc.
150 *
151 * firstc == ':' get ":" command line.
152 * firstc == '/' or '?' get search pattern
153 * firstc == '=' get expression
154 * firstc == '@' get text for input() function
155 * firstc == '>' get text for debug mode
156 * firstc == NUL get text for :insert command
157 * firstc == -1 like NUL, and break on CTRL-C
158 *
159 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
160 * command line.
161 *
162 * Careful: getcmdline() can be called recursively!
163 *
164 * Return pointer to allocated string if there is a commandline, NULL
165 * otherwise.
166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100168getcmdline(
169 int firstc,
170 long count UNUSED, /* only used for incremental search */
171 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172{
173 int c;
174 int i;
175 int j;
176 int gotesc = FALSE; /* TRUE when <ESC> just typed */
177 int do_abbr; /* when TRUE check for abbr. */
178#ifdef FEAT_CMDHIST
179 char_u *lookfor = NULL; /* string to match */
180 int hiscnt; /* current history line in use */
181 int histype; /* history type to be used */
182#endif
183#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200184 pos_T search_start; /* where 'incsearch' starts searching */
185 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200187 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200189 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200191 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200192 pos_T match_start = curwin->w_cursor;
193 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# ifdef FEAT_DIFF
195 int old_topfill;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200196 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# endif
198 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200199 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 int did_incsearch = FALSE;
201 int incsearch_postponed = FALSE;
202#endif
203 int did_wild_list = FALSE; /* did wild_list() recently */
204 int wim_index = 0; /* index in wim_flags[] */
205 int res;
206 int save_msg_scroll = msg_scroll;
207 int save_State = State; /* remember State when called */
208 int some_key_typed = FALSE; /* one of the keys was typed */
209#ifdef FEAT_MOUSE
210 /* mouse drag and release events are ignored, unless they are
211 * preceded with a mouse down event */
212 int ignore_drag_release = TRUE;
213#endif
214#ifdef FEAT_EVAL
215 int break_ctrl_c = FALSE;
216#endif
217 expand_T xpc;
218 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100219#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000220 /* Everything that may work recursively should save and restore the
221 * current command line in save_ccline. That includes update_screen(), a
222 * custom status line may invoke ":normal". */
223 struct cmdline_info save_ccline;
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#ifdef FEAT_EVAL
227 if (firstc == -1)
228 {
229 firstc = NUL;
230 break_ctrl_c = TRUE;
231 }
232#endif
233#ifdef FEAT_RIGHTLEFT
234 /* start without Hebrew mapping for a command line */
235 if (firstc == ':' || firstc == '=' || firstc == '>')
236 cmd_hkmap = 0;
237#endif
238
239 ccline.overstrike = FALSE; /* always start in insert mode */
240#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100241 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200242 save_cursor = curwin->w_cursor; /* may be restored later */
243 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 old_curswant = curwin->w_curswant;
245 old_leftcol = curwin->w_leftcol;
246 old_topline = curwin->w_topline;
247# ifdef FEAT_DIFF
248 old_topfill = curwin->w_topfill;
249# endif
250 old_botline = curwin->w_botline;
251#endif
252
253 /*
254 * set some variables for redrawcmd()
255 */
256 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000257 ccline.cmdindent = (firstc > 0 ? indent : 0);
258
259 /* alloc initial ccline.cmdbuff */
260 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 if (ccline.cmdbuff == NULL)
262 return NULL; /* out of memory */
263 ccline.cmdlen = ccline.cmdpos = 0;
264 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100265 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000267 /* autoindent for :insert and :append */
268 if (firstc <= 0)
269 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200270 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000271 ccline.cmdbuff[indent] = NUL;
272 ccline.cmdpos = indent;
273 ccline.cmdspos = indent;
274 ccline.cmdlen = indent;
275 }
276
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000278 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280#ifdef FEAT_RIGHTLEFT
281 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
282 && (firstc == '/' || firstc == '?'))
283 cmdmsg_rl = TRUE;
284 else
285 cmdmsg_rl = FALSE;
286#endif
287
288 redir_off = TRUE; /* don't redirect the typed command */
289 if (!cmd_silent)
290 {
291 i = msg_scrolled;
292 msg_scrolled = 0; /* avoid wait_return message */
293 gotocmdline(TRUE);
294 msg_scrolled += i;
295 redrawcmdprompt(); /* draw prompt or indent */
296 set_cmdspos();
297 }
298 xpc.xp_context = EXPAND_NOTHING;
299 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000300#ifndef BACKSLASH_IN_FILENAME
301 xpc.xp_shell = FALSE;
302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000304#if defined(FEAT_EVAL)
305 if (ccline.input_fn)
306 {
307 xpc.xp_context = ccline.xp_context;
308 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000309# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000310 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000311# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000312 }
313#endif
314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 /*
316 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
317 * doing ":@0" when register 0 doesn't contain a CR.
318 */
319 msg_scroll = FALSE;
320
321 State = CMDLINE;
322
323 if (firstc == '/' || firstc == '?' || firstc == '@')
324 {
325 /* Use ":lmap" mappings for search pattern and input(). */
326 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
327 b_im_ptr = &curbuf->b_p_iminsert;
328 else
329 b_im_ptr = &curbuf->b_p_imsearch;
330 if (*b_im_ptr == B_IMODE_LMAP)
331 State |= LANGMAP;
332#ifdef USE_IM_CONTROL
333 im_set_active(*b_im_ptr == B_IMODE_IM);
334#endif
335 }
336#ifdef USE_IM_CONTROL
337 else if (p_imcmdline)
338 im_set_active(TRUE);
339#endif
340
341#ifdef FEAT_MOUSE
342 setmouse();
343#endif
344#ifdef CURSOR_SHAPE
345 ui_cursor_shape(); /* may show different cursor shape */
346#endif
347
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000348 /* When inside an autocommand for writing "exiting" may be set and
349 * terminal mode set to cooked. Need to set raw mode here then. */
350 settmode(TMODE_RAW);
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_CMDHIST
353 init_history();
354 hiscnt = hislen; /* set hiscnt to impossible history value */
355 histype = hist_char2type(firstc);
356#endif
357
358#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000359 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#endif
361
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200362 /* If something above caused an error, reset the flags, we do want to type
363 * and execute commands. Display may be messed up a bit. */
364 if (did_emsg)
365 redrawcmd();
366 did_emsg = FALSE;
367 got_int = FALSE;
368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 /*
370 * Collect the command string, handling editing keys.
371 */
372 for (;;)
373 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000374 redir_off = TRUE; /* Don't redirect the typed command.
375 Repeated, because a ":redir" inside
376 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#ifdef USE_ON_FLY_SCROLL
378 dont_scroll = FALSE; /* allow scrolling here */
379#endif
380 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
381
382 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000383
384 /* Get a character. Ignore K_IGNORE, it should not do anything, such
385 * as stop completion. */
386 do
387 {
388 c = safe_vgetc();
389 } while (c == K_IGNORE);
390
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (KeyTyped)
392 {
393 some_key_typed = TRUE;
394#ifdef FEAT_RIGHTLEFT
395 if (cmd_hkmap)
396 c = hkmap(c);
397# ifdef FEAT_FKMAP
398 if (cmd_fkmap)
399 c = cmdl_fkmap(c);
400# endif
401 if (cmdmsg_rl && !KeyStuffed)
402 {
403 /* Invert horizontal movements and operations. Only when
404 * typed by the user directly, not when the result of a
405 * mapping. */
406 switch (c)
407 {
408 case K_RIGHT: c = K_LEFT; break;
409 case K_S_RIGHT: c = K_S_LEFT; break;
410 case K_C_RIGHT: c = K_C_LEFT; break;
411 case K_LEFT: c = K_RIGHT; break;
412 case K_S_LEFT: c = K_S_RIGHT; break;
413 case K_C_LEFT: c = K_C_RIGHT; break;
414 }
415 }
416#endif
417 }
418
419 /*
420 * Ignore got_int when CTRL-C was typed here.
421 * Don't ignore it in :global, we really need to break then, e.g., for
422 * ":g/pat/normal /pat" (without the <CR>).
423 * Don't ignore it for the input() function.
424 */
425 if ((c == Ctrl_C
426#ifdef UNIX
427 || c == intr_char
428#endif
429 )
430#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
431 && firstc != '@'
432#endif
433#ifdef FEAT_EVAL
434 && !break_ctrl_c
435#endif
436 && !global_busy)
437 got_int = FALSE;
438
439#ifdef FEAT_CMDHIST
440 /* free old command line when finished moving around in the history
441 * list */
442 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000443 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000444 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 && c != K_PAGEDOWN && c != K_PAGEUP
446 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000447 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
449 {
450 vim_free(lookfor);
451 lookfor = NULL;
452 }
453#endif
454
455 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000456 * When there are matching completions to select <S-Tab> works like
457 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000459 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 c = Ctrl_P;
461
462#ifdef FEAT_WILDMENU
463 /* Special translations for 'wildmenu' */
464 if (did_wild_list && p_wmnu)
465 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000466 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000468 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 c = Ctrl_N;
470 }
471 /* Hitting CR after "emenu Name.": complete submenu */
472 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
473 && ccline.cmdpos > 1
474 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
475 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
476 && (c == '\n' || c == '\r' || c == K_KENTER))
477 c = K_DOWN;
478#endif
479
480 /* free expanded names when finished walking through matches */
481 if (xpc.xp_numfiles != -1
482 && !(c == p_wc && KeyTyped) && c != p_wcm
483 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
484 && c != Ctrl_L)
485 {
486 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
487 did_wild_list = FALSE;
488#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000489 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#endif
491 xpc.xp_context = EXPAND_NOTHING;
492 wim_index = 0;
493#ifdef FEAT_WILDMENU
494 if (p_wmnu && wild_menu_showing != 0)
495 {
496 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000497 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000498
499 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000500 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
502 if (wild_menu_showing == WM_SCROLLED)
503 {
504 /* Entered command line, move it up */
505 cmdline_row--;
506 redrawcmd();
507 }
508 else if (save_p_ls != -1)
509 {
510 /* restore 'laststatus' and 'winminheight' */
511 p_ls = save_p_ls;
512 p_wmh = save_p_wmh;
513 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000514 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000516 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 redrawcmd();
518 save_p_ls = -1;
519 }
520 else
521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 redraw_statuslines();
524 }
525 KeyTyped = skt;
526 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000527 if (ccline.input_fn)
528 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
530#endif
531 }
532
533#ifdef FEAT_WILDMENU
534 /* Special translations for 'wildmenu' */
535 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
536 {
537 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000538 if (c == K_DOWN && ccline.cmdpos > 0
539 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000541 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {
543 /* Hitting <Up>: Remove one submenu name in front of the
544 * cursor */
545 int found = FALSE;
546
547 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
548 i = 0;
549 while (--j > 0)
550 {
551 /* check for start of menu name */
552 if (ccline.cmdbuff[j] == ' '
553 && ccline.cmdbuff[j - 1] != '\\')
554 {
555 i = j + 1;
556 break;
557 }
558 /* check for start of submenu name */
559 if (ccline.cmdbuff[j] == '.'
560 && ccline.cmdbuff[j - 1] != '\\')
561 {
562 if (found)
563 {
564 i = j + 1;
565 break;
566 }
567 else
568 found = TRUE;
569 }
570 }
571 if (i > 0)
572 cmdline_del(i);
573 c = p_wc;
574 xpc.xp_context = EXPAND_NOTHING;
575 }
576 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000577 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000578 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000579 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 char_u upseg[5];
582
583 upseg[0] = PATHSEP;
584 upseg[1] = '.';
585 upseg[2] = '.';
586 upseg[3] = PATHSEP;
587 upseg[4] = NUL;
588
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000589 if (c == K_DOWN
590 && ccline.cmdpos > 0
591 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
592 && (ccline.cmdpos < 3
593 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
595 {
596 /* go down a directory */
597 c = p_wc;
598 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000599 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 /* If in a direct ancestor, strip off one ../ to go down */
602 int found = FALSE;
603
604 j = ccline.cmdpos;
605 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
606 while (--j > i)
607 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000608#ifdef FEAT_MBYTE
609 if (has_mbyte)
610 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (vim_ispathsep(ccline.cmdbuff[j]))
613 {
614 found = TRUE;
615 break;
616 }
617 }
618 if (found
619 && ccline.cmdbuff[j - 1] == '.'
620 && ccline.cmdbuff[j - 2] == '.'
621 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
622 {
623 cmdline_del(j - 2);
624 c = p_wc;
625 }
626 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000627 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 /* go up a directory */
630 int found = FALSE;
631
632 j = ccline.cmdpos - 1;
633 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
634 while (--j > i)
635 {
636#ifdef FEAT_MBYTE
637 if (has_mbyte)
638 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
639#endif
640 if (vim_ispathsep(ccline.cmdbuff[j])
641#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100642 && vim_strchr((char_u *)" *?[{`$%#",
643 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#endif
645 )
646 {
647 if (found)
648 {
649 i = j + 1;
650 break;
651 }
652 else
653 found = TRUE;
654 }
655 }
656
657 if (!found)
658 j = i;
659 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
660 j += 4;
661 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
662 && j == i)
663 j += 3;
664 else
665 j = 0;
666 if (j > 0)
667 {
668 /* TODO this is only for DOS/UNIX systems - need to put in
669 * machine-specific stuff here and in upseg init */
670 cmdline_del(j);
671 put_on_cmdline(upseg + 1, 3, FALSE);
672 }
673 else if (ccline.cmdpos > i)
674 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100675
676 /* Now complete in the new directory. Set KeyTyped in case the
677 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100679 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683#endif /* FEAT_WILDMENU */
684
685 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
686 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
687 if (c == Ctrl_BSL)
688 {
689 ++no_mapping;
690 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000691 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 --no_mapping;
693 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200694 /* CTRL-\ e doesn't work when obtaining an expression, unless it
695 * is in a mapping. */
696 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
697 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
699 vungetc(c);
700 c = Ctrl_BSL;
701 }
702#ifdef FEAT_EVAL
703 else if (c == 'e')
704 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200705 char_u *p = NULL;
706 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
708 /*
709 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000710 * Need to save and restore the current command line, to be
711 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 */
713 if (ccline.cmdpos == ccline.cmdlen)
714 new_cmdpos = 99999; /* keep it at the end */
715 else
716 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000717
718 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000720 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (c == '=')
722 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000723 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000724 * to avoid nasty things like going to another buffer when
725 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000726 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000727 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000729 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000730 restore_cmdline(&save_ccline);
731
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200732 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200734 len = (int)STRLEN(p);
735 if (realloc_cmdbuff(len + 1) == OK)
736 {
737 ccline.cmdlen = len;
738 STRCPY(ccline.cmdbuff, p);
739 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200741 /* Restore the cursor or use the position set with
742 * set_cmdline_pos(). */
743 if (new_cmdpos > ccline.cmdlen)
744 ccline.cmdpos = ccline.cmdlen;
745 else
746 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200748 KeyTyped = FALSE; /* Don't do p_wc completion. */
749 redrawcmd();
750 goto cmdline_changed;
751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753 }
754 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100755 got_int = FALSE; /* don't abandon the command line */
756 did_emsg = FALSE;
757 emsg_on_display = FALSE;
758 redrawcmd();
759 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761#endif
762 else
763 {
764 if (c == Ctrl_G && p_im && restart_edit == 0)
765 restart_edit = 'a';
766 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
767 in history */
768 goto returncmd; /* back to Normal mode */
769 }
770 }
771
772#ifdef FEAT_CMDWIN
773 if (c == cedit_key || c == K_CMDWIN)
774 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200775 if (ex_normal_busy == 0 && got_int == FALSE)
776 {
777 /*
778 * Open a window to edit the command line (and history).
779 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200780 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200781 some_key_typed = TRUE;
782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784# ifdef FEAT_DIGRAPHS
785 else
786# endif
787#endif
788#ifdef FEAT_DIGRAPHS
789 c = do_digraph(c);
790#endif
791
792 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
793 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
794 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000795 /* In Ex mode a backslash escapes a newline. */
796 if (exmode_active
797 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000798 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000799 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000800 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000802 if (c == K_KENTER)
803 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000805 else
806 {
807 gotesc = FALSE; /* Might have typed ESC previously, don't
808 truncate the cmdline now. */
809 if (ccheck_abbr(c + ABBR_OFF))
810 goto cmdline_changed;
811 if (!cmd_silent)
812 {
813 windgoto(msg_row, 0);
814 out_flush();
815 }
816 break;
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
819
820 /*
821 * Completion for 'wildchar' or 'wildcharm' key.
822 * - hitting <ESC> twice means: abandon command line.
823 * - wildcard expansion is only done when the 'wildchar' key is really
824 * typed, not when it comes from a macro
825 */
826 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
827 {
828 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
829 {
830 /* if 'wildmode' contains "list" may still need to list */
831 if (xpc.xp_numfiles > 1
832 && !did_wild_list
833 && (wim_flags[wim_index] & WIM_LIST))
834 {
835 (void)showmatches(&xpc, FALSE);
836 redrawcmd();
837 did_wild_list = TRUE;
838 }
839 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100840 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
841 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100843 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
844 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 else
846 res = OK; /* don't insert 'wildchar' now */
847 }
848 else /* typed p_wc first time */
849 {
850 wim_index = 0;
851 j = ccline.cmdpos;
852 /* if 'wildmode' first contains "longest", get longest
853 * common part */
854 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100855 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
856 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100858 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
859 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861 /* if interrupted while completing, behave like it failed */
862 if (got_int)
863 {
864 (void)vpeekc(); /* remove <C-C> from input stream */
865 got_int = FALSE; /* don't abandon the command line */
866 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
867#ifdef FEAT_WILDMENU
868 xpc.xp_context = EXPAND_NOTHING;
869#endif
870 goto cmdline_changed;
871 }
872
873 /* when more than one match, and 'wildmode' first contains
874 * "list", or no change and 'wildmode' contains "longest,list",
875 * list all matches */
876 if (res == OK && xpc.xp_numfiles > 1)
877 {
878 /* a "longest" that didn't do anything is skipped (but not
879 * "list:longest") */
880 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
881 wim_index = 1;
882 if ((wim_flags[wim_index] & WIM_LIST)
883#ifdef FEAT_WILDMENU
884 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
885#endif
886 )
887 {
888 if (!(wim_flags[0] & WIM_LONGEST))
889 {
890#ifdef FEAT_WILDMENU
891 int p_wmnu_save = p_wmnu;
892 p_wmnu = 0;
893#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100894 /* remove match */
895 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#ifdef FEAT_WILDMENU
897 p_wmnu = p_wmnu_save;
898#endif
899 }
900#ifdef FEAT_WILDMENU
901 (void)showmatches(&xpc, p_wmnu
902 && ((wim_flags[wim_index] & WIM_LIST) == 0));
903#else
904 (void)showmatches(&xpc, FALSE);
905#endif
906 redrawcmd();
907 did_wild_list = TRUE;
908 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100909 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
910 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100912 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
913 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200916 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918#ifdef FEAT_WILDMENU
919 else if (xpc.xp_numfiles == -1)
920 xpc.xp_context = EXPAND_NOTHING;
921#endif
922 }
923 if (wim_index < 3)
924 ++wim_index;
925 if (c == ESC)
926 gotesc = TRUE;
927 if (res == OK)
928 goto cmdline_changed;
929 }
930
931 gotesc = FALSE;
932
933 /* <S-Tab> goes to last match, in a clumsy way */
934 if (c == K_S_TAB && KeyTyped)
935 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100936 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
937 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
938 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 goto cmdline_changed;
940 }
941
942 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
943 c = NL;
944
945 do_abbr = TRUE; /* default: check for abbreviation */
946
947 /*
948 * Big switch for a typed command line character.
949 */
950 switch (c)
951 {
952 case K_BS:
953 case Ctrl_H:
954 case K_DEL:
955 case K_KDEL:
956 case Ctrl_W:
957#ifdef FEAT_FKMAP
958 if (cmd_fkmap && c == K_BS)
959 c = K_DEL;
960#endif
961 if (c == K_KDEL)
962 c = K_DEL;
963
964 /*
965 * delete current character is the same as backspace on next
966 * character, except at end of line
967 */
968 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
969 ++ccline.cmdpos;
970#ifdef FEAT_MBYTE
971 if (has_mbyte && c == K_DEL)
972 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
973 ccline.cmdbuff + ccline.cmdpos);
974#endif
975 if (ccline.cmdpos > 0)
976 {
977 char_u *p;
978
979 j = ccline.cmdpos;
980 p = ccline.cmdbuff + j;
981#ifdef FEAT_MBYTE
982 if (has_mbyte)
983 {
984 p = mb_prevptr(ccline.cmdbuff, p);
985 if (c == Ctrl_W)
986 {
987 while (p > ccline.cmdbuff && vim_isspace(*p))
988 p = mb_prevptr(ccline.cmdbuff, p);
989 i = mb_get_class(p);
990 while (p > ccline.cmdbuff && mb_get_class(p) == i)
991 p = mb_prevptr(ccline.cmdbuff, p);
992 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000993 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995 }
996 else
997#endif
998 if (c == Ctrl_W)
999 {
1000 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1001 --p;
1002 i = vim_iswordc(p[-1]);
1003 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1004 && vim_iswordc(p[-1]) == i)
1005 --p;
1006 }
1007 else
1008 --p;
1009 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1010 ccline.cmdlen -= j - ccline.cmdpos;
1011 i = ccline.cmdpos;
1012 while (i < ccline.cmdlen)
1013 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1014
1015 /* Truncate at the end, required for multi-byte chars. */
1016 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001017#ifdef FEAT_SEARCH_EXTRA
1018 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001019 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001020 search_start = save_cursor;
1021 /* save view settings, so that the screen
1022 * won't be restored at the wrong position */
1023 old_curswant = init_curswant;
1024 old_leftcol = init_leftcol;
1025 old_topline = init_topline;
1026# ifdef FEAT_DIFF
1027 old_topfill = init_topfill;
1028# endif
1029 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001030 }
1031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 redrawcmd();
1033 }
1034 else if (ccline.cmdlen == 0 && c != Ctrl_W
1035 && ccline.cmdprompt == NULL && indent == 0)
1036 {
1037 /* In ex and debug mode it doesn't make sense to return. */
1038 if (exmode_active
1039#ifdef FEAT_EVAL
1040 || ccline.cmdfirstc == '>'
1041#endif
1042 )
1043 goto cmdline_not_changed;
1044
1045 vim_free(ccline.cmdbuff); /* no commandline to return */
1046 ccline.cmdbuff = NULL;
1047 if (!cmd_silent)
1048 {
1049#ifdef FEAT_RIGHTLEFT
1050 if (cmdmsg_rl)
1051 msg_col = Columns;
1052 else
1053#endif
1054 msg_col = 0;
1055 msg_putchar(' '); /* delete ':' */
1056 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001057#ifdef FEAT_SEARCH_EXTRA
1058 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001059 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 redraw_cmdline = TRUE;
1062 goto returncmd; /* back to cmd mode */
1063 }
1064 goto cmdline_changed;
1065
1066 case K_INS:
1067 case K_KINS:
1068#ifdef FEAT_FKMAP
1069 /* if Farsi mode set, we are in reverse insert mode -
1070 Do not change the mode */
1071 if (cmd_fkmap)
1072 beep_flush();
1073 else
1074#endif
1075 ccline.overstrike = !ccline.overstrike;
1076#ifdef CURSOR_SHAPE
1077 ui_cursor_shape(); /* may show different cursor shape */
1078#endif
1079 goto cmdline_not_changed;
1080
1081 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001082 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 /* ":lmap" mappings exists, toggle use of mappings. */
1085 State ^= LANGMAP;
1086#ifdef USE_IM_CONTROL
1087 im_set_active(FALSE); /* Disable input method */
1088#endif
1089 if (b_im_ptr != NULL)
1090 {
1091 if (State & LANGMAP)
1092 *b_im_ptr = B_IMODE_LMAP;
1093 else
1094 *b_im_ptr = B_IMODE_NONE;
1095 }
1096 }
1097#ifdef USE_IM_CONTROL
1098 else
1099 {
1100 /* There are no ":lmap" mappings, toggle IM. When
1101 * 'imdisable' is set don't try getting the status, it's
1102 * always off. */
1103 if ((p_imdisable && b_im_ptr != NULL)
1104 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1105 {
1106 im_set_active(FALSE); /* Disable input method */
1107 if (b_im_ptr != NULL)
1108 *b_im_ptr = B_IMODE_NONE;
1109 }
1110 else
1111 {
1112 im_set_active(TRUE); /* Enable input method */
1113 if (b_im_ptr != NULL)
1114 *b_im_ptr = B_IMODE_IM;
1115 }
1116 }
1117#endif
1118 if (b_im_ptr != NULL)
1119 {
1120 if (b_im_ptr == &curbuf->b_p_iminsert)
1121 set_iminsert_global();
1122 else
1123 set_imsearch_global();
1124 }
1125#ifdef CURSOR_SHAPE
1126 ui_cursor_shape(); /* may show different cursor shape */
1127#endif
1128#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1129 /* Show/unshow value of 'keymap' in status lines later. */
1130 status_redraw_curbuf();
1131#endif
1132 goto cmdline_not_changed;
1133
1134/* case '@': only in very old vi */
1135 case Ctrl_U:
1136 /* delete all characters left of the cursor */
1137 j = ccline.cmdpos;
1138 ccline.cmdlen -= j;
1139 i = ccline.cmdpos = 0;
1140 while (i < ccline.cmdlen)
1141 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1142 /* Truncate at the end, required for multi-byte chars. */
1143 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001144#ifdef FEAT_SEARCH_EXTRA
1145 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001146 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 redrawcmd();
1149 goto cmdline_changed;
1150
1151#ifdef FEAT_CLIPBOARD
1152 case Ctrl_Y:
1153 /* Copy the modeless selection, if there is one. */
1154 if (clip_star.state != SELECT_CLEARED)
1155 {
1156 if (clip_star.state == SELECT_DONE)
1157 clip_copy_modeless_selection(TRUE);
1158 goto cmdline_not_changed;
1159 }
1160 break;
1161#endif
1162
1163 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1164 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001165 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001166 * ":normal" runs out of characters. */
1167 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001168 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 goto cmdline_not_changed;
1170
1171 gotesc = TRUE; /* will free ccline.cmdbuff after
1172 putting it in history */
1173 goto returncmd; /* back to cmd mode */
1174
1175 case Ctrl_R: /* insert register */
1176#ifdef USE_ON_FLY_SCROLL
1177 dont_scroll = TRUE; /* disallow scrolling here */
1178#endif
1179 putcmdline('"', TRUE);
1180 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001181 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (i == Ctrl_O)
1183 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1184 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001185 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001186 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 --no_mapping;
1188#ifdef FEAT_EVAL
1189 /*
1190 * Insert the result of an expression.
1191 * Need to save the current command line, to be able to enter
1192 * a new one...
1193 */
1194 new_cmdpos = -1;
1195 if (c == '=')
1196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1198 {
1199 beep_flush();
1200 c = ESC;
1201 }
1202 else
1203 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001204 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001206 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 }
1209#endif
1210 if (c != ESC) /* use ESC to cancel inserting register */
1211 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001212 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001213
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001214#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001215 /* When there was a serious error abort getting the
1216 * command line. */
1217 if (aborting())
1218 {
1219 gotesc = TRUE; /* will free ccline.cmdbuff after
1220 putting it in history */
1221 goto returncmd; /* back to cmd mode */
1222 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 KeyTyped = FALSE; /* Don't do p_wc completion. */
1225#ifdef FEAT_EVAL
1226 if (new_cmdpos >= 0)
1227 {
1228 /* set_cmdline_pos() was used */
1229 if (new_cmdpos > ccline.cmdlen)
1230 ccline.cmdpos = ccline.cmdlen;
1231 else
1232 ccline.cmdpos = new_cmdpos;
1233 }
1234#endif
1235 }
1236 redrawcmd();
1237 goto cmdline_changed;
1238
1239 case Ctrl_D:
1240 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1241 break; /* Use ^D as normal char instead */
1242
1243 redrawcmd();
1244 continue; /* don't do incremental search now */
1245
1246 case K_RIGHT:
1247 case K_S_RIGHT:
1248 case K_C_RIGHT:
1249 do
1250 {
1251 if (ccline.cmdpos >= ccline.cmdlen)
1252 break;
1253 i = cmdline_charsize(ccline.cmdpos);
1254 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1255 break;
1256 ccline.cmdspos += i;
1257#ifdef FEAT_MBYTE
1258 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001259 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 + ccline.cmdpos);
1261 else
1262#endif
1263 ++ccline.cmdpos;
1264 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001265 while ((c == K_S_RIGHT || c == K_C_RIGHT
1266 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1268#ifdef FEAT_MBYTE
1269 if (has_mbyte)
1270 set_cmdspos_cursor();
1271#endif
1272 goto cmdline_not_changed;
1273
1274 case K_LEFT:
1275 case K_S_LEFT:
1276 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001277 if (ccline.cmdpos == 0)
1278 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 do
1280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 --ccline.cmdpos;
1282#ifdef FEAT_MBYTE
1283 if (has_mbyte) /* move to first byte of char */
1284 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1285 ccline.cmdbuff + ccline.cmdpos);
1286#endif
1287 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1288 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001289 while (ccline.cmdpos > 0
1290 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001291 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1293#ifdef FEAT_MBYTE
1294 if (has_mbyte)
1295 set_cmdspos_cursor();
1296#endif
1297 goto cmdline_not_changed;
1298
1299 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001300 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001301 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302
Bram Moolenaar4770d092006-01-12 23:22:24 +00001303#ifdef FEAT_GUI_W32
1304 /* On Win32 ignore <M-F4>, we get it when closing the window was
1305 * cancelled. */
1306 case K_F4:
1307 if (mod_mask == MOD_MASK_ALT)
1308 {
1309 redrawcmd(); /* somehow the cmdline is cleared */
1310 goto cmdline_not_changed;
1311 }
1312 break;
1313#endif
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315#ifdef FEAT_MOUSE
1316 case K_MIDDLEDRAG:
1317 case K_MIDDLERELEASE:
1318 goto cmdline_not_changed; /* Ignore mouse */
1319
1320 case K_MIDDLEMOUSE:
1321# ifdef FEAT_GUI
1322 /* When GUI is active, also paste when 'mouse' is empty */
1323 if (!gui.in_use)
1324# endif
1325 if (!mouse_has(MOUSE_COMMAND))
1326 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001327# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001329 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001331# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001332 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 redrawcmd();
1334 goto cmdline_changed;
1335
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001336# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001338 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 redrawcmd();
1340 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 case K_LEFTDRAG:
1344 case K_LEFTRELEASE:
1345 case K_RIGHTDRAG:
1346 case K_RIGHTRELEASE:
1347 /* Ignore drag and release events when the button-down wasn't
1348 * seen before. */
1349 if (ignore_drag_release)
1350 goto cmdline_not_changed;
1351 /* FALLTHROUGH */
1352 case K_LEFTMOUSE:
1353 case K_RIGHTMOUSE:
1354 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1355 ignore_drag_release = TRUE;
1356 else
1357 ignore_drag_release = FALSE;
1358# ifdef FEAT_GUI
1359 /* When GUI is active, also move when 'mouse' is empty */
1360 if (!gui.in_use)
1361# endif
1362 if (!mouse_has(MOUSE_COMMAND))
1363 goto cmdline_not_changed; /* Ignore mouse */
1364# ifdef FEAT_CLIPBOARD
1365 if (mouse_row < cmdline_row && clip_star.available)
1366 {
1367 int button, is_click, is_drag;
1368
1369 /*
1370 * Handle modeless selection.
1371 */
1372 button = get_mouse_button(KEY2TERMCAP1(c),
1373 &is_click, &is_drag);
1374 if (mouse_model_popup() && button == MOUSE_LEFT
1375 && (mod_mask & MOD_MASK_SHIFT))
1376 {
1377 /* Translate shift-left to right button. */
1378 button = MOUSE_RIGHT;
1379 mod_mask &= ~MOD_MASK_SHIFT;
1380 }
1381 clip_modeless(button, is_click, is_drag);
1382 goto cmdline_not_changed;
1383 }
1384# endif
1385
1386 set_cmdspos();
1387 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1388 ++ccline.cmdpos)
1389 {
1390 i = cmdline_charsize(ccline.cmdpos);
1391 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1392 && mouse_col < ccline.cmdspos % Columns + i)
1393 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001394# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (has_mbyte)
1396 {
1397 /* Count ">" for double-wide char that doesn't fit. */
1398 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001399 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 + ccline.cmdpos) - 1;
1401 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 ccline.cmdspos += i;
1404 }
1405 goto cmdline_not_changed;
1406
1407 /* Mouse scroll wheel: ignored here */
1408 case K_MOUSEDOWN:
1409 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001410 case K_MOUSELEFT:
1411 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 /* Alternate buttons ignored here */
1413 case K_X1MOUSE:
1414 case K_X1DRAG:
1415 case K_X1RELEASE:
1416 case K_X2MOUSE:
1417 case K_X2DRAG:
1418 case K_X2RELEASE:
1419 goto cmdline_not_changed;
1420
1421#endif /* FEAT_MOUSE */
1422
1423#ifdef FEAT_GUI
1424 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1425 case K_LEFTRELEASE_NM:
1426 goto cmdline_not_changed;
1427
1428 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001429 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
1431 gui_do_scroll();
1432 redrawcmd();
1433 }
1434 goto cmdline_not_changed;
1435
1436 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001437 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001439 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 redrawcmd();
1441 }
1442 goto cmdline_not_changed;
1443#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001444#ifdef FEAT_GUI_TABLINE
1445 case K_TABLINE:
1446 case K_TABMENU:
1447 /* Don't want to change any tabs here. Make sure the same tab
1448 * is still selected. */
1449 if (gui_use_tabline())
1450 gui_mch_set_curtab(tabpage_index(curtab));
1451 goto cmdline_not_changed;
1452#endif
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 case K_SELECT: /* end of Select mode mapping - ignore */
1455 goto cmdline_not_changed;
1456
1457 case Ctrl_B: /* begin of command line */
1458 case K_HOME:
1459 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 case K_S_HOME:
1461 case K_C_HOME:
1462 ccline.cmdpos = 0;
1463 set_cmdspos();
1464 goto cmdline_not_changed;
1465
1466 case Ctrl_E: /* end of command line */
1467 case K_END:
1468 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 case K_S_END:
1470 case K_C_END:
1471 ccline.cmdpos = ccline.cmdlen;
1472 set_cmdspos_cursor();
1473 goto cmdline_not_changed;
1474
1475 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001476 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 break;
1478 goto cmdline_changed;
1479
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001480 case Ctrl_L:
1481#ifdef FEAT_SEARCH_EXTRA
1482 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1483 {
1484 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001485 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001486 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001487 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001488 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001489 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001490 c = gchar_cursor();
1491 /* If 'ignorecase' and 'smartcase' are set and the
1492 * command line has no uppercase characters, convert
1493 * the character to lowercase */
1494 if (p_ic && p_scs
1495 && !pat_has_uppercase(ccline.cmdbuff))
1496 c = MB_TOLOWER(c);
1497 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001498 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001499 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001500 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001501 != NULL)
1502 {
1503 /* put a backslash before special
1504 * characters */
1505 stuffcharReadbuff(c);
1506 c = '\\';
1507 }
1508 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001509 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001510 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001511 }
1512 goto cmdline_not_changed;
1513 }
1514#endif
1515
1516 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001517 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 break;
1519 goto cmdline_changed;
1520
1521 case Ctrl_N: /* next match */
1522 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001523 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001525 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1526 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001528 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001530 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
1532#ifdef FEAT_CMDHIST
1533 case K_UP:
1534 case K_DOWN:
1535 case K_S_UP:
1536 case K_S_DOWN:
1537 case K_PAGEUP:
1538 case K_KPAGEUP:
1539 case K_PAGEDOWN:
1540 case K_KPAGEDOWN:
1541 if (hislen == 0 || firstc == NUL) /* no history */
1542 goto cmdline_not_changed;
1543
1544 i = hiscnt;
1545
1546 /* save current command string so it can be restored later */
1547 if (lookfor == NULL)
1548 {
1549 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1550 goto cmdline_not_changed;
1551 lookfor[ccline.cmdpos] = NUL;
1552 }
1553
1554 j = (int)STRLEN(lookfor);
1555 for (;;)
1556 {
1557 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001558 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001559 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 if (hiscnt == hislen) /* first time */
1562 hiscnt = hisidx[histype];
1563 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1564 hiscnt = hislen - 1;
1565 else if (hiscnt != hisidx[histype] + 1)
1566 --hiscnt;
1567 else /* at top of list */
1568 {
1569 hiscnt = i;
1570 break;
1571 }
1572 }
1573 else /* one step forwards */
1574 {
1575 /* on last entry, clear the line */
1576 if (hiscnt == hisidx[histype])
1577 {
1578 hiscnt = hislen;
1579 break;
1580 }
1581
1582 /* not on a history line, nothing to do */
1583 if (hiscnt == hislen)
1584 break;
1585 if (hiscnt == hislen - 1) /* wrap around */
1586 hiscnt = 0;
1587 else
1588 ++hiscnt;
1589 }
1590 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1591 {
1592 hiscnt = i;
1593 break;
1594 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001595 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001596 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 || STRNCMP(history[histype][hiscnt].hisstr,
1598 lookfor, (size_t)j) == 0)
1599 break;
1600 }
1601
1602 if (hiscnt != i) /* jumped to other entry */
1603 {
1604 char_u *p;
1605 int len;
1606 int old_firstc;
1607
1608 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001609 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (hiscnt == hislen)
1611 p = lookfor; /* back to the old one */
1612 else
1613 p = history[histype][hiscnt].hisstr;
1614
1615 if (histype == HIST_SEARCH
1616 && p != lookfor
1617 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1618 {
1619 /* Correct for the separator character used when
1620 * adding the history entry vs the one used now.
1621 * First loop: count length.
1622 * Second loop: copy the characters. */
1623 for (i = 0; i <= 1; ++i)
1624 {
1625 len = 0;
1626 for (j = 0; p[j] != NUL; ++j)
1627 {
1628 /* Replace old sep with new sep, unless it is
1629 * escaped. */
1630 if (p[j] == old_firstc
1631 && (j == 0 || p[j - 1] != '\\'))
1632 {
1633 if (i > 0)
1634 ccline.cmdbuff[len] = firstc;
1635 }
1636 else
1637 {
1638 /* Escape new sep, unless it is already
1639 * escaped. */
1640 if (p[j] == firstc
1641 && (j == 0 || p[j - 1] != '\\'))
1642 {
1643 if (i > 0)
1644 ccline.cmdbuff[len] = '\\';
1645 ++len;
1646 }
1647 if (i > 0)
1648 ccline.cmdbuff[len] = p[j];
1649 }
1650 ++len;
1651 }
1652 if (i == 0)
1653 {
1654 alloc_cmdbuff(len);
1655 if (ccline.cmdbuff == NULL)
1656 goto returncmd;
1657 }
1658 }
1659 ccline.cmdbuff[len] = NUL;
1660 }
1661 else
1662 {
1663 alloc_cmdbuff((int)STRLEN(p));
1664 if (ccline.cmdbuff == NULL)
1665 goto returncmd;
1666 STRCPY(ccline.cmdbuff, p);
1667 }
1668
1669 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1670 redrawcmd();
1671 goto cmdline_changed;
1672 }
1673 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001674#endif
1675 goto cmdline_not_changed;
1676
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001677#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001678 case Ctrl_G: /* next match */
1679 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001680 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1681 {
1682 pos_T t;
1683 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1684 + SEARCH_PEEK;
1685
1686 if (char_avail())
1687 continue;
1688 cursor_off();
1689 out_flush();
1690 if (c == Ctrl_G)
1691 {
1692 t = match_end;
1693 search_flags += SEARCH_COL;
1694 }
1695 else
1696 t = match_start;
1697 ++emsg_off;
1698 i = searchit(curwin, curbuf, &t,
1699 c == Ctrl_G ? FORWARD : BACKWARD,
1700 ccline.cmdbuff, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001701 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001702 --emsg_off;
1703 if (i)
1704 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001705 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001706 match_end = t;
1707 match_start = t;
1708 if (c == Ctrl_T && firstc == '/')
1709 {
1710 /* move just before the current match, so that
1711 * when nv_search finishes the cursor will be
1712 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001713 search_start = t;
1714 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001715 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001716 else if (c == Ctrl_G && firstc == '?')
1717 {
1718 /* move just after the current match, so that
1719 * when nv_search finishes the cursor will be
1720 * put back on the match */
1721 search_start = t;
1722 (void)incl(&search_start);
1723 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001724 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001725 {
1726 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001727 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001728 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001729 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001730 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001731 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001732 }
1733
1734 set_search_match(&match_end);
1735 curwin->w_cursor = match_start;
1736 changed_cline_bef_curs();
1737 update_topline();
1738 validate_cursor();
1739 highlight_match = TRUE;
1740 old_curswant = curwin->w_curswant;
1741 old_leftcol = curwin->w_leftcol;
1742 old_topline = curwin->w_topline;
1743# ifdef FEAT_DIFF
1744 old_topfill = curwin->w_topfill;
1745# endif
1746 old_botline = curwin->w_botline;
1747 update_screen(NOT_VALID);
1748 redrawcmdline();
1749 }
1750 else
1751 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001752 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001753 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001754 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755#endif
1756
1757 case Ctrl_V:
1758 case Ctrl_Q:
1759#ifdef FEAT_MOUSE
1760 ignore_drag_release = TRUE;
1761#endif
1762 putcmdline('^', TRUE);
1763 c = get_literal(); /* get next (two) character(s) */
1764 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001765 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_MBYTE
1767 /* may need to remove ^ when composing char was typed */
1768 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1769 {
1770 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1771 msg_putchar(' ');
1772 cursorcmd();
1773 }
1774#endif
1775 break;
1776
1777#ifdef FEAT_DIGRAPHS
1778 case Ctrl_K:
1779#ifdef FEAT_MOUSE
1780 ignore_drag_release = TRUE;
1781#endif
1782 putcmdline('?', TRUE);
1783#ifdef USE_ON_FLY_SCROLL
1784 dont_scroll = TRUE; /* disallow scrolling here */
1785#endif
1786 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001787 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (c != NUL)
1789 break;
1790
1791 redrawcmd();
1792 goto cmdline_not_changed;
1793#endif /* FEAT_DIGRAPHS */
1794
1795#ifdef FEAT_RIGHTLEFT
1796 case Ctrl__: /* CTRL-_: switch language mode */
1797 if (!p_ari)
1798 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001799# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (p_altkeymap)
1801 {
1802 cmd_fkmap = !cmd_fkmap;
1803 if (cmd_fkmap) /* in Farsi always in Insert mode */
1804 ccline.overstrike = FALSE;
1805 }
1806 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 cmd_hkmap = !cmd_hkmap;
1809 goto cmdline_not_changed;
1810#endif
1811
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001812 case K_PS:
1813 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1814 goto cmdline_changed;
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 default:
1817#ifdef UNIX
1818 if (c == intr_char)
1819 {
1820 gotesc = TRUE; /* will free ccline.cmdbuff after
1821 putting it in history */
1822 goto returncmd; /* back to Normal mode */
1823 }
1824#endif
1825 /*
1826 * Normal character with no special meaning. Just set mod_mask
1827 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1828 * the string <S-Space>. This should only happen after ^V.
1829 */
1830 if (!IS_SPECIAL(c))
1831 mod_mask = 0x0;
1832 break;
1833 }
1834 /*
1835 * End of switch on command line character.
1836 * We come here if we have a normal character.
1837 */
1838
Bram Moolenaarede3e632013-06-23 16:16:19 +02001839 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_MBYTE
1841 /* Add ABBR_OFF for characters above 0x100, this is
1842 * what check_abbr() expects. */
1843 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1844#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001845 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 goto cmdline_changed;
1847
1848 /*
1849 * put the character in the command line
1850 */
1851 if (IS_SPECIAL(c) || mod_mask != 0)
1852 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1853 else
1854 {
1855#ifdef FEAT_MBYTE
1856 if (has_mbyte)
1857 {
1858 j = (*mb_char2bytes)(c, IObuff);
1859 IObuff[j] = NUL; /* exclude composing chars */
1860 put_on_cmdline(IObuff, j, TRUE);
1861 }
1862 else
1863#endif
1864 {
1865 IObuff[0] = c;
1866 put_on_cmdline(IObuff, 1, TRUE);
1867 }
1868 }
1869 goto cmdline_changed;
1870
1871/*
1872 * This part implements incremental searches for "/" and "?"
1873 * Jump to cmdline_not_changed when a character has been read but the command
1874 * line did not change. Then we only search and redraw if something changed in
1875 * the past.
1876 * Jump to cmdline_changed when the command line did change.
1877 * (Sorry for the goto's, I know it is ugly).
1878 */
1879cmdline_not_changed:
1880#ifdef FEAT_SEARCH_EXTRA
1881 if (!incsearch_postponed)
1882 continue;
1883#endif
1884
1885cmdline_changed:
1886#ifdef FEAT_SEARCH_EXTRA
1887 /*
1888 * 'incsearch' highlighting.
1889 */
1890 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1891 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001892 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001893#ifdef FEAT_RELTIME
1894 proftime_T tm;
1895#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 /* if there is a character waiting, search and redraw later */
1898 if (char_avail())
1899 {
1900 incsearch_postponed = TRUE;
1901 continue;
1902 }
1903 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001904 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 /* If there is no command line, don't do anything */
1907 if (ccline.cmdlen == 0)
1908 i = 0;
1909 else
1910 {
1911 cursor_off(); /* so the user knows we're busy */
1912 out_flush();
1913 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001914#ifdef FEAT_RELTIME
1915 /* Set the time limit to half a second. */
1916 profile_setlimit(500L, &tm);
1917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001919 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1920#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001921 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001922#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001923 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001924#endif
1925 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 --emsg_off;
1927 /* if interrupted while searching, behave like it failed */
1928 if (got_int)
1929 {
1930 (void)vpeekc(); /* remove <C-C> from input stream */
1931 got_int = FALSE; /* don't abandon the command line */
1932 i = 0;
1933 }
1934 else if (char_avail())
1935 /* cancelled searching because a char was typed */
1936 incsearch_postponed = TRUE;
1937 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001938 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 highlight_match = TRUE; /* highlight position */
1940 else
1941 highlight_match = FALSE; /* remove highlight */
1942
1943 /* first restore the old curwin values, so the screen is
1944 * positioned in the same way as the actual search command */
1945 curwin->w_leftcol = old_leftcol;
1946 curwin->w_topline = old_topline;
1947# ifdef FEAT_DIFF
1948 curwin->w_topfill = old_topfill;
1949# endif
1950 curwin->w_botline = old_botline;
1951 changed_cline_bef_curs();
1952 update_topline();
1953
1954 if (i != 0)
1955 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001956 pos_T save_pos = curwin->w_cursor;
1957
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001958 match_start = curwin->w_cursor;
1959 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001961 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001962 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001963 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001965 else
1966 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001969# ifdef FEAT_WINDOWS
1970 /* May redraw the status line to show the cursor position. */
1971 if (p_ru && curwin->w_status_height > 0)
1972 curwin->w_redr_status = TRUE;
1973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001975 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001976 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001977 restore_cmdline(&save_ccline);
1978
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001979 /* Leave it at the end to make CTRL-R CTRL-W work. */
1980 if (i != 0)
1981 curwin->w_cursor = end_pos;
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 msg_starthere();
1984 redrawcmdline();
1985 did_incsearch = TRUE;
1986 }
1987#else /* FEAT_SEARCH_EXTRA */
1988 ;
1989#endif
1990
1991#ifdef FEAT_RIGHTLEFT
1992 if (cmdmsg_rl
1993# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001994 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995# endif
1996 )
1997 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001998 * right-left typing. Not efficient, but it works.
1999 * Do it only when there are no characters left to read
2000 * to avoid useless intermediate redraws. */
2001 if (vpeekc() == NUL)
2002 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#endif
2004 }
2005
2006returncmd:
2007
2008#ifdef FEAT_RIGHTLEFT
2009 cmdmsg_rl = FALSE;
2010#endif
2011
2012#ifdef FEAT_FKMAP
2013 cmd_fkmap = 0;
2014#endif
2015
2016 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002017 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019#ifdef FEAT_SEARCH_EXTRA
2020 if (did_incsearch)
2021 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002022 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002023 curwin->w_cursor = save_cursor;
2024 else
2025 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002026 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002027 {
2028 /* put the '" mark at the original position */
2029 curwin->w_cursor = save_cursor;
2030 setpcmark();
2031 }
2032 curwin->w_cursor = search_start;
2033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 curwin->w_curswant = old_curswant;
2035 curwin->w_leftcol = old_leftcol;
2036 curwin->w_topline = old_topline;
2037# ifdef FEAT_DIFF
2038 curwin->w_topfill = old_topfill;
2039# endif
2040 curwin->w_botline = old_botline;
2041 highlight_match = FALSE;
2042 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002043 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045#endif
2046
2047 if (ccline.cmdbuff != NULL)
2048 {
2049 /*
2050 * Put line in history buffer (":" and "=" only when it was typed).
2051 */
2052#ifdef FEAT_CMDHIST
2053 if (ccline.cmdlen && firstc != NUL
2054 && (some_key_typed || histype == HIST_SEARCH))
2055 {
2056 add_to_history(histype, ccline.cmdbuff, TRUE,
2057 histype == HIST_SEARCH ? firstc : NUL);
2058 if (firstc == ':')
2059 {
2060 vim_free(new_last_cmdline);
2061 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2062 }
2063 }
2064#endif
2065
2066 if (gotesc) /* abandon command line */
2067 {
2068 vim_free(ccline.cmdbuff);
2069 ccline.cmdbuff = NULL;
2070 if (msg_scrolled == 0)
2071 compute_cmdrow();
2072 MSG("");
2073 redraw_cmdline = TRUE;
2074 }
2075 }
2076
2077 /*
2078 * If the screen was shifted up, redraw the whole screen (later).
2079 * If the line is too long, clear it, so ruler and shown command do
2080 * not get printed in the middle of it.
2081 */
2082 msg_check();
2083 msg_scroll = save_msg_scroll;
2084 redir_off = FALSE;
2085
2086 /* When the command line was typed, no need for a wait-return prompt. */
2087 if (some_key_typed)
2088 need_wait_return = FALSE;
2089
2090 State = save_State;
2091#ifdef USE_IM_CONTROL
2092 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2093 im_save_status(b_im_ptr);
2094 im_set_active(FALSE);
2095#endif
2096#ifdef FEAT_MOUSE
2097 setmouse();
2098#endif
2099#ifdef CURSOR_SHAPE
2100 ui_cursor_shape(); /* may show different cursor shape */
2101#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002102 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002104 {
2105 char_u *p = ccline.cmdbuff;
2106
2107 /* Make ccline empty, getcmdline() may try to use it. */
2108 ccline.cmdbuff = NULL;
2109 return p;
2110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112
2113#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2114/*
2115 * Get a command line with a prompt.
2116 * This is prepared to be called recursively from getcmdline() (e.g. by
2117 * f_input() when evaluating an expression from CTRL-R =).
2118 * Returns the command line in allocated memory, or NULL.
2119 */
2120 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002121getcmdline_prompt(
2122 int firstc,
2123 char_u *prompt, /* command line prompt */
2124 int attr, /* attributes for prompt */
2125 int xp_context, /* type of expansion */
2126 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 char_u *s;
2129 struct cmdline_info save_ccline;
2130 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002131 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002133 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 ccline.cmdprompt = prompt;
2135 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002136# ifdef FEAT_EVAL
2137 ccline.xp_context = xp_context;
2138 ccline.xp_arg = xp_arg;
2139 ccline.input_fn = (firstc == '@');
2140# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002141 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002143 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002144 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002145 /* Restore msg_col, the prompt from input() may have changed it.
2146 * But only if called recursively and the commandline is therefore being
2147 * restored to an old one; if not, the input() prompt stays on the screen,
2148 * so we need its modified msg_col left intact. */
2149 if (ccline.cmdbuff != NULL)
2150 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 return s;
2153}
2154#endif
2155
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002156/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002157 * Return TRUE when the text must not be changed and we can't switch to
2158 * another window or buffer. Used when editing the command line, evaluating
2159 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002160 */
2161 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002162text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002163{
2164#ifdef FEAT_CMDWIN
2165 if (cmdwin_type != 0)
2166 return TRUE;
2167#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002168 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002169}
2170
2171/*
2172 * Give an error message for a command that isn't allowed while the cmdline
2173 * window is open or editing the cmdline in another way.
2174 */
2175 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002176text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002177{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002178 EMSG(_(get_text_locked_msg()));
2179}
2180
2181 char_u *
2182get_text_locked_msg(void)
2183{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002184#ifdef FEAT_CMDWIN
2185 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002186 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002187#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002188 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002189}
2190
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002191#if defined(FEAT_AUTOCMD) || defined(PROTO)
2192/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002193 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2194 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002195 */
2196 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002197curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198{
2199 if (curbuf_lock > 0)
2200 {
2201 EMSG(_("E788: Not allowed to edit another buffer now"));
2202 return TRUE;
2203 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002204 return allbuf_locked();
2205}
2206
2207/*
2208 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2209 * message.
2210 */
2211 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002212allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002213{
2214 if (allbuf_lock > 0)
2215 {
2216 EMSG(_("E811: Not allowed to change buffer information now"));
2217 return TRUE;
2218 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219 return FALSE;
2220}
2221#endif
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002224cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2227 if (cmdline_star > 0) /* showing '*', always 1 position */
2228 return 1;
2229#endif
2230 return ptr2cells(ccline.cmdbuff + idx);
2231}
2232
2233/*
2234 * Compute the offset of the cursor on the command line for the prompt and
2235 * indent.
2236 */
2237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002238set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002240 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ccline.cmdspos = 1 + ccline.cmdindent;
2242 else
2243 ccline.cmdspos = 0 + ccline.cmdindent;
2244}
2245
2246/*
2247 * Compute the screen position for the cursor on the command line.
2248 */
2249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002250set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252 int i, m, c;
2253
2254 set_cmdspos();
2255 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002258 if (m < 0) /* overflow, Columns or Rows at weird value */
2259 m = MAXCOL;
2260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 else
2262 m = MAXCOL;
2263 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2264 {
2265 c = cmdline_charsize(i);
2266#ifdef FEAT_MBYTE
2267 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2268 if (has_mbyte)
2269 correct_cmdspos(i, c);
2270#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002271 /* If the cmdline doesn't fit, show cursor on last visible char.
2272 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if ((ccline.cmdspos += c) >= m)
2274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ccline.cmdspos -= c;
2276 break;
2277 }
2278#ifdef FEAT_MBYTE
2279 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002280 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#endif
2282 }
2283}
2284
2285#ifdef FEAT_MBYTE
2286/*
2287 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2288 * character that doesn't fit, so that a ">" must be displayed.
2289 */
2290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002291correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002293 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2295 && ccline.cmdspos % Columns + cells > Columns)
2296 ccline.cmdspos++;
2297}
2298#endif
2299
2300/*
2301 * Get an Ex command line for the ":" command.
2302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002304getexline(
2305 int c, /* normally ':', NUL for ":append" */
2306 void *cookie UNUSED,
2307 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 /* When executing a register, remove ':' that's in front of each line. */
2310 if (exec_from_reg && vpeekc() == ':')
2311 (void)vgetc();
2312 return getcmdline(c, 1L, indent);
2313}
2314
2315/*
2316 * Get an Ex command line for Ex mode.
2317 * In Ex mode we only use the OS supplied line editing features and no
2318 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002322getexmodeline(
2323 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002324 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002325 void *cookie UNUSED,
2326 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002328 garray_T line_ga;
2329 char_u *pend;
2330 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002331 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002332 int escaped = FALSE; /* CTRL-V typed */
2333 int vcol = 0;
2334 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002335 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002336 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 /* Switch cursor on now. This avoids that it happens after the "\n", which
2339 * confuses the system function that computes tabstops. */
2340 cursor_on();
2341
2342 /* always start in column 0; write a newline if necessary */
2343 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002344 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002346 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002348 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002349 if (p_prompt)
2350 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 while (indent-- > 0)
2352 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355
2356 ga_init2(&line_ga, 1, 30);
2357
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002358 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002359 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002360 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002361 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002362 while (indent >= 8)
2363 {
2364 ga_append(&line_ga, TAB);
2365 msg_puts((char_u *)" ");
2366 indent -= 8;
2367 }
2368 while (indent-- > 0)
2369 {
2370 ga_append(&line_ga, ' ');
2371 msg_putchar(' ');
2372 }
2373 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002374 ++no_mapping;
2375 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 /*
2378 * Get the line, one character at a time.
2379 */
2380 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002381 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002383 long sw;
2384 char_u *s;
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (ga_grow(&line_ga, 40) == FAIL)
2387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaarba748c82017-02-26 14:00:07 +01002389 /*
2390 * Get one character at a time.
2391 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002392 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002393
2394 /* Check for a ":normal" command and no more characters left. */
2395 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2396 c1 = '\n';
2397 else
2398 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002401 * Handle line editing.
2402 * Previously this was left to the system, putting the terminal in
2403 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002405 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002407 msg_putchar('\n');
2408 break;
2409 }
2410
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002411 if (c1 == K_PS)
2412 {
2413 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2414 goto redraw;
2415 }
2416
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002417 if (!escaped)
2418 {
2419 /* CR typed means "enter", which is NL */
2420 if (c1 == '\r')
2421 c1 = '\n';
2422
2423 if (c1 == BS || c1 == K_BS
2424 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002426 if (line_ga.ga_len > 0)
2427 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002428#ifdef FEAT_MBYTE
2429 if (has_mbyte)
2430 {
2431 p = (char_u *)line_ga.ga_data;
2432 p[line_ga.ga_len] = NUL;
2433 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2434 line_ga.ga_len -= len;
2435 }
2436 else
2437#endif
2438 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002439 goto redraw;
2440 }
2441 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002444 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002446 msg_col = startcol;
2447 msg_clr_eos();
2448 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002449 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002450 }
2451
2452 if (c1 == Ctrl_T)
2453 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002454 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002455 p = (char_u *)line_ga.ga_data;
2456 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002457 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002458 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002459add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002460 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002462 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002463 p = (char_u *)line_ga.ga_data;
2464 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002465 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2466 *s = ' ';
2467 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002469redraw:
2470 /* redraw the line */
2471 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002472 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002473 p = (char_u *)line_ga.ga_data;
2474 p[line_ga.ga_len] = NUL;
2475 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002477 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002479 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002481 msg_putchar(' ');
2482 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002483 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002487 len = MB_PTR2LEN(p);
2488 msg_outtrans_len(p, len);
2489 vcol += ptr2cells(p);
2490 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002493 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002494 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002495 continue;
2496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002498 if (c1 == Ctrl_D)
2499 {
2500 /* Delete one shiftwidth. */
2501 p = (char_u *)line_ga.ga_data;
2502 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002504 if (prev_char == '^')
2505 ex_keep_indent = TRUE;
2506 indent = 0;
2507 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
2509 else
2510 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002511 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002512 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002513 if (indent > 0)
2514 {
2515 --indent;
2516 indent -= indent % get_sw_value(curbuf);
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002519 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002520 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002521 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002522 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2523 --line_ga.ga_len;
2524 }
2525 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002527
2528 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2529 {
2530 escaped = TRUE;
2531 continue;
2532 }
2533
2534 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2535 if (IS_SPECIAL(c1))
2536 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002538
2539 if (IS_SPECIAL(c1))
2540 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002541#ifdef FEAT_MBYTE
2542 if (has_mbyte)
2543 len = (*mb_char2bytes)(c1,
2544 (char_u *)line_ga.ga_data + line_ga.ga_len);
2545 else
2546#endif
2547 {
2548 len = 1;
2549 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2550 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 if (c1 == '\n')
2552 msg_putchar('\n');
2553 else if (c1 == TAB)
2554 {
2555 /* Don't use chartabsize(), 'ts' can be different */
2556 do
2557 {
2558 msg_putchar(' ');
2559 } while (++vcol % 8);
2560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002564 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002565 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002567 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 escaped = FALSE;
2569
2570 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002571 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002572
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002573 /* We are done when a NL is entered, but not when it comes after an
2574 * odd number of backslashes, that results in a NUL. */
2575 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002577 int bcount = 0;
2578
2579 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2580 ++bcount;
2581
2582 if (bcount > 0)
2583 {
2584 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2585 * "\NL", etc. */
2586 line_ga.ga_len -= (bcount + 1) / 2;
2587 pend -= (bcount + 1) / 2;
2588 pend[-1] = '\n';
2589 }
2590
2591 if ((bcount & 1) == 0)
2592 {
2593 --line_ga.ga_len;
2594 --pend;
2595 *pend = NUL;
2596 break;
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599 }
2600
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002601 --no_mapping;
2602 --allow_keys;
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 /* make following messages go to the next line */
2605 msg_didout = FALSE;
2606 msg_col = 0;
2607 if (msg_row < Rows - 1)
2608 ++msg_row;
2609 emsg_on_display = FALSE; /* don't want ui_delay() */
2610
2611 if (got_int)
2612 ga_clear(&line_ga);
2613
2614 return (char_u *)line_ga.ga_data;
2615}
2616
Bram Moolenaare344bea2005-09-01 20:46:49 +00002617# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2618 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619/*
2620 * Return TRUE if ccline.overstrike is on.
2621 */
2622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002623cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 return ccline.overstrike;
2626}
2627
2628/*
2629 * Return TRUE if the cursor is at the end of the cmdline.
2630 */
2631 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002632cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 return (ccline.cmdpos >= ccline.cmdlen);
2635}
2636#endif
2637
Bram Moolenaar9372a112005-12-06 19:59:18 +00002638#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639/*
2640 * Return the virtual column number at the current cursor position.
2641 * This is used by the IM code to obtain the start of the preedit string.
2642 */
2643 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002644cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
2646 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2647 return MAXCOL;
2648
2649# ifdef FEAT_MBYTE
2650 if (has_mbyte)
2651 {
2652 colnr_T col;
2653 int i = 0;
2654
2655 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002656 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 return col;
2659 }
2660 else
2661# endif
2662 return ccline.cmdpos;
2663}
2664#endif
2665
2666#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2667/*
2668 * If part of the command line is an IM preedit string, redraw it with
2669 * IM feedback attributes. The cursor position is restored after drawing.
2670 */
2671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002672redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 if ((State & CMDLINE)
2675 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002676 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 && !p_imdisable
2678 && im_is_preediting())
2679 {
2680 int cmdpos = 0;
2681 int cmdspos;
2682 int old_row;
2683 int old_col;
2684 colnr_T col;
2685
2686 old_row = msg_row;
2687 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002688 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690# ifdef FEAT_MBYTE
2691 if (has_mbyte)
2692 {
2693 for (col = 0; col < preedit_start_col
2694 && cmdpos < ccline.cmdlen; ++col)
2695 {
2696 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002697 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 }
2700 else
2701# endif
2702 {
2703 cmdspos += preedit_start_col;
2704 cmdpos += preedit_start_col;
2705 }
2706
2707 msg_row = cmdline_row + (cmdspos / (int)Columns);
2708 msg_col = cmdspos % (int)Columns;
2709 if (msg_row >= Rows)
2710 msg_row = Rows - 1;
2711
2712 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2713 {
2714 int char_len;
2715 int char_attr;
2716
2717 char_attr = im_get_feedback_attr(col);
2718 if (char_attr < 0)
2719 break; /* end of preedit string */
2720
2721# ifdef FEAT_MBYTE
2722 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002723 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 else
2725# endif
2726 char_len = 1;
2727
2728 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2729 cmdpos += char_len;
2730 }
2731
2732 msg_row = old_row;
2733 msg_col = old_col;
2734 }
2735}
2736#endif /* FEAT_XIM && FEAT_GUI_GTK */
2737
2738/*
2739 * Allocate a new command line buffer.
2740 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2741 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2742 */
2743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002744alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 /*
2747 * give some extra space to avoid having to allocate all the time
2748 */
2749 if (len < 80)
2750 len = 100;
2751 else
2752 len += 20;
2753
2754 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2755 ccline.cmdbufflen = len;
2756}
2757
2758/*
2759 * Re-allocate the command line to length len + something extra.
2760 * return FAIL for failure, OK otherwise
2761 */
2762 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002763realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 char_u *p;
2766
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002767 if (len < ccline.cmdbufflen)
2768 return OK; /* no need to resize */
2769
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 p = ccline.cmdbuff;
2771 alloc_cmdbuff(len); /* will get some more */
2772 if (ccline.cmdbuff == NULL) /* out of memory */
2773 {
2774 ccline.cmdbuff = p; /* keep the old one */
2775 return FAIL;
2776 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002777 /* There isn't always a NUL after the command, but it may need to be
2778 * there, thus copy up to the NUL and add a NUL. */
2779 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2780 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002782
2783 if (ccline.xpc != NULL
2784 && ccline.xpc->xp_pattern != NULL
2785 && ccline.xpc->xp_context != EXPAND_NOTHING
2786 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2787 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002788 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002789
2790 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2791 * to point into the newly allocated memory. */
2792 if (i >= 0 && i <= ccline.cmdlen)
2793 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2794 }
2795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return OK;
2797}
2798
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002799#if defined(FEAT_ARABIC) || defined(PROTO)
2800static char_u *arshape_buf = NULL;
2801
2802# if defined(EXITFREE) || defined(PROTO)
2803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002804free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002805{
2806 vim_free(arshape_buf);
2807}
2808# endif
2809#endif
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811/*
2812 * Draw part of the cmdline at the current cursor position. But draw stars
2813 * when cmdline_star is TRUE.
2814 */
2815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002816draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
2818#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2819 int i;
2820
2821 if (cmdline_star > 0)
2822 for (i = 0; i < len; ++i)
2823 {
2824 msg_putchar('*');
2825# ifdef FEAT_MBYTE
2826 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002827 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828# endif
2829 }
2830 else
2831#endif
2832#ifdef FEAT_ARABIC
2833 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 static int buflen = 0;
2836 char_u *p;
2837 int j;
2838 int newlen = 0;
2839 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002840 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 int prev_c = 0;
2842 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002843 int u8c;
2844 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847 /*
2848 * Do arabic shaping into a temporary buffer. This is very
2849 * inefficient!
2850 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002851 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
2853 /* Re-allocate the buffer. We keep it around to avoid a lot of
2854 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002855 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002856 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002857 arshape_buf = alloc(buflen);
2858 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 return; /* out of memory */
2860 }
2861
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002862 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2863 {
2864 /* Prepend a space to draw the leading composing char on. */
2865 arshape_buf[0] = ' ';
2866 newlen = 1;
2867 }
2868
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 for (j = start; j < start + len; j += mb_l)
2870 {
2871 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002872 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002873 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (ARABIC_CHAR(u8c))
2875 {
2876 /* Do Arabic shaping. */
2877 if (cmdmsg_rl)
2878 {
2879 /* displaying from right to left */
2880 pc = prev_c;
2881 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002882 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (j + mb_l >= start + len)
2884 nc = NUL;
2885 else
2886 nc = utf_ptr2char(p + mb_l);
2887 }
2888 else
2889 {
2890 /* displaying from left to right */
2891 if (j + mb_l >= start + len)
2892 pc = NUL;
2893 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002894 {
2895 int pcc[MAX_MCO];
2896
2897 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002899 pc1 = pcc[0];
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 nc = prev_c;
2902 }
2903 prev_c = u8c;
2904
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002905 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002907 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002908 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002910 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2911 if (u8cc[1] != 0)
2912 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002913 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 }
2916 else
2917 {
2918 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002919 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 newlen += mb_l;
2921 }
2922 }
2923
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002924 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926 else
2927#endif
2928 msg_outtrans_len(ccline.cmdbuff + start, len);
2929}
2930
2931/*
2932 * Put a character on the command line. Shifts the following text to the
2933 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2934 * "c" must be printable (fit in one display cell)!
2935 */
2936 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002937putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
2939 if (cmd_silent)
2940 return;
2941 msg_no_more = TRUE;
2942 msg_putchar(c);
2943 if (shift)
2944 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2945 msg_no_more = FALSE;
2946 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002947 extra_char = c;
2948 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949}
2950
2951/*
2952 * Undo a putcmdline(c, FALSE).
2953 */
2954 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002955unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 if (cmd_silent)
2958 return;
2959 msg_no_more = TRUE;
2960 if (ccline.cmdlen == ccline.cmdpos)
2961 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002962#ifdef FEAT_MBYTE
2963 else if (has_mbyte)
2964 draw_cmdline(ccline.cmdpos,
2965 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
2968 draw_cmdline(ccline.cmdpos, 1);
2969 msg_no_more = FALSE;
2970 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002971 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
2974/*
2975 * Put the given string, of the given length, onto the command line.
2976 * If len is -1, then STRLEN() is used to calculate the length.
2977 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2978 * part will be redrawn, otherwise it will not. If this function is called
2979 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2980 * called afterwards.
2981 */
2982 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002983put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 int retval;
2986 int i;
2987 int m;
2988 int c;
2989
2990 if (len < 0)
2991 len = (int)STRLEN(str);
2992
2993 /* Check if ccline.cmdbuff needs to be longer */
2994 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002995 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else
2997 retval = OK;
2998 if (retval == OK)
2999 {
3000 if (!ccline.overstrike)
3001 {
3002 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3003 ccline.cmdbuff + ccline.cmdpos,
3004 (size_t)(ccline.cmdlen - ccline.cmdpos));
3005 ccline.cmdlen += len;
3006 }
3007 else
3008 {
3009#ifdef FEAT_MBYTE
3010 if (has_mbyte)
3011 {
3012 /* Count nr of characters in the new string. */
3013 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003014 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 ++m;
3016 /* Count nr of bytes in cmdline that are overwritten by these
3017 * characters. */
3018 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003019 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 --m;
3021 if (i < ccline.cmdlen)
3022 {
3023 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3024 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3025 ccline.cmdlen += ccline.cmdpos + len - i;
3026 }
3027 else
3028 ccline.cmdlen = ccline.cmdpos + len;
3029 }
3030 else
3031#endif
3032 if (ccline.cmdpos + len > ccline.cmdlen)
3033 ccline.cmdlen = ccline.cmdpos + len;
3034 }
3035 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3036 ccline.cmdbuff[ccline.cmdlen] = NUL;
3037
3038#ifdef FEAT_MBYTE
3039 if (enc_utf8)
3040 {
3041 /* When the inserted text starts with a composing character,
3042 * backup to the character before it. There could be two of them.
3043 */
3044 i = 0;
3045 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3046 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3047 {
3048 i = (*mb_head_off)(ccline.cmdbuff,
3049 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3050 ccline.cmdpos -= i;
3051 len += i;
3052 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3053 }
3054# ifdef FEAT_ARABIC
3055 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3056 {
3057 /* Check the previous character for Arabic combining pair. */
3058 i = (*mb_head_off)(ccline.cmdbuff,
3059 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3060 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3061 + ccline.cmdpos - i), c))
3062 {
3063 ccline.cmdpos -= i;
3064 len += i;
3065 }
3066 else
3067 i = 0;
3068 }
3069# endif
3070 if (i != 0)
3071 {
3072 /* Also backup the cursor position. */
3073 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3074 ccline.cmdspos -= i;
3075 msg_col -= i;
3076 if (msg_col < 0)
3077 {
3078 msg_col += Columns;
3079 --msg_row;
3080 }
3081 }
3082 }
3083#endif
3084
3085 if (redraw && !cmd_silent)
3086 {
3087 msg_no_more = TRUE;
3088 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003089 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3091 /* Avoid clearing the rest of the line too often. */
3092 if (cmdline_row != i || ccline.overstrike)
3093 msg_clr_eos();
3094 msg_no_more = FALSE;
3095 }
3096#ifdef FEAT_FKMAP
3097 /*
3098 * If we are in Farsi command mode, the character input must be in
3099 * Insert mode. So do not advance the cmdpos.
3100 */
3101 if (!cmd_fkmap)
3102#endif
3103 {
3104 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003107 if (m < 0) /* overflow, Columns or Rows at weird value */
3108 m = MAXCOL;
3109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else
3111 m = MAXCOL;
3112 for (i = 0; i < len; ++i)
3113 {
3114 c = cmdline_charsize(ccline.cmdpos);
3115#ifdef FEAT_MBYTE
3116 /* count ">" for a double-wide char that doesn't fit. */
3117 if (has_mbyte)
3118 correct_cmdspos(ccline.cmdpos, c);
3119#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003120 /* Stop cursor at the end of the screen, but do increment the
3121 * insert position, so that entering a very long command
3122 * works, even though you can't see it. */
3123 if (ccline.cmdspos + c < m)
3124 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#ifdef FEAT_MBYTE
3126 if (has_mbyte)
3127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003128 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (c > len - i - 1)
3130 c = len - i - 1;
3131 ccline.cmdpos += c;
3132 i += c;
3133 }
3134#endif
3135 ++ccline.cmdpos;
3136 }
3137 }
3138 }
3139 if (redraw)
3140 msg_check();
3141 return retval;
3142}
3143
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003144static struct cmdline_info prev_ccline;
3145static int prev_ccline_used = FALSE;
3146
3147/*
3148 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3149 * and overwrite it. But get_cmdline_str() may need it, thus make it
3150 * available globally in prev_ccline.
3151 */
3152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003153save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003154{
3155 if (!prev_ccline_used)
3156 {
3157 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3158 prev_ccline_used = TRUE;
3159 }
3160 *ccp = prev_ccline;
3161 prev_ccline = ccline;
3162 ccline.cmdbuff = NULL;
3163 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003164 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003165}
3166
3167/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003168 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003169 */
3170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003171restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003172{
3173 ccline = prev_ccline;
3174 prev_ccline = *ccp;
3175}
3176
Bram Moolenaar5a305422006-04-28 22:38:25 +00003177#if defined(FEAT_EVAL) || defined(PROTO)
3178/*
3179 * Save the command line into allocated memory. Returns a pointer to be
3180 * passed to restore_cmdline_alloc() later.
3181 * Returns NULL when failed.
3182 */
3183 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003184save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003185{
3186 struct cmdline_info *p;
3187
3188 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3189 if (p != NULL)
3190 save_cmdline(p);
3191 return (char_u *)p;
3192}
3193
3194/*
3195 * Restore the command line from the return value of save_cmdline_alloc().
3196 */
3197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003198restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003199{
3200 if (p != NULL)
3201 {
3202 restore_cmdline((struct cmdline_info *)p);
3203 vim_free(p);
3204 }
3205}
3206#endif
3207
Bram Moolenaar8299df92004-07-10 09:47:34 +00003208/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003209 * Paste a yank register into the command line.
3210 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003211 * insert_reg() can't be used here, because special characters from the
3212 * register contents will be interpreted as commands.
3213 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003214 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003215 */
3216 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003217cmdline_paste(
3218 int regname,
3219 int literally, /* Insert text literally instead of "as typed" */
3220 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003221{
3222 long i;
3223 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003224 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003225 int allocated;
3226 struct cmdline_info save_ccline;
3227
3228 /* check for valid regname; also accept special characters for CTRL-R in
3229 * the command line */
3230 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3231 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3232 return FAIL;
3233
3234 /* A register containing CTRL-R can cause an endless loop. Allow using
3235 * CTRL-C to break the loop. */
3236 line_breakcheck();
3237 if (got_int)
3238 return FAIL;
3239
3240#ifdef FEAT_CLIPBOARD
3241 regname = may_get_selection(regname);
3242#endif
3243
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003244 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003245 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003246 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003247 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003248 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003249 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003250 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003251
3252 if (i)
3253 {
3254 /* Got the value of a special register in "arg". */
3255 if (arg == NULL)
3256 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003257
3258 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3259 * part of the word. */
3260 p = arg;
3261 if (p_is && regname == Ctrl_W)
3262 {
3263 char_u *w;
3264 int len;
3265
3266 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003267 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003268 {
3269#ifdef FEAT_MBYTE
3270 if (has_mbyte)
3271 {
3272 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3273 if (!vim_iswordc(mb_ptr2char(w - len)))
3274 break;
3275 w -= len;
3276 }
3277 else
3278#endif
3279 {
3280 if (!vim_iswordc(w[-1]))
3281 break;
3282 --w;
3283 }
3284 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003285 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003286 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3287 p += len;
3288 }
3289
3290 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003291 if (allocated)
3292 vim_free(arg);
3293 return OK;
3294 }
3295
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003296 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003297}
3298
3299/*
3300 * Put a string on the command line.
3301 * When "literally" is TRUE, insert literally.
3302 * When "literally" is FALSE, insert as typed, but don't leave the command
3303 * line.
3304 */
3305 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003306cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003307{
3308 int c, cv;
3309
3310 if (literally)
3311 put_on_cmdline(s, -1, TRUE);
3312 else
3313 while (*s != NUL)
3314 {
3315 cv = *s;
3316 if (cv == Ctrl_V && s[1])
3317 ++s;
3318#ifdef FEAT_MBYTE
3319 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003320 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003321 else
3322#endif
3323 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003324 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3325 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003326#ifdef UNIX
3327 || c == intr_char
3328#endif
3329 || (c == Ctrl_BSL && *s == Ctrl_N))
3330 stuffcharReadbuff(Ctrl_V);
3331 stuffcharReadbuff(c);
3332 }
3333}
3334
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#ifdef FEAT_WILDMENU
3336/*
3337 * Delete characters on the command line, from "from" to the current
3338 * position.
3339 */
3340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003341cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3344 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3345 ccline.cmdlen -= ccline.cmdpos - from;
3346 ccline.cmdpos = from;
3347}
3348#endif
3349
3350/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003351 * This function is called when the screen size changes and with incremental
3352 * search and in other situations where the command line may have been
3353 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 */
3355 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003356redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003358 redrawcmdline_ex(TRUE);
3359}
3360
3361 void
3362redrawcmdline_ex(int do_compute_cmdrow)
3363{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (cmd_silent)
3365 return;
3366 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003367 if (do_compute_cmdrow)
3368 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 redrawcmd();
3370 cursorcmd();
3371}
3372
3373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003374redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 int i;
3377
3378 if (cmd_silent)
3379 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003380 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 msg_putchar(ccline.cmdfirstc);
3382 if (ccline.cmdprompt != NULL)
3383 {
3384 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3385 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3386 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003387 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 --ccline.cmdindent;
3389 }
3390 else
3391 for (i = ccline.cmdindent; i > 0; --i)
3392 msg_putchar(' ');
3393}
3394
3395/*
3396 * Redraw what is currently on the command line.
3397 */
3398 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003399redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 if (cmd_silent)
3402 return;
3403
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003404 /* when 'incsearch' is set there may be no command line while redrawing */
3405 if (ccline.cmdbuff == NULL)
3406 {
3407 windgoto(cmdline_row, 0);
3408 msg_clr_eos();
3409 return;
3410 }
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 msg_start();
3413 redrawcmdprompt();
3414
3415 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3416 msg_no_more = TRUE;
3417 draw_cmdline(0, ccline.cmdlen);
3418 msg_clr_eos();
3419 msg_no_more = FALSE;
3420
3421 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003422 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003423 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 /*
3426 * An emsg() before may have set msg_scroll. This is used in normal mode,
3427 * in cmdline mode we can reset them now.
3428 */
3429 msg_scroll = FALSE; /* next message overwrites cmdline */
3430
3431 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3432 * in cmdline mode */
3433 skip_redraw = FALSE;
3434}
3435
3436 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003437compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003439 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 cmdline_row = Rows - 1;
3441 else
3442 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3443 + W_STATUS_HEIGHT(lastwin);
3444}
3445
3446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 if (cmd_silent)
3450 return;
3451
3452#ifdef FEAT_RIGHTLEFT
3453 if (cmdmsg_rl)
3454 {
3455 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3456 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3457 if (msg_row <= 0)
3458 msg_row = Rows - 1;
3459 }
3460 else
3461#endif
3462 {
3463 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3464 msg_col = ccline.cmdspos % (int)Columns;
3465 if (msg_row >= Rows)
3466 msg_row = Rows - 1;
3467 }
3468
3469 windgoto(msg_row, msg_col);
3470#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003471 if (p_imst == IM_ON_THE_SPOT)
3472 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
3474#ifdef MCH_CURSOR_SHAPE
3475 mch_update_cursor();
3476#endif
3477}
3478
3479 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003480gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
3482 msg_start();
3483#ifdef FEAT_RIGHTLEFT
3484 if (cmdmsg_rl)
3485 msg_col = Columns - 1;
3486 else
3487#endif
3488 msg_col = 0; /* always start in column 0 */
3489 if (clr) /* clear the bottom line(s) */
3490 msg_clr_eos(); /* will reset clear_cmdline */
3491 windgoto(cmdline_row, 0);
3492}
3493
3494/*
3495 * Check the word in front of the cursor for an abbreviation.
3496 * Called when the non-id character "c" has been entered.
3497 * When an abbreviation is recognized it is removed from the text with
3498 * backspaces and the replacement string is inserted, followed by "c".
3499 */
3500 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003501ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3504 return FALSE;
3505
3506 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3507}
3508
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003509#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3510 static int
3511#ifdef __BORLANDC__
3512_RTLENTRYF
3513#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003514sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003515{
3516 char_u *p1 = *(char_u **)s1;
3517 char_u *p2 = *(char_u **)s2;
3518
3519 if (*p1 != '<' && *p2 == '<') return -1;
3520 if (*p1 == '<' && *p2 != '<') return 1;
3521 return STRCMP(p1, p2);
3522}
3523#endif
3524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525/*
3526 * Return FAIL if this is not an appropriate context in which to do
3527 * completion of anything, return OK if it is (even if there are no matches).
3528 * For the caller, this means that the character is just passed through like a
3529 * normal character (instead of being expanded). This allows :s/^I^D etc.
3530 */
3531 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003532nextwild(
3533 expand_T *xp,
3534 int type,
3535 int options, /* extra options for ExpandOne() */
3536 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
3538 int i, j;
3539 char_u *p1;
3540 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 int difflen;
3542 int v;
3543
3544 if (xp->xp_numfiles == -1)
3545 {
3546 set_expand_context(xp);
3547 cmd_showtail = expand_showtail(xp);
3548 }
3549
3550 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3551 {
3552 beep_flush();
3553 return OK; /* Something illegal on command line */
3554 }
3555 if (xp->xp_context == EXPAND_NOTHING)
3556 {
3557 /* Caller can use the character as a normal char instead */
3558 return FAIL;
3559 }
3560
3561 MSG_PUTS("..."); /* show that we are busy */
3562 out_flush();
3563
3564 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003565 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
3567 if (type == WILD_NEXT || type == WILD_PREV)
3568 {
3569 /*
3570 * Get next/previous match for a previous expanded pattern.
3571 */
3572 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3573 }
3574 else
3575 {
3576 /*
3577 * Translate string into pattern and expand it.
3578 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003579 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3580 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 p2 = NULL;
3582 else
3583 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003584 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003585 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3586 if (escape)
3587 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003588
3589 if (p_wic)
3590 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003591 p2 = ExpandOne(xp, p1,
3592 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003593 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003595 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (p2 != NULL && type == WILD_LONGEST)
3597 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003598 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (ccline.cmdbuff[i + j] == '*'
3600 || ccline.cmdbuff[i + j] == '?')
3601 break;
3602 if ((int)STRLEN(p2) < j)
3603 {
3604 vim_free(p2);
3605 p2 = NULL;
3606 }
3607 }
3608 }
3609 }
3610
3611 if (p2 != NULL && !got_int)
3612 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003613 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003614 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003616 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 xp->xp_pattern = ccline.cmdbuff + i;
3618 }
3619 else
3620 v = OK;
3621 if (v == OK)
3622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003623 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3624 &ccline.cmdbuff[ccline.cmdpos],
3625 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3626 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 ccline.cmdlen += difflen;
3628 ccline.cmdpos += difflen;
3629 }
3630 }
3631 vim_free(p2);
3632
3633 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003634 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 /* When expanding a ":map" command and no matches are found, assume that
3637 * the key is supposed to be inserted literally */
3638 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3639 return FAIL;
3640
3641 if (xp->xp_numfiles <= 0 && p2 == NULL)
3642 beep_flush();
3643 else if (xp->xp_numfiles == 1)
3644 /* free expanded pattern */
3645 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3646
3647 return OK;
3648}
3649
3650/*
3651 * Do wildcard expansion on the string 'str'.
3652 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003653 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 * Return NULL for failure.
3655 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003656 * "orig" is the originally expanded string, copied to allocated memory. It
3657 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3658 * WILD_PREV "orig" should be NULL.
3659 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003660 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3661 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 *
3663 * mode = WILD_FREE: just free previously expanded matches
3664 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3665 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3666 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3667 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3668 * mode = WILD_ALL: return all matches concatenated
3669 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003670 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 *
3672 * options = WILD_LIST_NOTFOUND: list entries without a match
3673 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3674 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3675 * options = WILD_NO_BEEP: Don't beep for multiple matches
3676 * options = WILD_ADD_SLASH: add a slash after directory names
3677 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3678 * options = WILD_SILENT: don't print warning messages
3679 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003680 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 *
3682 * The variables xp->xp_context and xp->xp_backslash must have been set!
3683 */
3684 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003685ExpandOne(
3686 expand_T *xp,
3687 char_u *str,
3688 char_u *orig, /* allocated copy of original of expanded string */
3689 int options,
3690 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
3692 char_u *ss = NULL;
3693 static int findex;
3694 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003695 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 int i;
3697 long_u len;
3698 int non_suf_match; /* number without matching suffix */
3699
3700 /*
3701 * first handle the case of using an old match
3702 */
3703 if (mode == WILD_NEXT || mode == WILD_PREV)
3704 {
3705 if (xp->xp_numfiles > 0)
3706 {
3707 if (mode == WILD_PREV)
3708 {
3709 if (findex == -1)
3710 findex = xp->xp_numfiles;
3711 --findex;
3712 }
3713 else /* mode == WILD_NEXT */
3714 ++findex;
3715
3716 /*
3717 * When wrapping around, return the original string, set findex to
3718 * -1.
3719 */
3720 if (findex < 0)
3721 {
3722 if (orig_save == NULL)
3723 findex = xp->xp_numfiles - 1;
3724 else
3725 findex = -1;
3726 }
3727 if (findex >= xp->xp_numfiles)
3728 {
3729 if (orig_save == NULL)
3730 findex = 0;
3731 else
3732 findex = -1;
3733 }
3734#ifdef FEAT_WILDMENU
3735 if (p_wmnu)
3736 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3737 findex, cmd_showtail);
3738#endif
3739 if (findex == -1)
3740 return vim_strsave(orig_save);
3741 return vim_strsave(xp->xp_files[findex]);
3742 }
3743 else
3744 return NULL;
3745 }
3746
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003747 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3749 {
3750 FreeWild(xp->xp_numfiles, xp->xp_files);
3751 xp->xp_numfiles = -1;
3752 vim_free(orig_save);
3753 orig_save = NULL;
3754 }
3755 findex = 0;
3756
3757 if (mode == WILD_FREE) /* only release file name */
3758 return NULL;
3759
3760 if (xp->xp_numfiles == -1)
3761 {
3762 vim_free(orig_save);
3763 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003764 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
3766 /*
3767 * Do the expansion.
3768 */
3769 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3770 options) == FAIL)
3771 {
3772#ifdef FNAME_ILLEGAL
3773 /* Illegal file name has been silently skipped. But when there
3774 * are wildcards, the real problem is that there was no match,
3775 * causing the pattern to be added, which has illegal characters.
3776 */
3777 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3778 EMSG2(_(e_nomatch2), str);
3779#endif
3780 }
3781 else if (xp->xp_numfiles == 0)
3782 {
3783 if (!(options & WILD_SILENT))
3784 EMSG2(_(e_nomatch2), str);
3785 }
3786 else
3787 {
3788 /* Escape the matches for use on the command line. */
3789 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3790
3791 /*
3792 * Check for matching suffixes in file names.
3793 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003794 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3795 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797 if (xp->xp_numfiles)
3798 non_suf_match = xp->xp_numfiles;
3799 else
3800 non_suf_match = 1;
3801 if ((xp->xp_context == EXPAND_FILES
3802 || xp->xp_context == EXPAND_DIRECTORIES)
3803 && xp->xp_numfiles > 1)
3804 {
3805 /*
3806 * More than one match; check suffix.
3807 * The files will have been sorted on matching suffix in
3808 * expand_wildcards, only need to check the first two.
3809 */
3810 non_suf_match = 0;
3811 for (i = 0; i < 2; ++i)
3812 if (match_suffix(xp->xp_files[i]))
3813 ++non_suf_match;
3814 }
3815 if (non_suf_match != 1)
3816 {
3817 /* Can we ever get here unless it's while expanding
3818 * interactively? If not, we can get rid of this all
3819 * together. Don't really want to wait for this message
3820 * (and possibly have to hit return to continue!).
3821 */
3822 if (!(options & WILD_SILENT))
3823 EMSG(_(e_toomany));
3824 else if (!(options & WILD_NO_BEEP))
3825 beep_flush();
3826 }
3827 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3828 ss = vim_strsave(xp->xp_files[0]);
3829 }
3830 }
3831 }
3832
3833 /* Find longest common part */
3834 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3835 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003836 int mb_len = 1;
3837 int c0, ci;
3838
3839 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003841#ifdef FEAT_MBYTE
3842 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003844 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3845 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3846 }
3847 else
3848#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003849 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003850 for (i = 1; i < xp->xp_numfiles; ++i)
3851 {
3852#ifdef FEAT_MBYTE
3853 if (has_mbyte)
3854 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3855 else
3856#endif
3857 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003858 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003860 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003861 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003863 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 break;
3865 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003866 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 break;
3868 }
3869 if (i < xp->xp_numfiles)
3870 {
3871 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003872 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 break;
3874 }
3875 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 ss = alloc((unsigned)len + 1);
3878 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003879 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 findex = -1; /* next p_wc gets first one */
3881 }
3882
3883 /* Concatenate all matching names */
3884 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3885 {
3886 len = 0;
3887 for (i = 0; i < xp->xp_numfiles; ++i)
3888 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3889 ss = lalloc(len, TRUE);
3890 if (ss != NULL)
3891 {
3892 *ss = NUL;
3893 for (i = 0; i < xp->xp_numfiles; ++i)
3894 {
3895 STRCAT(ss, xp->xp_files[i]);
3896 if (i != xp->xp_numfiles - 1)
3897 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3898 }
3899 }
3900 }
3901
3902 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3903 ExpandCleanup(xp);
3904
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003905 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003906 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003907 vim_free(orig);
3908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return ss;
3910}
3911
3912/*
3913 * Prepare an expand structure for use.
3914 */
3915 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003916ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003918 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003919 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003921#ifndef BACKSLASH_IN_FILENAME
3922 xp->xp_shell = FALSE;
3923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 xp->xp_numfiles = -1;
3925 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003926#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3927 xp->xp_arg = NULL;
3928#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003929 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930}
3931
3932/*
3933 * Cleanup an expand structure after use.
3934 */
3935 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003936ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 if (xp->xp_numfiles >= 0)
3939 {
3940 FreeWild(xp->xp_numfiles, xp->xp_files);
3941 xp->xp_numfiles = -1;
3942 }
3943}
3944
3945 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003946ExpandEscape(
3947 expand_T *xp,
3948 char_u *str,
3949 int numfiles,
3950 char_u **files,
3951 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
3953 int i;
3954 char_u *p;
3955
3956 /*
3957 * May change home directory back to "~"
3958 */
3959 if (options & WILD_HOME_REPLACE)
3960 tilde_replace(str, numfiles, files);
3961
3962 if (options & WILD_ESCAPE)
3963 {
3964 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003965 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003966 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 || xp->xp_context == EXPAND_BUFFERS
3968 || xp->xp_context == EXPAND_DIRECTORIES)
3969 {
3970 /*
3971 * Insert a backslash into a file name before a space, \, %, #
3972 * and wildmatch characters, except '~'.
3973 */
3974 for (i = 0; i < numfiles; ++i)
3975 {
3976 /* for ":set path=" we need to escape spaces twice */
3977 if (xp->xp_backslash == XP_BS_THREE)
3978 {
3979 p = vim_strsave_escaped(files[i], (char_u *)" ");
3980 if (p != NULL)
3981 {
3982 vim_free(files[i]);
3983 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003984#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 p = vim_strsave_escaped(files[i], (char_u *)" ");
3986 if (p != NULL)
3987 {
3988 vim_free(files[i]);
3989 files[i] = p;
3990 }
3991#endif
3992 }
3993 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003994#ifdef BACKSLASH_IN_FILENAME
3995 p = vim_strsave_fnameescape(files[i], FALSE);
3996#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003997 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (p != NULL)
4000 {
4001 vim_free(files[i]);
4002 files[i] = p;
4003 }
4004
4005 /* If 'str' starts with "\~", replace "~" at start of
4006 * files[i] with "\~". */
4007 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004008 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004011
4012 /* If the first file starts with a '+' escape it. Otherwise it
4013 * could be seen as "+cmd". */
4014 if (*files[0] == '+')
4015 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 else if (xp->xp_context == EXPAND_TAGS)
4018 {
4019 /*
4020 * Insert a backslash before characters in a tag name that
4021 * would terminate the ":tag" command.
4022 */
4023 for (i = 0; i < numfiles; ++i)
4024 {
4025 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4026 if (p != NULL)
4027 {
4028 vim_free(files[i]);
4029 files[i] = p;
4030 }
4031 }
4032 }
4033 }
4034}
4035
4036/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004037 * Escape special characters in "fname" for when used as a file name argument
4038 * after a Vim command, or, when "shell" is non-zero, a shell command.
4039 * Returns the result in allocated memory.
4040 */
4041 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004042vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004043{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004044 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004045#ifdef BACKSLASH_IN_FILENAME
4046 char_u buf[20];
4047 int j = 0;
4048
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004049 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004050 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004051 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004052 buf[j++] = *p;
4053 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004054 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004055#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004056 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4057 if (shell && csh_like_shell() && p != NULL)
4058 {
4059 char_u *s;
4060
4061 /* For csh and similar shells need to put two backslashes before '!'.
4062 * One is taken by Vim, one by the shell. */
4063 s = vim_strsave_escaped(p, (char_u *)"!");
4064 vim_free(p);
4065 p = s;
4066 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004067#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004068
4069 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4070 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004071 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004072 escape_fname(&p);
4073
4074 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004075}
4076
4077/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004078 * Put a backslash before the file name in "pp", which is in allocated memory.
4079 */
4080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004081escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004082{
4083 char_u *p;
4084
4085 p = alloc((unsigned)(STRLEN(*pp) + 2));
4086 if (p != NULL)
4087 {
4088 p[0] = '\\';
4089 STRCPY(p + 1, *pp);
4090 vim_free(*pp);
4091 *pp = p;
4092 }
4093}
4094
4095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 * For each file name in files[num_files]:
4097 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4098 */
4099 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004100tilde_replace(
4101 char_u *orig_pat,
4102 int num_files,
4103 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 int i;
4106 char_u *p;
4107
4108 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4109 {
4110 for (i = 0; i < num_files; ++i)
4111 {
4112 p = home_replace_save(NULL, files[i]);
4113 if (p != NULL)
4114 {
4115 vim_free(files[i]);
4116 files[i] = p;
4117 }
4118 }
4119 }
4120}
4121
4122/*
4123 * Show all matches for completion on the command line.
4124 * Returns EXPAND_NOTHING when the character that triggered expansion should
4125 * be inserted like a normal character.
4126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004128showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4131 int num_files;
4132 char_u **files_found;
4133 int i, j, k;
4134 int maxlen;
4135 int lines;
4136 int columns;
4137 char_u *p;
4138 int lastlen;
4139 int attr;
4140 int showtail;
4141
4142 if (xp->xp_numfiles == -1)
4143 {
4144 set_expand_context(xp);
4145 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4146 &num_files, &files_found);
4147 showtail = expand_showtail(xp);
4148 if (i != EXPAND_OK)
4149 return i;
4150
4151 }
4152 else
4153 {
4154 num_files = xp->xp_numfiles;
4155 files_found = xp->xp_files;
4156 showtail = cmd_showtail;
4157 }
4158
4159#ifdef FEAT_WILDMENU
4160 if (!wildmenu)
4161 {
4162#endif
4163 msg_didany = FALSE; /* lines_left will be set */
4164 msg_start(); /* prepare for paging */
4165 msg_putchar('\n');
4166 out_flush();
4167 cmdline_row = msg_row;
4168 msg_didany = FALSE; /* lines_left will be set again */
4169 msg_start(); /* prepare for paging */
4170#ifdef FEAT_WILDMENU
4171 }
4172#endif
4173
4174 if (got_int)
4175 got_int = FALSE; /* only int. the completion, not the cmd line */
4176#ifdef FEAT_WILDMENU
4177 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004178 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179#endif
4180 else
4181 {
4182 /* find the length of the longest file name */
4183 maxlen = 0;
4184 for (i = 0; i < num_files; ++i)
4185 {
4186 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004187 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 || xp->xp_context == EXPAND_BUFFERS))
4189 {
4190 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4191 j = vim_strsize(NameBuff);
4192 }
4193 else
4194 j = vim_strsize(L_SHOWFILE(i));
4195 if (j > maxlen)
4196 maxlen = j;
4197 }
4198
4199 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4200 lines = num_files;
4201 else
4202 {
4203 /* compute the number of columns and lines for the listing */
4204 maxlen += 2; /* two spaces between file names */
4205 columns = ((int)Columns + 2) / maxlen;
4206 if (columns < 1)
4207 columns = 1;
4208 lines = (num_files + columns - 1) / columns;
4209 }
4210
Bram Moolenaar8820b482017-03-16 17:23:31 +01004211 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4214 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004215 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 msg_clr_eos();
4217 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004218 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220
4221 /* list the files line by line */
4222 for (i = 0; i < lines; ++i)
4223 {
4224 lastlen = 999;
4225 for (k = i; k < num_files; k += lines)
4226 {
4227 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4228 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004229 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 p = files_found[k] + STRLEN(files_found[k]) + 1;
4231 msg_advance(maxlen + 1);
4232 msg_puts(p);
4233 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004234 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 break;
4236 }
4237 for (j = maxlen - lastlen; --j >= 0; )
4238 msg_putchar(' ');
4239 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004240 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 || xp->xp_context == EXPAND_BUFFERS)
4242 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004243 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004244 if (xp->xp_numfiles != -1)
4245 {
4246 char_u *halved_slash;
4247 char_u *exp_path;
4248
4249 /* Expansion was done before and special characters
4250 * were escaped, need to halve backslashes. Also
4251 * $HOME has been replaced with ~/. */
4252 exp_path = expand_env_save_opt(files_found[k], TRUE);
4253 halved_slash = backslash_halve_save(
4254 exp_path != NULL ? exp_path : files_found[k]);
4255 j = mch_isdir(halved_slash != NULL ? halved_slash
4256 : files_found[k]);
4257 vim_free(exp_path);
4258 vim_free(halved_slash);
4259 }
4260 else
4261 /* Expansion was done here, file names are literal. */
4262 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (showtail)
4264 p = L_SHOWFILE(k);
4265 else
4266 {
4267 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4268 TRUE);
4269 p = NameBuff;
4270 }
4271 }
4272 else
4273 {
4274 j = FALSE;
4275 p = L_SHOWFILE(k);
4276 }
4277 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4278 }
4279 if (msg_col > 0) /* when not wrapped around */
4280 {
4281 msg_clr_eos();
4282 msg_putchar('\n');
4283 }
4284 out_flush(); /* show one line at a time */
4285 if (got_int)
4286 {
4287 got_int = FALSE;
4288 break;
4289 }
4290 }
4291
4292 /*
4293 * we redraw the command below the lines that we have just listed
4294 * This is a bit tricky, but it saves a lot of screen updating.
4295 */
4296 cmdline_row = msg_row; /* will put it back later */
4297 }
4298
4299 if (xp->xp_numfiles == -1)
4300 FreeWild(num_files, files_found);
4301
4302 return EXPAND_OK;
4303}
4304
4305/*
4306 * Private gettail for showmatches() (and win_redr_status_matches()):
4307 * Find tail of file name path, but ignore trailing "/".
4308 */
4309 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004310sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 char_u *p;
4313 char_u *t = s;
4314 int had_sep = FALSE;
4315
4316 for (p = s; *p != NUL; )
4317 {
4318 if (vim_ispathsep(*p)
4319#ifdef BACKSLASH_IN_FILENAME
4320 && !rem_backslash(p)
4321#endif
4322 )
4323 had_sep = TRUE;
4324 else if (had_sep)
4325 {
4326 t = p;
4327 had_sep = FALSE;
4328 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004329 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 return t;
4332}
4333
4334/*
4335 * Return TRUE if we only need to show the tail of completion matches.
4336 * When not completing file names or there is a wildcard in the path FALSE is
4337 * returned.
4338 */
4339 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004340expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
4342 char_u *s;
4343 char_u *end;
4344
4345 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004346 if (xp->xp_context != EXPAND_FILES
4347 && xp->xp_context != EXPAND_SHELLCMD
4348 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 return FALSE;
4350
4351 end = gettail(xp->xp_pattern);
4352 if (end == xp->xp_pattern) /* there is no path separator */
4353 return FALSE;
4354
4355 for (s = xp->xp_pattern; s < end; s++)
4356 {
4357 /* Skip escaped wildcards. Only when the backslash is not a path
4358 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4359 if (rem_backslash(s))
4360 ++s;
4361 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4362 return FALSE;
4363 }
4364 return TRUE;
4365}
4366
4367/*
4368 * Prepare a string for expansion.
4369 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004370 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 * When expanding other names: The string will be used with regcomp(). Copy
4372 * the name into allocated memory and prepend "^".
4373 */
4374 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004375addstar(
4376 char_u *fname,
4377 int len,
4378 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
4380 char_u *retval;
4381 int i, j;
4382 int new_len;
4383 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004384 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004386 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004387 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004388 && context != EXPAND_SHELLCMD
4389 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
4391 /*
4392 * Matching will be done internally (on something other than files).
4393 * So we convert the file-matching-type wildcards into our kind for
4394 * use with vim_regcomp(). First work out how long it will be:
4395 */
4396
4397 /* For help tags the translation is done in find_help_tags().
4398 * For a tag pattern starting with "/" no translation is needed. */
4399 if (context == EXPAND_HELP
4400 || context == EXPAND_COLORS
4401 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004402 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004403 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004404 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004405 || ((context == EXPAND_TAGS_LISTFILES
4406 || context == EXPAND_TAGS)
4407 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 retval = vim_strnsave(fname, len);
4409 else
4410 {
4411 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4412 for (i = 0; i < len; i++)
4413 {
4414 if (fname[i] == '*' || fname[i] == '~')
4415 new_len++; /* '*' needs to be replaced by ".*"
4416 '~' needs to be replaced by "\~" */
4417
4418 /* Buffer names are like file names. "." should be literal */
4419 if (context == EXPAND_BUFFERS && fname[i] == '.')
4420 new_len++; /* "." becomes "\." */
4421
4422 /* Custom expansion takes care of special things, match
4423 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004424 if ((context == EXPAND_USER_DEFINED
4425 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 new_len++; /* '\' becomes "\\" */
4427 }
4428 retval = alloc(new_len);
4429 if (retval != NULL)
4430 {
4431 retval[0] = '^';
4432 j = 1;
4433 for (i = 0; i < len; i++, j++)
4434 {
4435 /* Skip backslash. But why? At least keep it for custom
4436 * expansion. */
4437 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004438 && context != EXPAND_USER_LIST
4439 && fname[i] == '\\'
4440 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 break;
4442
4443 switch (fname[i])
4444 {
4445 case '*': retval[j++] = '.';
4446 break;
4447 case '~': retval[j++] = '\\';
4448 break;
4449 case '?': retval[j] = '.';
4450 continue;
4451 case '.': if (context == EXPAND_BUFFERS)
4452 retval[j++] = '\\';
4453 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004454 case '\\': if (context == EXPAND_USER_DEFINED
4455 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 retval[j++] = '\\';
4457 break;
4458 }
4459 retval[j] = fname[i];
4460 }
4461 retval[j] = NUL;
4462 }
4463 }
4464 }
4465 else
4466 {
4467 retval = alloc(len + 4);
4468 if (retval != NULL)
4469 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004470 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004473 * Don't add a star to *, ~, ~user, $var or `cmd`.
4474 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 * ~ would be at the start of the file name, but not the tail.
4476 * $ could be anywhere in the tail.
4477 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004478 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 */
4480 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004481 ends_in_star = (len > 0 && retval[len - 1] == '*');
4482#ifndef BACKSLASH_IN_FILENAME
4483 for (i = len - 2; i >= 0; --i)
4484 {
4485 if (retval[i] != '\\')
4486 break;
4487 ends_in_star = !ends_in_star;
4488 }
4489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004491 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 && vim_strchr(tail, '$') == NULL
4493 && vim_strchr(retval, '`') == NULL)
4494 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004495 else if (len > 0 && retval[len - 1] == '$')
4496 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 retval[len] = NUL;
4498 }
4499 }
4500 return retval;
4501}
4502
4503/*
4504 * Must parse the command line so far to work out what context we are in.
4505 * Completion can then be done based on that context.
4506 * This routine sets the variables:
4507 * xp->xp_pattern The start of the pattern to be expanded within
4508 * the command line (ends at the cursor).
4509 * xp->xp_context The type of thing to expand. Will be one of:
4510 *
4511 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4512 * the command line, like an unknown command. Caller
4513 * should beep.
4514 * EXPAND_NOTHING Unrecognised context for completion, use char like
4515 * a normal char, rather than for completion. eg
4516 * :s/^I/
4517 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4518 * it.
4519 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4520 * EXPAND_FILES After command with XFILE set, or after setting
4521 * with P_EXPAND set. eg :e ^I, :w>>^I
4522 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4523 * when we know only directories are of interest. eg
4524 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004525 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4527 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4528 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4529 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4530 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4531 * EXPAND_EVENTS Complete event names
4532 * EXPAND_SYNTAX Complete :syntax command arguments
4533 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4534 * EXPAND_AUGROUP Complete autocommand group names
4535 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4536 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4537 * eg :unmap a^I , :cunab x^I
4538 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4539 * eg :call sub^I
4540 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4541 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4542 * names in expressions, eg :while s^I
4543 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004544 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
4546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004547set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004549 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (ccline.cmdfirstc != ':'
4551#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004552 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004553 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#endif
4555 )
4556 {
4557 xp->xp_context = EXPAND_NOTHING;
4558 return;
4559 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004560 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561}
4562
4563 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004564set_cmd_context(
4565 expand_T *xp,
4566 char_u *str, /* start of command line */
4567 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004568 int col, /* position of cursor */
4569 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
4571 int old_char = NUL;
4572 char_u *nextcomm;
4573
4574 /*
4575 * Avoid a UMR warning from Purify, only save the character if it has been
4576 * written before.
4577 */
4578 if (col < len)
4579 old_char = str[col];
4580 str[col] = NUL;
4581 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004582
4583#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004584 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004585 {
4586# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004587 /* pass CMD_SIZE because there is no real command */
4588 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004589# endif
4590 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004591 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004592 {
4593 xp->xp_context = ccline.xp_context;
4594 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004595# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004596 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004597# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004598 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004599 else
4600#endif
4601 while (nextcomm != NULL)
4602 nextcomm = set_one_cmd_context(xp, nextcomm);
4603
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004604 /* Store the string here so that call_user_expand_func() can get to them
4605 * easily. */
4606 xp->xp_line = str;
4607 xp->xp_col = col;
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 str[col] = old_char;
4610}
4611
4612/*
4613 * Expand the command line "str" from context "xp".
4614 * "xp" must have been set by set_cmd_context().
4615 * xp->xp_pattern points into "str", to where the text that is to be expanded
4616 * starts.
4617 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4618 * cursor.
4619 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4620 * key that triggered expansion literally.
4621 * Returns EXPAND_OK otherwise.
4622 */
4623 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004624expand_cmdline(
4625 expand_T *xp,
4626 char_u *str, /* start of command line */
4627 int col, /* position of cursor */
4628 int *matchcount, /* return: nr of matches */
4629 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004632 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4635 {
4636 beep_flush();
4637 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4638 }
4639 if (xp->xp_context == EXPAND_NOTHING)
4640 {
4641 /* Caller can use the character as a normal char instead */
4642 return EXPAND_NOTHING;
4643 }
4644
4645 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004646 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4647 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (file_str == NULL)
4649 return EXPAND_UNSUCCESSFUL;
4650
Bram Moolenaar94950a92010-12-02 16:01:29 +01004651 if (p_wic)
4652 options += WILD_ICASE;
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004655 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
4657 *matchcount = 0;
4658 *matches = NULL;
4659 }
4660 vim_free(file_str);
4661
4662 return EXPAND_OK;
4663}
4664
4665#ifdef FEAT_MULTI_LANG
4666/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004667 * Cleanup matches for help tags:
4668 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4669 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004671static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004674cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 int i, j;
4677 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004678 char_u buf[4];
4679 char_u *p = buf;
4680
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004681 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004682 {
4683 *p++ = '@';
4684 *p++ = p_hlg[0];
4685 *p++ = p_hlg[1];
4686 }
4687 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
4689 for (i = 0; i < num_file; ++i)
4690 {
4691 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004692 if (len <= 0)
4693 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004694 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
4696 /* Sorting on priority means the same item in another language may
4697 * be anywhere. Search all items for a match up to the "@en". */
4698 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004699 if (j != i && (int)STRLEN(file[j]) == len + 3
4700 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 break;
4702 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004703 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 file[i][len] = NUL;
4705 }
4706 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004707
4708 if (*buf != NUL)
4709 for (i = 0; i < num_file; ++i)
4710 {
4711 len = (int)STRLEN(file[i]) - 3;
4712 if (len <= 0)
4713 continue;
4714 if (STRCMP(file[i] + len, buf) == 0)
4715 {
4716 /* remove the default language */
4717 file[i][len] = NUL;
4718 }
4719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721#endif
4722
4723/*
4724 * Do the expansion based on xp->xp_context and "pat".
4725 */
4726 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004727ExpandFromContext(
4728 expand_T *xp,
4729 char_u *pat,
4730 int *num_file,
4731 char_u ***file,
4732 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734#ifdef FEAT_CMDL_COMPL
4735 regmatch_T regmatch;
4736#endif
4737 int ret;
4738 int flags;
4739
4740 flags = EW_DIR; /* include directories */
4741 if (options & WILD_LIST_NOTFOUND)
4742 flags |= EW_NOTFOUND;
4743 if (options & WILD_ADD_SLASH)
4744 flags |= EW_ADDSLASH;
4745 if (options & WILD_KEEP_ALL)
4746 flags |= EW_KEEPALL;
4747 if (options & WILD_SILENT)
4748 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004749 if (options & WILD_ALLLINKS)
4750 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004752 if (xp->xp_context == EXPAND_FILES
4753 || xp->xp_context == EXPAND_DIRECTORIES
4754 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 /*
4757 * Expand file or directory names.
4758 */
4759 int free_pat = FALSE;
4760 int i;
4761
4762 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4763 * space */
4764 if (xp->xp_backslash != XP_BS_NONE)
4765 {
4766 free_pat = TRUE;
4767 pat = vim_strsave(pat);
4768 for (i = 0; pat[i]; ++i)
4769 if (pat[i] == '\\')
4770 {
4771 if (xp->xp_backslash == XP_BS_THREE
4772 && pat[i + 1] == '\\'
4773 && pat[i + 2] == '\\'
4774 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004775 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (xp->xp_backslash == XP_BS_ONE
4777 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004778 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 }
4781
4782 if (xp->xp_context == EXPAND_FILES)
4783 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004784 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4785 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
4787 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004788 if (options & WILD_ICASE)
4789 flags |= EW_ICASE;
4790
Bram Moolenaard7834d32009-12-02 16:14:36 +00004791 /* Expand wildcards, supporting %:h and the like. */
4792 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (free_pat)
4794 vim_free(pat);
4795 return ret;
4796 }
4797
4798 *file = (char_u **)"";
4799 *num_file = 0;
4800 if (xp->xp_context == EXPAND_HELP)
4801 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004802 /* With an empty argument we would get all the help tags, which is
4803 * very slow. Get matches for "help" instead. */
4804 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4805 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
4807#ifdef FEAT_MULTI_LANG
4808 cleanup_help_tags(*num_file, *file);
4809#endif
4810 return OK;
4811 }
4812 return FAIL;
4813 }
4814
4815#ifndef FEAT_CMDL_COMPL
4816 return FAIL;
4817#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004818 if (xp->xp_context == EXPAND_SHELLCMD)
4819 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (xp->xp_context == EXPAND_OLD_SETTING)
4821 return ExpandOldSetting(num_file, file);
4822 if (xp->xp_context == EXPAND_BUFFERS)
4823 return ExpandBufnames(pat, num_file, file, options);
4824 if (xp->xp_context == EXPAND_TAGS
4825 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4826 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4827 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004828 {
4829 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004830 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4831 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004834 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004835 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004836 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004837 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004838 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004839 {
4840 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004841 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004842 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004843 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004844 {
4845 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004846 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004847 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004848# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4849 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004850 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004851# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004852 if (xp->xp_context == EXPAND_PACKADD)
4853 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4856 if (regmatch.regprog == NULL)
4857 return FAIL;
4858
4859 /* set ignore-case according to p_ic, p_scs and pat */
4860 regmatch.rm_ic = ignorecase(pat);
4861
4862 if (xp->xp_context == EXPAND_SETTINGS
4863 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4864 ret = ExpandSettings(xp, &regmatch, num_file, file);
4865 else if (xp->xp_context == EXPAND_MAPPINGS)
4866 ret = ExpandMappings(&regmatch, num_file, file);
4867# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4868 else if (xp->xp_context == EXPAND_USER_DEFINED)
4869 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4870# endif
4871 else
4872 {
4873 static struct expgen
4874 {
4875 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004876 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004878 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 } tab[] =
4880 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004881 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4882 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004883 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004884 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004885#ifdef FEAT_CMDHIST
4886 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004889 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004890 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004891 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4892 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4893 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#endif
4895#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004896 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4897 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4898 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4899 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900#endif
4901#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004902 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4903 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#endif
4905#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004906 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004908#ifdef FEAT_PROFILE
4909 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4910#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004911 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004913 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4914 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004916#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004917 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004918#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004919#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004920 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004921#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004922#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004923 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4926 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004927 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4928 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004930 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004931 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 };
4933 int i;
4934
4935 /*
4936 * Find a context in the table and call the ExpandGeneric() with the
4937 * right function to do the expansion.
4938 */
4939 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004940 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (xp->xp_context == tab[i].context)
4942 {
4943 if (tab[i].ic)
4944 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004945 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004946 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 break;
4948 }
4949 }
4950
Bram Moolenaar473de612013-06-08 18:19:48 +02004951 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 return ret;
4954#endif /* FEAT_CMDL_COMPL */
4955}
4956
4957#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4958/*
4959 * Expand a list of names.
4960 *
4961 * Generic function for command line completion. It calls a function to
4962 * obtain strings, one by one. The strings are matched against a regexp
4963 * program. Matching strings are copied into an array, which is returned.
4964 *
4965 * Returns OK when no problems encountered, FAIL for error (out of memory).
4966 */
4967 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004968ExpandGeneric(
4969 expand_T *xp,
4970 regmatch_T *regmatch,
4971 int *num_file,
4972 char_u ***file,
4973 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004975 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
4977 int i;
4978 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004979 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 char_u *str;
4981
4982 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004983 * round == 0: count the number of matching names
4984 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004986 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
4988 for (i = 0; ; ++i)
4989 {
4990 str = (*func)(xp, i);
4991 if (str == NULL) /* end of list */
4992 break;
4993 if (*str == NUL) /* skip empty strings */
4994 continue;
4995
4996 if (vim_regexec(regmatch, str, (colnr_T)0))
4997 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004998 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005000 if (escaped)
5001 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5002 else
5003 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 (*file)[count] = str;
5005#ifdef FEAT_MENU
5006 if (func == get_menu_names && str != NULL)
5007 {
5008 /* test for separator added by get_menu_names() */
5009 str += STRLEN(str) - 1;
5010 if (*str == '\001')
5011 *str = '.';
5012 }
5013#endif
5014 }
5015 ++count;
5016 }
5017 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005018 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 if (count == 0)
5021 return OK;
5022 *num_file = count;
5023 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5024 if (*file == NULL)
5025 {
5026 *file = (char_u **)"";
5027 return FAIL;
5028 }
5029 count = 0;
5030 }
5031 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005032
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005033 /* Sort the results. Keep menu's in the specified order. */
5034 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005035 {
5036 if (xp->xp_context == EXPAND_EXPRESSION
5037 || xp->xp_context == EXPAND_FUNCTIONS
5038 || xp->xp_context == EXPAND_USER_FUNC)
5039 /* <SNR> functions should be sorted to the end. */
5040 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5041 sort_func_compare);
5042 else
5043 sort_strings(*file, *num_file);
5044 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005045
Bram Moolenaar4f688582007-07-24 12:34:30 +00005046#ifdef FEAT_CMDL_COMPL
5047 /* Reset the variables used for special highlight names expansion, so that
5048 * they don't show up when getting normal highlight names by ID. */
5049 reset_expand_highlight();
5050#endif
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 return OK;
5053}
5054
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005055/*
5056 * Complete a shell command.
5057 * Returns FAIL or OK;
5058 */
5059 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005060expand_shellcmd(
5061 char_u *filepat, /* pattern to match with command names */
5062 int *num_file, /* return: number of matches */
5063 char_u ***file, /* return: array with matches */
5064 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005065{
5066 char_u *pat;
5067 int i;
5068 char_u *path;
5069 int mustfree = FALSE;
5070 garray_T ga;
5071 char_u *buf = alloc(MAXPATHL);
5072 size_t l;
5073 char_u *s, *e;
5074 int flags = flagsarg;
5075 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005076 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005077
5078 if (buf == NULL)
5079 return FAIL;
5080
5081 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5082 * space */
5083 pat = vim_strsave(filepat);
5084 for (i = 0; pat[i]; ++i)
5085 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005086 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005087
Bram Moolenaarb5971142015-03-21 17:32:19 +01005088 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005089
5090 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005091 if (mch_isFullName(pat))
5092 path = (char_u *)" ";
5093 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005094 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5095 path = (char_u *)".";
5096 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005097 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005098 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005099 if (path == NULL)
5100 path = (char_u *)"";
5101 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005102
5103 /*
5104 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005105 * collect them in "ga". When "." is not in $PATH also expand for the
5106 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005107 */
5108 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005109 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005110 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005111 if (*s == NUL)
5112 {
5113 if (did_curdir)
5114 break;
5115 /* Find directories in the current directory, path is empty. */
5116 did_curdir = TRUE;
5117 }
5118 else if (*s == '.')
5119 did_curdir = TRUE;
5120
Bram Moolenaar68c31742006-09-02 15:54:18 +00005121 if (*s == ' ')
5122 ++s; /* Skip space used for absolute path name. */
5123
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005124#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005125 e = vim_strchr(s, ';');
5126#else
5127 e = vim_strchr(s, ':');
5128#endif
5129 if (e == NULL)
5130 e = s + STRLEN(s);
5131
5132 l = e - s;
5133 if (l > MAXPATHL - 5)
5134 break;
5135 vim_strncpy(buf, s, l);
5136 add_pathsep(buf);
5137 l = STRLEN(buf);
5138 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5139
5140 /* Expand matches in one directory of $PATH. */
5141 ret = expand_wildcards(1, &buf, num_file, file, flags);
5142 if (ret == OK)
5143 {
5144 if (ga_grow(&ga, *num_file) == FAIL)
5145 FreeWild(*num_file, *file);
5146 else
5147 {
5148 for (i = 0; i < *num_file; ++i)
5149 {
5150 s = (*file)[i];
5151 if (STRLEN(s) > l)
5152 {
5153 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005154 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005155 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5156 }
5157 else
5158 vim_free(s);
5159 }
5160 vim_free(*file);
5161 }
5162 }
5163 if (*e != NUL)
5164 ++e;
5165 }
5166 *file = ga.ga_data;
5167 *num_file = ga.ga_len;
5168
5169 vim_free(buf);
5170 vim_free(pat);
5171 if (mustfree)
5172 vim_free(path);
5173 return OK;
5174}
5175
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005178static 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 +00005179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005181 * Call "user_expand_func()" to invoke a user defined Vim script function and
5182 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005184 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005185call_user_expand_func(
5186 void *(*user_expand_func)(char_u *, int, char_u **, int),
5187 expand_T *xp,
5188 int *num_file,
5189 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005191 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005192 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005195 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005196 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
Bram Moolenaarb7515462013-06-29 12:58:33 +02005198 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005199 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 *num_file = 0;
5201 *file = NULL;
5202
Bram Moolenaarb7515462013-06-29 12:58:33 +02005203 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005204 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005205 keep = ccline.cmdbuff[ccline.cmdlen];
5206 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005207 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005208
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005209 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005210 args[1] = xp->xp_line;
5211 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 args[2] = num;
5213
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005214 /* Save the cmdline, we don't know what the function may do. */
5215 save_ccline = ccline;
5216 ccline.cmdbuff = NULL;
5217 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005219
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005220 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005221
5222 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005224 if (ccline.cmdbuff != NULL)
5225 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005226
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005227 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005228 return ret;
5229}
5230
5231/*
5232 * Expand names with a function defined by the user.
5233 */
5234 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005235ExpandUserDefined(
5236 expand_T *xp,
5237 regmatch_T *regmatch,
5238 int *num_file,
5239 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005240{
5241 char_u *retstr;
5242 char_u *s;
5243 char_u *e;
5244 char_u keep;
5245 garray_T ga;
5246
5247 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5248 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 return FAIL;
5250
5251 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005252 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
5254 e = vim_strchr(s, '\n');
5255 if (e == NULL)
5256 e = s + STRLEN(s);
5257 keep = *e;
5258 *e = 0;
5259
5260 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5261 {
5262 *e = keep;
5263 if (*e != NUL)
5264 ++e;
5265 continue;
5266 }
5267
5268 if (ga_grow(&ga, 1) == FAIL)
5269 break;
5270
5271 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5272 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274 *e = keep;
5275 if (*e != NUL)
5276 ++e;
5277 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005278 vim_free(retstr);
5279 *file = ga.ga_data;
5280 *num_file = ga.ga_len;
5281 return OK;
5282}
5283
5284/*
5285 * Expand names with a list returned by a function defined by the user.
5286 */
5287 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005288ExpandUserList(
5289 expand_T *xp,
5290 int *num_file,
5291 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005292{
5293 list_T *retlist;
5294 listitem_T *li;
5295 garray_T ga;
5296
5297 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5298 if (retlist == NULL)
5299 return FAIL;
5300
5301 ga_init2(&ga, (int)sizeof(char *), 3);
5302 /* Loop over the items in the list. */
5303 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5304 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005305 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5306 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005307
5308 if (ga_grow(&ga, 1) == FAIL)
5309 break;
5310
5311 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005312 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005313 ++ga.ga_len;
5314 }
5315 list_unref(retlist);
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 *file = ga.ga_data;
5318 *num_file = ga.ga_len;
5319 return OK;
5320}
5321#endif
5322
5323/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005324 * Expand color scheme, compiler or filetype names.
5325 * Search from 'runtimepath':
5326 * 'runtimepath'/{dirnames}/{pat}.vim
5327 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5328 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5329 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5330 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005331 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
5333 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005334ExpandRTDir(
5335 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005336 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005337 int *num_file,
5338 char_u ***file,
5339 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 char_u *s;
5342 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005343 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005345 int i;
5346 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
5348 *num_file = 0;
5349 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005350 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005351 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005353 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005355 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5356 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005358 ga_clear_strings(&ga);
5359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005361 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005362 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005363 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005365
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005366 if (flags & DIP_START) {
5367 for (i = 0; dirnames[i] != NULL; ++i)
5368 {
5369 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5370 if (s == NULL)
5371 {
5372 ga_clear_strings(&ga);
5373 return FAIL;
5374 }
5375 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5376 globpath(p_pp, s, &ga, 0);
5377 vim_free(s);
5378 }
5379 }
5380
5381 if (flags & DIP_OPT) {
5382 for (i = 0; dirnames[i] != NULL; ++i)
5383 {
5384 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5385 if (s == NULL)
5386 {
5387 ga_clear_strings(&ga);
5388 return FAIL;
5389 }
5390 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5391 globpath(p_pp, s, &ga, 0);
5392 vim_free(s);
5393 }
5394 }
5395
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005396 for (i = 0; i < ga.ga_len; ++i)
5397 {
5398 match = ((char_u **)ga.ga_data)[i];
5399 s = match;
5400 e = s + STRLEN(s);
5401 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5402 {
5403 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005404 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005405 if (s < match || vim_ispathsep(*s))
5406 break;
5407 ++s;
5408 *e = NUL;
5409 mch_memmove(match, s, e - s + 1);
5410 }
5411 }
5412
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005413 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005414 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005415
5416 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005417 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005418 remove_duplicates(&ga);
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 *file = ga.ga_data;
5421 *num_file = ga.ga_len;
5422 return OK;
5423}
5424
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005425/*
5426 * Expand loadplugin names:
5427 * 'packpath'/pack/ * /opt/{pat}
5428 */
5429 static int
5430ExpandPackAddDir(
5431 char_u *pat,
5432 int *num_file,
5433 char_u ***file)
5434{
5435 char_u *s;
5436 char_u *e;
5437 char_u *match;
5438 garray_T ga;
5439 int i;
5440 int pat_len;
5441
5442 *num_file = 0;
5443 *file = NULL;
5444 pat_len = (int)STRLEN(pat);
5445 ga_init2(&ga, (int)sizeof(char *), 10);
5446
5447 s = alloc((unsigned)(pat_len + 26));
5448 if (s == NULL)
5449 {
5450 ga_clear_strings(&ga);
5451 return FAIL;
5452 }
5453 sprintf((char *)s, "pack/*/opt/%s*", pat);
5454 globpath(p_pp, s, &ga, 0);
5455 vim_free(s);
5456
5457 for (i = 0; i < ga.ga_len; ++i)
5458 {
5459 match = ((char_u **)ga.ga_data)[i];
5460 s = gettail(match);
5461 e = s + STRLEN(s);
5462 mch_memmove(match, s, e - s + 1);
5463 }
5464
5465 if (ga.ga_len == 0)
5466 return FAIL;
5467
5468 /* Sort and remove duplicates which can happen when specifying multiple
5469 * directories in dirnames. */
5470 remove_duplicates(&ga);
5471
5472 *file = ga.ga_data;
5473 *num_file = ga.ga_len;
5474 return OK;
5475}
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#endif
5478
5479#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5480/*
5481 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005482 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005484 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005485globpath(
5486 char_u *path,
5487 char_u *file,
5488 garray_T *ga,
5489 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 expand_T xpc;
5492 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 int num_p;
5495 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 buf = alloc(MAXPATHL);
5498 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005499 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005501 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 /* Loop over all entries in {path}. */
5505 while (*path != NUL)
5506 {
5507 /* Copy one item of the path to buf[] and concatenate the file name. */
5508 copy_option_part(&path, buf, MAXPATHL, ",");
5509 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5510 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005511# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005512 /* Using the platform's path separator (\) makes vim incorrectly
5513 * treat it as an escape character, use '/' instead. */
5514 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5515 STRCAT(buf, "/");
5516# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005520 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5521 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005523 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005525 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 for (i = 0; i < num_p; ++i)
5528 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005529 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005530 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005531 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 FreeWild(num_p, p);
5536 }
5537 }
5538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541}
5542
5543#endif
5544
5545#if defined(FEAT_CMDHIST) || defined(PROTO)
5546
5547/*********************************
5548 * Command line history stuff *
5549 *********************************/
5550
5551/*
5552 * Translate a history character to the associated type number.
5553 */
5554 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 if (c == ':')
5558 return HIST_CMD;
5559 if (c == '=')
5560 return HIST_EXPR;
5561 if (c == '@')
5562 return HIST_INPUT;
5563 if (c == '>')
5564 return HIST_DEBUG;
5565 return HIST_SEARCH; /* must be '?' or '/' */
5566}
5567
5568/*
5569 * Table of history names.
5570 * These names are used in :history and various hist...() functions.
5571 * It is sufficient to give the significant prefix of a history name.
5572 */
5573
5574static char *(history_names[]) =
5575{
5576 "cmd",
5577 "search",
5578 "expr",
5579 "input",
5580 "debug",
5581 NULL
5582};
5583
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005584#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5585/*
5586 * Function given to ExpandGeneric() to obtain the possible first
5587 * arguments of the ":history command.
5588 */
5589 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005591{
5592 static char_u compl[2] = { NUL, NUL };
5593 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005594 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005595 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5596
5597 if (idx < short_names_count)
5598 {
5599 compl[0] = (char_u)short_names[idx];
5600 return compl;
5601 }
5602 if (idx < short_names_count + history_name_count)
5603 return (char_u *)history_names[idx - short_names_count];
5604 if (idx == short_names_count + history_name_count)
5605 return (char_u *)"all";
5606 return NULL;
5607}
5608#endif
5609
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610/*
5611 * init_history() - Initialize the command line history.
5612 * Also used to re-allocate the history when the size changes.
5613 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005614 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 int newlen; /* new length of history table */
5618 histentry_T *temp;
5619 int i;
5620 int j;
5621 int type;
5622
5623 /*
5624 * If size of history table changed, reallocate it
5625 */
5626 newlen = (int)p_hi;
5627 if (newlen != hislen) /* history length changed */
5628 {
5629 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5630 {
5631 if (newlen)
5632 {
5633 temp = (histentry_T *)lalloc(
5634 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5635 if (temp == NULL) /* out of memory! */
5636 {
5637 if (type == 0) /* first one: just keep the old length */
5638 {
5639 newlen = hislen;
5640 break;
5641 }
5642 /* Already changed one table, now we can only have zero
5643 * length for all tables. */
5644 newlen = 0;
5645 type = -1;
5646 continue;
5647 }
5648 }
5649 else
5650 temp = NULL;
5651 if (newlen == 0 || temp != NULL)
5652 {
5653 if (hisidx[type] < 0) /* there are no entries yet */
5654 {
5655 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005656 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 else if (newlen > hislen) /* array becomes bigger */
5659 {
5660 for (i = 0; i <= hisidx[type]; ++i)
5661 temp[i] = history[type][i];
5662 j = i;
5663 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005664 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 for ( ; j < hislen; ++i, ++j)
5666 temp[i] = history[type][j];
5667 }
5668 else /* array becomes smaller or 0 */
5669 {
5670 j = hisidx[type];
5671 for (i = newlen - 1; ; --i)
5672 {
5673 if (i >= 0) /* copy newest entries */
5674 temp[i] = history[type][j];
5675 else /* remove older entries */
5676 vim_free(history[type][j].hisstr);
5677 if (--j < 0)
5678 j = hislen - 1;
5679 if (j == hisidx[type])
5680 break;
5681 }
5682 hisidx[type] = newlen - 1;
5683 }
5684 vim_free(history[type]);
5685 history[type] = temp;
5686 }
5687 }
5688 hislen = newlen;
5689 }
5690}
5691
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005694{
5695 hisptr->hisnum = 0;
5696 hisptr->viminfo = FALSE;
5697 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005698 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005699}
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701/*
5702 * Check if command line 'str' is already in history.
5703 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5704 */
5705 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706in_history(
5707 int type,
5708 char_u *str,
5709 int move_to_front, /* Move the entry to the front if it exists */
5710 int sep,
5711 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 int i;
5714 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005715 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 if (hisidx[type] < 0)
5718 return FALSE;
5719 i = hisidx[type];
5720 do
5721 {
5722 if (history[type][i].hisstr == NULL)
5723 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005724
5725 /* For search history, check that the separator character matches as
5726 * well. */
5727 p = history[type][i].hisstr;
5728 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005729 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005730 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
5732 if (!move_to_front)
5733 return TRUE;
5734 last_i = i;
5735 break;
5736 }
5737 if (--i < 0)
5738 i = hislen - 1;
5739 } while (i != hisidx[type]);
5740
5741 if (last_i >= 0)
5742 {
5743 str = history[type][i].hisstr;
5744 while (i != hisidx[type])
5745 {
5746 if (++i >= hislen)
5747 i = 0;
5748 history[type][last_i] = history[type][i];
5749 last_i = i;
5750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005752 history[type][i].viminfo = FALSE;
5753 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005754 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 return TRUE;
5756 }
5757 return FALSE;
5758}
5759
5760/*
5761 * Convert history name (from table above) to its HIST_ equivalent.
5762 * When "name" is empty, return "cmd" history.
5763 * Returns -1 for unknown history name.
5764 */
5765 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005766get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 int i;
5769 int len = (int)STRLEN(name);
5770
5771 /* No argument: use current history. */
5772 if (len == 0)
5773 return hist_char2type(ccline.cmdfirstc);
5774
5775 for (i = 0; history_names[i] != NULL; ++i)
5776 if (STRNICMP(name, history_names[i], len) == 0)
5777 return i;
5778
5779 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5780 return hist_char2type(name[0]);
5781
5782 return -1;
5783}
5784
5785static int last_maptick = -1; /* last seen maptick */
5786
5787/*
5788 * Add the given string to the given history. If the string is already in the
5789 * history then it is moved to the front. "histype" may be one of he HIST_
5790 * values.
5791 */
5792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005793add_to_history(
5794 int histype,
5795 char_u *new_entry,
5796 int in_map, /* consider maptick when inside a mapping */
5797 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 histentry_T *hisptr;
5800 int len;
5801
5802 if (hislen == 0) /* no history */
5803 return;
5804
Bram Moolenaara939e432013-11-09 05:30:26 +01005805 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5806 return;
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 /*
5809 * Searches inside the same mapping overwrite each other, so that only
5810 * the last line is kept. Be careful not to remove a line that was moved
5811 * down, only lines that were added.
5812 */
5813 if (histype == HIST_SEARCH && in_map)
5814 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005815 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
5817 /* Current line is from the same mapping, remove it */
5818 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5819 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005820 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 --hisnum[histype];
5822 if (--hisidx[HIST_SEARCH] < 0)
5823 hisidx[HIST_SEARCH] = hislen - 1;
5824 }
5825 last_maptick = -1;
5826 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005827 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 {
5829 if (++hisidx[histype] == hislen)
5830 hisidx[histype] = 0;
5831 hisptr = &history[histype][hisidx[histype]];
5832 vim_free(hisptr->hisstr);
5833
5834 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005835 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5837 if (hisptr->hisstr != NULL)
5838 hisptr->hisstr[len + 1] = sep;
5839
5840 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005841 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005842 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 if (histype == HIST_SEARCH && in_map)
5844 last_maptick = maptick;
5845 }
5846}
5847
5848#if defined(FEAT_EVAL) || defined(PROTO)
5849
5850/*
5851 * Get identifier of newest history entry.
5852 * "histype" may be one of the HIST_ values.
5853 */
5854 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005855get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5858 || hisidx[histype] < 0)
5859 return -1;
5860
5861 return history[histype][hisidx[histype]].hisnum;
5862}
5863
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 * Calculate history index from a number:
5866 * num > 0: seen as identifying number of a history entry
5867 * num < 0: relative position in history wrt newest entry
5868 * "histype" may be one of the HIST_ values.
5869 */
5870 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005871calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 int i;
5874 histentry_T *hist;
5875 int wrapped = FALSE;
5876
5877 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5878 || (i = hisidx[histype]) < 0 || num == 0)
5879 return -1;
5880
5881 hist = history[histype];
5882 if (num > 0)
5883 {
5884 while (hist[i].hisnum > num)
5885 if (--i < 0)
5886 {
5887 if (wrapped)
5888 break;
5889 i += hislen;
5890 wrapped = TRUE;
5891 }
5892 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5893 return i;
5894 }
5895 else if (-num <= hislen)
5896 {
5897 i += num + 1;
5898 if (i < 0)
5899 i += hislen;
5900 if (hist[i].hisstr != NULL)
5901 return i;
5902 }
5903 return -1;
5904}
5905
5906/*
5907 * Get a history entry by its index.
5908 * "histype" may be one of the HIST_ values.
5909 */
5910 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005911get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 idx = calc_hist_idx(histype, idx);
5914 if (idx >= 0)
5915 return history[histype][idx].hisstr;
5916 else
5917 return (char_u *)"";
5918}
5919
5920/*
5921 * Clear all entries of a history.
5922 * "histype" may be one of the HIST_ values.
5923 */
5924 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005925clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 int i;
5928 histentry_T *hisptr;
5929
5930 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5931 {
5932 hisptr = history[histype];
5933 for (i = hislen; i--;)
5934 {
5935 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005936 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005937 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 }
5939 hisidx[histype] = -1; /* mark history as cleared */
5940 hisnum[histype] = 0; /* reset identifier counter */
5941 return OK;
5942 }
5943 return FAIL;
5944}
5945
5946/*
5947 * Remove all entries matching {str} from a history.
5948 * "histype" may be one of the HIST_ values.
5949 */
5950 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 regmatch_T regmatch;
5954 histentry_T *hisptr;
5955 int idx;
5956 int i;
5957 int last;
5958 int found = FALSE;
5959
5960 regmatch.regprog = NULL;
5961 regmatch.rm_ic = FALSE; /* always match case */
5962 if (hislen != 0
5963 && histype >= 0
5964 && histype < HIST_COUNT
5965 && *str != NUL
5966 && (idx = hisidx[histype]) >= 0
5967 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5968 != NULL)
5969 {
5970 i = last = idx;
5971 do
5972 {
5973 hisptr = &history[histype][i];
5974 if (hisptr->hisstr == NULL)
5975 break;
5976 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5977 {
5978 found = TRUE;
5979 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005980 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 }
5982 else
5983 {
5984 if (i != last)
5985 {
5986 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005987 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
5989 if (--last < 0)
5990 last += hislen;
5991 }
5992 if (--i < 0)
5993 i += hislen;
5994 } while (i != idx);
5995 if (history[histype][idx].hisstr == NULL)
5996 hisidx[histype] = -1;
5997 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005998 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 return found;
6000}
6001
6002/*
6003 * Remove an indexed entry from a history.
6004 * "histype" may be one of the HIST_ values.
6005 */
6006 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 int i, j;
6010
6011 i = calc_hist_idx(histype, idx);
6012 if (i < 0)
6013 return FALSE;
6014 idx = hisidx[histype];
6015 vim_free(history[histype][i].hisstr);
6016
6017 /* When deleting the last added search string in a mapping, reset
6018 * last_maptick, so that the last added search string isn't deleted again.
6019 */
6020 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6021 last_maptick = -1;
6022
6023 while (i != idx)
6024 {
6025 j = (i + 1) % hislen;
6026 history[histype][i] = history[histype][j];
6027 i = j;
6028 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006029 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (--i < 0)
6031 i += hislen;
6032 hisidx[histype] = i;
6033 return TRUE;
6034}
6035
6036#endif /* FEAT_EVAL */
6037
6038#if defined(FEAT_CRYPT) || defined(PROTO)
6039/*
6040 * Very specific function to remove the value in ":set key=val" from the
6041 * history.
6042 */
6043 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006044remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 char_u *p;
6047 int i;
6048
6049 i = hisidx[HIST_CMD];
6050 if (i < 0)
6051 return;
6052 p = history[HIST_CMD][i].hisstr;
6053 if (p != NULL)
6054 for ( ; *p; ++p)
6055 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6056 {
6057 p = vim_strchr(p + 3, '=');
6058 if (p == NULL)
6059 break;
6060 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006061 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (p[i] == '\\' && p[i + 1])
6063 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006064 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 --p;
6066 }
6067}
6068#endif
6069
6070#endif /* FEAT_CMDHIST */
6071
Bram Moolenaar064154c2016-03-19 22:50:43 +01006072#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006073/*
6074 * Get pointer to the command line info to use. cmdline_paste() may clear
6075 * ccline and put the previous value in prev_ccline.
6076 */
6077 static struct cmdline_info *
6078get_ccline_ptr(void)
6079{
6080 if ((State & CMDLINE) == 0)
6081 return NULL;
6082 if (ccline.cmdbuff != NULL)
6083 return &ccline;
6084 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6085 return &prev_ccline;
6086 return NULL;
6087}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006088#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006089
Bram Moolenaar064154c2016-03-19 22:50:43 +01006090#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006091/*
6092 * Get the current command line in allocated memory.
6093 * Only works when the command line is being edited.
6094 * Returns NULL when something is wrong.
6095 */
6096 char_u *
6097get_cmdline_str(void)
6098{
6099 struct cmdline_info *p = get_ccline_ptr();
6100
6101 if (p == NULL)
6102 return NULL;
6103 return vim_strnsave(p->cmdbuff, p->cmdlen);
6104}
6105
6106/*
6107 * Get the current command line position, counted in bytes.
6108 * Zero is the first position.
6109 * Only works when the command line is being edited.
6110 * Returns -1 when something is wrong.
6111 */
6112 int
6113get_cmdline_pos(void)
6114{
6115 struct cmdline_info *p = get_ccline_ptr();
6116
6117 if (p == NULL)
6118 return -1;
6119 return p->cmdpos;
6120}
6121
6122/*
6123 * Set the command line byte position to "pos". Zero is the first position.
6124 * Only works when the command line is being edited.
6125 * Returns 1 when failed, 0 when OK.
6126 */
6127 int
6128set_cmdline_pos(
6129 int pos)
6130{
6131 struct cmdline_info *p = get_ccline_ptr();
6132
6133 if (p == NULL)
6134 return 1;
6135
6136 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6137 * changed the command line. */
6138 if (pos < 0)
6139 new_cmdpos = 0;
6140 else
6141 new_cmdpos = pos;
6142 return 0;
6143}
6144#endif
6145
6146#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6147/*
6148 * Get the current command-line type.
6149 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6150 * Only works when the command line is being edited.
6151 * Returns NUL when something is wrong.
6152 */
6153 int
6154get_cmdline_type(void)
6155{
6156 struct cmdline_info *p = get_ccline_ptr();
6157
6158 if (p == NULL)
6159 return NUL;
6160 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006161 return
6162# ifdef FEAT_EVAL
6163 (p->input_fn) ? '@' :
6164# endif
6165 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006166 return p->cmdfirstc;
6167}
6168#endif
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6171/*
6172 * Get indices "num1,num2" that specify a range within a list (not a range of
6173 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6174 * Returns OK if parsed successfully, otherwise FAIL.
6175 */
6176 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006177get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 int len;
6180 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006181 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 *str = skipwhite(*str);
6184 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6185 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006186 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 *str += len;
6188 *num1 = (int)num;
6189 first = TRUE;
6190 }
6191 *str = skipwhite(*str);
6192 if (**str == ',') /* parse "to" part of range */
6193 {
6194 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006195 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 if (len > 0)
6197 {
6198 *num2 = (int)num;
6199 *str = skipwhite(*str + len);
6200 }
6201 else if (!first) /* no number given at all */
6202 return FAIL;
6203 }
6204 else if (first) /* only one number given */
6205 *num2 = *num1;
6206 return OK;
6207}
6208#endif
6209
6210#if defined(FEAT_CMDHIST) || defined(PROTO)
6211/*
6212 * :history command - print a history
6213 */
6214 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006215ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 histentry_T *hist;
6218 int histype1 = HIST_CMD;
6219 int histype2 = HIST_CMD;
6220 int hisidx1 = 1;
6221 int hisidx2 = -1;
6222 int idx;
6223 int i, j, k;
6224 char_u *end;
6225 char_u *arg = eap->arg;
6226
6227 if (hislen == 0)
6228 {
6229 MSG(_("'history' option is zero"));
6230 return;
6231 }
6232
6233 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6234 {
6235 end = arg;
6236 while (ASCII_ISALPHA(*end)
6237 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6238 end++;
6239 i = *end;
6240 *end = NUL;
6241 histype1 = get_histtype(arg);
6242 if (histype1 == -1)
6243 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006244 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
6246 histype1 = 0;
6247 histype2 = HIST_COUNT-1;
6248 }
6249 else
6250 {
6251 *end = i;
6252 EMSG(_(e_trailing));
6253 return;
6254 }
6255 }
6256 else
6257 histype2 = histype1;
6258 *end = i;
6259 }
6260 else
6261 end = arg;
6262 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6263 {
6264 EMSG(_(e_trailing));
6265 return;
6266 }
6267
6268 for (; !got_int && histype1 <= histype2; ++histype1)
6269 {
6270 STRCPY(IObuff, "\n # ");
6271 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6272 MSG_PUTS_TITLE(IObuff);
6273 idx = hisidx[histype1];
6274 hist = history[histype1];
6275 j = hisidx1;
6276 k = hisidx2;
6277 if (j < 0)
6278 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6279 if (k < 0)
6280 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6281 if (idx >= 0 && j <= k)
6282 for (i = idx + 1; !got_int; ++i)
6283 {
6284 if (i == hislen)
6285 i = 0;
6286 if (hist[i].hisstr != NULL
6287 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6288 {
6289 msg_putchar('\n');
6290 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6291 hist[i].hisnum);
6292 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6293 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006294 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 else
6296 STRCAT(IObuff, hist[i].hisstr);
6297 msg_outtrans(IObuff);
6298 out_flush();
6299 }
6300 if (i == idx)
6301 break;
6302 }
6303 }
6304}
6305#endif
6306
6307#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006308/*
6309 * Buffers for history read from a viminfo file. Only valid while reading.
6310 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006311static histentry_T *viminfo_history[HIST_COUNT] =
6312 {NULL, NULL, NULL, NULL, NULL};
6313static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6314static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315static int viminfo_add_at_front = FALSE;
6316
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006317static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319/*
6320 * Translate a history type number to the associated character.
6321 */
6322 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323hist_type2char(
6324 int type,
6325 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 if (type == HIST_CMD)
6328 return ':';
6329 if (type == HIST_SEARCH)
6330 {
6331 if (use_question)
6332 return '?';
6333 else
6334 return '/';
6335 }
6336 if (type == HIST_EXPR)
6337 return '=';
6338 return '@';
6339}
6340
6341/*
6342 * Prepare for reading the history from the viminfo file.
6343 * This allocates history arrays to store the read history lines.
6344 */
6345 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006346prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 int i;
6349 int num;
6350 int type;
6351 int len;
6352
6353 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006354 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 if (asklen > hislen)
6356 asklen = hislen;
6357
6358 for (type = 0; type < HIST_COUNT; ++type)
6359 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006360 /* Count the number of empty spaces in the history list. Entries read
6361 * from viminfo previously are also considered empty. If there are
6362 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006364 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 num++;
6366 len = asklen;
6367 if (num > len)
6368 len = num;
6369 if (len <= 0)
6370 viminfo_history[type] = NULL;
6371 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006372 viminfo_history[type] = (histentry_T *)lalloc(
6373 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (viminfo_history[type] == NULL)
6375 len = 0;
6376 viminfo_hislen[type] = len;
6377 viminfo_hisidx[type] = 0;
6378 }
6379}
6380
6381/*
6382 * Accept a line from the viminfo, store it in the history array when it's
6383 * new.
6384 */
6385 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006386read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 int type;
6389 long_u len;
6390 char_u *val;
6391 char_u *p;
6392
6393 type = hist_char2type(virp->vir_line[0]);
6394 if (viminfo_hisidx[type] < viminfo_hislen[type])
6395 {
6396 val = viminfo_readstring(virp, 1, TRUE);
6397 if (val != NULL && *val != NUL)
6398 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006399 int sep = (*val == ' ' ? NUL : *val);
6400
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006402 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 {
6404 /* Need to re-allocate to append the separator byte. */
6405 len = STRLEN(val);
6406 p = lalloc(len + 2, TRUE);
6407 if (p != NULL)
6408 {
6409 if (type == HIST_SEARCH)
6410 {
6411 /* Search entry: Move the separator from the first
6412 * column to after the NUL. */
6413 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006414 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416 else
6417 {
6418 /* Not a search entry: No separator in the viminfo
6419 * file, add a NUL separator. */
6420 mch_memmove(p, val, (size_t)len + 1);
6421 p[len + 1] = NUL;
6422 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006423 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6424 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006425 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6426 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006427 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
6429 }
6430 }
6431 vim_free(val);
6432 }
6433 return viminfo_readline(virp);
6434}
6435
Bram Moolenaar07219f92013-04-14 23:19:36 +02006436/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006437 * Accept a new style history line from the viminfo, store it in the history
6438 * array when it's new.
6439 */
6440 void
6441handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006442 garray_T *values,
6443 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006444{
6445 int type;
6446 long_u len;
6447 char_u *val;
6448 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006449 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006450
6451 /* Check the format:
6452 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006453 if (values->ga_len < 4
6454 || vp[0].bv_type != BVAL_NR
6455 || vp[1].bv_type != BVAL_NR
6456 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6457 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006458 return;
6459
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006460 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006461 if (type >= HIST_COUNT)
6462 return;
6463 if (viminfo_hisidx[type] < viminfo_hislen[type])
6464 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006465 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006466 if (val != NULL && *val != NUL)
6467 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006468 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6469 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006470 int idx;
6471 int overwrite = FALSE;
6472
6473 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6474 {
6475 /* If lines were written by an older Vim we need to avoid
6476 * getting duplicates. See if the entry already exists. */
6477 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6478 {
6479 p = viminfo_history[type][idx].hisstr;
6480 if (STRCMP(val, p) == 0
6481 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6482 {
6483 overwrite = TRUE;
6484 break;
6485 }
6486 }
6487
6488 if (!overwrite)
6489 {
6490 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006491 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006492 p = lalloc(len + 2, TRUE);
6493 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006494 else
6495 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006496 if (p != NULL)
6497 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006498 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006499 if (!overwrite)
6500 {
6501 mch_memmove(p, val, (size_t)len + 1);
6502 /* Put the separator after the NUL. */
6503 p[len + 1] = sep;
6504 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006505 viminfo_history[type][idx].hisnum = 0;
6506 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006507 viminfo_hisidx[type]++;
6508 }
6509 }
6510 }
6511 }
6512 }
6513}
6514
6515/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006516 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006517 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006518 static void
6519concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 int idx;
6522 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006523
6524 idx = hisidx[type] + viminfo_hisidx[type];
6525 if (idx >= hislen)
6526 idx -= hislen;
6527 else if (idx < 0)
6528 idx = hislen - 1;
6529 if (viminfo_add_at_front)
6530 hisidx[type] = idx;
6531 else
6532 {
6533 if (hisidx[type] == -1)
6534 hisidx[type] = hislen - 1;
6535 do
6536 {
6537 if (history[type][idx].hisstr != NULL
6538 || history[type][idx].viminfo)
6539 break;
6540 if (++idx == hislen)
6541 idx = 0;
6542 } while (idx != hisidx[type]);
6543 if (idx != hisidx[type] && --idx < 0)
6544 idx = hislen - 1;
6545 }
6546 for (i = 0; i < viminfo_hisidx[type]; i++)
6547 {
6548 vim_free(history[type][idx].hisstr);
6549 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6550 history[type][idx].viminfo = TRUE;
6551 history[type][idx].time_set = viminfo_history[type][i].time_set;
6552 if (--idx < 0)
6553 idx = hislen - 1;
6554 }
6555 idx += 1;
6556 idx %= hislen;
6557 for (i = 0; i < viminfo_hisidx[type]; i++)
6558 {
6559 history[type][idx++].hisnum = ++hisnum[type];
6560 idx %= hislen;
6561 }
6562}
6563
6564#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6565 static int
6566#ifdef __BORLANDC__
6567_RTLENTRYF
6568#endif
6569sort_hist(const void *s1, const void *s2)
6570{
6571 histentry_T *p1 = *(histentry_T **)s1;
6572 histentry_T *p2 = *(histentry_T **)s2;
6573
6574 if (p1->time_set < p2->time_set) return -1;
6575 if (p1->time_set > p2->time_set) return 1;
6576 return 0;
6577}
6578#endif
6579
6580/*
6581 * Merge history lines from viminfo and lines typed in this Vim based on the
6582 * timestamp;
6583 */
6584 static void
6585merge_history(int type)
6586{
6587 int max_len;
6588 histentry_T **tot_hist;
6589 histentry_T *new_hist;
6590 int i;
6591 int len;
6592
6593 /* Make one long list with all entries. */
6594 max_len = hislen + viminfo_hisidx[type];
6595 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6596 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6597 if (tot_hist == NULL || new_hist == NULL)
6598 {
6599 vim_free(tot_hist);
6600 vim_free(new_hist);
6601 return;
6602 }
6603 for (i = 0; i < viminfo_hisidx[type]; i++)
6604 tot_hist[i] = &viminfo_history[type][i];
6605 len = i;
6606 for (i = 0; i < hislen; i++)
6607 if (history[type][i].hisstr != NULL)
6608 tot_hist[len++] = &history[type][i];
6609
6610 /* Sort the list on timestamp. */
6611 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6612
6613 /* Keep the newest ones. */
6614 for (i = 0; i < hislen; i++)
6615 {
6616 if (i < len)
6617 {
6618 new_hist[i] = *tot_hist[i];
6619 tot_hist[i]->hisstr = NULL;
6620 if (new_hist[i].hisnum == 0)
6621 new_hist[i].hisnum = ++hisnum[type];
6622 }
6623 else
6624 clear_hist_entry(&new_hist[i]);
6625 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006626 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006627
6628 /* Free what is not kept. */
6629 for (i = 0; i < viminfo_hisidx[type]; i++)
6630 vim_free(viminfo_history[type][i].hisstr);
6631 for (i = 0; i < hislen; i++)
6632 vim_free(history[type][i].hisstr);
6633 vim_free(history[type]);
6634 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006635 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006636}
6637
6638/*
6639 * Finish reading history lines from viminfo. Not used when writing viminfo.
6640 */
6641 void
6642finish_viminfo_history(vir_T *virp)
6643{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006645 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646
6647 for (type = 0; type < HIST_COUNT; ++type)
6648 {
6649 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006650 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006651
6652 if (merge)
6653 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006655 concat_history(type);
6656
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 vim_free(viminfo_history[type]);
6658 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006659 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 }
6661}
6662
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006663/*
6664 * Write history to viminfo file in "fp".
6665 * When "merge" is TRUE merge history lines with a previously read viminfo
6666 * file, data is in viminfo_history[].
6667 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006670write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int i;
6673 int type;
6674 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006675 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676
6677 init_history();
6678 if (hislen == 0)
6679 return;
6680 for (type = 0; type < HIST_COUNT; ++type)
6681 {
6682 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6683 if (num_saved == 0)
6684 continue;
6685 if (num_saved < 0) /* Use default */
6686 num_saved = hislen;
6687 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6688 type == HIST_CMD ? _("Command Line") :
6689 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006690 type == HIST_EXPR ? _("Expression") :
6691 type == HIST_INPUT ? _("Input Line") :
6692 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (num_saved > hislen)
6694 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006695
6696 /*
6697 * Merge typed and viminfo history:
6698 * round 1: history of typed commands.
6699 * round 2: history from recently read viminfo.
6700 */
6701 for (round = 1; round <= 2; ++round)
6702 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006703 if (round == 1)
6704 /* start at newest entry, somewhere in the list */
6705 i = hisidx[type];
6706 else if (viminfo_hisidx[type] > 0)
6707 /* start at newest entry, first in the list */
6708 i = 0;
6709 else
6710 /* empty list */
6711 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006712 if (i >= 0)
6713 while (num_saved > 0
6714 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006716 char_u *p;
6717 time_t timestamp;
6718 int c = NUL;
6719
6720 if (round == 1)
6721 {
6722 p = history[type][i].hisstr;
6723 timestamp = history[type][i].time_set;
6724 }
6725 else
6726 {
6727 p = viminfo_history[type] == NULL ? NULL
6728 : viminfo_history[type][i].hisstr;
6729 timestamp = viminfo_history[type] == NULL ? 0
6730 : viminfo_history[type][i].time_set;
6731 }
6732
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006733 if (p != NULL && (round == 2
6734 || !merge
6735 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006737 --num_saved;
6738 fputc(hist_type2char(type, TRUE), fp);
6739 /* For the search history: put the separator in the
6740 * second column; use a space if there isn't one. */
6741 if (type == HIST_SEARCH)
6742 {
6743 c = p[STRLEN(p) + 1];
6744 putc(c == NUL ? ' ' : c, fp);
6745 }
6746 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006747
6748 {
6749 char cbuf[NUMBUFLEN];
6750
6751 /* New style history with a bar line. Format:
6752 * |{bartype},{histtype},{timestamp},{separator},"text" */
6753 if (c == NUL)
6754 cbuf[0] = NUL;
6755 else
6756 sprintf(cbuf, "%d", c);
6757 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6758 type, (long)timestamp, cbuf);
6759 barline_writestring(fp, p, LSIZE - 20);
6760 putc('\n', fp);
6761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006763 if (round == 1)
6764 {
6765 /* Decrement index, loop around and stop when back at
6766 * the start. */
6767 if (--i < 0)
6768 i = hislen - 1;
6769 if (i == hisidx[type])
6770 break;
6771 }
6772 else
6773 {
6774 /* Increment index. Stop at the end in the while. */
6775 ++i;
6776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006778 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006779 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006780 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006781 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006782 vim_free(viminfo_history[type]);
6783 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006784 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
6786}
6787#endif /* FEAT_VIMINFO */
6788
6789#if defined(FEAT_FKMAP) || defined(PROTO)
6790/*
6791 * Write a character at the current cursor+offset position.
6792 * It is directly written into the command buffer block.
6793 */
6794 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006795cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6798 {
6799 EMSG(_("E198: cmd_pchar beyond the command length"));
6800 return;
6801 }
6802 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6803 ccline.cmdbuff[ccline.cmdlen] = NUL;
6804}
6805
6806 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006807cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6810 {
6811 /* EMSG(_("cmd_gchar beyond the command length")); */
6812 return NUL;
6813 }
6814 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6815}
6816#endif
6817
6818#if defined(FEAT_CMDWIN) || defined(PROTO)
6819/*
6820 * Open a window on the current command line and history. Allow editing in
6821 * the window. Returns when the window is closed.
6822 * Returns:
6823 * CR if the command is to be executed
6824 * Ctrl_C if it is to be abandoned
6825 * K_IGNORE if editing continues
6826 */
6827 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006828open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006831 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006833 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 win_T *wp;
6835 int i;
6836 linenr_T lnum;
6837 int histtype;
6838 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006839#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 int save_restart_edit = restart_edit;
6843 int save_State = State;
6844 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006845#ifdef FEAT_RIGHTLEFT
6846 int save_cmdmsg_rl = cmdmsg_rl;
6847#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006848#ifdef FEAT_FOLDING
6849 int save_KeyTyped;
6850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
6852 /* Can't do this recursively. Can't do it when typing a password. */
6853 if (cmdwin_type != 0
6854# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6855 || cmdline_star > 0
6856# endif
6857 )
6858 {
6859 beep_flush();
6860 return K_IGNORE;
6861 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006862 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
6864 /* Save current window sizes. */
6865 win_size_save(&winsizes);
6866
6867# ifdef FEAT_AUTOCMD
6868 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006869 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006871 /* don't use a new tab page */
6872 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006873 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006874
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 /* Create a window for the command-line buffer. */
6876 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6877 {
6878 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006879# ifdef FEAT_AUTOCMD
6880 unblock_autocmds();
6881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 return K_IGNORE;
6883 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006884 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
6886 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006887 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006888 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006891#ifdef FEAT_FOLDING
6892 curwin->w_p_fen = FALSE;
6893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006895 curwin->w_p_rl = cmdmsg_rl;
6896 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006898 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
6900# ifdef FEAT_AUTOCMD
6901 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006902 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006903 /* But don't allow switching to another buffer. */
6904 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905# endif
6906
Bram Moolenaar46152342005-09-07 21:18:43 +00006907 /* Showing the prompt may have set need_wait_return, reset it. */
6908 need_wait_return = FALSE;
6909
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006910 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6912 {
6913 if (p_wc == TAB)
6914 {
6915 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6916 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6917 }
6918 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6919 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006920# ifdef FEAT_AUTOCMD
6921 --curbuf_lock;
6922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006924 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6925 * sets 'textwidth' to 78). */
6926 curbuf->b_p_tw = 0;
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 /* Fill the buffer with the history. */
6929 init_history();
6930 if (hislen > 0)
6931 {
6932 i = hisidx[histtype];
6933 if (i >= 0)
6934 {
6935 lnum = 0;
6936 do
6937 {
6938 if (++i == hislen)
6939 i = 0;
6940 if (history[histtype][i].hisstr != NULL)
6941 ml_append(lnum++, history[histtype][i].hisstr,
6942 (colnr_T)0, FALSE);
6943 }
6944 while (i != hisidx[histtype]);
6945 }
6946 }
6947
6948 /* Replace the empty last line with the current command-line and put the
6949 * cursor there. */
6950 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6951 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6952 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006953 changed_line_abv_curs();
6954 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006955 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
6957 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006958 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
6960 /* No Ex mode here! */
6961 exmode_active = 0;
6962
6963 State = NORMAL;
6964# ifdef FEAT_MOUSE
6965 setmouse();
6966# endif
6967
6968# ifdef FEAT_AUTOCMD
6969 /* Trigger CmdwinEnter autocommands. */
6970 typestr[0] = cmdwin_type;
6971 typestr[1] = NUL;
6972 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006973 if (restart_edit != 0) /* autocmd with ":startinsert" */
6974 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975# endif
6976
6977 i = RedrawingDisabled;
6978 RedrawingDisabled = 0;
6979
6980 /*
6981 * Call the main loop until <CR> or CTRL-C is typed.
6982 */
6983 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006984 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 RedrawingDisabled = i;
6987
6988# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006989
6990# ifdef FEAT_FOLDING
6991 save_KeyTyped = KeyTyped;
6992# endif
6993
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 /* Trigger CmdwinLeave autocommands. */
6995 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006996
6997# ifdef FEAT_FOLDING
6998 /* Restore KeyTyped in case it is modified by autocommands */
6999 KeyTyped = save_KeyTyped;
7000# endif
7001
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002# endif
7003
Bram Moolenaarccc18222007-05-10 18:25:20 +00007004 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007005 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 cmdwin_type = 0;
7007
7008 exmode_active = save_exmode;
7009
Bram Moolenaarf9821062008-06-20 16:31:07 +00007010 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007012 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
7014 cmdwin_result = Ctrl_C;
7015 EMSG(_("E199: Active window or buffer deleted"));
7016 }
7017 else
7018 {
7019# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7020 /* autocmds may abort script processing */
7021 if (aborting() && cmdwin_result != K_IGNORE)
7022 cmdwin_result = Ctrl_C;
7023# endif
7024 /* Set the new command line from the cmdline buffer. */
7025 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007026 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007028 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7029
7030 if (histtype == HIST_CMD)
7031 {
7032 /* Execute the command directly. */
7033 ccline.cmdbuff = vim_strsave((char_u *)p);
7034 cmdwin_result = CAR;
7035 }
7036 else
7037 {
7038 /* First need to cancel what we were doing. */
7039 ccline.cmdbuff = NULL;
7040 stuffcharReadbuff(':');
7041 stuffReadbuff((char_u *)p);
7042 stuffcharReadbuff(CAR);
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045 else if (cmdwin_result == K_XF2) /* :qa typed */
7046 {
7047 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7048 cmdwin_result = CAR;
7049 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007050 else if (cmdwin_result == Ctrl_C)
7051 {
7052 /* :q or :close, don't execute any command
7053 * and don't modify the cmd window. */
7054 ccline.cmdbuff = NULL;
7055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 else
7057 ccline.cmdbuff = vim_strsave(ml_get_curline());
7058 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007059 {
7060 ccline.cmdbuff = vim_strsave((char_u *)"");
7061 ccline.cmdlen = 0;
7062 ccline.cmdbufflen = 1;
7063 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 else
7067 {
7068 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7069 ccline.cmdbufflen = ccline.cmdlen + 1;
7070 ccline.cmdpos = curwin->w_cursor.col;
7071 if (ccline.cmdpos > ccline.cmdlen)
7072 ccline.cmdpos = ccline.cmdlen;
7073 if (cmdwin_result == K_IGNORE)
7074 {
7075 set_cmdspos_cursor();
7076 redrawcmd();
7077 }
7078 }
7079
7080# ifdef FEAT_AUTOCMD
7081 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007082 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007084# ifdef FEAT_CONCEAL
7085 /* Avoid command-line window first character being concealed. */
7086 curwin->w_p_cole = 0;
7087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007089 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 win_goto(old_curwin);
7091 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007092
7093 /* win_close() may have already wiped the buffer when 'bh' is
7094 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007095 if (bufref_valid(&bufref))
7096 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098 /* Restore window sizes. */
7099 win_size_restore(&winsizes);
7100
7101# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007102 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103# endif
7104 }
7105
7106 ga_clear(&winsizes);
7107 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007108# ifdef FEAT_RIGHTLEFT
7109 cmdmsg_rl = save_cmdmsg_rl;
7110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
7112 State = save_State;
7113# ifdef FEAT_MOUSE
7114 setmouse();
7115# endif
7116
7117 return cmdwin_result;
7118}
7119#endif /* FEAT_CMDWIN */
7120
7121/*
7122 * Used for commands that either take a simple command string argument, or:
7123 * cmd << endmarker
7124 * {script}
7125 * endmarker
7126 * Returns a pointer to allocated memory with {script} or NULL.
7127 */
7128 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 char_u *theline;
7132 char *end_pattern = NULL;
7133 char dot[] = ".";
7134 garray_T ga;
7135
7136 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7137 return NULL;
7138
7139 ga_init2(&ga, 1, 0x400);
7140
7141 if (cmd[2] != NUL)
7142 end_pattern = (char *)skipwhite(cmd + 2);
7143 else
7144 end_pattern = dot;
7145
7146 for (;;)
7147 {
7148 theline = eap->getline(
7149#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007150 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151#endif
7152 NUL, eap->cookie, 0);
7153
7154 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007155 {
7156 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
7160 ga_concat(&ga, theline);
7161 ga_append(&ga, '\n');
7162 vim_free(theline);
7163 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007164 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
7166 return (char_u *)ga.ga_data;
7167}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007168
7169#ifdef FEAT_SEARCH_EXTRA
7170 static void
7171set_search_match(pos_T *t)
7172{
7173 /*
7174 * First move cursor to end of match, then to the start. This
7175 * moves the whole match onto the screen when 'nowrap' is set.
7176 */
7177 t->lnum += search_match_lines;
7178 t->col = search_match_endcol;
7179 if (t->lnum > curbuf->b_ml.ml_line_count)
7180 {
7181 t->lnum = curbuf->b_ml.ml_line_count;
7182 coladvance((colnr_T)MAXCOL);
7183 }
7184}
7185#endif