blob: 9f7dad99aa5ea73af39b470917375b978c577304 [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
Bram Moolenaar4033c552017-09-16 20:54:51 +02001128#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 /* 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 /* May redraw the status line to show the cursor position. */
1970 if (p_ru && curwin->w_status_height > 0)
1971 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001973 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001974 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001975 restore_cmdline(&save_ccline);
1976
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001977 /* Leave it at the end to make CTRL-R CTRL-W work. */
1978 if (i != 0)
1979 curwin->w_cursor = end_pos;
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 msg_starthere();
1982 redrawcmdline();
1983 did_incsearch = TRUE;
1984 }
1985#else /* FEAT_SEARCH_EXTRA */
1986 ;
1987#endif
1988
1989#ifdef FEAT_RIGHTLEFT
1990 if (cmdmsg_rl
1991# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001992 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993# endif
1994 )
1995 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001996 * right-left typing. Not efficient, but it works.
1997 * Do it only when there are no characters left to read
1998 * to avoid useless intermediate redraws. */
1999 if (vpeekc() == NUL)
2000 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#endif
2002 }
2003
2004returncmd:
2005
2006#ifdef FEAT_RIGHTLEFT
2007 cmdmsg_rl = FALSE;
2008#endif
2009
2010#ifdef FEAT_FKMAP
2011 cmd_fkmap = 0;
2012#endif
2013
2014 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002015 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
2017#ifdef FEAT_SEARCH_EXTRA
2018 if (did_incsearch)
2019 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002020 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002021 curwin->w_cursor = save_cursor;
2022 else
2023 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002024 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002025 {
2026 /* put the '" mark at the original position */
2027 curwin->w_cursor = save_cursor;
2028 setpcmark();
2029 }
2030 curwin->w_cursor = search_start;
2031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 curwin->w_curswant = old_curswant;
2033 curwin->w_leftcol = old_leftcol;
2034 curwin->w_topline = old_topline;
2035# ifdef FEAT_DIFF
2036 curwin->w_topfill = old_topfill;
2037# endif
2038 curwin->w_botline = old_botline;
2039 highlight_match = FALSE;
2040 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002041 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
2043#endif
2044
2045 if (ccline.cmdbuff != NULL)
2046 {
2047 /*
2048 * Put line in history buffer (":" and "=" only when it was typed).
2049 */
2050#ifdef FEAT_CMDHIST
2051 if (ccline.cmdlen && firstc != NUL
2052 && (some_key_typed || histype == HIST_SEARCH))
2053 {
2054 add_to_history(histype, ccline.cmdbuff, TRUE,
2055 histype == HIST_SEARCH ? firstc : NUL);
2056 if (firstc == ':')
2057 {
2058 vim_free(new_last_cmdline);
2059 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2060 }
2061 }
2062#endif
2063
2064 if (gotesc) /* abandon command line */
2065 {
2066 vim_free(ccline.cmdbuff);
2067 ccline.cmdbuff = NULL;
2068 if (msg_scrolled == 0)
2069 compute_cmdrow();
2070 MSG("");
2071 redraw_cmdline = TRUE;
2072 }
2073 }
2074
2075 /*
2076 * If the screen was shifted up, redraw the whole screen (later).
2077 * If the line is too long, clear it, so ruler and shown command do
2078 * not get printed in the middle of it.
2079 */
2080 msg_check();
2081 msg_scroll = save_msg_scroll;
2082 redir_off = FALSE;
2083
2084 /* When the command line was typed, no need for a wait-return prompt. */
2085 if (some_key_typed)
2086 need_wait_return = FALSE;
2087
2088 State = save_State;
2089#ifdef USE_IM_CONTROL
2090 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2091 im_save_status(b_im_ptr);
2092 im_set_active(FALSE);
2093#endif
2094#ifdef FEAT_MOUSE
2095 setmouse();
2096#endif
2097#ifdef CURSOR_SHAPE
2098 ui_cursor_shape(); /* may show different cursor shape */
2099#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002100 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002102 {
2103 char_u *p = ccline.cmdbuff;
2104
2105 /* Make ccline empty, getcmdline() may try to use it. */
2106 ccline.cmdbuff = NULL;
2107 return p;
2108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109}
2110
2111#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2112/*
2113 * Get a command line with a prompt.
2114 * This is prepared to be called recursively from getcmdline() (e.g. by
2115 * f_input() when evaluating an expression from CTRL-R =).
2116 * Returns the command line in allocated memory, or NULL.
2117 */
2118 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002119getcmdline_prompt(
2120 int firstc,
2121 char_u *prompt, /* command line prompt */
2122 int attr, /* attributes for prompt */
2123 int xp_context, /* type of expansion */
2124 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 char_u *s;
2127 struct cmdline_info save_ccline;
2128 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002129 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002131 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ccline.cmdprompt = prompt;
2133 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002134# ifdef FEAT_EVAL
2135 ccline.xp_context = xp_context;
2136 ccline.xp_arg = xp_arg;
2137 ccline.input_fn = (firstc == '@');
2138# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002139 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002141 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002142 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002143 /* Restore msg_col, the prompt from input() may have changed it.
2144 * But only if called recursively and the commandline is therefore being
2145 * restored to an old one; if not, the input() prompt stays on the screen,
2146 * so we need its modified msg_col left intact. */
2147 if (ccline.cmdbuff != NULL)
2148 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 return s;
2151}
2152#endif
2153
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002154/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002155 * Return TRUE when the text must not be changed and we can't switch to
2156 * another window or buffer. Used when editing the command line, evaluating
2157 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002158 */
2159 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002160text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002161{
2162#ifdef FEAT_CMDWIN
2163 if (cmdwin_type != 0)
2164 return TRUE;
2165#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002166 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002167}
2168
2169/*
2170 * Give an error message for a command that isn't allowed while the cmdline
2171 * window is open or editing the cmdline in another way.
2172 */
2173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002174text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002175{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002176 EMSG(_(get_text_locked_msg()));
2177}
2178
2179 char_u *
2180get_text_locked_msg(void)
2181{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002182#ifdef FEAT_CMDWIN
2183 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002184 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002185#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002186 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002187}
2188
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189#if defined(FEAT_AUTOCMD) || defined(PROTO)
2190/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002191 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2192 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002193 */
2194 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002195curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196{
2197 if (curbuf_lock > 0)
2198 {
2199 EMSG(_("E788: Not allowed to edit another buffer now"));
2200 return TRUE;
2201 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002202 return allbuf_locked();
2203}
2204
2205/*
2206 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2207 * message.
2208 */
2209 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002210allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002211{
2212 if (allbuf_lock > 0)
2213 {
2214 EMSG(_("E811: Not allowed to change buffer information now"));
2215 return TRUE;
2216 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002217 return FALSE;
2218}
2219#endif
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002222cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2225 if (cmdline_star > 0) /* showing '*', always 1 position */
2226 return 1;
2227#endif
2228 return ptr2cells(ccline.cmdbuff + idx);
2229}
2230
2231/*
2232 * Compute the offset of the cursor on the command line for the prompt and
2233 * indent.
2234 */
2235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002236set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002238 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 ccline.cmdspos = 1 + ccline.cmdindent;
2240 else
2241 ccline.cmdspos = 0 + ccline.cmdindent;
2242}
2243
2244/*
2245 * Compute the screen position for the cursor on the command line.
2246 */
2247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002248set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250 int i, m, c;
2251
2252 set_cmdspos();
2253 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002256 if (m < 0) /* overflow, Columns or Rows at weird value */
2257 m = MAXCOL;
2258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 else
2260 m = MAXCOL;
2261 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2262 {
2263 c = cmdline_charsize(i);
2264#ifdef FEAT_MBYTE
2265 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2266 if (has_mbyte)
2267 correct_cmdspos(i, c);
2268#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002269 /* If the cmdline doesn't fit, show cursor on last visible char.
2270 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if ((ccline.cmdspos += c) >= m)
2272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 ccline.cmdspos -= c;
2274 break;
2275 }
2276#ifdef FEAT_MBYTE
2277 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002278 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279#endif
2280 }
2281}
2282
2283#ifdef FEAT_MBYTE
2284/*
2285 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2286 * character that doesn't fit, so that a ">" must be displayed.
2287 */
2288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002289correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002291 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2293 && ccline.cmdspos % Columns + cells > Columns)
2294 ccline.cmdspos++;
2295}
2296#endif
2297
2298/*
2299 * Get an Ex command line for the ":" command.
2300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002302getexline(
2303 int c, /* normally ':', NUL for ":append" */
2304 void *cookie UNUSED,
2305 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 /* When executing a register, remove ':' that's in front of each line. */
2308 if (exec_from_reg && vpeekc() == ':')
2309 (void)vgetc();
2310 return getcmdline(c, 1L, indent);
2311}
2312
2313/*
2314 * Get an Ex command line for Ex mode.
2315 * In Ex mode we only use the OS supplied line editing features and no
2316 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002320getexmodeline(
2321 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002323 void *cookie UNUSED,
2324 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 garray_T line_ga;
2327 char_u *pend;
2328 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002329 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 int escaped = FALSE; /* CTRL-V typed */
2331 int vcol = 0;
2332 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002333 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002334 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
2336 /* Switch cursor on now. This avoids that it happens after the "\n", which
2337 * confuses the system function that computes tabstops. */
2338 cursor_on();
2339
2340 /* always start in column 0; write a newline if necessary */
2341 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002344 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002346 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002347 if (p_prompt)
2348 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 while (indent-- > 0)
2350 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353
2354 ga_init2(&line_ga, 1, 30);
2355
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002356 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002357 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002358 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002359 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002360 while (indent >= 8)
2361 {
2362 ga_append(&line_ga, TAB);
2363 msg_puts((char_u *)" ");
2364 indent -= 8;
2365 }
2366 while (indent-- > 0)
2367 {
2368 ga_append(&line_ga, ' ');
2369 msg_putchar(' ');
2370 }
2371 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002372 ++no_mapping;
2373 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002374
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 /*
2376 * Get the line, one character at a time.
2377 */
2378 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002379 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002381 long sw;
2382 char_u *s;
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (ga_grow(&line_ga, 40) == FAIL)
2385 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaarba748c82017-02-26 14:00:07 +01002387 /*
2388 * Get one character at a time.
2389 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002390 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002391
2392 /* Check for a ":normal" command and no more characters left. */
2393 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2394 c1 = '\n';
2395 else
2396 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002399 * Handle line editing.
2400 * Previously this was left to the system, putting the terminal in
2401 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002403 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002405 msg_putchar('\n');
2406 break;
2407 }
2408
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002409 if (c1 == K_PS)
2410 {
2411 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2412 goto redraw;
2413 }
2414
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002415 if (!escaped)
2416 {
2417 /* CR typed means "enter", which is NL */
2418 if (c1 == '\r')
2419 c1 = '\n';
2420
2421 if (c1 == BS || c1 == K_BS
2422 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002424 if (line_ga.ga_len > 0)
2425 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002426#ifdef FEAT_MBYTE
2427 if (has_mbyte)
2428 {
2429 p = (char_u *)line_ga.ga_data;
2430 p[line_ga.ga_len] = NUL;
2431 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2432 line_ga.ga_len -= len;
2433 }
2434 else
2435#endif
2436 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002437 goto redraw;
2438 }
2439 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
2441
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002442 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002444 msg_col = startcol;
2445 msg_clr_eos();
2446 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002447 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002448 }
2449
2450 if (c1 == Ctrl_T)
2451 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002452 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002453 p = (char_u *)line_ga.ga_data;
2454 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002455 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002456 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002457add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002458 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002460 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002461 p = (char_u *)line_ga.ga_data;
2462 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002463 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2464 *s = ' ';
2465 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002467redraw:
2468 /* redraw the line */
2469 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002470 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002471 p = (char_u *)line_ga.ga_data;
2472 p[line_ga.ga_len] = NUL;
2473 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002475 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002477 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002479 msg_putchar(' ');
2480 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002481 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002483 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002485 len = MB_PTR2LEN(p);
2486 msg_outtrans_len(p, len);
2487 vcol += ptr2cells(p);
2488 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002491 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002492 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002493 continue;
2494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002496 if (c1 == Ctrl_D)
2497 {
2498 /* Delete one shiftwidth. */
2499 p = (char_u *)line_ga.ga_data;
2500 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002502 if (prev_char == '^')
2503 ex_keep_indent = TRUE;
2504 indent = 0;
2505 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507 else
2508 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002509 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002510 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002511 if (indent > 0)
2512 {
2513 --indent;
2514 indent -= indent % get_sw_value(curbuf);
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002517 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002518 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002519 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002520 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2521 --line_ga.ga_len;
2522 }
2523 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002525
2526 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2527 {
2528 escaped = TRUE;
2529 continue;
2530 }
2531
2532 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2533 if (IS_SPECIAL(c1))
2534 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002536
2537 if (IS_SPECIAL(c1))
2538 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002539#ifdef FEAT_MBYTE
2540 if (has_mbyte)
2541 len = (*mb_char2bytes)(c1,
2542 (char_u *)line_ga.ga_data + line_ga.ga_len);
2543 else
2544#endif
2545 {
2546 len = 1;
2547 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2548 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002549 if (c1 == '\n')
2550 msg_putchar('\n');
2551 else if (c1 == TAB)
2552 {
2553 /* Don't use chartabsize(), 'ts' can be different */
2554 do
2555 {
2556 msg_putchar(' ');
2557 } while (++vcol % 8);
2558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002561 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002562 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002565 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002566 escaped = FALSE;
2567
2568 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002569 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002570
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002571 /* We are done when a NL is entered, but not when it comes after an
2572 * odd number of backslashes, that results in a NUL. */
2573 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002575 int bcount = 0;
2576
2577 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2578 ++bcount;
2579
2580 if (bcount > 0)
2581 {
2582 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2583 * "\NL", etc. */
2584 line_ga.ga_len -= (bcount + 1) / 2;
2585 pend -= (bcount + 1) / 2;
2586 pend[-1] = '\n';
2587 }
2588
2589 if ((bcount & 1) == 0)
2590 {
2591 --line_ga.ga_len;
2592 --pend;
2593 *pend = NUL;
2594 break;
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597 }
2598
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002599 --no_mapping;
2600 --allow_keys;
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 /* make following messages go to the next line */
2603 msg_didout = FALSE;
2604 msg_col = 0;
2605 if (msg_row < Rows - 1)
2606 ++msg_row;
2607 emsg_on_display = FALSE; /* don't want ui_delay() */
2608
2609 if (got_int)
2610 ga_clear(&line_ga);
2611
2612 return (char_u *)line_ga.ga_data;
2613}
2614
Bram Moolenaare344bea2005-09-01 20:46:49 +00002615# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2616 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617/*
2618 * Return TRUE if ccline.overstrike is on.
2619 */
2620 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002621cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
2623 return ccline.overstrike;
2624}
2625
2626/*
2627 * Return TRUE if the cursor is at the end of the cmdline.
2628 */
2629 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002630cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 return (ccline.cmdpos >= ccline.cmdlen);
2633}
2634#endif
2635
Bram Moolenaar9372a112005-12-06 19:59:18 +00002636#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637/*
2638 * Return the virtual column number at the current cursor position.
2639 * This is used by the IM code to obtain the start of the preedit string.
2640 */
2641 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002642cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2645 return MAXCOL;
2646
2647# ifdef FEAT_MBYTE
2648 if (has_mbyte)
2649 {
2650 colnr_T col;
2651 int i = 0;
2652
2653 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002654 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 return col;
2657 }
2658 else
2659# endif
2660 return ccline.cmdpos;
2661}
2662#endif
2663
2664#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2665/*
2666 * If part of the command line is an IM preedit string, redraw it with
2667 * IM feedback attributes. The cursor position is restored after drawing.
2668 */
2669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002670redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671{
2672 if ((State & CMDLINE)
2673 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002674 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 && !p_imdisable
2676 && im_is_preediting())
2677 {
2678 int cmdpos = 0;
2679 int cmdspos;
2680 int old_row;
2681 int old_col;
2682 colnr_T col;
2683
2684 old_row = msg_row;
2685 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002686 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688# ifdef FEAT_MBYTE
2689 if (has_mbyte)
2690 {
2691 for (col = 0; col < preedit_start_col
2692 && cmdpos < ccline.cmdlen; ++col)
2693 {
2694 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002695 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697 }
2698 else
2699# endif
2700 {
2701 cmdspos += preedit_start_col;
2702 cmdpos += preedit_start_col;
2703 }
2704
2705 msg_row = cmdline_row + (cmdspos / (int)Columns);
2706 msg_col = cmdspos % (int)Columns;
2707 if (msg_row >= Rows)
2708 msg_row = Rows - 1;
2709
2710 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2711 {
2712 int char_len;
2713 int char_attr;
2714
2715 char_attr = im_get_feedback_attr(col);
2716 if (char_attr < 0)
2717 break; /* end of preedit string */
2718
2719# ifdef FEAT_MBYTE
2720 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002721 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 else
2723# endif
2724 char_len = 1;
2725
2726 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2727 cmdpos += char_len;
2728 }
2729
2730 msg_row = old_row;
2731 msg_col = old_col;
2732 }
2733}
2734#endif /* FEAT_XIM && FEAT_GUI_GTK */
2735
2736/*
2737 * Allocate a new command line buffer.
2738 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2739 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2740 */
2741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002742alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 /*
2745 * give some extra space to avoid having to allocate all the time
2746 */
2747 if (len < 80)
2748 len = 100;
2749 else
2750 len += 20;
2751
2752 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2753 ccline.cmdbufflen = len;
2754}
2755
2756/*
2757 * Re-allocate the command line to length len + something extra.
2758 * return FAIL for failure, OK otherwise
2759 */
2760 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002761realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
2763 char_u *p;
2764
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002765 if (len < ccline.cmdbufflen)
2766 return OK; /* no need to resize */
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 p = ccline.cmdbuff;
2769 alloc_cmdbuff(len); /* will get some more */
2770 if (ccline.cmdbuff == NULL) /* out of memory */
2771 {
2772 ccline.cmdbuff = p; /* keep the old one */
2773 return FAIL;
2774 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002775 /* There isn't always a NUL after the command, but it may need to be
2776 * there, thus copy up to the NUL and add a NUL. */
2777 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2778 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002780
2781 if (ccline.xpc != NULL
2782 && ccline.xpc->xp_pattern != NULL
2783 && ccline.xpc->xp_context != EXPAND_NOTHING
2784 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2785 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002786 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002787
2788 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2789 * to point into the newly allocated memory. */
2790 if (i >= 0 && i <= ccline.cmdlen)
2791 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2792 }
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return OK;
2795}
2796
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002797#if defined(FEAT_ARABIC) || defined(PROTO)
2798static char_u *arshape_buf = NULL;
2799
2800# if defined(EXITFREE) || defined(PROTO)
2801 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002802free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002803{
2804 vim_free(arshape_buf);
2805}
2806# endif
2807#endif
2808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809/*
2810 * Draw part of the cmdline at the current cursor position. But draw stars
2811 * when cmdline_star is TRUE.
2812 */
2813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002814draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2817 int i;
2818
2819 if (cmdline_star > 0)
2820 for (i = 0; i < len; ++i)
2821 {
2822 msg_putchar('*');
2823# ifdef FEAT_MBYTE
2824 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002825 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826# endif
2827 }
2828 else
2829#endif
2830#ifdef FEAT_ARABIC
2831 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 static int buflen = 0;
2834 char_u *p;
2835 int j;
2836 int newlen = 0;
2837 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002838 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 int prev_c = 0;
2840 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002841 int u8c;
2842 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 /*
2846 * Do arabic shaping into a temporary buffer. This is very
2847 * inefficient!
2848 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002849 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
2851 /* Re-allocate the buffer. We keep it around to avoid a lot of
2852 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002853 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002854 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002855 arshape_buf = alloc(buflen);
2856 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 return; /* out of memory */
2858 }
2859
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002860 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2861 {
2862 /* Prepend a space to draw the leading composing char on. */
2863 arshape_buf[0] = ' ';
2864 newlen = 1;
2865 }
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 for (j = start; j < start + len; j += mb_l)
2868 {
2869 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002870 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002871 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (ARABIC_CHAR(u8c))
2873 {
2874 /* Do Arabic shaping. */
2875 if (cmdmsg_rl)
2876 {
2877 /* displaying from right to left */
2878 pc = prev_c;
2879 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002880 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (j + mb_l >= start + len)
2882 nc = NUL;
2883 else
2884 nc = utf_ptr2char(p + mb_l);
2885 }
2886 else
2887 {
2888 /* displaying from left to right */
2889 if (j + mb_l >= start + len)
2890 pc = NUL;
2891 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002892 {
2893 int pcc[MAX_MCO];
2894
2895 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002897 pc1 = pcc[0];
2898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 nc = prev_c;
2900 }
2901 prev_c = u8c;
2902
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002903 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002905 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002906 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002908 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2909 if (u8cc[1] != 0)
2910 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002911 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 }
2914 else
2915 {
2916 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002917 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 newlen += mb_l;
2919 }
2920 }
2921
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002922 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 else
2925#endif
2926 msg_outtrans_len(ccline.cmdbuff + start, len);
2927}
2928
2929/*
2930 * Put a character on the command line. Shifts the following text to the
2931 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2932 * "c" must be printable (fit in one display cell)!
2933 */
2934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002935putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 if (cmd_silent)
2938 return;
2939 msg_no_more = TRUE;
2940 msg_putchar(c);
2941 if (shift)
2942 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2943 msg_no_more = FALSE;
2944 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002945 extra_char = c;
2946 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947}
2948
2949/*
2950 * Undo a putcmdline(c, FALSE).
2951 */
2952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002953unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 if (cmd_silent)
2956 return;
2957 msg_no_more = TRUE;
2958 if (ccline.cmdlen == ccline.cmdpos)
2959 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002960#ifdef FEAT_MBYTE
2961 else if (has_mbyte)
2962 draw_cmdline(ccline.cmdpos,
2963 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else
2966 draw_cmdline(ccline.cmdpos, 1);
2967 msg_no_more = FALSE;
2968 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002969 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970}
2971
2972/*
2973 * Put the given string, of the given length, onto the command line.
2974 * If len is -1, then STRLEN() is used to calculate the length.
2975 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2976 * part will be redrawn, otherwise it will not. If this function is called
2977 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2978 * called afterwards.
2979 */
2980 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002981put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 int retval;
2984 int i;
2985 int m;
2986 int c;
2987
2988 if (len < 0)
2989 len = (int)STRLEN(str);
2990
2991 /* Check if ccline.cmdbuff needs to be longer */
2992 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002993 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 else
2995 retval = OK;
2996 if (retval == OK)
2997 {
2998 if (!ccline.overstrike)
2999 {
3000 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3001 ccline.cmdbuff + ccline.cmdpos,
3002 (size_t)(ccline.cmdlen - ccline.cmdpos));
3003 ccline.cmdlen += len;
3004 }
3005 else
3006 {
3007#ifdef FEAT_MBYTE
3008 if (has_mbyte)
3009 {
3010 /* Count nr of characters in the new string. */
3011 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003012 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 ++m;
3014 /* Count nr of bytes in cmdline that are overwritten by these
3015 * characters. */
3016 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003017 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 --m;
3019 if (i < ccline.cmdlen)
3020 {
3021 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3022 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3023 ccline.cmdlen += ccline.cmdpos + len - i;
3024 }
3025 else
3026 ccline.cmdlen = ccline.cmdpos + len;
3027 }
3028 else
3029#endif
3030 if (ccline.cmdpos + len > ccline.cmdlen)
3031 ccline.cmdlen = ccline.cmdpos + len;
3032 }
3033 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3034 ccline.cmdbuff[ccline.cmdlen] = NUL;
3035
3036#ifdef FEAT_MBYTE
3037 if (enc_utf8)
3038 {
3039 /* When the inserted text starts with a composing character,
3040 * backup to the character before it. There could be two of them.
3041 */
3042 i = 0;
3043 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3044 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3045 {
3046 i = (*mb_head_off)(ccline.cmdbuff,
3047 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3048 ccline.cmdpos -= i;
3049 len += i;
3050 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3051 }
3052# ifdef FEAT_ARABIC
3053 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3054 {
3055 /* Check the previous character for Arabic combining pair. */
3056 i = (*mb_head_off)(ccline.cmdbuff,
3057 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3058 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3059 + ccline.cmdpos - i), c))
3060 {
3061 ccline.cmdpos -= i;
3062 len += i;
3063 }
3064 else
3065 i = 0;
3066 }
3067# endif
3068 if (i != 0)
3069 {
3070 /* Also backup the cursor position. */
3071 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3072 ccline.cmdspos -= i;
3073 msg_col -= i;
3074 if (msg_col < 0)
3075 {
3076 msg_col += Columns;
3077 --msg_row;
3078 }
3079 }
3080 }
3081#endif
3082
3083 if (redraw && !cmd_silent)
3084 {
3085 msg_no_more = TRUE;
3086 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003087 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3089 /* Avoid clearing the rest of the line too often. */
3090 if (cmdline_row != i || ccline.overstrike)
3091 msg_clr_eos();
3092 msg_no_more = FALSE;
3093 }
3094#ifdef FEAT_FKMAP
3095 /*
3096 * If we are in Farsi command mode, the character input must be in
3097 * Insert mode. So do not advance the cmdpos.
3098 */
3099 if (!cmd_fkmap)
3100#endif
3101 {
3102 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003105 if (m < 0) /* overflow, Columns or Rows at weird value */
3106 m = MAXCOL;
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else
3109 m = MAXCOL;
3110 for (i = 0; i < len; ++i)
3111 {
3112 c = cmdline_charsize(ccline.cmdpos);
3113#ifdef FEAT_MBYTE
3114 /* count ">" for a double-wide char that doesn't fit. */
3115 if (has_mbyte)
3116 correct_cmdspos(ccline.cmdpos, c);
3117#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003118 /* Stop cursor at the end of the screen, but do increment the
3119 * insert position, so that entering a very long command
3120 * works, even though you can't see it. */
3121 if (ccline.cmdspos + c < m)
3122 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#ifdef FEAT_MBYTE
3124 if (has_mbyte)
3125 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003126 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (c > len - i - 1)
3128 c = len - i - 1;
3129 ccline.cmdpos += c;
3130 i += c;
3131 }
3132#endif
3133 ++ccline.cmdpos;
3134 }
3135 }
3136 }
3137 if (redraw)
3138 msg_check();
3139 return retval;
3140}
3141
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003142static struct cmdline_info prev_ccline;
3143static int prev_ccline_used = FALSE;
3144
3145/*
3146 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3147 * and overwrite it. But get_cmdline_str() may need it, thus make it
3148 * available globally in prev_ccline.
3149 */
3150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003152{
3153 if (!prev_ccline_used)
3154 {
3155 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3156 prev_ccline_used = TRUE;
3157 }
3158 *ccp = prev_ccline;
3159 prev_ccline = ccline;
3160 ccline.cmdbuff = NULL;
3161 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003162 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003163}
3164
3165/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003166 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003167 */
3168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003169restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003170{
3171 ccline = prev_ccline;
3172 prev_ccline = *ccp;
3173}
3174
Bram Moolenaar5a305422006-04-28 22:38:25 +00003175#if defined(FEAT_EVAL) || defined(PROTO)
3176/*
3177 * Save the command line into allocated memory. Returns a pointer to be
3178 * passed to restore_cmdline_alloc() later.
3179 * Returns NULL when failed.
3180 */
3181 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003182save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003183{
3184 struct cmdline_info *p;
3185
3186 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3187 if (p != NULL)
3188 save_cmdline(p);
3189 return (char_u *)p;
3190}
3191
3192/*
3193 * Restore the command line from the return value of save_cmdline_alloc().
3194 */
3195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003197{
3198 if (p != NULL)
3199 {
3200 restore_cmdline((struct cmdline_info *)p);
3201 vim_free(p);
3202 }
3203}
3204#endif
3205
Bram Moolenaar8299df92004-07-10 09:47:34 +00003206/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003207 * Paste a yank register into the command line.
3208 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003209 * insert_reg() can't be used here, because special characters from the
3210 * register contents will be interpreted as commands.
3211 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003212 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003213 */
3214 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003215cmdline_paste(
3216 int regname,
3217 int literally, /* Insert text literally instead of "as typed" */
3218 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003219{
3220 long i;
3221 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003222 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003223 int allocated;
3224 struct cmdline_info save_ccline;
3225
3226 /* check for valid regname; also accept special characters for CTRL-R in
3227 * the command line */
3228 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3229 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3230 return FAIL;
3231
3232 /* A register containing CTRL-R can cause an endless loop. Allow using
3233 * CTRL-C to break the loop. */
3234 line_breakcheck();
3235 if (got_int)
3236 return FAIL;
3237
3238#ifdef FEAT_CLIPBOARD
3239 regname = may_get_selection(regname);
3240#endif
3241
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003242 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003243 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003244 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003245 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003246 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003247 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003248 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003249
3250 if (i)
3251 {
3252 /* Got the value of a special register in "arg". */
3253 if (arg == NULL)
3254 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003255
3256 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3257 * part of the word. */
3258 p = arg;
3259 if (p_is && regname == Ctrl_W)
3260 {
3261 char_u *w;
3262 int len;
3263
3264 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003265 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003266 {
3267#ifdef FEAT_MBYTE
3268 if (has_mbyte)
3269 {
3270 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3271 if (!vim_iswordc(mb_ptr2char(w - len)))
3272 break;
3273 w -= len;
3274 }
3275 else
3276#endif
3277 {
3278 if (!vim_iswordc(w[-1]))
3279 break;
3280 --w;
3281 }
3282 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003283 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003284 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3285 p += len;
3286 }
3287
3288 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003289 if (allocated)
3290 vim_free(arg);
3291 return OK;
3292 }
3293
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003294 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003295}
3296
3297/*
3298 * Put a string on the command line.
3299 * When "literally" is TRUE, insert literally.
3300 * When "literally" is FALSE, insert as typed, but don't leave the command
3301 * line.
3302 */
3303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003304cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003305{
3306 int c, cv;
3307
3308 if (literally)
3309 put_on_cmdline(s, -1, TRUE);
3310 else
3311 while (*s != NUL)
3312 {
3313 cv = *s;
3314 if (cv == Ctrl_V && s[1])
3315 ++s;
3316#ifdef FEAT_MBYTE
3317 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003318 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003319 else
3320#endif
3321 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003322 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3323 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003324#ifdef UNIX
3325 || c == intr_char
3326#endif
3327 || (c == Ctrl_BSL && *s == Ctrl_N))
3328 stuffcharReadbuff(Ctrl_V);
3329 stuffcharReadbuff(c);
3330 }
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#ifdef FEAT_WILDMENU
3334/*
3335 * Delete characters on the command line, from "from" to the current
3336 * position.
3337 */
3338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003339cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3342 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3343 ccline.cmdlen -= ccline.cmdpos - from;
3344 ccline.cmdpos = from;
3345}
3346#endif
3347
3348/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003349 * This function is called when the screen size changes and with incremental
3350 * search and in other situations where the command line may have been
3351 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
3353 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003354redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003356 redrawcmdline_ex(TRUE);
3357}
3358
3359 void
3360redrawcmdline_ex(int do_compute_cmdrow)
3361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (cmd_silent)
3363 return;
3364 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003365 if (do_compute_cmdrow)
3366 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 redrawcmd();
3368 cursorcmd();
3369}
3370
3371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003372redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 int i;
3375
3376 if (cmd_silent)
3377 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003378 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 msg_putchar(ccline.cmdfirstc);
3380 if (ccline.cmdprompt != NULL)
3381 {
3382 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3383 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3384 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003385 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 --ccline.cmdindent;
3387 }
3388 else
3389 for (i = ccline.cmdindent; i > 0; --i)
3390 msg_putchar(' ');
3391}
3392
3393/*
3394 * Redraw what is currently on the command line.
3395 */
3396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003397redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 if (cmd_silent)
3400 return;
3401
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003402 /* when 'incsearch' is set there may be no command line while redrawing */
3403 if (ccline.cmdbuff == NULL)
3404 {
3405 windgoto(cmdline_row, 0);
3406 msg_clr_eos();
3407 return;
3408 }
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 msg_start();
3411 redrawcmdprompt();
3412
3413 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3414 msg_no_more = TRUE;
3415 draw_cmdline(0, ccline.cmdlen);
3416 msg_clr_eos();
3417 msg_no_more = FALSE;
3418
3419 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003420 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003421 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /*
3424 * An emsg() before may have set msg_scroll. This is used in normal mode,
3425 * in cmdline mode we can reset them now.
3426 */
3427 msg_scroll = FALSE; /* next message overwrites cmdline */
3428
3429 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3430 * in cmdline mode */
3431 skip_redraw = FALSE;
3432}
3433
3434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003435compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003437 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 cmdline_row = Rows - 1;
3439 else
3440 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003441 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003445cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 if (cmd_silent)
3448 return;
3449
3450#ifdef FEAT_RIGHTLEFT
3451 if (cmdmsg_rl)
3452 {
3453 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3454 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3455 if (msg_row <= 0)
3456 msg_row = Rows - 1;
3457 }
3458 else
3459#endif
3460 {
3461 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3462 msg_col = ccline.cmdspos % (int)Columns;
3463 if (msg_row >= Rows)
3464 msg_row = Rows - 1;
3465 }
3466
3467 windgoto(msg_row, msg_col);
3468#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003469 if (p_imst == IM_ON_THE_SPOT)
3470 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471#endif
3472#ifdef MCH_CURSOR_SHAPE
3473 mch_update_cursor();
3474#endif
3475}
3476
3477 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003478gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480 msg_start();
3481#ifdef FEAT_RIGHTLEFT
3482 if (cmdmsg_rl)
3483 msg_col = Columns - 1;
3484 else
3485#endif
3486 msg_col = 0; /* always start in column 0 */
3487 if (clr) /* clear the bottom line(s) */
3488 msg_clr_eos(); /* will reset clear_cmdline */
3489 windgoto(cmdline_row, 0);
3490}
3491
3492/*
3493 * Check the word in front of the cursor for an abbreviation.
3494 * Called when the non-id character "c" has been entered.
3495 * When an abbreviation is recognized it is removed from the text with
3496 * backspaces and the replacement string is inserted, followed by "c".
3497 */
3498 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003499ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3502 return FALSE;
3503
3504 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3505}
3506
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003507#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3508 static int
3509#ifdef __BORLANDC__
3510_RTLENTRYF
3511#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003512sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003513{
3514 char_u *p1 = *(char_u **)s1;
3515 char_u *p2 = *(char_u **)s2;
3516
3517 if (*p1 != '<' && *p2 == '<') return -1;
3518 if (*p1 == '<' && *p2 != '<') return 1;
3519 return STRCMP(p1, p2);
3520}
3521#endif
3522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523/*
3524 * Return FAIL if this is not an appropriate context in which to do
3525 * completion of anything, return OK if it is (even if there are no matches).
3526 * For the caller, this means that the character is just passed through like a
3527 * normal character (instead of being expanded). This allows :s/^I^D etc.
3528 */
3529 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003530nextwild(
3531 expand_T *xp,
3532 int type,
3533 int options, /* extra options for ExpandOne() */
3534 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
3536 int i, j;
3537 char_u *p1;
3538 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 int difflen;
3540 int v;
3541
3542 if (xp->xp_numfiles == -1)
3543 {
3544 set_expand_context(xp);
3545 cmd_showtail = expand_showtail(xp);
3546 }
3547
3548 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3549 {
3550 beep_flush();
3551 return OK; /* Something illegal on command line */
3552 }
3553 if (xp->xp_context == EXPAND_NOTHING)
3554 {
3555 /* Caller can use the character as a normal char instead */
3556 return FAIL;
3557 }
3558
3559 MSG_PUTS("..."); /* show that we are busy */
3560 out_flush();
3561
3562 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003563 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 if (type == WILD_NEXT || type == WILD_PREV)
3566 {
3567 /*
3568 * Get next/previous match for a previous expanded pattern.
3569 */
3570 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3571 }
3572 else
3573 {
3574 /*
3575 * Translate string into pattern and expand it.
3576 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003577 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3578 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 p2 = NULL;
3580 else
3581 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003582 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003583 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3584 if (escape)
3585 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003586
3587 if (p_wic)
3588 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003589 p2 = ExpandOne(xp, p1,
3590 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003591 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003593 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (p2 != NULL && type == WILD_LONGEST)
3595 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003596 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (ccline.cmdbuff[i + j] == '*'
3598 || ccline.cmdbuff[i + j] == '?')
3599 break;
3600 if ((int)STRLEN(p2) < j)
3601 {
3602 vim_free(p2);
3603 p2 = NULL;
3604 }
3605 }
3606 }
3607 }
3608
3609 if (p2 != NULL && !got_int)
3610 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003611 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003612 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003614 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 xp->xp_pattern = ccline.cmdbuff + i;
3616 }
3617 else
3618 v = OK;
3619 if (v == OK)
3620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003621 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3622 &ccline.cmdbuff[ccline.cmdpos],
3623 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3624 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 ccline.cmdlen += difflen;
3626 ccline.cmdpos += difflen;
3627 }
3628 }
3629 vim_free(p2);
3630
3631 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003632 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /* When expanding a ":map" command and no matches are found, assume that
3635 * the key is supposed to be inserted literally */
3636 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3637 return FAIL;
3638
3639 if (xp->xp_numfiles <= 0 && p2 == NULL)
3640 beep_flush();
3641 else if (xp->xp_numfiles == 1)
3642 /* free expanded pattern */
3643 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3644
3645 return OK;
3646}
3647
3648/*
3649 * Do wildcard expansion on the string 'str'.
3650 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003651 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 * Return NULL for failure.
3653 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003654 * "orig" is the originally expanded string, copied to allocated memory. It
3655 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3656 * WILD_PREV "orig" should be NULL.
3657 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003658 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3659 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 *
3661 * mode = WILD_FREE: just free previously expanded matches
3662 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3663 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3664 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3665 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3666 * mode = WILD_ALL: return all matches concatenated
3667 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003668 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 *
3670 * options = WILD_LIST_NOTFOUND: list entries without a match
3671 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3672 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3673 * options = WILD_NO_BEEP: Don't beep for multiple matches
3674 * options = WILD_ADD_SLASH: add a slash after directory names
3675 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3676 * options = WILD_SILENT: don't print warning messages
3677 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003678 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 *
3680 * The variables xp->xp_context and xp->xp_backslash must have been set!
3681 */
3682 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003683ExpandOne(
3684 expand_T *xp,
3685 char_u *str,
3686 char_u *orig, /* allocated copy of original of expanded string */
3687 int options,
3688 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
3690 char_u *ss = NULL;
3691 static int findex;
3692 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003693 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 int i;
3695 long_u len;
3696 int non_suf_match; /* number without matching suffix */
3697
3698 /*
3699 * first handle the case of using an old match
3700 */
3701 if (mode == WILD_NEXT || mode == WILD_PREV)
3702 {
3703 if (xp->xp_numfiles > 0)
3704 {
3705 if (mode == WILD_PREV)
3706 {
3707 if (findex == -1)
3708 findex = xp->xp_numfiles;
3709 --findex;
3710 }
3711 else /* mode == WILD_NEXT */
3712 ++findex;
3713
3714 /*
3715 * When wrapping around, return the original string, set findex to
3716 * -1.
3717 */
3718 if (findex < 0)
3719 {
3720 if (orig_save == NULL)
3721 findex = xp->xp_numfiles - 1;
3722 else
3723 findex = -1;
3724 }
3725 if (findex >= xp->xp_numfiles)
3726 {
3727 if (orig_save == NULL)
3728 findex = 0;
3729 else
3730 findex = -1;
3731 }
3732#ifdef FEAT_WILDMENU
3733 if (p_wmnu)
3734 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3735 findex, cmd_showtail);
3736#endif
3737 if (findex == -1)
3738 return vim_strsave(orig_save);
3739 return vim_strsave(xp->xp_files[findex]);
3740 }
3741 else
3742 return NULL;
3743 }
3744
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003745 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3747 {
3748 FreeWild(xp->xp_numfiles, xp->xp_files);
3749 xp->xp_numfiles = -1;
3750 vim_free(orig_save);
3751 orig_save = NULL;
3752 }
3753 findex = 0;
3754
3755 if (mode == WILD_FREE) /* only release file name */
3756 return NULL;
3757
3758 if (xp->xp_numfiles == -1)
3759 {
3760 vim_free(orig_save);
3761 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003762 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 /*
3765 * Do the expansion.
3766 */
3767 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3768 options) == FAIL)
3769 {
3770#ifdef FNAME_ILLEGAL
3771 /* Illegal file name has been silently skipped. But when there
3772 * are wildcards, the real problem is that there was no match,
3773 * causing the pattern to be added, which has illegal characters.
3774 */
3775 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3776 EMSG2(_(e_nomatch2), str);
3777#endif
3778 }
3779 else if (xp->xp_numfiles == 0)
3780 {
3781 if (!(options & WILD_SILENT))
3782 EMSG2(_(e_nomatch2), str);
3783 }
3784 else
3785 {
3786 /* Escape the matches for use on the command line. */
3787 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3788
3789 /*
3790 * Check for matching suffixes in file names.
3791 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003792 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3793 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
3795 if (xp->xp_numfiles)
3796 non_suf_match = xp->xp_numfiles;
3797 else
3798 non_suf_match = 1;
3799 if ((xp->xp_context == EXPAND_FILES
3800 || xp->xp_context == EXPAND_DIRECTORIES)
3801 && xp->xp_numfiles > 1)
3802 {
3803 /*
3804 * More than one match; check suffix.
3805 * The files will have been sorted on matching suffix in
3806 * expand_wildcards, only need to check the first two.
3807 */
3808 non_suf_match = 0;
3809 for (i = 0; i < 2; ++i)
3810 if (match_suffix(xp->xp_files[i]))
3811 ++non_suf_match;
3812 }
3813 if (non_suf_match != 1)
3814 {
3815 /* Can we ever get here unless it's while expanding
3816 * interactively? If not, we can get rid of this all
3817 * together. Don't really want to wait for this message
3818 * (and possibly have to hit return to continue!).
3819 */
3820 if (!(options & WILD_SILENT))
3821 EMSG(_(e_toomany));
3822 else if (!(options & WILD_NO_BEEP))
3823 beep_flush();
3824 }
3825 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3826 ss = vim_strsave(xp->xp_files[0]);
3827 }
3828 }
3829 }
3830
3831 /* Find longest common part */
3832 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3833 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003834 int mb_len = 1;
3835 int c0, ci;
3836
3837 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003839#ifdef FEAT_MBYTE
3840 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003842 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3843 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3844 }
3845 else
3846#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003847 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003848 for (i = 1; i < xp->xp_numfiles; ++i)
3849 {
3850#ifdef FEAT_MBYTE
3851 if (has_mbyte)
3852 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3853 else
3854#endif
3855 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003856 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003858 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003859 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003861 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 break;
3863 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003864 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 break;
3866 }
3867 if (i < xp->xp_numfiles)
3868 {
3869 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003870 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 break;
3872 }
3873 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 ss = alloc((unsigned)len + 1);
3876 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003877 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 findex = -1; /* next p_wc gets first one */
3879 }
3880
3881 /* Concatenate all matching names */
3882 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3883 {
3884 len = 0;
3885 for (i = 0; i < xp->xp_numfiles; ++i)
3886 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3887 ss = lalloc(len, TRUE);
3888 if (ss != NULL)
3889 {
3890 *ss = NUL;
3891 for (i = 0; i < xp->xp_numfiles; ++i)
3892 {
3893 STRCAT(ss, xp->xp_files[i]);
3894 if (i != xp->xp_numfiles - 1)
3895 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3896 }
3897 }
3898 }
3899
3900 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3901 ExpandCleanup(xp);
3902
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003903 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003904 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003905 vim_free(orig);
3906
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 return ss;
3908}
3909
3910/*
3911 * Prepare an expand structure for use.
3912 */
3913 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003914ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003916 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003917 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003919#ifndef BACKSLASH_IN_FILENAME
3920 xp->xp_shell = FALSE;
3921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 xp->xp_numfiles = -1;
3923 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003924#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3925 xp->xp_arg = NULL;
3926#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003927 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928}
3929
3930/*
3931 * Cleanup an expand structure after use.
3932 */
3933 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003934ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
3936 if (xp->xp_numfiles >= 0)
3937 {
3938 FreeWild(xp->xp_numfiles, xp->xp_files);
3939 xp->xp_numfiles = -1;
3940 }
3941}
3942
3943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003944ExpandEscape(
3945 expand_T *xp,
3946 char_u *str,
3947 int numfiles,
3948 char_u **files,
3949 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
3951 int i;
3952 char_u *p;
3953
3954 /*
3955 * May change home directory back to "~"
3956 */
3957 if (options & WILD_HOME_REPLACE)
3958 tilde_replace(str, numfiles, files);
3959
3960 if (options & WILD_ESCAPE)
3961 {
3962 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003963 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003964 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 || xp->xp_context == EXPAND_BUFFERS
3966 || xp->xp_context == EXPAND_DIRECTORIES)
3967 {
3968 /*
3969 * Insert a backslash into a file name before a space, \, %, #
3970 * and wildmatch characters, except '~'.
3971 */
3972 for (i = 0; i < numfiles; ++i)
3973 {
3974 /* for ":set path=" we need to escape spaces twice */
3975 if (xp->xp_backslash == XP_BS_THREE)
3976 {
3977 p = vim_strsave_escaped(files[i], (char_u *)" ");
3978 if (p != NULL)
3979 {
3980 vim_free(files[i]);
3981 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003982#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 p = vim_strsave_escaped(files[i], (char_u *)" ");
3984 if (p != NULL)
3985 {
3986 vim_free(files[i]);
3987 files[i] = p;
3988 }
3989#endif
3990 }
3991 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003992#ifdef BACKSLASH_IN_FILENAME
3993 p = vim_strsave_fnameescape(files[i], FALSE);
3994#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003995 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 if (p != NULL)
3998 {
3999 vim_free(files[i]);
4000 files[i] = p;
4001 }
4002
4003 /* If 'str' starts with "\~", replace "~" at start of
4004 * files[i] with "\~". */
4005 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004006 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
4008 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004009
4010 /* If the first file starts with a '+' escape it. Otherwise it
4011 * could be seen as "+cmd". */
4012 if (*files[0] == '+')
4013 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 else if (xp->xp_context == EXPAND_TAGS)
4016 {
4017 /*
4018 * Insert a backslash before characters in a tag name that
4019 * would terminate the ":tag" command.
4020 */
4021 for (i = 0; i < numfiles; ++i)
4022 {
4023 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4024 if (p != NULL)
4025 {
4026 vim_free(files[i]);
4027 files[i] = p;
4028 }
4029 }
4030 }
4031 }
4032}
4033
4034/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004035 * Escape special characters in "fname" for when used as a file name argument
4036 * after a Vim command, or, when "shell" is non-zero, a shell command.
4037 * Returns the result in allocated memory.
4038 */
4039 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004040vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004041{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004042 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004043#ifdef BACKSLASH_IN_FILENAME
4044 char_u buf[20];
4045 int j = 0;
4046
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004047 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004048 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004049 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004050 buf[j++] = *p;
4051 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004052 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004053#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004054 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4055 if (shell && csh_like_shell() && p != NULL)
4056 {
4057 char_u *s;
4058
4059 /* For csh and similar shells need to put two backslashes before '!'.
4060 * One is taken by Vim, one by the shell. */
4061 s = vim_strsave_escaped(p, (char_u *)"!");
4062 vim_free(p);
4063 p = s;
4064 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004065#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004066
4067 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4068 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004069 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004070 escape_fname(&p);
4071
4072 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004073}
4074
4075/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004076 * Put a backslash before the file name in "pp", which is in allocated memory.
4077 */
4078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004079escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004080{
4081 char_u *p;
4082
4083 p = alloc((unsigned)(STRLEN(*pp) + 2));
4084 if (p != NULL)
4085 {
4086 p[0] = '\\';
4087 STRCPY(p + 1, *pp);
4088 vim_free(*pp);
4089 *pp = p;
4090 }
4091}
4092
4093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 * For each file name in files[num_files]:
4095 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4096 */
4097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004098tilde_replace(
4099 char_u *orig_pat,
4100 int num_files,
4101 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
4103 int i;
4104 char_u *p;
4105
4106 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4107 {
4108 for (i = 0; i < num_files; ++i)
4109 {
4110 p = home_replace_save(NULL, files[i]);
4111 if (p != NULL)
4112 {
4113 vim_free(files[i]);
4114 files[i] = p;
4115 }
4116 }
4117 }
4118}
4119
4120/*
4121 * Show all matches for completion on the command line.
4122 * Returns EXPAND_NOTHING when the character that triggered expansion should
4123 * be inserted like a normal character.
4124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004126showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
4128#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4129 int num_files;
4130 char_u **files_found;
4131 int i, j, k;
4132 int maxlen;
4133 int lines;
4134 int columns;
4135 char_u *p;
4136 int lastlen;
4137 int attr;
4138 int showtail;
4139
4140 if (xp->xp_numfiles == -1)
4141 {
4142 set_expand_context(xp);
4143 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4144 &num_files, &files_found);
4145 showtail = expand_showtail(xp);
4146 if (i != EXPAND_OK)
4147 return i;
4148
4149 }
4150 else
4151 {
4152 num_files = xp->xp_numfiles;
4153 files_found = xp->xp_files;
4154 showtail = cmd_showtail;
4155 }
4156
4157#ifdef FEAT_WILDMENU
4158 if (!wildmenu)
4159 {
4160#endif
4161 msg_didany = FALSE; /* lines_left will be set */
4162 msg_start(); /* prepare for paging */
4163 msg_putchar('\n');
4164 out_flush();
4165 cmdline_row = msg_row;
4166 msg_didany = FALSE; /* lines_left will be set again */
4167 msg_start(); /* prepare for paging */
4168#ifdef FEAT_WILDMENU
4169 }
4170#endif
4171
4172 if (got_int)
4173 got_int = FALSE; /* only int. the completion, not the cmd line */
4174#ifdef FEAT_WILDMENU
4175 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004176 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#endif
4178 else
4179 {
4180 /* find the length of the longest file name */
4181 maxlen = 0;
4182 for (i = 0; i < num_files; ++i)
4183 {
4184 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004185 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 || xp->xp_context == EXPAND_BUFFERS))
4187 {
4188 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4189 j = vim_strsize(NameBuff);
4190 }
4191 else
4192 j = vim_strsize(L_SHOWFILE(i));
4193 if (j > maxlen)
4194 maxlen = j;
4195 }
4196
4197 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4198 lines = num_files;
4199 else
4200 {
4201 /* compute the number of columns and lines for the listing */
4202 maxlen += 2; /* two spaces between file names */
4203 columns = ((int)Columns + 2) / maxlen;
4204 if (columns < 1)
4205 columns = 1;
4206 lines = (num_files + columns - 1) / columns;
4207 }
4208
Bram Moolenaar8820b482017-03-16 17:23:31 +01004209 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4212 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004213 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 msg_clr_eos();
4215 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004216 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218
4219 /* list the files line by line */
4220 for (i = 0; i < lines; ++i)
4221 {
4222 lastlen = 999;
4223 for (k = i; k < num_files; k += lines)
4224 {
4225 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4226 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004227 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 p = files_found[k] + STRLEN(files_found[k]) + 1;
4229 msg_advance(maxlen + 1);
4230 msg_puts(p);
4231 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004232 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 break;
4234 }
4235 for (j = maxlen - lastlen; --j >= 0; )
4236 msg_putchar(' ');
4237 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004238 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 || xp->xp_context == EXPAND_BUFFERS)
4240 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004241 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004242 if (xp->xp_numfiles != -1)
4243 {
4244 char_u *halved_slash;
4245 char_u *exp_path;
4246
4247 /* Expansion was done before and special characters
4248 * were escaped, need to halve backslashes. Also
4249 * $HOME has been replaced with ~/. */
4250 exp_path = expand_env_save_opt(files_found[k], TRUE);
4251 halved_slash = backslash_halve_save(
4252 exp_path != NULL ? exp_path : files_found[k]);
4253 j = mch_isdir(halved_slash != NULL ? halved_slash
4254 : files_found[k]);
4255 vim_free(exp_path);
4256 vim_free(halved_slash);
4257 }
4258 else
4259 /* Expansion was done here, file names are literal. */
4260 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (showtail)
4262 p = L_SHOWFILE(k);
4263 else
4264 {
4265 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4266 TRUE);
4267 p = NameBuff;
4268 }
4269 }
4270 else
4271 {
4272 j = FALSE;
4273 p = L_SHOWFILE(k);
4274 }
4275 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4276 }
4277 if (msg_col > 0) /* when not wrapped around */
4278 {
4279 msg_clr_eos();
4280 msg_putchar('\n');
4281 }
4282 out_flush(); /* show one line at a time */
4283 if (got_int)
4284 {
4285 got_int = FALSE;
4286 break;
4287 }
4288 }
4289
4290 /*
4291 * we redraw the command below the lines that we have just listed
4292 * This is a bit tricky, but it saves a lot of screen updating.
4293 */
4294 cmdline_row = msg_row; /* will put it back later */
4295 }
4296
4297 if (xp->xp_numfiles == -1)
4298 FreeWild(num_files, files_found);
4299
4300 return EXPAND_OK;
4301}
4302
4303/*
4304 * Private gettail for showmatches() (and win_redr_status_matches()):
4305 * Find tail of file name path, but ignore trailing "/".
4306 */
4307 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004308sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309{
4310 char_u *p;
4311 char_u *t = s;
4312 int had_sep = FALSE;
4313
4314 for (p = s; *p != NUL; )
4315 {
4316 if (vim_ispathsep(*p)
4317#ifdef BACKSLASH_IN_FILENAME
4318 && !rem_backslash(p)
4319#endif
4320 )
4321 had_sep = TRUE;
4322 else if (had_sep)
4323 {
4324 t = p;
4325 had_sep = FALSE;
4326 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004327 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329 return t;
4330}
4331
4332/*
4333 * Return TRUE if we only need to show the tail of completion matches.
4334 * When not completing file names or there is a wildcard in the path FALSE is
4335 * returned.
4336 */
4337 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004338expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
4340 char_u *s;
4341 char_u *end;
4342
4343 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004344 if (xp->xp_context != EXPAND_FILES
4345 && xp->xp_context != EXPAND_SHELLCMD
4346 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 return FALSE;
4348
4349 end = gettail(xp->xp_pattern);
4350 if (end == xp->xp_pattern) /* there is no path separator */
4351 return FALSE;
4352
4353 for (s = xp->xp_pattern; s < end; s++)
4354 {
4355 /* Skip escaped wildcards. Only when the backslash is not a path
4356 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4357 if (rem_backslash(s))
4358 ++s;
4359 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4360 return FALSE;
4361 }
4362 return TRUE;
4363}
4364
4365/*
4366 * Prepare a string for expansion.
4367 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004368 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 * When expanding other names: The string will be used with regcomp(). Copy
4370 * the name into allocated memory and prepend "^".
4371 */
4372 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004373addstar(
4374 char_u *fname,
4375 int len,
4376 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
4378 char_u *retval;
4379 int i, j;
4380 int new_len;
4381 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004382 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004384 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004385 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004386 && context != EXPAND_SHELLCMD
4387 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
4389 /*
4390 * Matching will be done internally (on something other than files).
4391 * So we convert the file-matching-type wildcards into our kind for
4392 * use with vim_regcomp(). First work out how long it will be:
4393 */
4394
4395 /* For help tags the translation is done in find_help_tags().
4396 * For a tag pattern starting with "/" no translation is needed. */
4397 if (context == EXPAND_HELP
4398 || context == EXPAND_COLORS
4399 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004400 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004401 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004402 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004403 || ((context == EXPAND_TAGS_LISTFILES
4404 || context == EXPAND_TAGS)
4405 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 retval = vim_strnsave(fname, len);
4407 else
4408 {
4409 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4410 for (i = 0; i < len; i++)
4411 {
4412 if (fname[i] == '*' || fname[i] == '~')
4413 new_len++; /* '*' needs to be replaced by ".*"
4414 '~' needs to be replaced by "\~" */
4415
4416 /* Buffer names are like file names. "." should be literal */
4417 if (context == EXPAND_BUFFERS && fname[i] == '.')
4418 new_len++; /* "." becomes "\." */
4419
4420 /* Custom expansion takes care of special things, match
4421 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004422 if ((context == EXPAND_USER_DEFINED
4423 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 new_len++; /* '\' becomes "\\" */
4425 }
4426 retval = alloc(new_len);
4427 if (retval != NULL)
4428 {
4429 retval[0] = '^';
4430 j = 1;
4431 for (i = 0; i < len; i++, j++)
4432 {
4433 /* Skip backslash. But why? At least keep it for custom
4434 * expansion. */
4435 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004436 && context != EXPAND_USER_LIST
4437 && fname[i] == '\\'
4438 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 break;
4440
4441 switch (fname[i])
4442 {
4443 case '*': retval[j++] = '.';
4444 break;
4445 case '~': retval[j++] = '\\';
4446 break;
4447 case '?': retval[j] = '.';
4448 continue;
4449 case '.': if (context == EXPAND_BUFFERS)
4450 retval[j++] = '\\';
4451 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004452 case '\\': if (context == EXPAND_USER_DEFINED
4453 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 retval[j++] = '\\';
4455 break;
4456 }
4457 retval[j] = fname[i];
4458 }
4459 retval[j] = NUL;
4460 }
4461 }
4462 }
4463 else
4464 {
4465 retval = alloc(len + 4);
4466 if (retval != NULL)
4467 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004468 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004471 * Don't add a star to *, ~, ~user, $var or `cmd`.
4472 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 * ~ would be at the start of the file name, but not the tail.
4474 * $ could be anywhere in the tail.
4475 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004476 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 */
4478 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004479 ends_in_star = (len > 0 && retval[len - 1] == '*');
4480#ifndef BACKSLASH_IN_FILENAME
4481 for (i = len - 2; i >= 0; --i)
4482 {
4483 if (retval[i] != '\\')
4484 break;
4485 ends_in_star = !ends_in_star;
4486 }
4487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004489 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 && vim_strchr(tail, '$') == NULL
4491 && vim_strchr(retval, '`') == NULL)
4492 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004493 else if (len > 0 && retval[len - 1] == '$')
4494 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 retval[len] = NUL;
4496 }
4497 }
4498 return retval;
4499}
4500
4501/*
4502 * Must parse the command line so far to work out what context we are in.
4503 * Completion can then be done based on that context.
4504 * This routine sets the variables:
4505 * xp->xp_pattern The start of the pattern to be expanded within
4506 * the command line (ends at the cursor).
4507 * xp->xp_context The type of thing to expand. Will be one of:
4508 *
4509 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4510 * the command line, like an unknown command. Caller
4511 * should beep.
4512 * EXPAND_NOTHING Unrecognised context for completion, use char like
4513 * a normal char, rather than for completion. eg
4514 * :s/^I/
4515 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4516 * it.
4517 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4518 * EXPAND_FILES After command with XFILE set, or after setting
4519 * with P_EXPAND set. eg :e ^I, :w>>^I
4520 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4521 * when we know only directories are of interest. eg
4522 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004523 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4525 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4526 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4527 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4528 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4529 * EXPAND_EVENTS Complete event names
4530 * EXPAND_SYNTAX Complete :syntax command arguments
4531 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4532 * EXPAND_AUGROUP Complete autocommand group names
4533 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4534 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4535 * eg :unmap a^I , :cunab x^I
4536 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4537 * eg :call sub^I
4538 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4539 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4540 * names in expressions, eg :while s^I
4541 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004542 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 */
4544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004545set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004547 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (ccline.cmdfirstc != ':'
4549#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004550 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004551 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#endif
4553 )
4554 {
4555 xp->xp_context = EXPAND_NOTHING;
4556 return;
4557 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004558 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559}
4560
4561 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004562set_cmd_context(
4563 expand_T *xp,
4564 char_u *str, /* start of command line */
4565 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004566 int col, /* position of cursor */
4567 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568{
4569 int old_char = NUL;
4570 char_u *nextcomm;
4571
4572 /*
4573 * Avoid a UMR warning from Purify, only save the character if it has been
4574 * written before.
4575 */
4576 if (col < len)
4577 old_char = str[col];
4578 str[col] = NUL;
4579 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004580
4581#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004582 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004583 {
4584# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004585 /* pass CMD_SIZE because there is no real command */
4586 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004587# endif
4588 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004589 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004590 {
4591 xp->xp_context = ccline.xp_context;
4592 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004593# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004594 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004595# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004596 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004597 else
4598#endif
4599 while (nextcomm != NULL)
4600 nextcomm = set_one_cmd_context(xp, nextcomm);
4601
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004602 /* Store the string here so that call_user_expand_func() can get to them
4603 * easily. */
4604 xp->xp_line = str;
4605 xp->xp_col = col;
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 str[col] = old_char;
4608}
4609
4610/*
4611 * Expand the command line "str" from context "xp".
4612 * "xp" must have been set by set_cmd_context().
4613 * xp->xp_pattern points into "str", to where the text that is to be expanded
4614 * starts.
4615 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4616 * cursor.
4617 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4618 * key that triggered expansion literally.
4619 * Returns EXPAND_OK otherwise.
4620 */
4621 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004622expand_cmdline(
4623 expand_T *xp,
4624 char_u *str, /* start of command line */
4625 int col, /* position of cursor */
4626 int *matchcount, /* return: nr of matches */
4627 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004630 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
4632 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4633 {
4634 beep_flush();
4635 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4636 }
4637 if (xp->xp_context == EXPAND_NOTHING)
4638 {
4639 /* Caller can use the character as a normal char instead */
4640 return EXPAND_NOTHING;
4641 }
4642
4643 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004644 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4645 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (file_str == NULL)
4647 return EXPAND_UNSUCCESSFUL;
4648
Bram Moolenaar94950a92010-12-02 16:01:29 +01004649 if (p_wic)
4650 options += WILD_ICASE;
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004653 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
4655 *matchcount = 0;
4656 *matches = NULL;
4657 }
4658 vim_free(file_str);
4659
4660 return EXPAND_OK;
4661}
4662
4663#ifdef FEAT_MULTI_LANG
4664/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004665 * Cleanup matches for help tags:
4666 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4667 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004669static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004672cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
4674 int i, j;
4675 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004676 char_u buf[4];
4677 char_u *p = buf;
4678
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004679 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004680 {
4681 *p++ = '@';
4682 *p++ = p_hlg[0];
4683 *p++ = p_hlg[1];
4684 }
4685 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
4687 for (i = 0; i < num_file; ++i)
4688 {
4689 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004690 if (len <= 0)
4691 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004692 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
4694 /* Sorting on priority means the same item in another language may
4695 * be anywhere. Search all items for a match up to the "@en". */
4696 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004697 if (j != i && (int)STRLEN(file[j]) == len + 3
4698 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 break;
4700 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004701 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 file[i][len] = NUL;
4703 }
4704 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004705
4706 if (*buf != NUL)
4707 for (i = 0; i < num_file; ++i)
4708 {
4709 len = (int)STRLEN(file[i]) - 3;
4710 if (len <= 0)
4711 continue;
4712 if (STRCMP(file[i] + len, buf) == 0)
4713 {
4714 /* remove the default language */
4715 file[i][len] = NUL;
4716 }
4717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
4719#endif
4720
4721/*
4722 * Do the expansion based on xp->xp_context and "pat".
4723 */
4724 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004725ExpandFromContext(
4726 expand_T *xp,
4727 char_u *pat,
4728 int *num_file,
4729 char_u ***file,
4730 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732#ifdef FEAT_CMDL_COMPL
4733 regmatch_T regmatch;
4734#endif
4735 int ret;
4736 int flags;
4737
4738 flags = EW_DIR; /* include directories */
4739 if (options & WILD_LIST_NOTFOUND)
4740 flags |= EW_NOTFOUND;
4741 if (options & WILD_ADD_SLASH)
4742 flags |= EW_ADDSLASH;
4743 if (options & WILD_KEEP_ALL)
4744 flags |= EW_KEEPALL;
4745 if (options & WILD_SILENT)
4746 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004747 if (options & WILD_ALLLINKS)
4748 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004750 if (xp->xp_context == EXPAND_FILES
4751 || xp->xp_context == EXPAND_DIRECTORIES
4752 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 /*
4755 * Expand file or directory names.
4756 */
4757 int free_pat = FALSE;
4758 int i;
4759
4760 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4761 * space */
4762 if (xp->xp_backslash != XP_BS_NONE)
4763 {
4764 free_pat = TRUE;
4765 pat = vim_strsave(pat);
4766 for (i = 0; pat[i]; ++i)
4767 if (pat[i] == '\\')
4768 {
4769 if (xp->xp_backslash == XP_BS_THREE
4770 && pat[i + 1] == '\\'
4771 && pat[i + 2] == '\\'
4772 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004773 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (xp->xp_backslash == XP_BS_ONE
4775 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004776 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 }
4779
4780 if (xp->xp_context == EXPAND_FILES)
4781 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004782 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4783 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 else
4785 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004786 if (options & WILD_ICASE)
4787 flags |= EW_ICASE;
4788
Bram Moolenaard7834d32009-12-02 16:14:36 +00004789 /* Expand wildcards, supporting %:h and the like. */
4790 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (free_pat)
4792 vim_free(pat);
4793 return ret;
4794 }
4795
4796 *file = (char_u **)"";
4797 *num_file = 0;
4798 if (xp->xp_context == EXPAND_HELP)
4799 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004800 /* With an empty argument we would get all the help tags, which is
4801 * very slow. Get matches for "help" instead. */
4802 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4803 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805#ifdef FEAT_MULTI_LANG
4806 cleanup_help_tags(*num_file, *file);
4807#endif
4808 return OK;
4809 }
4810 return FAIL;
4811 }
4812
4813#ifndef FEAT_CMDL_COMPL
4814 return FAIL;
4815#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004816 if (xp->xp_context == EXPAND_SHELLCMD)
4817 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (xp->xp_context == EXPAND_OLD_SETTING)
4819 return ExpandOldSetting(num_file, file);
4820 if (xp->xp_context == EXPAND_BUFFERS)
4821 return ExpandBufnames(pat, num_file, file, options);
4822 if (xp->xp_context == EXPAND_TAGS
4823 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4824 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4825 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004826 {
4827 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004828 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4829 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004832 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004833 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004834 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004835 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004836 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004837 {
4838 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004839 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004840 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004841 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004842 {
4843 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004844 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004845 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004846# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4847 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004848 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004849# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004850 if (xp->xp_context == EXPAND_PACKADD)
4851 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
4853 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4854 if (regmatch.regprog == NULL)
4855 return FAIL;
4856
4857 /* set ignore-case according to p_ic, p_scs and pat */
4858 regmatch.rm_ic = ignorecase(pat);
4859
4860 if (xp->xp_context == EXPAND_SETTINGS
4861 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4862 ret = ExpandSettings(xp, &regmatch, num_file, file);
4863 else if (xp->xp_context == EXPAND_MAPPINGS)
4864 ret = ExpandMappings(&regmatch, num_file, file);
4865# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4866 else if (xp->xp_context == EXPAND_USER_DEFINED)
4867 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4868# endif
4869 else
4870 {
4871 static struct expgen
4872 {
4873 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004874 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004876 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 } tab[] =
4878 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004879 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4880 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004881 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004882 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004883#ifdef FEAT_CMDHIST
4884 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004887 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004888 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004889 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4890 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4891 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#endif
4893#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004894 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4895 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4896 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4897 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#endif
4899#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004900 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4901 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
4903#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004904 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004906#ifdef FEAT_PROFILE
4907 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4908#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004909 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004911 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4912 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004914#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004915 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004916#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004917#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004918 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004919#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004920#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004921 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4924 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004925 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4926 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004928 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004929 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 };
4931 int i;
4932
4933 /*
4934 * Find a context in the table and call the ExpandGeneric() with the
4935 * right function to do the expansion.
4936 */
4937 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004938 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (xp->xp_context == tab[i].context)
4940 {
4941 if (tab[i].ic)
4942 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004943 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004944 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 break;
4946 }
4947 }
4948
Bram Moolenaar473de612013-06-08 18:19:48 +02004949 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 return ret;
4952#endif /* FEAT_CMDL_COMPL */
4953}
4954
4955#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4956/*
4957 * Expand a list of names.
4958 *
4959 * Generic function for command line completion. It calls a function to
4960 * obtain strings, one by one. The strings are matched against a regexp
4961 * program. Matching strings are copied into an array, which is returned.
4962 *
4963 * Returns OK when no problems encountered, FAIL for error (out of memory).
4964 */
4965 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004966ExpandGeneric(
4967 expand_T *xp,
4968 regmatch_T *regmatch,
4969 int *num_file,
4970 char_u ***file,
4971 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004973 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 int i;
4976 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004977 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 char_u *str;
4979
4980 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004981 * round == 0: count the number of matching names
4982 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004984 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 for (i = 0; ; ++i)
4987 {
4988 str = (*func)(xp, i);
4989 if (str == NULL) /* end of list */
4990 break;
4991 if (*str == NUL) /* skip empty strings */
4992 continue;
4993
4994 if (vim_regexec(regmatch, str, (colnr_T)0))
4995 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004996 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004998 if (escaped)
4999 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5000 else
5001 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 (*file)[count] = str;
5003#ifdef FEAT_MENU
5004 if (func == get_menu_names && str != NULL)
5005 {
5006 /* test for separator added by get_menu_names() */
5007 str += STRLEN(str) - 1;
5008 if (*str == '\001')
5009 *str = '.';
5010 }
5011#endif
5012 }
5013 ++count;
5014 }
5015 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005016 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 if (count == 0)
5019 return OK;
5020 *num_file = count;
5021 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5022 if (*file == NULL)
5023 {
5024 *file = (char_u **)"";
5025 return FAIL;
5026 }
5027 count = 0;
5028 }
5029 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005030
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005031 /* Sort the results. Keep menu's in the specified order. */
5032 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005033 {
5034 if (xp->xp_context == EXPAND_EXPRESSION
5035 || xp->xp_context == EXPAND_FUNCTIONS
5036 || xp->xp_context == EXPAND_USER_FUNC)
5037 /* <SNR> functions should be sorted to the end. */
5038 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5039 sort_func_compare);
5040 else
5041 sort_strings(*file, *num_file);
5042 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005043
Bram Moolenaar4f688582007-07-24 12:34:30 +00005044#ifdef FEAT_CMDL_COMPL
5045 /* Reset the variables used for special highlight names expansion, so that
5046 * they don't show up when getting normal highlight names by ID. */
5047 reset_expand_highlight();
5048#endif
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 return OK;
5051}
5052
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005053/*
5054 * Complete a shell command.
5055 * Returns FAIL or OK;
5056 */
5057 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005058expand_shellcmd(
5059 char_u *filepat, /* pattern to match with command names */
5060 int *num_file, /* return: number of matches */
5061 char_u ***file, /* return: array with matches */
5062 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005063{
5064 char_u *pat;
5065 int i;
5066 char_u *path;
5067 int mustfree = FALSE;
5068 garray_T ga;
5069 char_u *buf = alloc(MAXPATHL);
5070 size_t l;
5071 char_u *s, *e;
5072 int flags = flagsarg;
5073 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005074 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005075
5076 if (buf == NULL)
5077 return FAIL;
5078
5079 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5080 * space */
5081 pat = vim_strsave(filepat);
5082 for (i = 0; pat[i]; ++i)
5083 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005084 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005085
Bram Moolenaarb5971142015-03-21 17:32:19 +01005086 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005087
5088 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005089 if (mch_isFullName(pat))
5090 path = (char_u *)" ";
5091 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005092 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5093 path = (char_u *)".";
5094 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005095 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005096 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005097 if (path == NULL)
5098 path = (char_u *)"";
5099 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005100
5101 /*
5102 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005103 * collect them in "ga". When "." is not in $PATH also expand for the
5104 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005105 */
5106 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005107 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005108 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005109 if (*s == NUL)
5110 {
5111 if (did_curdir)
5112 break;
5113 /* Find directories in the current directory, path is empty. */
5114 did_curdir = TRUE;
5115 }
5116 else if (*s == '.')
5117 did_curdir = TRUE;
5118
Bram Moolenaar68c31742006-09-02 15:54:18 +00005119 if (*s == ' ')
5120 ++s; /* Skip space used for absolute path name. */
5121
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005122#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005123 e = vim_strchr(s, ';');
5124#else
5125 e = vim_strchr(s, ':');
5126#endif
5127 if (e == NULL)
5128 e = s + STRLEN(s);
5129
5130 l = e - s;
5131 if (l > MAXPATHL - 5)
5132 break;
5133 vim_strncpy(buf, s, l);
5134 add_pathsep(buf);
5135 l = STRLEN(buf);
5136 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5137
5138 /* Expand matches in one directory of $PATH. */
5139 ret = expand_wildcards(1, &buf, num_file, file, flags);
5140 if (ret == OK)
5141 {
5142 if (ga_grow(&ga, *num_file) == FAIL)
5143 FreeWild(*num_file, *file);
5144 else
5145 {
5146 for (i = 0; i < *num_file; ++i)
5147 {
5148 s = (*file)[i];
5149 if (STRLEN(s) > l)
5150 {
5151 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005152 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005153 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5154 }
5155 else
5156 vim_free(s);
5157 }
5158 vim_free(*file);
5159 }
5160 }
5161 if (*e != NUL)
5162 ++e;
5163 }
5164 *file = ga.ga_data;
5165 *num_file = ga.ga_len;
5166
5167 vim_free(buf);
5168 vim_free(pat);
5169 if (mustfree)
5170 vim_free(path);
5171 return OK;
5172}
5173
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005176static 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 +00005177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005179 * Call "user_expand_func()" to invoke a user defined Vim script function and
5180 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005182 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005183call_user_expand_func(
5184 void *(*user_expand_func)(char_u *, int, char_u **, int),
5185 expand_T *xp,
5186 int *num_file,
5187 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005189 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005190 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005193 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005194 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
Bram Moolenaarb7515462013-06-29 12:58:33 +02005196 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005197 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 *num_file = 0;
5199 *file = NULL;
5200
Bram Moolenaarb7515462013-06-29 12:58:33 +02005201 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005202 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005203 keep = ccline.cmdbuff[ccline.cmdlen];
5204 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005205 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005206
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005207 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005208 args[1] = xp->xp_line;
5209 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 args[2] = num;
5211
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005212 /* Save the cmdline, we don't know what the function may do. */
5213 save_ccline = ccline;
5214 ccline.cmdbuff = NULL;
5215 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005217
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005218 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005219
5220 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005222 if (ccline.cmdbuff != NULL)
5223 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005224
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005225 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005226 return ret;
5227}
5228
5229/*
5230 * Expand names with a function defined by the user.
5231 */
5232 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005233ExpandUserDefined(
5234 expand_T *xp,
5235 regmatch_T *regmatch,
5236 int *num_file,
5237 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005238{
5239 char_u *retstr;
5240 char_u *s;
5241 char_u *e;
5242 char_u keep;
5243 garray_T ga;
5244
5245 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5246 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 return FAIL;
5248
5249 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005250 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 e = vim_strchr(s, '\n');
5253 if (e == NULL)
5254 e = s + STRLEN(s);
5255 keep = *e;
5256 *e = 0;
5257
5258 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5259 {
5260 *e = keep;
5261 if (*e != NUL)
5262 ++e;
5263 continue;
5264 }
5265
5266 if (ga_grow(&ga, 1) == FAIL)
5267 break;
5268
5269 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5270 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 *e = keep;
5273 if (*e != NUL)
5274 ++e;
5275 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005276 vim_free(retstr);
5277 *file = ga.ga_data;
5278 *num_file = ga.ga_len;
5279 return OK;
5280}
5281
5282/*
5283 * Expand names with a list returned by a function defined by the user.
5284 */
5285 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005286ExpandUserList(
5287 expand_T *xp,
5288 int *num_file,
5289 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005290{
5291 list_T *retlist;
5292 listitem_T *li;
5293 garray_T ga;
5294
5295 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5296 if (retlist == NULL)
5297 return FAIL;
5298
5299 ga_init2(&ga, (int)sizeof(char *), 3);
5300 /* Loop over the items in the list. */
5301 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5302 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005303 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5304 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005305
5306 if (ga_grow(&ga, 1) == FAIL)
5307 break;
5308
5309 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005310 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005311 ++ga.ga_len;
5312 }
5313 list_unref(retlist);
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 *file = ga.ga_data;
5316 *num_file = ga.ga_len;
5317 return OK;
5318}
5319#endif
5320
5321/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005322 * Expand color scheme, compiler or filetype names.
5323 * Search from 'runtimepath':
5324 * 'runtimepath'/{dirnames}/{pat}.vim
5325 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5326 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5327 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5328 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005329 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 */
5331 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005332ExpandRTDir(
5333 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005334 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005335 int *num_file,
5336 char_u ***file,
5337 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u *s;
5340 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005341 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005343 int i;
5344 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 *num_file = 0;
5347 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005348 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005349 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005351 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005353 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5354 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005356 ga_clear_strings(&ga);
5357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005359 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005360 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005361 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005363
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005364 if (flags & DIP_START) {
5365 for (i = 0; dirnames[i] != NULL; ++i)
5366 {
5367 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5368 if (s == NULL)
5369 {
5370 ga_clear_strings(&ga);
5371 return FAIL;
5372 }
5373 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5374 globpath(p_pp, s, &ga, 0);
5375 vim_free(s);
5376 }
5377 }
5378
5379 if (flags & DIP_OPT) {
5380 for (i = 0; dirnames[i] != NULL; ++i)
5381 {
5382 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5383 if (s == NULL)
5384 {
5385 ga_clear_strings(&ga);
5386 return FAIL;
5387 }
5388 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5389 globpath(p_pp, s, &ga, 0);
5390 vim_free(s);
5391 }
5392 }
5393
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005394 for (i = 0; i < ga.ga_len; ++i)
5395 {
5396 match = ((char_u **)ga.ga_data)[i];
5397 s = match;
5398 e = s + STRLEN(s);
5399 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5400 {
5401 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005402 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005403 if (s < match || vim_ispathsep(*s))
5404 break;
5405 ++s;
5406 *e = NUL;
5407 mch_memmove(match, s, e - s + 1);
5408 }
5409 }
5410
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005411 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005412 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005413
5414 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005415 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005416 remove_duplicates(&ga);
5417
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 *file = ga.ga_data;
5419 *num_file = ga.ga_len;
5420 return OK;
5421}
5422
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005423/*
5424 * Expand loadplugin names:
5425 * 'packpath'/pack/ * /opt/{pat}
5426 */
5427 static int
5428ExpandPackAddDir(
5429 char_u *pat,
5430 int *num_file,
5431 char_u ***file)
5432{
5433 char_u *s;
5434 char_u *e;
5435 char_u *match;
5436 garray_T ga;
5437 int i;
5438 int pat_len;
5439
5440 *num_file = 0;
5441 *file = NULL;
5442 pat_len = (int)STRLEN(pat);
5443 ga_init2(&ga, (int)sizeof(char *), 10);
5444
5445 s = alloc((unsigned)(pat_len + 26));
5446 if (s == NULL)
5447 {
5448 ga_clear_strings(&ga);
5449 return FAIL;
5450 }
5451 sprintf((char *)s, "pack/*/opt/%s*", pat);
5452 globpath(p_pp, s, &ga, 0);
5453 vim_free(s);
5454
5455 for (i = 0; i < ga.ga_len; ++i)
5456 {
5457 match = ((char_u **)ga.ga_data)[i];
5458 s = gettail(match);
5459 e = s + STRLEN(s);
5460 mch_memmove(match, s, e - s + 1);
5461 }
5462
5463 if (ga.ga_len == 0)
5464 return FAIL;
5465
5466 /* Sort and remove duplicates which can happen when specifying multiple
5467 * directories in dirnames. */
5468 remove_duplicates(&ga);
5469
5470 *file = ga.ga_data;
5471 *num_file = ga.ga_len;
5472 return OK;
5473}
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475#endif
5476
5477#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5478/*
5479 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005480 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483globpath(
5484 char_u *path,
5485 char_u *file,
5486 garray_T *ga,
5487 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 expand_T xpc;
5490 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 int num_p;
5493 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 buf = alloc(MAXPATHL);
5496 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005499 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 /* Loop over all entries in {path}. */
5503 while (*path != NUL)
5504 {
5505 /* Copy one item of the path to buf[] and concatenate the file name. */
5506 copy_option_part(&path, buf, MAXPATHL, ",");
5507 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5508 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005509# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005510 /* Using the platform's path separator (\) makes vim incorrectly
5511 * treat it as an escape character, use '/' instead. */
5512 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5513 STRCAT(buf, "/");
5514# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005518 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5519 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005521 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005523 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 for (i = 0; i < num_p; ++i)
5526 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005527 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005528 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005529 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 FreeWild(num_p, p);
5534 }
5535 }
5536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539}
5540
5541#endif
5542
5543#if defined(FEAT_CMDHIST) || defined(PROTO)
5544
5545/*********************************
5546 * Command line history stuff *
5547 *********************************/
5548
5549/*
5550 * Translate a history character to the associated type number.
5551 */
5552 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 if (c == ':')
5556 return HIST_CMD;
5557 if (c == '=')
5558 return HIST_EXPR;
5559 if (c == '@')
5560 return HIST_INPUT;
5561 if (c == '>')
5562 return HIST_DEBUG;
5563 return HIST_SEARCH; /* must be '?' or '/' */
5564}
5565
5566/*
5567 * Table of history names.
5568 * These names are used in :history and various hist...() functions.
5569 * It is sufficient to give the significant prefix of a history name.
5570 */
5571
5572static char *(history_names[]) =
5573{
5574 "cmd",
5575 "search",
5576 "expr",
5577 "input",
5578 "debug",
5579 NULL
5580};
5581
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005582#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5583/*
5584 * Function given to ExpandGeneric() to obtain the possible first
5585 * arguments of the ":history command.
5586 */
5587 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005589{
5590 static char_u compl[2] = { NUL, NUL };
5591 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005592 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005593 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5594
5595 if (idx < short_names_count)
5596 {
5597 compl[0] = (char_u)short_names[idx];
5598 return compl;
5599 }
5600 if (idx < short_names_count + history_name_count)
5601 return (char_u *)history_names[idx - short_names_count];
5602 if (idx == short_names_count + history_name_count)
5603 return (char_u *)"all";
5604 return NULL;
5605}
5606#endif
5607
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608/*
5609 * init_history() - Initialize the command line history.
5610 * Also used to re-allocate the history when the size changes.
5611 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 int newlen; /* new length of history table */
5616 histentry_T *temp;
5617 int i;
5618 int j;
5619 int type;
5620
5621 /*
5622 * If size of history table changed, reallocate it
5623 */
5624 newlen = (int)p_hi;
5625 if (newlen != hislen) /* history length changed */
5626 {
5627 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5628 {
5629 if (newlen)
5630 {
5631 temp = (histentry_T *)lalloc(
5632 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5633 if (temp == NULL) /* out of memory! */
5634 {
5635 if (type == 0) /* first one: just keep the old length */
5636 {
5637 newlen = hislen;
5638 break;
5639 }
5640 /* Already changed one table, now we can only have zero
5641 * length for all tables. */
5642 newlen = 0;
5643 type = -1;
5644 continue;
5645 }
5646 }
5647 else
5648 temp = NULL;
5649 if (newlen == 0 || temp != NULL)
5650 {
5651 if (hisidx[type] < 0) /* there are no entries yet */
5652 {
5653 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005654 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
5656 else if (newlen > hislen) /* array becomes bigger */
5657 {
5658 for (i = 0; i <= hisidx[type]; ++i)
5659 temp[i] = history[type][i];
5660 j = i;
5661 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005662 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 for ( ; j < hislen; ++i, ++j)
5664 temp[i] = history[type][j];
5665 }
5666 else /* array becomes smaller or 0 */
5667 {
5668 j = hisidx[type];
5669 for (i = newlen - 1; ; --i)
5670 {
5671 if (i >= 0) /* copy newest entries */
5672 temp[i] = history[type][j];
5673 else /* remove older entries */
5674 vim_free(history[type][j].hisstr);
5675 if (--j < 0)
5676 j = hislen - 1;
5677 if (j == hisidx[type])
5678 break;
5679 }
5680 hisidx[type] = newlen - 1;
5681 }
5682 vim_free(history[type]);
5683 history[type] = temp;
5684 }
5685 }
5686 hislen = newlen;
5687 }
5688}
5689
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005692{
5693 hisptr->hisnum = 0;
5694 hisptr->viminfo = FALSE;
5695 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005696 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005697}
5698
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699/*
5700 * Check if command line 'str' is already in history.
5701 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5702 */
5703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005704in_history(
5705 int type,
5706 char_u *str,
5707 int move_to_front, /* Move the entry to the front if it exists */
5708 int sep,
5709 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 int i;
5712 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005713 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 if (hisidx[type] < 0)
5716 return FALSE;
5717 i = hisidx[type];
5718 do
5719 {
5720 if (history[type][i].hisstr == NULL)
5721 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005722
5723 /* For search history, check that the separator character matches as
5724 * well. */
5725 p = history[type][i].hisstr;
5726 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005727 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005728 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
5730 if (!move_to_front)
5731 return TRUE;
5732 last_i = i;
5733 break;
5734 }
5735 if (--i < 0)
5736 i = hislen - 1;
5737 } while (i != hisidx[type]);
5738
5739 if (last_i >= 0)
5740 {
5741 str = history[type][i].hisstr;
5742 while (i != hisidx[type])
5743 {
5744 if (++i >= hislen)
5745 i = 0;
5746 history[type][last_i] = history[type][i];
5747 last_i = i;
5748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005750 history[type][i].viminfo = FALSE;
5751 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005752 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 return TRUE;
5754 }
5755 return FALSE;
5756}
5757
5758/*
5759 * Convert history name (from table above) to its HIST_ equivalent.
5760 * When "name" is empty, return "cmd" history.
5761 * Returns -1 for unknown history name.
5762 */
5763 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 int i;
5767 int len = (int)STRLEN(name);
5768
5769 /* No argument: use current history. */
5770 if (len == 0)
5771 return hist_char2type(ccline.cmdfirstc);
5772
5773 for (i = 0; history_names[i] != NULL; ++i)
5774 if (STRNICMP(name, history_names[i], len) == 0)
5775 return i;
5776
5777 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5778 return hist_char2type(name[0]);
5779
5780 return -1;
5781}
5782
5783static int last_maptick = -1; /* last seen maptick */
5784
5785/*
5786 * Add the given string to the given history. If the string is already in the
5787 * history then it is moved to the front. "histype" may be one of he HIST_
5788 * values.
5789 */
5790 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005791add_to_history(
5792 int histype,
5793 char_u *new_entry,
5794 int in_map, /* consider maptick when inside a mapping */
5795 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 histentry_T *hisptr;
5798 int len;
5799
5800 if (hislen == 0) /* no history */
5801 return;
5802
Bram Moolenaara939e432013-11-09 05:30:26 +01005803 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5804 return;
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 /*
5807 * Searches inside the same mapping overwrite each other, so that only
5808 * the last line is kept. Be careful not to remove a line that was moved
5809 * down, only lines that were added.
5810 */
5811 if (histype == HIST_SEARCH && in_map)
5812 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005813 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
5815 /* Current line is from the same mapping, remove it */
5816 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5817 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005818 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 --hisnum[histype];
5820 if (--hisidx[HIST_SEARCH] < 0)
5821 hisidx[HIST_SEARCH] = hislen - 1;
5822 }
5823 last_maptick = -1;
5824 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005825 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
5827 if (++hisidx[histype] == hislen)
5828 hisidx[histype] = 0;
5829 hisptr = &history[histype][hisidx[histype]];
5830 vim_free(hisptr->hisstr);
5831
5832 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005833 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5835 if (hisptr->hisstr != NULL)
5836 hisptr->hisstr[len + 1] = sep;
5837
5838 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005839 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005840 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 if (histype == HIST_SEARCH && in_map)
5842 last_maptick = maptick;
5843 }
5844}
5845
5846#if defined(FEAT_EVAL) || defined(PROTO)
5847
5848/*
5849 * Get identifier of newest history entry.
5850 * "histype" may be one of the HIST_ values.
5851 */
5852 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5856 || hisidx[histype] < 0)
5857 return -1;
5858
5859 return history[histype][hisidx[histype]].hisnum;
5860}
5861
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 * Calculate history index from a number:
5864 * num > 0: seen as identifying number of a history entry
5865 * num < 0: relative position in history wrt newest entry
5866 * "histype" may be one of the HIST_ values.
5867 */
5868 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005869calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 int i;
5872 histentry_T *hist;
5873 int wrapped = FALSE;
5874
5875 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5876 || (i = hisidx[histype]) < 0 || num == 0)
5877 return -1;
5878
5879 hist = history[histype];
5880 if (num > 0)
5881 {
5882 while (hist[i].hisnum > num)
5883 if (--i < 0)
5884 {
5885 if (wrapped)
5886 break;
5887 i += hislen;
5888 wrapped = TRUE;
5889 }
5890 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5891 return i;
5892 }
5893 else if (-num <= hislen)
5894 {
5895 i += num + 1;
5896 if (i < 0)
5897 i += hislen;
5898 if (hist[i].hisstr != NULL)
5899 return i;
5900 }
5901 return -1;
5902}
5903
5904/*
5905 * Get a history entry by its index.
5906 * "histype" may be one of the HIST_ values.
5907 */
5908 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005909get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
5911 idx = calc_hist_idx(histype, idx);
5912 if (idx >= 0)
5913 return history[histype][idx].hisstr;
5914 else
5915 return (char_u *)"";
5916}
5917
5918/*
5919 * Clear all entries of a history.
5920 * "histype" may be one of the HIST_ values.
5921 */
5922 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005923clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 int i;
5926 histentry_T *hisptr;
5927
5928 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5929 {
5930 hisptr = history[histype];
5931 for (i = hislen; i--;)
5932 {
5933 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005934 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005935 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 hisidx[histype] = -1; /* mark history as cleared */
5938 hisnum[histype] = 0; /* reset identifier counter */
5939 return OK;
5940 }
5941 return FAIL;
5942}
5943
5944/*
5945 * Remove all entries matching {str} from a history.
5946 * "histype" may be one of the HIST_ values.
5947 */
5948 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 regmatch_T regmatch;
5952 histentry_T *hisptr;
5953 int idx;
5954 int i;
5955 int last;
5956 int found = FALSE;
5957
5958 regmatch.regprog = NULL;
5959 regmatch.rm_ic = FALSE; /* always match case */
5960 if (hislen != 0
5961 && histype >= 0
5962 && histype < HIST_COUNT
5963 && *str != NUL
5964 && (idx = hisidx[histype]) >= 0
5965 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5966 != NULL)
5967 {
5968 i = last = idx;
5969 do
5970 {
5971 hisptr = &history[histype][i];
5972 if (hisptr->hisstr == NULL)
5973 break;
5974 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5975 {
5976 found = TRUE;
5977 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005978 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980 else
5981 {
5982 if (i != last)
5983 {
5984 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005985 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 }
5987 if (--last < 0)
5988 last += hislen;
5989 }
5990 if (--i < 0)
5991 i += hislen;
5992 } while (i != idx);
5993 if (history[histype][idx].hisstr == NULL)
5994 hisidx[histype] = -1;
5995 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005996 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 return found;
5998}
5999
6000/*
6001 * Remove an indexed entry from a history.
6002 * "histype" may be one of the HIST_ values.
6003 */
6004 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006{
6007 int i, j;
6008
6009 i = calc_hist_idx(histype, idx);
6010 if (i < 0)
6011 return FALSE;
6012 idx = hisidx[histype];
6013 vim_free(history[histype][i].hisstr);
6014
6015 /* When deleting the last added search string in a mapping, reset
6016 * last_maptick, so that the last added search string isn't deleted again.
6017 */
6018 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6019 last_maptick = -1;
6020
6021 while (i != idx)
6022 {
6023 j = (i + 1) % hislen;
6024 history[histype][i] = history[histype][j];
6025 i = j;
6026 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006027 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (--i < 0)
6029 i += hislen;
6030 hisidx[histype] = i;
6031 return TRUE;
6032}
6033
6034#endif /* FEAT_EVAL */
6035
6036#if defined(FEAT_CRYPT) || defined(PROTO)
6037/*
6038 * Very specific function to remove the value in ":set key=val" from the
6039 * history.
6040 */
6041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 char_u *p;
6045 int i;
6046
6047 i = hisidx[HIST_CMD];
6048 if (i < 0)
6049 return;
6050 p = history[HIST_CMD][i].hisstr;
6051 if (p != NULL)
6052 for ( ; *p; ++p)
6053 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6054 {
6055 p = vim_strchr(p + 3, '=');
6056 if (p == NULL)
6057 break;
6058 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006059 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (p[i] == '\\' && p[i + 1])
6061 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006062 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 --p;
6064 }
6065}
6066#endif
6067
6068#endif /* FEAT_CMDHIST */
6069
Bram Moolenaar064154c2016-03-19 22:50:43 +01006070#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006071/*
6072 * Get pointer to the command line info to use. cmdline_paste() may clear
6073 * ccline and put the previous value in prev_ccline.
6074 */
6075 static struct cmdline_info *
6076get_ccline_ptr(void)
6077{
6078 if ((State & CMDLINE) == 0)
6079 return NULL;
6080 if (ccline.cmdbuff != NULL)
6081 return &ccline;
6082 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6083 return &prev_ccline;
6084 return NULL;
6085}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006086#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006087
Bram Moolenaar064154c2016-03-19 22:50:43 +01006088#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006089/*
6090 * Get the current command line in allocated memory.
6091 * Only works when the command line is being edited.
6092 * Returns NULL when something is wrong.
6093 */
6094 char_u *
6095get_cmdline_str(void)
6096{
6097 struct cmdline_info *p = get_ccline_ptr();
6098
6099 if (p == NULL)
6100 return NULL;
6101 return vim_strnsave(p->cmdbuff, p->cmdlen);
6102}
6103
6104/*
6105 * Get the current command line position, counted in bytes.
6106 * Zero is the first position.
6107 * Only works when the command line is being edited.
6108 * Returns -1 when something is wrong.
6109 */
6110 int
6111get_cmdline_pos(void)
6112{
6113 struct cmdline_info *p = get_ccline_ptr();
6114
6115 if (p == NULL)
6116 return -1;
6117 return p->cmdpos;
6118}
6119
6120/*
6121 * Set the command line byte position to "pos". Zero is the first position.
6122 * Only works when the command line is being edited.
6123 * Returns 1 when failed, 0 when OK.
6124 */
6125 int
6126set_cmdline_pos(
6127 int pos)
6128{
6129 struct cmdline_info *p = get_ccline_ptr();
6130
6131 if (p == NULL)
6132 return 1;
6133
6134 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6135 * changed the command line. */
6136 if (pos < 0)
6137 new_cmdpos = 0;
6138 else
6139 new_cmdpos = pos;
6140 return 0;
6141}
6142#endif
6143
6144#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6145/*
6146 * Get the current command-line type.
6147 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6148 * Only works when the command line is being edited.
6149 * Returns NUL when something is wrong.
6150 */
6151 int
6152get_cmdline_type(void)
6153{
6154 struct cmdline_info *p = get_ccline_ptr();
6155
6156 if (p == NULL)
6157 return NUL;
6158 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006159 return
6160# ifdef FEAT_EVAL
6161 (p->input_fn) ? '@' :
6162# endif
6163 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006164 return p->cmdfirstc;
6165}
6166#endif
6167
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6169/*
6170 * Get indices "num1,num2" that specify a range within a list (not a range of
6171 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6172 * Returns OK if parsed successfully, otherwise FAIL.
6173 */
6174 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006175get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 int len;
6178 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006179 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 *str = skipwhite(*str);
6182 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6183 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006184 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 *str += len;
6186 *num1 = (int)num;
6187 first = TRUE;
6188 }
6189 *str = skipwhite(*str);
6190 if (**str == ',') /* parse "to" part of range */
6191 {
6192 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006193 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (len > 0)
6195 {
6196 *num2 = (int)num;
6197 *str = skipwhite(*str + len);
6198 }
6199 else if (!first) /* no number given at all */
6200 return FAIL;
6201 }
6202 else if (first) /* only one number given */
6203 *num2 = *num1;
6204 return OK;
6205}
6206#endif
6207
6208#if defined(FEAT_CMDHIST) || defined(PROTO)
6209/*
6210 * :history command - print a history
6211 */
6212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006213ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 histentry_T *hist;
6216 int histype1 = HIST_CMD;
6217 int histype2 = HIST_CMD;
6218 int hisidx1 = 1;
6219 int hisidx2 = -1;
6220 int idx;
6221 int i, j, k;
6222 char_u *end;
6223 char_u *arg = eap->arg;
6224
6225 if (hislen == 0)
6226 {
6227 MSG(_("'history' option is zero"));
6228 return;
6229 }
6230
6231 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6232 {
6233 end = arg;
6234 while (ASCII_ISALPHA(*end)
6235 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6236 end++;
6237 i = *end;
6238 *end = NUL;
6239 histype1 = get_histtype(arg);
6240 if (histype1 == -1)
6241 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006242 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
6244 histype1 = 0;
6245 histype2 = HIST_COUNT-1;
6246 }
6247 else
6248 {
6249 *end = i;
6250 EMSG(_(e_trailing));
6251 return;
6252 }
6253 }
6254 else
6255 histype2 = histype1;
6256 *end = i;
6257 }
6258 else
6259 end = arg;
6260 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6261 {
6262 EMSG(_(e_trailing));
6263 return;
6264 }
6265
6266 for (; !got_int && histype1 <= histype2; ++histype1)
6267 {
6268 STRCPY(IObuff, "\n # ");
6269 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6270 MSG_PUTS_TITLE(IObuff);
6271 idx = hisidx[histype1];
6272 hist = history[histype1];
6273 j = hisidx1;
6274 k = hisidx2;
6275 if (j < 0)
6276 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6277 if (k < 0)
6278 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6279 if (idx >= 0 && j <= k)
6280 for (i = idx + 1; !got_int; ++i)
6281 {
6282 if (i == hislen)
6283 i = 0;
6284 if (hist[i].hisstr != NULL
6285 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6286 {
6287 msg_putchar('\n');
6288 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6289 hist[i].hisnum);
6290 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6291 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006292 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 else
6294 STRCAT(IObuff, hist[i].hisstr);
6295 msg_outtrans(IObuff);
6296 out_flush();
6297 }
6298 if (i == idx)
6299 break;
6300 }
6301 }
6302}
6303#endif
6304
6305#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006306/*
6307 * Buffers for history read from a viminfo file. Only valid while reading.
6308 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006309static histentry_T *viminfo_history[HIST_COUNT] =
6310 {NULL, NULL, NULL, NULL, NULL};
6311static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6312static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313static int viminfo_add_at_front = FALSE;
6314
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006315static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317/*
6318 * Translate a history type number to the associated character.
6319 */
6320 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006321hist_type2char(
6322 int type,
6323 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 if (type == HIST_CMD)
6326 return ':';
6327 if (type == HIST_SEARCH)
6328 {
6329 if (use_question)
6330 return '?';
6331 else
6332 return '/';
6333 }
6334 if (type == HIST_EXPR)
6335 return '=';
6336 return '@';
6337}
6338
6339/*
6340 * Prepare for reading the history from the viminfo file.
6341 * This allocates history arrays to store the read history lines.
6342 */
6343 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006344prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 int i;
6347 int num;
6348 int type;
6349 int len;
6350
6351 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006352 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 if (asklen > hislen)
6354 asklen = hislen;
6355
6356 for (type = 0; type < HIST_COUNT; ++type)
6357 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006358 /* Count the number of empty spaces in the history list. Entries read
6359 * from viminfo previously are also considered empty. If there are
6360 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006362 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 num++;
6364 len = asklen;
6365 if (num > len)
6366 len = num;
6367 if (len <= 0)
6368 viminfo_history[type] = NULL;
6369 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006370 viminfo_history[type] = (histentry_T *)lalloc(
6371 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (viminfo_history[type] == NULL)
6373 len = 0;
6374 viminfo_hislen[type] = len;
6375 viminfo_hisidx[type] = 0;
6376 }
6377}
6378
6379/*
6380 * Accept a line from the viminfo, store it in the history array when it's
6381 * new.
6382 */
6383 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006384read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 int type;
6387 long_u len;
6388 char_u *val;
6389 char_u *p;
6390
6391 type = hist_char2type(virp->vir_line[0]);
6392 if (viminfo_hisidx[type] < viminfo_hislen[type])
6393 {
6394 val = viminfo_readstring(virp, 1, TRUE);
6395 if (val != NULL && *val != NUL)
6396 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006397 int sep = (*val == ' ' ? NUL : *val);
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006400 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 /* Need to re-allocate to append the separator byte. */
6403 len = STRLEN(val);
6404 p = lalloc(len + 2, TRUE);
6405 if (p != NULL)
6406 {
6407 if (type == HIST_SEARCH)
6408 {
6409 /* Search entry: Move the separator from the first
6410 * column to after the NUL. */
6411 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006412 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 }
6414 else
6415 {
6416 /* Not a search entry: No separator in the viminfo
6417 * file, add a NUL separator. */
6418 mch_memmove(p, val, (size_t)len + 1);
6419 p[len + 1] = NUL;
6420 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006421 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6422 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006423 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6424 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006425 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427 }
6428 }
6429 vim_free(val);
6430 }
6431 return viminfo_readline(virp);
6432}
6433
Bram Moolenaar07219f92013-04-14 23:19:36 +02006434/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006435 * Accept a new style history line from the viminfo, store it in the history
6436 * array when it's new.
6437 */
6438 void
6439handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006440 garray_T *values,
6441 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006442{
6443 int type;
6444 long_u len;
6445 char_u *val;
6446 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006447 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006448
6449 /* Check the format:
6450 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006451 if (values->ga_len < 4
6452 || vp[0].bv_type != BVAL_NR
6453 || vp[1].bv_type != BVAL_NR
6454 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6455 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006456 return;
6457
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006458 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006459 if (type >= HIST_COUNT)
6460 return;
6461 if (viminfo_hisidx[type] < viminfo_hislen[type])
6462 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006463 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006464 if (val != NULL && *val != NUL)
6465 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006466 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6467 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006468 int idx;
6469 int overwrite = FALSE;
6470
6471 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6472 {
6473 /* If lines were written by an older Vim we need to avoid
6474 * getting duplicates. See if the entry already exists. */
6475 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6476 {
6477 p = viminfo_history[type][idx].hisstr;
6478 if (STRCMP(val, p) == 0
6479 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6480 {
6481 overwrite = TRUE;
6482 break;
6483 }
6484 }
6485
6486 if (!overwrite)
6487 {
6488 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006489 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006490 p = lalloc(len + 2, TRUE);
6491 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006492 else
6493 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006494 if (p != NULL)
6495 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006496 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006497 if (!overwrite)
6498 {
6499 mch_memmove(p, val, (size_t)len + 1);
6500 /* Put the separator after the NUL. */
6501 p[len + 1] = sep;
6502 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006503 viminfo_history[type][idx].hisnum = 0;
6504 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006505 viminfo_hisidx[type]++;
6506 }
6507 }
6508 }
6509 }
6510 }
6511}
6512
6513/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006514 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006515 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006516 static void
6517concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 int idx;
6520 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006521
6522 idx = hisidx[type] + viminfo_hisidx[type];
6523 if (idx >= hislen)
6524 idx -= hislen;
6525 else if (idx < 0)
6526 idx = hislen - 1;
6527 if (viminfo_add_at_front)
6528 hisidx[type] = idx;
6529 else
6530 {
6531 if (hisidx[type] == -1)
6532 hisidx[type] = hislen - 1;
6533 do
6534 {
6535 if (history[type][idx].hisstr != NULL
6536 || history[type][idx].viminfo)
6537 break;
6538 if (++idx == hislen)
6539 idx = 0;
6540 } while (idx != hisidx[type]);
6541 if (idx != hisidx[type] && --idx < 0)
6542 idx = hislen - 1;
6543 }
6544 for (i = 0; i < viminfo_hisidx[type]; i++)
6545 {
6546 vim_free(history[type][idx].hisstr);
6547 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6548 history[type][idx].viminfo = TRUE;
6549 history[type][idx].time_set = viminfo_history[type][i].time_set;
6550 if (--idx < 0)
6551 idx = hislen - 1;
6552 }
6553 idx += 1;
6554 idx %= hislen;
6555 for (i = 0; i < viminfo_hisidx[type]; i++)
6556 {
6557 history[type][idx++].hisnum = ++hisnum[type];
6558 idx %= hislen;
6559 }
6560}
6561
6562#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6563 static int
6564#ifdef __BORLANDC__
6565_RTLENTRYF
6566#endif
6567sort_hist(const void *s1, const void *s2)
6568{
6569 histentry_T *p1 = *(histentry_T **)s1;
6570 histentry_T *p2 = *(histentry_T **)s2;
6571
6572 if (p1->time_set < p2->time_set) return -1;
6573 if (p1->time_set > p2->time_set) return 1;
6574 return 0;
6575}
6576#endif
6577
6578/*
6579 * Merge history lines from viminfo and lines typed in this Vim based on the
6580 * timestamp;
6581 */
6582 static void
6583merge_history(int type)
6584{
6585 int max_len;
6586 histentry_T **tot_hist;
6587 histentry_T *new_hist;
6588 int i;
6589 int len;
6590
6591 /* Make one long list with all entries. */
6592 max_len = hislen + viminfo_hisidx[type];
6593 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6594 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6595 if (tot_hist == NULL || new_hist == NULL)
6596 {
6597 vim_free(tot_hist);
6598 vim_free(new_hist);
6599 return;
6600 }
6601 for (i = 0; i < viminfo_hisidx[type]; i++)
6602 tot_hist[i] = &viminfo_history[type][i];
6603 len = i;
6604 for (i = 0; i < hislen; i++)
6605 if (history[type][i].hisstr != NULL)
6606 tot_hist[len++] = &history[type][i];
6607
6608 /* Sort the list on timestamp. */
6609 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6610
6611 /* Keep the newest ones. */
6612 for (i = 0; i < hislen; i++)
6613 {
6614 if (i < len)
6615 {
6616 new_hist[i] = *tot_hist[i];
6617 tot_hist[i]->hisstr = NULL;
6618 if (new_hist[i].hisnum == 0)
6619 new_hist[i].hisnum = ++hisnum[type];
6620 }
6621 else
6622 clear_hist_entry(&new_hist[i]);
6623 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006624 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006625
6626 /* Free what is not kept. */
6627 for (i = 0; i < viminfo_hisidx[type]; i++)
6628 vim_free(viminfo_history[type][i].hisstr);
6629 for (i = 0; i < hislen; i++)
6630 vim_free(history[type][i].hisstr);
6631 vim_free(history[type]);
6632 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006633 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006634}
6635
6636/*
6637 * Finish reading history lines from viminfo. Not used when writing viminfo.
6638 */
6639 void
6640finish_viminfo_history(vir_T *virp)
6641{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006643 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 for (type = 0; type < HIST_COUNT; ++type)
6646 {
6647 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006648 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006649
6650 if (merge)
6651 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006653 concat_history(type);
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 vim_free(viminfo_history[type]);
6656 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006657 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659}
6660
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006661/*
6662 * Write history to viminfo file in "fp".
6663 * When "merge" is TRUE merge history lines with a previously read viminfo
6664 * file, data is in viminfo_history[].
6665 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006668write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 int i;
6671 int type;
6672 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006673 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 init_history();
6676 if (hislen == 0)
6677 return;
6678 for (type = 0; type < HIST_COUNT; ++type)
6679 {
6680 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6681 if (num_saved == 0)
6682 continue;
6683 if (num_saved < 0) /* Use default */
6684 num_saved = hislen;
6685 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6686 type == HIST_CMD ? _("Command Line") :
6687 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006688 type == HIST_EXPR ? _("Expression") :
6689 type == HIST_INPUT ? _("Input Line") :
6690 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (num_saved > hislen)
6692 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006693
6694 /*
6695 * Merge typed and viminfo history:
6696 * round 1: history of typed commands.
6697 * round 2: history from recently read viminfo.
6698 */
6699 for (round = 1; round <= 2; ++round)
6700 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006701 if (round == 1)
6702 /* start at newest entry, somewhere in the list */
6703 i = hisidx[type];
6704 else if (viminfo_hisidx[type] > 0)
6705 /* start at newest entry, first in the list */
6706 i = 0;
6707 else
6708 /* empty list */
6709 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006710 if (i >= 0)
6711 while (num_saved > 0
6712 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006714 char_u *p;
6715 time_t timestamp;
6716 int c = NUL;
6717
6718 if (round == 1)
6719 {
6720 p = history[type][i].hisstr;
6721 timestamp = history[type][i].time_set;
6722 }
6723 else
6724 {
6725 p = viminfo_history[type] == NULL ? NULL
6726 : viminfo_history[type][i].hisstr;
6727 timestamp = viminfo_history[type] == NULL ? 0
6728 : viminfo_history[type][i].time_set;
6729 }
6730
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006731 if (p != NULL && (round == 2
6732 || !merge
6733 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006735 --num_saved;
6736 fputc(hist_type2char(type, TRUE), fp);
6737 /* For the search history: put the separator in the
6738 * second column; use a space if there isn't one. */
6739 if (type == HIST_SEARCH)
6740 {
6741 c = p[STRLEN(p) + 1];
6742 putc(c == NUL ? ' ' : c, fp);
6743 }
6744 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006745
6746 {
6747 char cbuf[NUMBUFLEN];
6748
6749 /* New style history with a bar line. Format:
6750 * |{bartype},{histtype},{timestamp},{separator},"text" */
6751 if (c == NUL)
6752 cbuf[0] = NUL;
6753 else
6754 sprintf(cbuf, "%d", c);
6755 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6756 type, (long)timestamp, cbuf);
6757 barline_writestring(fp, p, LSIZE - 20);
6758 putc('\n', fp);
6759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006761 if (round == 1)
6762 {
6763 /* Decrement index, loop around and stop when back at
6764 * the start. */
6765 if (--i < 0)
6766 i = hislen - 1;
6767 if (i == hisidx[type])
6768 break;
6769 }
6770 else
6771 {
6772 /* Increment index. Stop at the end in the while. */
6773 ++i;
6774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006776 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006777 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006778 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006779 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006780 vim_free(viminfo_history[type]);
6781 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006782 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 }
6784}
6785#endif /* FEAT_VIMINFO */
6786
6787#if defined(FEAT_FKMAP) || defined(PROTO)
6788/*
6789 * Write a character at the current cursor+offset position.
6790 * It is directly written into the command buffer block.
6791 */
6792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006793cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6796 {
6797 EMSG(_("E198: cmd_pchar beyond the command length"));
6798 return;
6799 }
6800 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6801 ccline.cmdbuff[ccline.cmdlen] = NUL;
6802}
6803
6804 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006805cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6808 {
6809 /* EMSG(_("cmd_gchar beyond the command length")); */
6810 return NUL;
6811 }
6812 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6813}
6814#endif
6815
6816#if defined(FEAT_CMDWIN) || defined(PROTO)
6817/*
6818 * Open a window on the current command line and history. Allow editing in
6819 * the window. Returns when the window is closed.
6820 * Returns:
6821 * CR if the command is to be executed
6822 * Ctrl_C if it is to be abandoned
6823 * K_IGNORE if editing continues
6824 */
6825 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006826open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006829 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006831 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 win_T *wp;
6833 int i;
6834 linenr_T lnum;
6835 int histtype;
6836 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006837#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 int save_restart_edit = restart_edit;
6841 int save_State = State;
6842 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006843#ifdef FEAT_RIGHTLEFT
6844 int save_cmdmsg_rl = cmdmsg_rl;
6845#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006846#ifdef FEAT_FOLDING
6847 int save_KeyTyped;
6848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
6850 /* Can't do this recursively. Can't do it when typing a password. */
6851 if (cmdwin_type != 0
6852# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6853 || cmdline_star > 0
6854# endif
6855 )
6856 {
6857 beep_flush();
6858 return K_IGNORE;
6859 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006860 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
6862 /* Save current window sizes. */
6863 win_size_save(&winsizes);
6864
6865# ifdef FEAT_AUTOCMD
6866 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006867 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006869 /* don't use a new tab page */
6870 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006871 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 /* Create a window for the command-line buffer. */
6874 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6875 {
6876 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006877# ifdef FEAT_AUTOCMD
6878 unblock_autocmds();
6879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 return K_IGNORE;
6881 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006882 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006885 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006886 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006889#ifdef FEAT_FOLDING
6890 curwin->w_p_fen = FALSE;
6891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006893 curwin->w_p_rl = cmdmsg_rl;
6894 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006896 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898# ifdef FEAT_AUTOCMD
6899 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006900 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006901 /* But don't allow switching to another buffer. */
6902 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903# endif
6904
Bram Moolenaar46152342005-09-07 21:18:43 +00006905 /* Showing the prompt may have set need_wait_return, reset it. */
6906 need_wait_return = FALSE;
6907
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006908 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6910 {
6911 if (p_wc == TAB)
6912 {
6913 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6914 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6915 }
6916 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6917 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006918# ifdef FEAT_AUTOCMD
6919 --curbuf_lock;
6920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006922 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6923 * sets 'textwidth' to 78). */
6924 curbuf->b_p_tw = 0;
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 /* Fill the buffer with the history. */
6927 init_history();
6928 if (hislen > 0)
6929 {
6930 i = hisidx[histtype];
6931 if (i >= 0)
6932 {
6933 lnum = 0;
6934 do
6935 {
6936 if (++i == hislen)
6937 i = 0;
6938 if (history[histtype][i].hisstr != NULL)
6939 ml_append(lnum++, history[histtype][i].hisstr,
6940 (colnr_T)0, FALSE);
6941 }
6942 while (i != hisidx[histtype]);
6943 }
6944 }
6945
6946 /* Replace the empty last line with the current command-line and put the
6947 * cursor there. */
6948 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6949 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6950 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006951 changed_line_abv_curs();
6952 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006953 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006956 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958 /* No Ex mode here! */
6959 exmode_active = 0;
6960
6961 State = NORMAL;
6962# ifdef FEAT_MOUSE
6963 setmouse();
6964# endif
6965
6966# ifdef FEAT_AUTOCMD
6967 /* Trigger CmdwinEnter autocommands. */
6968 typestr[0] = cmdwin_type;
6969 typestr[1] = NUL;
6970 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006971 if (restart_edit != 0) /* autocmd with ":startinsert" */
6972 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973# endif
6974
6975 i = RedrawingDisabled;
6976 RedrawingDisabled = 0;
6977
6978 /*
6979 * Call the main loop until <CR> or CTRL-C is typed.
6980 */
6981 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006982 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 RedrawingDisabled = i;
6985
6986# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006987
6988# ifdef FEAT_FOLDING
6989 save_KeyTyped = KeyTyped;
6990# endif
6991
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 /* Trigger CmdwinLeave autocommands. */
6993 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006994
6995# ifdef FEAT_FOLDING
6996 /* Restore KeyTyped in case it is modified by autocommands */
6997 KeyTyped = save_KeyTyped;
6998# endif
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000# endif
7001
Bram Moolenaarccc18222007-05-10 18:25:20 +00007002 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007003 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 cmdwin_type = 0;
7005
7006 exmode_active = save_exmode;
7007
Bram Moolenaarf9821062008-06-20 16:31:07 +00007008 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007010 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
7012 cmdwin_result = Ctrl_C;
7013 EMSG(_("E199: Active window or buffer deleted"));
7014 }
7015 else
7016 {
7017# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7018 /* autocmds may abort script processing */
7019 if (aborting() && cmdwin_result != K_IGNORE)
7020 cmdwin_result = Ctrl_C;
7021# endif
7022 /* Set the new command line from the cmdline buffer. */
7023 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007024 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007026 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7027
7028 if (histtype == HIST_CMD)
7029 {
7030 /* Execute the command directly. */
7031 ccline.cmdbuff = vim_strsave((char_u *)p);
7032 cmdwin_result = CAR;
7033 }
7034 else
7035 {
7036 /* First need to cancel what we were doing. */
7037 ccline.cmdbuff = NULL;
7038 stuffcharReadbuff(':');
7039 stuffReadbuff((char_u *)p);
7040 stuffcharReadbuff(CAR);
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 else if (cmdwin_result == K_XF2) /* :qa typed */
7044 {
7045 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7046 cmdwin_result = CAR;
7047 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007048 else if (cmdwin_result == Ctrl_C)
7049 {
7050 /* :q or :close, don't execute any command
7051 * and don't modify the cmd window. */
7052 ccline.cmdbuff = NULL;
7053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 else
7055 ccline.cmdbuff = vim_strsave(ml_get_curline());
7056 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007057 {
7058 ccline.cmdbuff = vim_strsave((char_u *)"");
7059 ccline.cmdlen = 0;
7060 ccline.cmdbufflen = 1;
7061 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 else
7065 {
7066 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7067 ccline.cmdbufflen = ccline.cmdlen + 1;
7068 ccline.cmdpos = curwin->w_cursor.col;
7069 if (ccline.cmdpos > ccline.cmdlen)
7070 ccline.cmdpos = ccline.cmdlen;
7071 if (cmdwin_result == K_IGNORE)
7072 {
7073 set_cmdspos_cursor();
7074 redrawcmd();
7075 }
7076 }
7077
7078# ifdef FEAT_AUTOCMD
7079 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007080 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007082# ifdef FEAT_CONCEAL
7083 /* Avoid command-line window first character being concealed. */
7084 curwin->w_p_cole = 0;
7085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007087 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 win_goto(old_curwin);
7089 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007090
7091 /* win_close() may have already wiped the buffer when 'bh' is
7092 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007093 if (bufref_valid(&bufref))
7094 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
7096 /* Restore window sizes. */
7097 win_size_restore(&winsizes);
7098
7099# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007100 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101# endif
7102 }
7103
7104 ga_clear(&winsizes);
7105 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007106# ifdef FEAT_RIGHTLEFT
7107 cmdmsg_rl = save_cmdmsg_rl;
7108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110 State = save_State;
7111# ifdef FEAT_MOUSE
7112 setmouse();
7113# endif
7114
7115 return cmdwin_result;
7116}
7117#endif /* FEAT_CMDWIN */
7118
7119/*
7120 * Used for commands that either take a simple command string argument, or:
7121 * cmd << endmarker
7122 * {script}
7123 * endmarker
7124 * Returns a pointer to allocated memory with {script} or NULL.
7125 */
7126 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007127script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 char_u *theline;
7130 char *end_pattern = NULL;
7131 char dot[] = ".";
7132 garray_T ga;
7133
7134 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7135 return NULL;
7136
7137 ga_init2(&ga, 1, 0x400);
7138
7139 if (cmd[2] != NUL)
7140 end_pattern = (char *)skipwhite(cmd + 2);
7141 else
7142 end_pattern = dot;
7143
7144 for (;;)
7145 {
7146 theline = eap->getline(
7147#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007148 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#endif
7150 NUL, eap->cookie, 0);
7151
7152 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007153 {
7154 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
7158 ga_concat(&ga, theline);
7159 ga_append(&ga, '\n');
7160 vim_free(theline);
7161 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007162 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
7164 return (char_u *)ga.ga_data;
7165}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007166
7167#ifdef FEAT_SEARCH_EXTRA
7168 static void
7169set_search_match(pos_T *t)
7170{
7171 /*
7172 * First move cursor to end of match, then to the start. This
7173 * moves the whole match onto the screen when 'nowrap' is set.
7174 */
7175 t->lnum += search_match_lines;
7176 t->col = search_match_endcol;
7177 if (t->lnum > curbuf->b_ml.ml_line_count)
7178 {
7179 t->lnum = curbuf->b_ml.ml_line_count;
7180 coladvance((colnr_T)MAXCOL);
7181 }
7182}
7183#endif