blob: 110a95afd68331a0e5344d9374b83a3dd6ec4e27 [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
55#ifdef FEAT_CMDHIST
56typedef struct hist_entry
57{
58 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020059 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020061 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000062} histentry_T;
63
64static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
65static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
66static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
67 /* identifying (unique) number of newest history entry */
68static int hislen = 0; /* actual length of history tables */
69
Bram Moolenaard25c16e2016-01-29 22:13:30 +010070static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaard25c16e2016-01-29 22:13:30 +010072static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075# endif
76#endif
77
78#ifdef FEAT_RIGHTLEFT
79static int cmd_hkmap = 0; /* Hebrew mapping during command line */
80#endif
81
82#ifdef FEAT_FKMAP
83static int cmd_fkmap = 0; /* Farsi mapping during command line */
84#endif
85
Bram Moolenaard25c16e2016-01-29 22:13:30 +010086static int cmdline_charsize(int idx);
87static void set_cmdspos(void);
88static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void alloc_cmdbuff(int len);
93static int realloc_cmdbuff(int len);
94static void draw_cmdline(int start, int len);
95static void save_cmdline(struct cmdline_info *ccp);
96static void restore_cmdline(struct cmdline_info *ccp);
97static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010099static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
101#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100104static void redrawcmdprompt(void);
105static void cursorcmd(void);
106static int ccheck_abbr(int);
107static int nextwild(expand_T *xp, int type, int options, int escape);
108static void escape_fname(char_u **pp);
109static int showmatches(expand_T *xp, int wildmenu);
110static void set_expand_context(expand_T *xp);
111static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
112static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100114static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100115static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100116static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200117# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100121static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
122static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123# endif
124#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200125#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_CMDWIN
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static int ex_window(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
134static int
135#ifdef __BORLANDC__
136_RTLENTRYF
137#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100138sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200139#endif
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200140#ifdef FEAT_SEARCH_EXTRA
141static void set_search_match(pos_T *t);
142#endif
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144/*
145 * getcmdline() - accept a command line starting with firstc.
146 *
147 * firstc == ':' get ":" command line.
148 * firstc == '/' or '?' get search pattern
149 * firstc == '=' get expression
150 * firstc == '@' get text for input() function
151 * firstc == '>' get text for debug mode
152 * firstc == NUL get text for :insert command
153 * firstc == -1 like NUL, and break on CTRL-C
154 *
155 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
156 * command line.
157 *
158 * Careful: getcmdline() can be called recursively!
159 *
160 * Return pointer to allocated string if there is a commandline, NULL
161 * otherwise.
162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100164getcmdline(
165 int firstc,
166 long count UNUSED, /* only used for incremental search */
167 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 int c;
170 int i;
171 int j;
172 int gotesc = FALSE; /* TRUE when <ESC> just typed */
173 int do_abbr; /* when TRUE check for abbr. */
174#ifdef FEAT_CMDHIST
175 char_u *lookfor = NULL; /* string to match */
176 int hiscnt; /* current history line in use */
177 int histype; /* history type to be used */
178#endif
179#ifdef FEAT_SEARCH_EXTRA
180 pos_T old_cursor;
181 colnr_T old_curswant;
182 colnr_T old_leftcol;
183 linenr_T old_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200184 pos_T cursor_start;
185 pos_T match_start = curwin->w_cursor;
186 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187# ifdef FEAT_DIFF
188 int old_topfill;
189# endif
190 linenr_T old_botline;
191 int did_incsearch = FALSE;
192 int incsearch_postponed = FALSE;
193#endif
194 int did_wild_list = FALSE; /* did wild_list() recently */
195 int wim_index = 0; /* index in wim_flags[] */
196 int res;
197 int save_msg_scroll = msg_scroll;
198 int save_State = State; /* remember State when called */
199 int some_key_typed = FALSE; /* one of the keys was typed */
200#ifdef FEAT_MOUSE
201 /* mouse drag and release events are ignored, unless they are
202 * preceded with a mouse down event */
203 int ignore_drag_release = TRUE;
204#endif
205#ifdef FEAT_EVAL
206 int break_ctrl_c = FALSE;
207#endif
208 expand_T xpc;
209 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000210#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
211 /* Everything that may work recursively should save and restore the
212 * current command line in save_ccline. That includes update_screen(), a
213 * custom status line may invoke ":normal". */
214 struct cmdline_info save_ccline;
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#ifdef FEAT_EVAL
218 if (firstc == -1)
219 {
220 firstc = NUL;
221 break_ctrl_c = TRUE;
222 }
223#endif
224#ifdef FEAT_RIGHTLEFT
225 /* start without Hebrew mapping for a command line */
226 if (firstc == ':' || firstc == '=' || firstc == '>')
227 cmd_hkmap = 0;
228#endif
229
230 ccline.overstrike = FALSE; /* always start in insert mode */
231#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200232 clearpos(&match_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 old_cursor = curwin->w_cursor; /* needs to be restored later */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200234 cursor_start = old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 old_curswant = curwin->w_curswant;
236 old_leftcol = curwin->w_leftcol;
237 old_topline = curwin->w_topline;
238# ifdef FEAT_DIFF
239 old_topfill = curwin->w_topfill;
240# endif
241 old_botline = curwin->w_botline;
242#endif
243
244 /*
245 * set some variables for redrawcmd()
246 */
247 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000248 ccline.cmdindent = (firstc > 0 ? indent : 0);
249
250 /* alloc initial ccline.cmdbuff */
251 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 if (ccline.cmdbuff == NULL)
253 return NULL; /* out of memory */
254 ccline.cmdlen = ccline.cmdpos = 0;
255 ccline.cmdbuff[0] = NUL;
256
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000257 /* autoindent for :insert and :append */
258 if (firstc <= 0)
259 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200260 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000261 ccline.cmdbuff[indent] = NUL;
262 ccline.cmdpos = indent;
263 ccline.cmdspos = indent;
264 ccline.cmdlen = indent;
265 }
266
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000268 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
270#ifdef FEAT_RIGHTLEFT
271 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
272 && (firstc == '/' || firstc == '?'))
273 cmdmsg_rl = TRUE;
274 else
275 cmdmsg_rl = FALSE;
276#endif
277
278 redir_off = TRUE; /* don't redirect the typed command */
279 if (!cmd_silent)
280 {
281 i = msg_scrolled;
282 msg_scrolled = 0; /* avoid wait_return message */
283 gotocmdline(TRUE);
284 msg_scrolled += i;
285 redrawcmdprompt(); /* draw prompt or indent */
286 set_cmdspos();
287 }
288 xpc.xp_context = EXPAND_NOTHING;
289 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000290#ifndef BACKSLASH_IN_FILENAME
291 xpc.xp_shell = FALSE;
292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000294#if defined(FEAT_EVAL)
295 if (ccline.input_fn)
296 {
297 xpc.xp_context = ccline.xp_context;
298 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000299# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000300 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000301# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000302 }
303#endif
304
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 /*
306 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
307 * doing ":@0" when register 0 doesn't contain a CR.
308 */
309 msg_scroll = FALSE;
310
311 State = CMDLINE;
312
313 if (firstc == '/' || firstc == '?' || firstc == '@')
314 {
315 /* Use ":lmap" mappings for search pattern and input(). */
316 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
317 b_im_ptr = &curbuf->b_p_iminsert;
318 else
319 b_im_ptr = &curbuf->b_p_imsearch;
320 if (*b_im_ptr == B_IMODE_LMAP)
321 State |= LANGMAP;
322#ifdef USE_IM_CONTROL
323 im_set_active(*b_im_ptr == B_IMODE_IM);
324#endif
325 }
326#ifdef USE_IM_CONTROL
327 else if (p_imcmdline)
328 im_set_active(TRUE);
329#endif
330
331#ifdef FEAT_MOUSE
332 setmouse();
333#endif
334#ifdef CURSOR_SHAPE
335 ui_cursor_shape(); /* may show different cursor shape */
336#endif
337
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000338 /* When inside an autocommand for writing "exiting" may be set and
339 * terminal mode set to cooked. Need to set raw mode here then. */
340 settmode(TMODE_RAW);
341
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#ifdef FEAT_CMDHIST
343 init_history();
344 hiscnt = hislen; /* set hiscnt to impossible history value */
345 histype = hist_char2type(firstc);
346#endif
347
348#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000349 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#endif
351
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200352 /* If something above caused an error, reset the flags, we do want to type
353 * and execute commands. Display may be messed up a bit. */
354 if (did_emsg)
355 redrawcmd();
356 did_emsg = FALSE;
357 got_int = FALSE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 /*
360 * Collect the command string, handling editing keys.
361 */
362 for (;;)
363 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000364 redir_off = TRUE; /* Don't redirect the typed command.
365 Repeated, because a ":redir" inside
366 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#ifdef USE_ON_FLY_SCROLL
368 dont_scroll = FALSE; /* allow scrolling here */
369#endif
370 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
371
372 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000373
374 /* Get a character. Ignore K_IGNORE, it should not do anything, such
375 * as stop completion. */
376 do
377 {
378 c = safe_vgetc();
379 } while (c == K_IGNORE);
380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 if (KeyTyped)
382 {
383 some_key_typed = TRUE;
384#ifdef FEAT_RIGHTLEFT
385 if (cmd_hkmap)
386 c = hkmap(c);
387# ifdef FEAT_FKMAP
388 if (cmd_fkmap)
389 c = cmdl_fkmap(c);
390# endif
391 if (cmdmsg_rl && !KeyStuffed)
392 {
393 /* Invert horizontal movements and operations. Only when
394 * typed by the user directly, not when the result of a
395 * mapping. */
396 switch (c)
397 {
398 case K_RIGHT: c = K_LEFT; break;
399 case K_S_RIGHT: c = K_S_LEFT; break;
400 case K_C_RIGHT: c = K_C_LEFT; break;
401 case K_LEFT: c = K_RIGHT; break;
402 case K_S_LEFT: c = K_S_RIGHT; break;
403 case K_C_LEFT: c = K_C_RIGHT; break;
404 }
405 }
406#endif
407 }
408
409 /*
410 * Ignore got_int when CTRL-C was typed here.
411 * Don't ignore it in :global, we really need to break then, e.g., for
412 * ":g/pat/normal /pat" (without the <CR>).
413 * Don't ignore it for the input() function.
414 */
415 if ((c == Ctrl_C
416#ifdef UNIX
417 || c == intr_char
418#endif
419 )
420#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
421 && firstc != '@'
422#endif
423#ifdef FEAT_EVAL
424 && !break_ctrl_c
425#endif
426 && !global_busy)
427 got_int = FALSE;
428
429#ifdef FEAT_CMDHIST
430 /* free old command line when finished moving around in the history
431 * list */
432 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000433 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000434 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 && c != K_PAGEDOWN && c != K_PAGEUP
436 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000437 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
439 {
440 vim_free(lookfor);
441 lookfor = NULL;
442 }
443#endif
444
445 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000446 * When there are matching completions to select <S-Tab> works like
447 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000449 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 c = Ctrl_P;
451
452#ifdef FEAT_WILDMENU
453 /* Special translations for 'wildmenu' */
454 if (did_wild_list && p_wmnu)
455 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000456 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000458 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 c = Ctrl_N;
460 }
461 /* Hitting CR after "emenu Name.": complete submenu */
462 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
463 && ccline.cmdpos > 1
464 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
465 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
466 && (c == '\n' || c == '\r' || c == K_KENTER))
467 c = K_DOWN;
468#endif
469
470 /* free expanded names when finished walking through matches */
471 if (xpc.xp_numfiles != -1
472 && !(c == p_wc && KeyTyped) && c != p_wcm
473 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
474 && c != Ctrl_L)
475 {
476 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
477 did_wild_list = FALSE;
478#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000479 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480#endif
481 xpc.xp_context = EXPAND_NOTHING;
482 wim_index = 0;
483#ifdef FEAT_WILDMENU
484 if (p_wmnu && wild_menu_showing != 0)
485 {
486 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000487 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000488
489 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000490 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 if (wild_menu_showing == WM_SCROLLED)
493 {
494 /* Entered command line, move it up */
495 cmdline_row--;
496 redrawcmd();
497 }
498 else if (save_p_ls != -1)
499 {
500 /* restore 'laststatus' and 'winminheight' */
501 p_ls = save_p_ls;
502 p_wmh = save_p_wmh;
503 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000504 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000506 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 redrawcmd();
508 save_p_ls = -1;
509 }
510 else
511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 redraw_statuslines();
514 }
515 KeyTyped = skt;
516 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000517 if (ccline.input_fn)
518 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520#endif
521 }
522
523#ifdef FEAT_WILDMENU
524 /* Special translations for 'wildmenu' */
525 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
526 {
527 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000528 if (c == K_DOWN && ccline.cmdpos > 0
529 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000531 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
533 /* Hitting <Up>: Remove one submenu name in front of the
534 * cursor */
535 int found = FALSE;
536
537 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
538 i = 0;
539 while (--j > 0)
540 {
541 /* check for start of menu name */
542 if (ccline.cmdbuff[j] == ' '
543 && ccline.cmdbuff[j - 1] != '\\')
544 {
545 i = j + 1;
546 break;
547 }
548 /* check for start of submenu name */
549 if (ccline.cmdbuff[j] == '.'
550 && ccline.cmdbuff[j - 1] != '\\')
551 {
552 if (found)
553 {
554 i = j + 1;
555 break;
556 }
557 else
558 found = TRUE;
559 }
560 }
561 if (i > 0)
562 cmdline_del(i);
563 c = p_wc;
564 xpc.xp_context = EXPAND_NOTHING;
565 }
566 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000567 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000568 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000569 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 char_u upseg[5];
572
573 upseg[0] = PATHSEP;
574 upseg[1] = '.';
575 upseg[2] = '.';
576 upseg[3] = PATHSEP;
577 upseg[4] = NUL;
578
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000579 if (c == K_DOWN
580 && ccline.cmdpos > 0
581 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
582 && (ccline.cmdpos < 3
583 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
585 {
586 /* go down a directory */
587 c = p_wc;
588 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000589 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
591 /* If in a direct ancestor, strip off one ../ to go down */
592 int found = FALSE;
593
594 j = ccline.cmdpos;
595 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
596 while (--j > i)
597 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000598#ifdef FEAT_MBYTE
599 if (has_mbyte)
600 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (vim_ispathsep(ccline.cmdbuff[j]))
603 {
604 found = TRUE;
605 break;
606 }
607 }
608 if (found
609 && ccline.cmdbuff[j - 1] == '.'
610 && ccline.cmdbuff[j - 2] == '.'
611 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
612 {
613 cmdline_del(j - 2);
614 c = p_wc;
615 }
616 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000617 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 {
619 /* go up a directory */
620 int found = FALSE;
621
622 j = ccline.cmdpos - 1;
623 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
624 while (--j > i)
625 {
626#ifdef FEAT_MBYTE
627 if (has_mbyte)
628 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
629#endif
630 if (vim_ispathsep(ccline.cmdbuff[j])
631#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100632 && vim_strchr((char_u *)" *?[{`$%#",
633 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635 )
636 {
637 if (found)
638 {
639 i = j + 1;
640 break;
641 }
642 else
643 found = TRUE;
644 }
645 }
646
647 if (!found)
648 j = i;
649 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
650 j += 4;
651 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
652 && j == i)
653 j += 3;
654 else
655 j = 0;
656 if (j > 0)
657 {
658 /* TODO this is only for DOS/UNIX systems - need to put in
659 * machine-specific stuff here and in upseg init */
660 cmdline_del(j);
661 put_on_cmdline(upseg + 1, 3, FALSE);
662 }
663 else if (ccline.cmdpos > i)
664 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100665
666 /* Now complete in the new directory. Set KeyTyped in case the
667 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100669 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
673#endif /* FEAT_WILDMENU */
674
675 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
676 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
677 if (c == Ctrl_BSL)
678 {
679 ++no_mapping;
680 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000681 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 --no_mapping;
683 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200684 /* CTRL-\ e doesn't work when obtaining an expression, unless it
685 * is in a mapping. */
686 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
687 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
689 vungetc(c);
690 c = Ctrl_BSL;
691 }
692#ifdef FEAT_EVAL
693 else if (c == 'e')
694 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200695 char_u *p = NULL;
696 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 /*
699 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000700 * Need to save and restore the current command line, to be
701 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 */
703 if (ccline.cmdpos == ccline.cmdlen)
704 new_cmdpos = 99999; /* keep it at the end */
705 else
706 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000707
708 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000710 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 if (c == '=')
712 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000713 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000714 * to avoid nasty things like going to another buffer when
715 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000716 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000717 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000719 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000720 restore_cmdline(&save_ccline);
721
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200722 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200724 len = (int)STRLEN(p);
725 if (realloc_cmdbuff(len + 1) == OK)
726 {
727 ccline.cmdlen = len;
728 STRCPY(ccline.cmdbuff, p);
729 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200731 /* Restore the cursor or use the position set with
732 * set_cmdline_pos(). */
733 if (new_cmdpos > ccline.cmdlen)
734 ccline.cmdpos = ccline.cmdlen;
735 else
736 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200738 KeyTyped = FALSE; /* Don't do p_wc completion. */
739 redrawcmd();
740 goto cmdline_changed;
741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743 }
744 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100745 got_int = FALSE; /* don't abandon the command line */
746 did_emsg = FALSE;
747 emsg_on_display = FALSE;
748 redrawcmd();
749 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751#endif
752 else
753 {
754 if (c == Ctrl_G && p_im && restart_edit == 0)
755 restart_edit = 'a';
756 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
757 in history */
758 goto returncmd; /* back to Normal mode */
759 }
760 }
761
762#ifdef FEAT_CMDWIN
763 if (c == cedit_key || c == K_CMDWIN)
764 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200765 if (ex_normal_busy == 0 && got_int == FALSE)
766 {
767 /*
768 * Open a window to edit the command line (and history).
769 */
770 c = ex_window();
771 some_key_typed = TRUE;
772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774# ifdef FEAT_DIGRAPHS
775 else
776# endif
777#endif
778#ifdef FEAT_DIGRAPHS
779 c = do_digraph(c);
780#endif
781
782 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
783 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
784 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000785 /* In Ex mode a backslash escapes a newline. */
786 if (exmode_active
787 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000788 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000789 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000790 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000792 if (c == K_KENTER)
793 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000795 else
796 {
797 gotesc = FALSE; /* Might have typed ESC previously, don't
798 truncate the cmdline now. */
799 if (ccheck_abbr(c + ABBR_OFF))
800 goto cmdline_changed;
801 if (!cmd_silent)
802 {
803 windgoto(msg_row, 0);
804 out_flush();
805 }
806 break;
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809
810 /*
811 * Completion for 'wildchar' or 'wildcharm' key.
812 * - hitting <ESC> twice means: abandon command line.
813 * - wildcard expansion is only done when the 'wildchar' key is really
814 * typed, not when it comes from a macro
815 */
816 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
817 {
818 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
819 {
820 /* if 'wildmode' contains "list" may still need to list */
821 if (xpc.xp_numfiles > 1
822 && !did_wild_list
823 && (wim_flags[wim_index] & WIM_LIST))
824 {
825 (void)showmatches(&xpc, FALSE);
826 redrawcmd();
827 did_wild_list = TRUE;
828 }
829 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100830 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
831 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100833 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
834 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 else
836 res = OK; /* don't insert 'wildchar' now */
837 }
838 else /* typed p_wc first time */
839 {
840 wim_index = 0;
841 j = ccline.cmdpos;
842 /* if 'wildmode' first contains "longest", get longest
843 * common part */
844 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100845 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
846 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100848 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
849 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
851 /* if interrupted while completing, behave like it failed */
852 if (got_int)
853 {
854 (void)vpeekc(); /* remove <C-C> from input stream */
855 got_int = FALSE; /* don't abandon the command line */
856 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
857#ifdef FEAT_WILDMENU
858 xpc.xp_context = EXPAND_NOTHING;
859#endif
860 goto cmdline_changed;
861 }
862
863 /* when more than one match, and 'wildmode' first contains
864 * "list", or no change and 'wildmode' contains "longest,list",
865 * list all matches */
866 if (res == OK && xpc.xp_numfiles > 1)
867 {
868 /* a "longest" that didn't do anything is skipped (but not
869 * "list:longest") */
870 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
871 wim_index = 1;
872 if ((wim_flags[wim_index] & WIM_LIST)
873#ifdef FEAT_WILDMENU
874 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
875#endif
876 )
877 {
878 if (!(wim_flags[0] & WIM_LONGEST))
879 {
880#ifdef FEAT_WILDMENU
881 int p_wmnu_save = p_wmnu;
882 p_wmnu = 0;
883#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100884 /* remove match */
885 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_WILDMENU
887 p_wmnu = p_wmnu_save;
888#endif
889 }
890#ifdef FEAT_WILDMENU
891 (void)showmatches(&xpc, p_wmnu
892 && ((wim_flags[wim_index] & WIM_LIST) == 0));
893#else
894 (void)showmatches(&xpc, FALSE);
895#endif
896 redrawcmd();
897 did_wild_list = TRUE;
898 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100899 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
900 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100902 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
903 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200906 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
908#ifdef FEAT_WILDMENU
909 else if (xpc.xp_numfiles == -1)
910 xpc.xp_context = EXPAND_NOTHING;
911#endif
912 }
913 if (wim_index < 3)
914 ++wim_index;
915 if (c == ESC)
916 gotesc = TRUE;
917 if (res == OK)
918 goto cmdline_changed;
919 }
920
921 gotesc = FALSE;
922
923 /* <S-Tab> goes to last match, in a clumsy way */
924 if (c == K_S_TAB && KeyTyped)
925 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100926 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
927 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
928 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 goto cmdline_changed;
930 }
931
932 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
933 c = NL;
934
935 do_abbr = TRUE; /* default: check for abbreviation */
936
937 /*
938 * Big switch for a typed command line character.
939 */
940 switch (c)
941 {
942 case K_BS:
943 case Ctrl_H:
944 case K_DEL:
945 case K_KDEL:
946 case Ctrl_W:
947#ifdef FEAT_FKMAP
948 if (cmd_fkmap && c == K_BS)
949 c = K_DEL;
950#endif
951 if (c == K_KDEL)
952 c = K_DEL;
953
954 /*
955 * delete current character is the same as backspace on next
956 * character, except at end of line
957 */
958 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
959 ++ccline.cmdpos;
960#ifdef FEAT_MBYTE
961 if (has_mbyte && c == K_DEL)
962 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
963 ccline.cmdbuff + ccline.cmdpos);
964#endif
965 if (ccline.cmdpos > 0)
966 {
967 char_u *p;
968
969 j = ccline.cmdpos;
970 p = ccline.cmdbuff + j;
971#ifdef FEAT_MBYTE
972 if (has_mbyte)
973 {
974 p = mb_prevptr(ccline.cmdbuff, p);
975 if (c == Ctrl_W)
976 {
977 while (p > ccline.cmdbuff && vim_isspace(*p))
978 p = mb_prevptr(ccline.cmdbuff, p);
979 i = mb_get_class(p);
980 while (p > ccline.cmdbuff && mb_get_class(p) == i)
981 p = mb_prevptr(ccline.cmdbuff, p);
982 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000983 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
985 }
986 else
987#endif
988 if (c == Ctrl_W)
989 {
990 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
991 --p;
992 i = vim_iswordc(p[-1]);
993 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
994 && vim_iswordc(p[-1]) == i)
995 --p;
996 }
997 else
998 --p;
999 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1000 ccline.cmdlen -= j - ccline.cmdpos;
1001 i = ccline.cmdpos;
1002 while (i < ccline.cmdlen)
1003 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1004
1005 /* Truncate at the end, required for multi-byte chars. */
1006 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001007#ifdef FEAT_SEARCH_EXTRA
1008 if (ccline.cmdlen == 0)
1009 old_cursor = cursor_start;
1010 else
1011 {
1012 old_cursor = match_start;
1013 decl(&old_cursor);
1014 }
1015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 redrawcmd();
1017 }
1018 else if (ccline.cmdlen == 0 && c != Ctrl_W
1019 && ccline.cmdprompt == NULL && indent == 0)
1020 {
1021 /* In ex and debug mode it doesn't make sense to return. */
1022 if (exmode_active
1023#ifdef FEAT_EVAL
1024 || ccline.cmdfirstc == '>'
1025#endif
1026 )
1027 goto cmdline_not_changed;
1028
1029 vim_free(ccline.cmdbuff); /* no commandline to return */
1030 ccline.cmdbuff = NULL;
1031 if (!cmd_silent)
1032 {
1033#ifdef FEAT_RIGHTLEFT
1034 if (cmdmsg_rl)
1035 msg_col = Columns;
1036 else
1037#endif
1038 msg_col = 0;
1039 msg_putchar(' '); /* delete ':' */
1040 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001041#ifdef FEAT_SEARCH_EXTRA
1042 if (ccline.cmdlen == 0)
1043 old_cursor = cursor_start;
1044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 redraw_cmdline = TRUE;
1046 goto returncmd; /* back to cmd mode */
1047 }
1048 goto cmdline_changed;
1049
1050 case K_INS:
1051 case K_KINS:
1052#ifdef FEAT_FKMAP
1053 /* if Farsi mode set, we are in reverse insert mode -
1054 Do not change the mode */
1055 if (cmd_fkmap)
1056 beep_flush();
1057 else
1058#endif
1059 ccline.overstrike = !ccline.overstrike;
1060#ifdef CURSOR_SHAPE
1061 ui_cursor_shape(); /* may show different cursor shape */
1062#endif
1063 goto cmdline_not_changed;
1064
1065 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001066 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 /* ":lmap" mappings exists, toggle use of mappings. */
1069 State ^= LANGMAP;
1070#ifdef USE_IM_CONTROL
1071 im_set_active(FALSE); /* Disable input method */
1072#endif
1073 if (b_im_ptr != NULL)
1074 {
1075 if (State & LANGMAP)
1076 *b_im_ptr = B_IMODE_LMAP;
1077 else
1078 *b_im_ptr = B_IMODE_NONE;
1079 }
1080 }
1081#ifdef USE_IM_CONTROL
1082 else
1083 {
1084 /* There are no ":lmap" mappings, toggle IM. When
1085 * 'imdisable' is set don't try getting the status, it's
1086 * always off. */
1087 if ((p_imdisable && b_im_ptr != NULL)
1088 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1089 {
1090 im_set_active(FALSE); /* Disable input method */
1091 if (b_im_ptr != NULL)
1092 *b_im_ptr = B_IMODE_NONE;
1093 }
1094 else
1095 {
1096 im_set_active(TRUE); /* Enable input method */
1097 if (b_im_ptr != NULL)
1098 *b_im_ptr = B_IMODE_IM;
1099 }
1100 }
1101#endif
1102 if (b_im_ptr != NULL)
1103 {
1104 if (b_im_ptr == &curbuf->b_p_iminsert)
1105 set_iminsert_global();
1106 else
1107 set_imsearch_global();
1108 }
1109#ifdef CURSOR_SHAPE
1110 ui_cursor_shape(); /* may show different cursor shape */
1111#endif
1112#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1113 /* Show/unshow value of 'keymap' in status lines later. */
1114 status_redraw_curbuf();
1115#endif
1116 goto cmdline_not_changed;
1117
1118/* case '@': only in very old vi */
1119 case Ctrl_U:
1120 /* delete all characters left of the cursor */
1121 j = ccline.cmdpos;
1122 ccline.cmdlen -= j;
1123 i = ccline.cmdpos = 0;
1124 while (i < ccline.cmdlen)
1125 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1126 /* Truncate at the end, required for multi-byte chars. */
1127 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001128#ifdef FEAT_SEARCH_EXTRA
1129 if (ccline.cmdlen == 0)
1130 old_cursor = cursor_start;
1131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 redrawcmd();
1133 goto cmdline_changed;
1134
1135#ifdef FEAT_CLIPBOARD
1136 case Ctrl_Y:
1137 /* Copy the modeless selection, if there is one. */
1138 if (clip_star.state != SELECT_CLEARED)
1139 {
1140 if (clip_star.state == SELECT_DONE)
1141 clip_copy_modeless_selection(TRUE);
1142 goto cmdline_not_changed;
1143 }
1144 break;
1145#endif
1146
1147 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1148 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001149 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001150 * ":normal" runs out of characters. */
1151 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001152 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 goto cmdline_not_changed;
1154
1155 gotesc = TRUE; /* will free ccline.cmdbuff after
1156 putting it in history */
1157 goto returncmd; /* back to cmd mode */
1158
1159 case Ctrl_R: /* insert register */
1160#ifdef USE_ON_FLY_SCROLL
1161 dont_scroll = TRUE; /* disallow scrolling here */
1162#endif
1163 putcmdline('"', TRUE);
1164 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001165 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (i == Ctrl_O)
1167 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1168 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001169 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 --no_mapping;
1171#ifdef FEAT_EVAL
1172 /*
1173 * Insert the result of an expression.
1174 * Need to save the current command line, to be able to enter
1175 * a new one...
1176 */
1177 new_cmdpos = -1;
1178 if (c == '=')
1179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1181 {
1182 beep_flush();
1183 c = ESC;
1184 }
1185 else
1186 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001187 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001189 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 }
1192#endif
1193 if (c != ESC) /* use ESC to cancel inserting register */
1194 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001195 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001196
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001197#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001198 /* When there was a serious error abort getting the
1199 * command line. */
1200 if (aborting())
1201 {
1202 gotesc = TRUE; /* will free ccline.cmdbuff after
1203 putting it in history */
1204 goto returncmd; /* back to cmd mode */
1205 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 KeyTyped = FALSE; /* Don't do p_wc completion. */
1208#ifdef FEAT_EVAL
1209 if (new_cmdpos >= 0)
1210 {
1211 /* set_cmdline_pos() was used */
1212 if (new_cmdpos > ccline.cmdlen)
1213 ccline.cmdpos = ccline.cmdlen;
1214 else
1215 ccline.cmdpos = new_cmdpos;
1216 }
1217#endif
1218 }
1219 redrawcmd();
1220 goto cmdline_changed;
1221
1222 case Ctrl_D:
1223 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1224 break; /* Use ^D as normal char instead */
1225
1226 redrawcmd();
1227 continue; /* don't do incremental search now */
1228
1229 case K_RIGHT:
1230 case K_S_RIGHT:
1231 case K_C_RIGHT:
1232 do
1233 {
1234 if (ccline.cmdpos >= ccline.cmdlen)
1235 break;
1236 i = cmdline_charsize(ccline.cmdpos);
1237 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1238 break;
1239 ccline.cmdspos += i;
1240#ifdef FEAT_MBYTE
1241 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001242 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 + ccline.cmdpos);
1244 else
1245#endif
1246 ++ccline.cmdpos;
1247 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001248 while ((c == K_S_RIGHT || c == K_C_RIGHT
1249 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1251#ifdef FEAT_MBYTE
1252 if (has_mbyte)
1253 set_cmdspos_cursor();
1254#endif
1255 goto cmdline_not_changed;
1256
1257 case K_LEFT:
1258 case K_S_LEFT:
1259 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001260 if (ccline.cmdpos == 0)
1261 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 do
1263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 --ccline.cmdpos;
1265#ifdef FEAT_MBYTE
1266 if (has_mbyte) /* move to first byte of char */
1267 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1268 ccline.cmdbuff + ccline.cmdpos);
1269#endif
1270 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1271 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001272 while (ccline.cmdpos > 0
1273 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001274 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1276#ifdef FEAT_MBYTE
1277 if (has_mbyte)
1278 set_cmdspos_cursor();
1279#endif
1280 goto cmdline_not_changed;
1281
1282 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001283 /* Ignore mouse event or ex_window() result. */
1284 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaar4770d092006-01-12 23:22:24 +00001286#ifdef FEAT_GUI_W32
1287 /* On Win32 ignore <M-F4>, we get it when closing the window was
1288 * cancelled. */
1289 case K_F4:
1290 if (mod_mask == MOD_MASK_ALT)
1291 {
1292 redrawcmd(); /* somehow the cmdline is cleared */
1293 goto cmdline_not_changed;
1294 }
1295 break;
1296#endif
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298#ifdef FEAT_MOUSE
1299 case K_MIDDLEDRAG:
1300 case K_MIDDLERELEASE:
1301 goto cmdline_not_changed; /* Ignore mouse */
1302
1303 case K_MIDDLEMOUSE:
1304# ifdef FEAT_GUI
1305 /* When GUI is active, also paste when 'mouse' is empty */
1306 if (!gui.in_use)
1307# endif
1308 if (!mouse_has(MOUSE_COMMAND))
1309 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001310# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001312 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001314# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001315 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 redrawcmd();
1317 goto cmdline_changed;
1318
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001319# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001321 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 redrawcmd();
1323 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326 case K_LEFTDRAG:
1327 case K_LEFTRELEASE:
1328 case K_RIGHTDRAG:
1329 case K_RIGHTRELEASE:
1330 /* Ignore drag and release events when the button-down wasn't
1331 * seen before. */
1332 if (ignore_drag_release)
1333 goto cmdline_not_changed;
1334 /* FALLTHROUGH */
1335 case K_LEFTMOUSE:
1336 case K_RIGHTMOUSE:
1337 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1338 ignore_drag_release = TRUE;
1339 else
1340 ignore_drag_release = FALSE;
1341# ifdef FEAT_GUI
1342 /* When GUI is active, also move when 'mouse' is empty */
1343 if (!gui.in_use)
1344# endif
1345 if (!mouse_has(MOUSE_COMMAND))
1346 goto cmdline_not_changed; /* Ignore mouse */
1347# ifdef FEAT_CLIPBOARD
1348 if (mouse_row < cmdline_row && clip_star.available)
1349 {
1350 int button, is_click, is_drag;
1351
1352 /*
1353 * Handle modeless selection.
1354 */
1355 button = get_mouse_button(KEY2TERMCAP1(c),
1356 &is_click, &is_drag);
1357 if (mouse_model_popup() && button == MOUSE_LEFT
1358 && (mod_mask & MOD_MASK_SHIFT))
1359 {
1360 /* Translate shift-left to right button. */
1361 button = MOUSE_RIGHT;
1362 mod_mask &= ~MOD_MASK_SHIFT;
1363 }
1364 clip_modeless(button, is_click, is_drag);
1365 goto cmdline_not_changed;
1366 }
1367# endif
1368
1369 set_cmdspos();
1370 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1371 ++ccline.cmdpos)
1372 {
1373 i = cmdline_charsize(ccline.cmdpos);
1374 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1375 && mouse_col < ccline.cmdspos % Columns + i)
1376 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001377# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (has_mbyte)
1379 {
1380 /* Count ">" for double-wide char that doesn't fit. */
1381 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001382 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 + ccline.cmdpos) - 1;
1384 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 ccline.cmdspos += i;
1387 }
1388 goto cmdline_not_changed;
1389
1390 /* Mouse scroll wheel: ignored here */
1391 case K_MOUSEDOWN:
1392 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001393 case K_MOUSELEFT:
1394 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 /* Alternate buttons ignored here */
1396 case K_X1MOUSE:
1397 case K_X1DRAG:
1398 case K_X1RELEASE:
1399 case K_X2MOUSE:
1400 case K_X2DRAG:
1401 case K_X2RELEASE:
1402 goto cmdline_not_changed;
1403
1404#endif /* FEAT_MOUSE */
1405
1406#ifdef FEAT_GUI
1407 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1408 case K_LEFTRELEASE_NM:
1409 goto cmdline_not_changed;
1410
1411 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001412 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
1414 gui_do_scroll();
1415 redrawcmd();
1416 }
1417 goto cmdline_not_changed;
1418
1419 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001420 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001422 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 redrawcmd();
1424 }
1425 goto cmdline_not_changed;
1426#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001427#ifdef FEAT_GUI_TABLINE
1428 case K_TABLINE:
1429 case K_TABMENU:
1430 /* Don't want to change any tabs here. Make sure the same tab
1431 * is still selected. */
1432 if (gui_use_tabline())
1433 gui_mch_set_curtab(tabpage_index(curtab));
1434 goto cmdline_not_changed;
1435#endif
1436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 case K_SELECT: /* end of Select mode mapping - ignore */
1438 goto cmdline_not_changed;
1439
1440 case Ctrl_B: /* begin of command line */
1441 case K_HOME:
1442 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 case K_S_HOME:
1444 case K_C_HOME:
1445 ccline.cmdpos = 0;
1446 set_cmdspos();
1447 goto cmdline_not_changed;
1448
1449 case Ctrl_E: /* end of command line */
1450 case K_END:
1451 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 case K_S_END:
1453 case K_C_END:
1454 ccline.cmdpos = ccline.cmdlen;
1455 set_cmdspos_cursor();
1456 goto cmdline_not_changed;
1457
1458 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001459 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 break;
1461 goto cmdline_changed;
1462
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001463 case Ctrl_L:
1464#ifdef FEAT_SEARCH_EXTRA
1465 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1466 {
1467 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001468 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001469 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001470 curwin->w_cursor = match_end;
1471 if (!equalpos(curwin->w_cursor, old_cursor))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001472 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001473 c = gchar_cursor();
1474 /* If 'ignorecase' and 'smartcase' are set and the
1475 * command line has no uppercase characters, convert
1476 * the character to lowercase */
1477 if (p_ic && p_scs
1478 && !pat_has_uppercase(ccline.cmdbuff))
1479 c = MB_TOLOWER(c);
1480 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001481 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001482 if (c == firstc || vim_strchr((char_u *)(
1483 p_magic ? "\\^$.*[" : "\\^$"), c)
1484 != NULL)
1485 {
1486 /* put a backslash before special
1487 * characters */
1488 stuffcharReadbuff(c);
1489 c = '\\';
1490 }
1491 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001492 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001493 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001494 }
1495 goto cmdline_not_changed;
1496 }
1497#endif
1498
1499 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001500 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 break;
1502 goto cmdline_changed;
1503
1504 case Ctrl_N: /* next match */
1505 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001506 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001508 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1509 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001511 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001513 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
1515#ifdef FEAT_CMDHIST
1516 case K_UP:
1517 case K_DOWN:
1518 case K_S_UP:
1519 case K_S_DOWN:
1520 case K_PAGEUP:
1521 case K_KPAGEUP:
1522 case K_PAGEDOWN:
1523 case K_KPAGEDOWN:
1524 if (hislen == 0 || firstc == NUL) /* no history */
1525 goto cmdline_not_changed;
1526
1527 i = hiscnt;
1528
1529 /* save current command string so it can be restored later */
1530 if (lookfor == NULL)
1531 {
1532 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1533 goto cmdline_not_changed;
1534 lookfor[ccline.cmdpos] = NUL;
1535 }
1536
1537 j = (int)STRLEN(lookfor);
1538 for (;;)
1539 {
1540 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001541 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001542 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 if (hiscnt == hislen) /* first time */
1545 hiscnt = hisidx[histype];
1546 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1547 hiscnt = hislen - 1;
1548 else if (hiscnt != hisidx[histype] + 1)
1549 --hiscnt;
1550 else /* at top of list */
1551 {
1552 hiscnt = i;
1553 break;
1554 }
1555 }
1556 else /* one step forwards */
1557 {
1558 /* on last entry, clear the line */
1559 if (hiscnt == hisidx[histype])
1560 {
1561 hiscnt = hislen;
1562 break;
1563 }
1564
1565 /* not on a history line, nothing to do */
1566 if (hiscnt == hislen)
1567 break;
1568 if (hiscnt == hislen - 1) /* wrap around */
1569 hiscnt = 0;
1570 else
1571 ++hiscnt;
1572 }
1573 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1574 {
1575 hiscnt = i;
1576 break;
1577 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001578 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001579 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 || STRNCMP(history[histype][hiscnt].hisstr,
1581 lookfor, (size_t)j) == 0)
1582 break;
1583 }
1584
1585 if (hiscnt != i) /* jumped to other entry */
1586 {
1587 char_u *p;
1588 int len;
1589 int old_firstc;
1590
1591 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001592 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 if (hiscnt == hislen)
1594 p = lookfor; /* back to the old one */
1595 else
1596 p = history[histype][hiscnt].hisstr;
1597
1598 if (histype == HIST_SEARCH
1599 && p != lookfor
1600 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1601 {
1602 /* Correct for the separator character used when
1603 * adding the history entry vs the one used now.
1604 * First loop: count length.
1605 * Second loop: copy the characters. */
1606 for (i = 0; i <= 1; ++i)
1607 {
1608 len = 0;
1609 for (j = 0; p[j] != NUL; ++j)
1610 {
1611 /* Replace old sep with new sep, unless it is
1612 * escaped. */
1613 if (p[j] == old_firstc
1614 && (j == 0 || p[j - 1] != '\\'))
1615 {
1616 if (i > 0)
1617 ccline.cmdbuff[len] = firstc;
1618 }
1619 else
1620 {
1621 /* Escape new sep, unless it is already
1622 * escaped. */
1623 if (p[j] == firstc
1624 && (j == 0 || p[j - 1] != '\\'))
1625 {
1626 if (i > 0)
1627 ccline.cmdbuff[len] = '\\';
1628 ++len;
1629 }
1630 if (i > 0)
1631 ccline.cmdbuff[len] = p[j];
1632 }
1633 ++len;
1634 }
1635 if (i == 0)
1636 {
1637 alloc_cmdbuff(len);
1638 if (ccline.cmdbuff == NULL)
1639 goto returncmd;
1640 }
1641 }
1642 ccline.cmdbuff[len] = NUL;
1643 }
1644 else
1645 {
1646 alloc_cmdbuff((int)STRLEN(p));
1647 if (ccline.cmdbuff == NULL)
1648 goto returncmd;
1649 STRCPY(ccline.cmdbuff, p);
1650 }
1651
1652 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1653 redrawcmd();
1654 goto cmdline_changed;
1655 }
1656 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001657#endif
1658 goto cmdline_not_changed;
1659
1660 case Ctrl_G: /* next match */
1661 case Ctrl_T: /* previous match */
1662#ifdef FEAT_SEARCH_EXTRA
1663 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1664 {
1665 pos_T t;
1666 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1667 + SEARCH_PEEK;
1668
1669 if (char_avail())
1670 continue;
1671 cursor_off();
1672 out_flush();
1673 if (c == Ctrl_G)
1674 {
1675 t = match_end;
1676 search_flags += SEARCH_COL;
1677 }
1678 else
1679 t = match_start;
1680 ++emsg_off;
1681 i = searchit(curwin, curbuf, &t,
1682 c == Ctrl_G ? FORWARD : BACKWARD,
1683 ccline.cmdbuff, count, search_flags,
1684 RE_SEARCH, 0, NULL);
1685 --emsg_off;
1686 if (i)
1687 {
1688 old_cursor = match_start;
1689 match_end = t;
1690 match_start = t;
1691 if (c == Ctrl_T && firstc == '/')
1692 {
1693 /* move just before the current match, so that
1694 * when nv_search finishes the cursor will be
1695 * put back on the match */
1696 old_cursor = t;
1697 (void)decl(&old_cursor);
1698 }
1699 if (lt(t, old_cursor) && c == Ctrl_G)
1700 {
1701 /* wrap around */
1702 old_cursor = t;
1703 if (firstc == '?')
1704 (void)incl(&old_cursor);
1705 else
1706 (void)decl(&old_cursor);
1707 }
1708
1709 set_search_match(&match_end);
1710 curwin->w_cursor = match_start;
1711 changed_cline_bef_curs();
1712 update_topline();
1713 validate_cursor();
1714 highlight_match = TRUE;
1715 old_curswant = curwin->w_curswant;
1716 old_leftcol = curwin->w_leftcol;
1717 old_topline = curwin->w_topline;
1718# ifdef FEAT_DIFF
1719 old_topfill = curwin->w_topfill;
1720# endif
1721 old_botline = curwin->w_botline;
1722 update_screen(NOT_VALID);
1723 redrawcmdline();
1724 }
1725 else
1726 vim_beep(BO_ERROR);
1727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 goto cmdline_not_changed;
1729#endif
1730
1731 case Ctrl_V:
1732 case Ctrl_Q:
1733#ifdef FEAT_MOUSE
1734 ignore_drag_release = TRUE;
1735#endif
1736 putcmdline('^', TRUE);
1737 c = get_literal(); /* get next (two) character(s) */
1738 do_abbr = FALSE; /* don't do abbreviation now */
1739#ifdef FEAT_MBYTE
1740 /* may need to remove ^ when composing char was typed */
1741 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1742 {
1743 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1744 msg_putchar(' ');
1745 cursorcmd();
1746 }
1747#endif
1748 break;
1749
1750#ifdef FEAT_DIGRAPHS
1751 case Ctrl_K:
1752#ifdef FEAT_MOUSE
1753 ignore_drag_release = TRUE;
1754#endif
1755 putcmdline('?', TRUE);
1756#ifdef USE_ON_FLY_SCROLL
1757 dont_scroll = TRUE; /* disallow scrolling here */
1758#endif
1759 c = get_digraph(TRUE);
1760 if (c != NUL)
1761 break;
1762
1763 redrawcmd();
1764 goto cmdline_not_changed;
1765#endif /* FEAT_DIGRAPHS */
1766
1767#ifdef FEAT_RIGHTLEFT
1768 case Ctrl__: /* CTRL-_: switch language mode */
1769 if (!p_ari)
1770 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001771# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 if (p_altkeymap)
1773 {
1774 cmd_fkmap = !cmd_fkmap;
1775 if (cmd_fkmap) /* in Farsi always in Insert mode */
1776 ccline.overstrike = FALSE;
1777 }
1778 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 cmd_hkmap = !cmd_hkmap;
1781 goto cmdline_not_changed;
1782#endif
1783
1784 default:
1785#ifdef UNIX
1786 if (c == intr_char)
1787 {
1788 gotesc = TRUE; /* will free ccline.cmdbuff after
1789 putting it in history */
1790 goto returncmd; /* back to Normal mode */
1791 }
1792#endif
1793 /*
1794 * Normal character with no special meaning. Just set mod_mask
1795 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1796 * the string <S-Space>. This should only happen after ^V.
1797 */
1798 if (!IS_SPECIAL(c))
1799 mod_mask = 0x0;
1800 break;
1801 }
1802 /*
1803 * End of switch on command line character.
1804 * We come here if we have a normal character.
1805 */
1806
Bram Moolenaarede3e632013-06-23 16:16:19 +02001807 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#ifdef FEAT_MBYTE
1809 /* Add ABBR_OFF for characters above 0x100, this is
1810 * what check_abbr() expects. */
1811 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1812#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001813 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 goto cmdline_changed;
1815
1816 /*
1817 * put the character in the command line
1818 */
1819 if (IS_SPECIAL(c) || mod_mask != 0)
1820 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1821 else
1822 {
1823#ifdef FEAT_MBYTE
1824 if (has_mbyte)
1825 {
1826 j = (*mb_char2bytes)(c, IObuff);
1827 IObuff[j] = NUL; /* exclude composing chars */
1828 put_on_cmdline(IObuff, j, TRUE);
1829 }
1830 else
1831#endif
1832 {
1833 IObuff[0] = c;
1834 put_on_cmdline(IObuff, 1, TRUE);
1835 }
1836 }
1837 goto cmdline_changed;
1838
1839/*
1840 * This part implements incremental searches for "/" and "?"
1841 * Jump to cmdline_not_changed when a character has been read but the command
1842 * line did not change. Then we only search and redraw if something changed in
1843 * the past.
1844 * Jump to cmdline_changed when the command line did change.
1845 * (Sorry for the goto's, I know it is ugly).
1846 */
1847cmdline_not_changed:
1848#ifdef FEAT_SEARCH_EXTRA
1849 if (!incsearch_postponed)
1850 continue;
1851#endif
1852
1853cmdline_changed:
1854#ifdef FEAT_SEARCH_EXTRA
1855 /*
1856 * 'incsearch' highlighting.
1857 */
1858 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1859 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001860 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001861#ifdef FEAT_RELTIME
1862 proftime_T tm;
1863#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 /* if there is a character waiting, search and redraw later */
1866 if (char_avail())
1867 {
1868 incsearch_postponed = TRUE;
1869 continue;
1870 }
1871 incsearch_postponed = FALSE;
1872 curwin->w_cursor = old_cursor; /* start at old position */
1873
1874 /* If there is no command line, don't do anything */
1875 if (ccline.cmdlen == 0)
1876 i = 0;
1877 else
1878 {
1879 cursor_off(); /* so the user knows we're busy */
1880 out_flush();
1881 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001882#ifdef FEAT_RELTIME
1883 /* Set the time limit to half a second. */
1884 profile_setlimit(500L, &tm);
1885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001887 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1888#ifdef FEAT_RELTIME
1889 &tm
1890#else
1891 NULL
1892#endif
1893 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 --emsg_off;
1895 /* if interrupted while searching, behave like it failed */
1896 if (got_int)
1897 {
1898 (void)vpeekc(); /* remove <C-C> from input stream */
1899 got_int = FALSE; /* don't abandon the command line */
1900 i = 0;
1901 }
1902 else if (char_avail())
1903 /* cancelled searching because a char was typed */
1904 incsearch_postponed = TRUE;
1905 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001906 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 highlight_match = TRUE; /* highlight position */
1908 else
1909 highlight_match = FALSE; /* remove highlight */
1910
1911 /* first restore the old curwin values, so the screen is
1912 * positioned in the same way as the actual search command */
1913 curwin->w_leftcol = old_leftcol;
1914 curwin->w_topline = old_topline;
1915# ifdef FEAT_DIFF
1916 curwin->w_topfill = old_topfill;
1917# endif
1918 curwin->w_botline = old_botline;
1919 changed_cline_bef_curs();
1920 update_topline();
1921
1922 if (i != 0)
1923 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001924 pos_T save_pos = curwin->w_cursor;
1925
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001926 match_start = curwin->w_cursor;
1927 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001929 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001930 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001931 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001933 else
1934 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001937# ifdef FEAT_WINDOWS
1938 /* May redraw the status line to show the cursor position. */
1939 if (p_ru && curwin->w_status_height > 0)
1940 curwin->w_redr_status = TRUE;
1941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001943 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001944 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001945 restore_cmdline(&save_ccline);
1946
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001947 /* Leave it at the end to make CTRL-R CTRL-W work. */
1948 if (i != 0)
1949 curwin->w_cursor = end_pos;
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 msg_starthere();
1952 redrawcmdline();
1953 did_incsearch = TRUE;
1954 }
1955#else /* FEAT_SEARCH_EXTRA */
1956 ;
1957#endif
1958
1959#ifdef FEAT_RIGHTLEFT
1960 if (cmdmsg_rl
1961# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001962 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963# endif
1964 )
1965 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001966 * right-left typing. Not efficient, but it works.
1967 * Do it only when there are no characters left to read
1968 * to avoid useless intermediate redraws. */
1969 if (vpeekc() == NUL)
1970 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971#endif
1972 }
1973
1974returncmd:
1975
1976#ifdef FEAT_RIGHTLEFT
1977 cmdmsg_rl = FALSE;
1978#endif
1979
1980#ifdef FEAT_FKMAP
1981 cmd_fkmap = 0;
1982#endif
1983
1984 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001985 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987#ifdef FEAT_SEARCH_EXTRA
1988 if (did_incsearch)
1989 {
1990 curwin->w_cursor = old_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001991 if (gotesc)
1992 curwin->w_cursor = cursor_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 curwin->w_curswant = old_curswant;
1994 curwin->w_leftcol = old_leftcol;
1995 curwin->w_topline = old_topline;
1996# ifdef FEAT_DIFF
1997 curwin->w_topfill = old_topfill;
1998# endif
1999 curwin->w_botline = old_botline;
2000 highlight_match = FALSE;
2001 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002002 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004#endif
2005
2006 if (ccline.cmdbuff != NULL)
2007 {
2008 /*
2009 * Put line in history buffer (":" and "=" only when it was typed).
2010 */
2011#ifdef FEAT_CMDHIST
2012 if (ccline.cmdlen && firstc != NUL
2013 && (some_key_typed || histype == HIST_SEARCH))
2014 {
2015 add_to_history(histype, ccline.cmdbuff, TRUE,
2016 histype == HIST_SEARCH ? firstc : NUL);
2017 if (firstc == ':')
2018 {
2019 vim_free(new_last_cmdline);
2020 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2021 }
2022 }
2023#endif
2024
2025 if (gotesc) /* abandon command line */
2026 {
2027 vim_free(ccline.cmdbuff);
2028 ccline.cmdbuff = NULL;
2029 if (msg_scrolled == 0)
2030 compute_cmdrow();
2031 MSG("");
2032 redraw_cmdline = TRUE;
2033 }
2034 }
2035
2036 /*
2037 * If the screen was shifted up, redraw the whole screen (later).
2038 * If the line is too long, clear it, so ruler and shown command do
2039 * not get printed in the middle of it.
2040 */
2041 msg_check();
2042 msg_scroll = save_msg_scroll;
2043 redir_off = FALSE;
2044
2045 /* When the command line was typed, no need for a wait-return prompt. */
2046 if (some_key_typed)
2047 need_wait_return = FALSE;
2048
2049 State = save_State;
2050#ifdef USE_IM_CONTROL
2051 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2052 im_save_status(b_im_ptr);
2053 im_set_active(FALSE);
2054#endif
2055#ifdef FEAT_MOUSE
2056 setmouse();
2057#endif
2058#ifdef CURSOR_SHAPE
2059 ui_cursor_shape(); /* may show different cursor shape */
2060#endif
2061
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002062 {
2063 char_u *p = ccline.cmdbuff;
2064
2065 /* Make ccline empty, getcmdline() may try to use it. */
2066 ccline.cmdbuff = NULL;
2067 return p;
2068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069}
2070
2071#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2072/*
2073 * Get a command line with a prompt.
2074 * This is prepared to be called recursively from getcmdline() (e.g. by
2075 * f_input() when evaluating an expression from CTRL-R =).
2076 * Returns the command line in allocated memory, or NULL.
2077 */
2078 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002079getcmdline_prompt(
2080 int firstc,
2081 char_u *prompt, /* command line prompt */
2082 int attr, /* attributes for prompt */
2083 int xp_context, /* type of expansion */
2084 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085{
2086 char_u *s;
2087 struct cmdline_info save_ccline;
2088 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002089 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002091 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 ccline.cmdprompt = prompt;
2093 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002094# ifdef FEAT_EVAL
2095 ccline.xp_context = xp_context;
2096 ccline.xp_arg = xp_arg;
2097 ccline.input_fn = (firstc == '@');
2098# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002099 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002101 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002102 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002103 /* Restore msg_col, the prompt from input() may have changed it.
2104 * But only if called recursively and the commandline is therefore being
2105 * restored to an old one; if not, the input() prompt stays on the screen,
2106 * so we need its modified msg_col left intact. */
2107 if (ccline.cmdbuff != NULL)
2108 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
2110 return s;
2111}
2112#endif
2113
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002114/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002115 * Return TRUE when the text must not be changed and we can't switch to
2116 * another window or buffer. Used when editing the command line, evaluating
2117 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002118 */
2119 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002120text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002121{
2122#ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0)
2124 return TRUE;
2125#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002126 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002127}
2128
2129/*
2130 * Give an error message for a command that isn't allowed while the cmdline
2131 * window is open or editing the cmdline in another way.
2132 */
2133 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002134text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002135{
2136#ifdef FEAT_CMDWIN
2137 if (cmdwin_type != 0)
2138 EMSG(_(e_cmdwin));
2139 else
2140#endif
2141 EMSG(_(e_secure));
2142}
2143
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144#if defined(FEAT_AUTOCMD) || defined(PROTO)
2145/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002146 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2147 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002148 */
2149 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002150curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002151{
2152 if (curbuf_lock > 0)
2153 {
2154 EMSG(_("E788: Not allowed to edit another buffer now"));
2155 return TRUE;
2156 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002157 return allbuf_locked();
2158}
2159
2160/*
2161 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2162 * message.
2163 */
2164 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002165allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002166{
2167 if (allbuf_lock > 0)
2168 {
2169 EMSG(_("E811: Not allowed to change buffer information now"));
2170 return TRUE;
2171 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002172 return FALSE;
2173}
2174#endif
2175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002177cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2180 if (cmdline_star > 0) /* showing '*', always 1 position */
2181 return 1;
2182#endif
2183 return ptr2cells(ccline.cmdbuff + idx);
2184}
2185
2186/*
2187 * Compute the offset of the cursor on the command line for the prompt and
2188 * indent.
2189 */
2190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002191set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002193 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 ccline.cmdspos = 1 + ccline.cmdindent;
2195 else
2196 ccline.cmdspos = 0 + ccline.cmdindent;
2197}
2198
2199/*
2200 * Compute the screen position for the cursor on the command line.
2201 */
2202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002203set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 int i, m, c;
2206
2207 set_cmdspos();
2208 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002211 if (m < 0) /* overflow, Columns or Rows at weird value */
2212 m = MAXCOL;
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else
2215 m = MAXCOL;
2216 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2217 {
2218 c = cmdline_charsize(i);
2219#ifdef FEAT_MBYTE
2220 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2221 if (has_mbyte)
2222 correct_cmdspos(i, c);
2223#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002224 /* If the cmdline doesn't fit, show cursor on last visible char.
2225 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 if ((ccline.cmdspos += c) >= m)
2227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ccline.cmdspos -= c;
2229 break;
2230 }
2231#ifdef FEAT_MBYTE
2232 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002233 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#endif
2235 }
2236}
2237
2238#ifdef FEAT_MBYTE
2239/*
2240 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2241 * character that doesn't fit, so that a ">" must be displayed.
2242 */
2243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002244correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002246 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2248 && ccline.cmdspos % Columns + cells > Columns)
2249 ccline.cmdspos++;
2250}
2251#endif
2252
2253/*
2254 * Get an Ex command line for the ":" command.
2255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002257getexline(
2258 int c, /* normally ':', NUL for ":append" */
2259 void *cookie UNUSED,
2260 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
2262 /* When executing a register, remove ':' that's in front of each line. */
2263 if (exec_from_reg && vpeekc() == ':')
2264 (void)vgetc();
2265 return getcmdline(c, 1L, indent);
2266}
2267
2268/*
2269 * Get an Ex command line for Ex mode.
2270 * In Ex mode we only use the OS supplied line editing features and no
2271 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002272 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002275getexmodeline(
2276 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002277 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002278 void *cookie UNUSED,
2279 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002281 garray_T line_ga;
2282 char_u *pend;
2283 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002284 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285 int escaped = FALSE; /* CTRL-V typed */
2286 int vcol = 0;
2287 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002288 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 /* Switch cursor on now. This avoids that it happens after the "\n", which
2292 * confuses the system function that computes tabstops. */
2293 cursor_on();
2294
2295 /* always start in column 0; write a newline if necessary */
2296 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002301 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 if (p_prompt)
2303 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 while (indent-- > 0)
2305 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308
2309 ga_init2(&line_ga, 1, 30);
2310
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002311 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002312 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002313 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002314 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002315 while (indent >= 8)
2316 {
2317 ga_append(&line_ga, TAB);
2318 msg_puts((char_u *)" ");
2319 indent -= 8;
2320 }
2321 while (indent-- > 0)
2322 {
2323 ga_append(&line_ga, ' ');
2324 msg_putchar(' ');
2325 }
2326 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002327 ++no_mapping;
2328 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 /*
2331 * Get the line, one character at a time.
2332 */
2333 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002334 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002336 long sw;
2337 char_u *s;
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (ga_grow(&line_ga, 40) == FAIL)
2340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 /* Get one character at a time. Don't use inchar(), it can't handle
2343 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002344 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002345 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002348 * Handle line editing.
2349 * Previously this was left to the system, putting the terminal in
2350 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002352 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002354 msg_putchar('\n');
2355 break;
2356 }
2357
2358 if (!escaped)
2359 {
2360 /* CR typed means "enter", which is NL */
2361 if (c1 == '\r')
2362 c1 = '\n';
2363
2364 if (c1 == BS || c1 == K_BS
2365 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002367 if (line_ga.ga_len > 0)
2368 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002369#ifdef FEAT_MBYTE
2370 if (has_mbyte)
2371 {
2372 p = (char_u *)line_ga.ga_data;
2373 p[line_ga.ga_len] = NUL;
2374 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2375 line_ga.ga_len -= len;
2376 }
2377 else
2378#endif
2379 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002380 goto redraw;
2381 }
2382 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
2384
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002385 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002387 msg_col = startcol;
2388 msg_clr_eos();
2389 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002390 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002391 }
2392
2393 if (c1 == Ctrl_T)
2394 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002395 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002396 p = (char_u *)line_ga.ga_data;
2397 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002398 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002399 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002400add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002401 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002403 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002404 p = (char_u *)line_ga.ga_data;
2405 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002406 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2407 *s = ' ';
2408 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002410redraw:
2411 /* redraw the line */
2412 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002413 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002414 p = (char_u *)line_ga.ga_data;
2415 p[line_ga.ga_len] = NUL;
2416 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002418 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002420 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002422 msg_putchar(' ');
2423 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002424 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002428 len = MB_PTR2LEN(p);
2429 msg_outtrans_len(p, len);
2430 vcol += ptr2cells(p);
2431 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002434 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002435 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002436 continue;
2437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002439 if (c1 == Ctrl_D)
2440 {
2441 /* Delete one shiftwidth. */
2442 p = (char_u *)line_ga.ga_data;
2443 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002445 if (prev_char == '^')
2446 ex_keep_indent = TRUE;
2447 indent = 0;
2448 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450 else
2451 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002452 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002453 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002454 if (indent > 0)
2455 {
2456 --indent;
2457 indent -= indent % get_sw_value(curbuf);
2458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002460 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002461 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002462 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002463 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2464 --line_ga.ga_len;
2465 }
2466 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002468
2469 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2470 {
2471 escaped = TRUE;
2472 continue;
2473 }
2474
2475 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2476 if (IS_SPECIAL(c1))
2477 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002479
2480 if (IS_SPECIAL(c1))
2481 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002482#ifdef FEAT_MBYTE
2483 if (has_mbyte)
2484 len = (*mb_char2bytes)(c1,
2485 (char_u *)line_ga.ga_data + line_ga.ga_len);
2486 else
2487#endif
2488 {
2489 len = 1;
2490 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2491 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002492 if (c1 == '\n')
2493 msg_putchar('\n');
2494 else if (c1 == TAB)
2495 {
2496 /* Don't use chartabsize(), 'ts' can be different */
2497 do
2498 {
2499 msg_putchar(' ');
2500 } while (++vcol % 8);
2501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002504 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002505 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002506 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002508 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002509 escaped = FALSE;
2510
2511 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002512 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002513
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002514 /* We are done when a NL is entered, but not when it comes after an
2515 * odd number of backslashes, that results in a NUL. */
2516 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002518 int bcount = 0;
2519
2520 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2521 ++bcount;
2522
2523 if (bcount > 0)
2524 {
2525 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2526 * "\NL", etc. */
2527 line_ga.ga_len -= (bcount + 1) / 2;
2528 pend -= (bcount + 1) / 2;
2529 pend[-1] = '\n';
2530 }
2531
2532 if ((bcount & 1) == 0)
2533 {
2534 --line_ga.ga_len;
2535 --pend;
2536 *pend = NUL;
2537 break;
2538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540 }
2541
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002542 --no_mapping;
2543 --allow_keys;
2544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 /* make following messages go to the next line */
2546 msg_didout = FALSE;
2547 msg_col = 0;
2548 if (msg_row < Rows - 1)
2549 ++msg_row;
2550 emsg_on_display = FALSE; /* don't want ui_delay() */
2551
2552 if (got_int)
2553 ga_clear(&line_ga);
2554
2555 return (char_u *)line_ga.ga_data;
2556}
2557
Bram Moolenaare344bea2005-09-01 20:46:49 +00002558# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2559 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560/*
2561 * Return TRUE if ccline.overstrike is on.
2562 */
2563 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002564cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565{
2566 return ccline.overstrike;
2567}
2568
2569/*
2570 * Return TRUE if the cursor is at the end of the cmdline.
2571 */
2572 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002573cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 return (ccline.cmdpos >= ccline.cmdlen);
2576}
2577#endif
2578
Bram Moolenaar9372a112005-12-06 19:59:18 +00002579#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580/*
2581 * Return the virtual column number at the current cursor position.
2582 * This is used by the IM code to obtain the start of the preedit string.
2583 */
2584 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002585cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2588 return MAXCOL;
2589
2590# ifdef FEAT_MBYTE
2591 if (has_mbyte)
2592 {
2593 colnr_T col;
2594 int i = 0;
2595
2596 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002597 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 return col;
2600 }
2601 else
2602# endif
2603 return ccline.cmdpos;
2604}
2605#endif
2606
2607#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2608/*
2609 * If part of the command line is an IM preedit string, redraw it with
2610 * IM feedback attributes. The cursor position is restored after drawing.
2611 */
2612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002613redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 if ((State & CMDLINE)
2616 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002617 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 && !p_imdisable
2619 && im_is_preediting())
2620 {
2621 int cmdpos = 0;
2622 int cmdspos;
2623 int old_row;
2624 int old_col;
2625 colnr_T col;
2626
2627 old_row = msg_row;
2628 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002629 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
2631# ifdef FEAT_MBYTE
2632 if (has_mbyte)
2633 {
2634 for (col = 0; col < preedit_start_col
2635 && cmdpos < ccline.cmdlen; ++col)
2636 {
2637 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002638 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 }
2641 else
2642# endif
2643 {
2644 cmdspos += preedit_start_col;
2645 cmdpos += preedit_start_col;
2646 }
2647
2648 msg_row = cmdline_row + (cmdspos / (int)Columns);
2649 msg_col = cmdspos % (int)Columns;
2650 if (msg_row >= Rows)
2651 msg_row = Rows - 1;
2652
2653 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2654 {
2655 int char_len;
2656 int char_attr;
2657
2658 char_attr = im_get_feedback_attr(col);
2659 if (char_attr < 0)
2660 break; /* end of preedit string */
2661
2662# ifdef FEAT_MBYTE
2663 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002664 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 else
2666# endif
2667 char_len = 1;
2668
2669 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2670 cmdpos += char_len;
2671 }
2672
2673 msg_row = old_row;
2674 msg_col = old_col;
2675 }
2676}
2677#endif /* FEAT_XIM && FEAT_GUI_GTK */
2678
2679/*
2680 * Allocate a new command line buffer.
2681 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2682 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2683 */
2684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002685alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 /*
2688 * give some extra space to avoid having to allocate all the time
2689 */
2690 if (len < 80)
2691 len = 100;
2692 else
2693 len += 20;
2694
2695 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2696 ccline.cmdbufflen = len;
2697}
2698
2699/*
2700 * Re-allocate the command line to length len + something extra.
2701 * return FAIL for failure, OK otherwise
2702 */
2703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002704realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
2706 char_u *p;
2707
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002708 if (len < ccline.cmdbufflen)
2709 return OK; /* no need to resize */
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 p = ccline.cmdbuff;
2712 alloc_cmdbuff(len); /* will get some more */
2713 if (ccline.cmdbuff == NULL) /* out of memory */
2714 {
2715 ccline.cmdbuff = p; /* keep the old one */
2716 return FAIL;
2717 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002718 /* There isn't always a NUL after the command, but it may need to be
2719 * there, thus copy up to the NUL and add a NUL. */
2720 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2721 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002723
2724 if (ccline.xpc != NULL
2725 && ccline.xpc->xp_pattern != NULL
2726 && ccline.xpc->xp_context != EXPAND_NOTHING
2727 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2728 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002729 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002730
2731 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2732 * to point into the newly allocated memory. */
2733 if (i >= 0 && i <= ccline.cmdlen)
2734 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2735 }
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 return OK;
2738}
2739
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002740#if defined(FEAT_ARABIC) || defined(PROTO)
2741static char_u *arshape_buf = NULL;
2742
2743# if defined(EXITFREE) || defined(PROTO)
2744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002745free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002746{
2747 vim_free(arshape_buf);
2748}
2749# endif
2750#endif
2751
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752/*
2753 * Draw part of the cmdline at the current cursor position. But draw stars
2754 * when cmdline_star is TRUE.
2755 */
2756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002757draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2760 int i;
2761
2762 if (cmdline_star > 0)
2763 for (i = 0; i < len; ++i)
2764 {
2765 msg_putchar('*');
2766# ifdef FEAT_MBYTE
2767 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002768 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769# endif
2770 }
2771 else
2772#endif
2773#ifdef FEAT_ARABIC
2774 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 static int buflen = 0;
2777 char_u *p;
2778 int j;
2779 int newlen = 0;
2780 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002781 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 int prev_c = 0;
2783 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002784 int u8c;
2785 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
2788 /*
2789 * Do arabic shaping into a temporary buffer. This is very
2790 * inefficient!
2791 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002792 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
2794 /* Re-allocate the buffer. We keep it around to avoid a lot of
2795 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002796 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002797 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002798 arshape_buf = alloc(buflen);
2799 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 return; /* out of memory */
2801 }
2802
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002803 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2804 {
2805 /* Prepend a space to draw the leading composing char on. */
2806 arshape_buf[0] = ' ';
2807 newlen = 1;
2808 }
2809
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 for (j = start; j < start + len; j += mb_l)
2811 {
2812 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002813 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002814 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (ARABIC_CHAR(u8c))
2816 {
2817 /* Do Arabic shaping. */
2818 if (cmdmsg_rl)
2819 {
2820 /* displaying from right to left */
2821 pc = prev_c;
2822 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002823 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 if (j + mb_l >= start + len)
2825 nc = NUL;
2826 else
2827 nc = utf_ptr2char(p + mb_l);
2828 }
2829 else
2830 {
2831 /* displaying from left to right */
2832 if (j + mb_l >= start + len)
2833 pc = NUL;
2834 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002835 {
2836 int pcc[MAX_MCO];
2837
2838 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002840 pc1 = pcc[0];
2841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 nc = prev_c;
2843 }
2844 prev_c = u8c;
2845
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002846 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002848 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002849 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002851 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2852 if (u8cc[1] != 0)
2853 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002854 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
2856 }
2857 else
2858 {
2859 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002860 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 newlen += mb_l;
2862 }
2863 }
2864
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002865 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 else
2868#endif
2869 msg_outtrans_len(ccline.cmdbuff + start, len);
2870}
2871
2872/*
2873 * Put a character on the command line. Shifts the following text to the
2874 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2875 * "c" must be printable (fit in one display cell)!
2876 */
2877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002878putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 if (cmd_silent)
2881 return;
2882 msg_no_more = TRUE;
2883 msg_putchar(c);
2884 if (shift)
2885 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2886 msg_no_more = FALSE;
2887 cursorcmd();
2888}
2889
2890/*
2891 * Undo a putcmdline(c, FALSE).
2892 */
2893 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002894unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 if (cmd_silent)
2897 return;
2898 msg_no_more = TRUE;
2899 if (ccline.cmdlen == ccline.cmdpos)
2900 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002901#ifdef FEAT_MBYTE
2902 else if (has_mbyte)
2903 draw_cmdline(ccline.cmdpos,
2904 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 else
2907 draw_cmdline(ccline.cmdpos, 1);
2908 msg_no_more = FALSE;
2909 cursorcmd();
2910}
2911
2912/*
2913 * Put the given string, of the given length, onto the command line.
2914 * If len is -1, then STRLEN() is used to calculate the length.
2915 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2916 * part will be redrawn, otherwise it will not. If this function is called
2917 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2918 * called afterwards.
2919 */
2920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002921put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 int retval;
2924 int i;
2925 int m;
2926 int c;
2927
2928 if (len < 0)
2929 len = (int)STRLEN(str);
2930
2931 /* Check if ccline.cmdbuff needs to be longer */
2932 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002933 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 else
2935 retval = OK;
2936 if (retval == OK)
2937 {
2938 if (!ccline.overstrike)
2939 {
2940 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2941 ccline.cmdbuff + ccline.cmdpos,
2942 (size_t)(ccline.cmdlen - ccline.cmdpos));
2943 ccline.cmdlen += len;
2944 }
2945 else
2946 {
2947#ifdef FEAT_MBYTE
2948 if (has_mbyte)
2949 {
2950 /* Count nr of characters in the new string. */
2951 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002952 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 ++m;
2954 /* Count nr of bytes in cmdline that are overwritten by these
2955 * characters. */
2956 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002957 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 --m;
2959 if (i < ccline.cmdlen)
2960 {
2961 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2962 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2963 ccline.cmdlen += ccline.cmdpos + len - i;
2964 }
2965 else
2966 ccline.cmdlen = ccline.cmdpos + len;
2967 }
2968 else
2969#endif
2970 if (ccline.cmdpos + len > ccline.cmdlen)
2971 ccline.cmdlen = ccline.cmdpos + len;
2972 }
2973 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2974 ccline.cmdbuff[ccline.cmdlen] = NUL;
2975
2976#ifdef FEAT_MBYTE
2977 if (enc_utf8)
2978 {
2979 /* When the inserted text starts with a composing character,
2980 * backup to the character before it. There could be two of them.
2981 */
2982 i = 0;
2983 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2984 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2985 {
2986 i = (*mb_head_off)(ccline.cmdbuff,
2987 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2988 ccline.cmdpos -= i;
2989 len += i;
2990 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2991 }
2992# ifdef FEAT_ARABIC
2993 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2994 {
2995 /* Check the previous character for Arabic combining pair. */
2996 i = (*mb_head_off)(ccline.cmdbuff,
2997 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2998 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2999 + ccline.cmdpos - i), c))
3000 {
3001 ccline.cmdpos -= i;
3002 len += i;
3003 }
3004 else
3005 i = 0;
3006 }
3007# endif
3008 if (i != 0)
3009 {
3010 /* Also backup the cursor position. */
3011 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3012 ccline.cmdspos -= i;
3013 msg_col -= i;
3014 if (msg_col < 0)
3015 {
3016 msg_col += Columns;
3017 --msg_row;
3018 }
3019 }
3020 }
3021#endif
3022
3023 if (redraw && !cmd_silent)
3024 {
3025 msg_no_more = TRUE;
3026 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003027 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3029 /* Avoid clearing the rest of the line too often. */
3030 if (cmdline_row != i || ccline.overstrike)
3031 msg_clr_eos();
3032 msg_no_more = FALSE;
3033 }
3034#ifdef FEAT_FKMAP
3035 /*
3036 * If we are in Farsi command mode, the character input must be in
3037 * Insert mode. So do not advance the cmdpos.
3038 */
3039 if (!cmd_fkmap)
3040#endif
3041 {
3042 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003045 if (m < 0) /* overflow, Columns or Rows at weird value */
3046 m = MAXCOL;
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 else
3049 m = MAXCOL;
3050 for (i = 0; i < len; ++i)
3051 {
3052 c = cmdline_charsize(ccline.cmdpos);
3053#ifdef FEAT_MBYTE
3054 /* count ">" for a double-wide char that doesn't fit. */
3055 if (has_mbyte)
3056 correct_cmdspos(ccline.cmdpos, c);
3057#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003058 /* Stop cursor at the end of the screen, but do increment the
3059 * insert position, so that entering a very long command
3060 * works, even though you can't see it. */
3061 if (ccline.cmdspos + c < m)
3062 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#ifdef FEAT_MBYTE
3064 if (has_mbyte)
3065 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003066 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (c > len - i - 1)
3068 c = len - i - 1;
3069 ccline.cmdpos += c;
3070 i += c;
3071 }
3072#endif
3073 ++ccline.cmdpos;
3074 }
3075 }
3076 }
3077 if (redraw)
3078 msg_check();
3079 return retval;
3080}
3081
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003082static struct cmdline_info prev_ccline;
3083static int prev_ccline_used = FALSE;
3084
3085/*
3086 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3087 * and overwrite it. But get_cmdline_str() may need it, thus make it
3088 * available globally in prev_ccline.
3089 */
3090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003092{
3093 if (!prev_ccline_used)
3094 {
3095 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3096 prev_ccline_used = TRUE;
3097 }
3098 *ccp = prev_ccline;
3099 prev_ccline = ccline;
3100 ccline.cmdbuff = NULL;
3101 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003102 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003103}
3104
3105/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003106 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003107 */
3108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003110{
3111 ccline = prev_ccline;
3112 prev_ccline = *ccp;
3113}
3114
Bram Moolenaar5a305422006-04-28 22:38:25 +00003115#if defined(FEAT_EVAL) || defined(PROTO)
3116/*
3117 * Save the command line into allocated memory. Returns a pointer to be
3118 * passed to restore_cmdline_alloc() later.
3119 * Returns NULL when failed.
3120 */
3121 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003122save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003123{
3124 struct cmdline_info *p;
3125
3126 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3127 if (p != NULL)
3128 save_cmdline(p);
3129 return (char_u *)p;
3130}
3131
3132/*
3133 * Restore the command line from the return value of save_cmdline_alloc().
3134 */
3135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003136restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003137{
3138 if (p != NULL)
3139 {
3140 restore_cmdline((struct cmdline_info *)p);
3141 vim_free(p);
3142 }
3143}
3144#endif
3145
Bram Moolenaar8299df92004-07-10 09:47:34 +00003146/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003147 * Paste a yank register into the command line.
3148 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003149 * insert_reg() can't be used here, because special characters from the
3150 * register contents will be interpreted as commands.
3151 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003152 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003153 */
3154 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003155cmdline_paste(
3156 int regname,
3157 int literally, /* Insert text literally instead of "as typed" */
3158 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003159{
3160 long i;
3161 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003162 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003163 int allocated;
3164 struct cmdline_info save_ccline;
3165
3166 /* check for valid regname; also accept special characters for CTRL-R in
3167 * the command line */
3168 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3169 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3170 return FAIL;
3171
3172 /* A register containing CTRL-R can cause an endless loop. Allow using
3173 * CTRL-C to break the loop. */
3174 line_breakcheck();
3175 if (got_int)
3176 return FAIL;
3177
3178#ifdef FEAT_CLIPBOARD
3179 regname = may_get_selection(regname);
3180#endif
3181
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003182 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003183 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003184 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003185 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003186 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003187 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003188 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003189
3190 if (i)
3191 {
3192 /* Got the value of a special register in "arg". */
3193 if (arg == NULL)
3194 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003195
3196 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3197 * part of the word. */
3198 p = arg;
3199 if (p_is && regname == Ctrl_W)
3200 {
3201 char_u *w;
3202 int len;
3203
3204 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003205 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003206 {
3207#ifdef FEAT_MBYTE
3208 if (has_mbyte)
3209 {
3210 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3211 if (!vim_iswordc(mb_ptr2char(w - len)))
3212 break;
3213 w -= len;
3214 }
3215 else
3216#endif
3217 {
3218 if (!vim_iswordc(w[-1]))
3219 break;
3220 --w;
3221 }
3222 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003223 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003224 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3225 p += len;
3226 }
3227
3228 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003229 if (allocated)
3230 vim_free(arg);
3231 return OK;
3232 }
3233
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003234 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003235}
3236
3237/*
3238 * Put a string on the command line.
3239 * When "literally" is TRUE, insert literally.
3240 * When "literally" is FALSE, insert as typed, but don't leave the command
3241 * line.
3242 */
3243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003244cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003245{
3246 int c, cv;
3247
3248 if (literally)
3249 put_on_cmdline(s, -1, TRUE);
3250 else
3251 while (*s != NUL)
3252 {
3253 cv = *s;
3254 if (cv == Ctrl_V && s[1])
3255 ++s;
3256#ifdef FEAT_MBYTE
3257 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003258 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003259 else
3260#endif
3261 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003262 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3263 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003264#ifdef UNIX
3265 || c == intr_char
3266#endif
3267 || (c == Ctrl_BSL && *s == Ctrl_N))
3268 stuffcharReadbuff(Ctrl_V);
3269 stuffcharReadbuff(c);
3270 }
3271}
3272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#ifdef FEAT_WILDMENU
3274/*
3275 * Delete characters on the command line, from "from" to the current
3276 * position.
3277 */
3278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003279cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3282 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3283 ccline.cmdlen -= ccline.cmdpos - from;
3284 ccline.cmdpos = from;
3285}
3286#endif
3287
3288/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003289 * This function is called when the screen size changes and with incremental
3290 * search and in other situations where the command line may have been
3291 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 */
3293 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003294redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
3296 if (cmd_silent)
3297 return;
3298 need_wait_return = FALSE;
3299 compute_cmdrow();
3300 redrawcmd();
3301 cursorcmd();
3302}
3303
3304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003305redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 int i;
3308
3309 if (cmd_silent)
3310 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003311 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 msg_putchar(ccline.cmdfirstc);
3313 if (ccline.cmdprompt != NULL)
3314 {
3315 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3316 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3317 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003318 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 --ccline.cmdindent;
3320 }
3321 else
3322 for (i = ccline.cmdindent; i > 0; --i)
3323 msg_putchar(' ');
3324}
3325
3326/*
3327 * Redraw what is currently on the command line.
3328 */
3329 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003330redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
3332 if (cmd_silent)
3333 return;
3334
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003335 /* when 'incsearch' is set there may be no command line while redrawing */
3336 if (ccline.cmdbuff == NULL)
3337 {
3338 windgoto(cmdline_row, 0);
3339 msg_clr_eos();
3340 return;
3341 }
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 msg_start();
3344 redrawcmdprompt();
3345
3346 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3347 msg_no_more = TRUE;
3348 draw_cmdline(0, ccline.cmdlen);
3349 msg_clr_eos();
3350 msg_no_more = FALSE;
3351
3352 set_cmdspos_cursor();
3353
3354 /*
3355 * An emsg() before may have set msg_scroll. This is used in normal mode,
3356 * in cmdline mode we can reset them now.
3357 */
3358 msg_scroll = FALSE; /* next message overwrites cmdline */
3359
3360 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3361 * in cmdline mode */
3362 skip_redraw = FALSE;
3363}
3364
3365 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003366compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003368 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 cmdline_row = Rows - 1;
3370 else
3371 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3372 + W_STATUS_HEIGHT(lastwin);
3373}
3374
3375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003376cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 if (cmd_silent)
3379 return;
3380
3381#ifdef FEAT_RIGHTLEFT
3382 if (cmdmsg_rl)
3383 {
3384 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3385 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3386 if (msg_row <= 0)
3387 msg_row = Rows - 1;
3388 }
3389 else
3390#endif
3391 {
3392 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3393 msg_col = ccline.cmdspos % (int)Columns;
3394 if (msg_row >= Rows)
3395 msg_row = Rows - 1;
3396 }
3397
3398 windgoto(msg_row, msg_col);
3399#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3400 redrawcmd_preedit();
3401#endif
3402#ifdef MCH_CURSOR_SHAPE
3403 mch_update_cursor();
3404#endif
3405}
3406
3407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003408gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
3410 msg_start();
3411#ifdef FEAT_RIGHTLEFT
3412 if (cmdmsg_rl)
3413 msg_col = Columns - 1;
3414 else
3415#endif
3416 msg_col = 0; /* always start in column 0 */
3417 if (clr) /* clear the bottom line(s) */
3418 msg_clr_eos(); /* will reset clear_cmdline */
3419 windgoto(cmdline_row, 0);
3420}
3421
3422/*
3423 * Check the word in front of the cursor for an abbreviation.
3424 * Called when the non-id character "c" has been entered.
3425 * When an abbreviation is recognized it is removed from the text with
3426 * backspaces and the replacement string is inserted, followed by "c".
3427 */
3428 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003429ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3432 return FALSE;
3433
3434 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3435}
3436
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003437#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3438 static int
3439#ifdef __BORLANDC__
3440_RTLENTRYF
3441#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003442sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003443{
3444 char_u *p1 = *(char_u **)s1;
3445 char_u *p2 = *(char_u **)s2;
3446
3447 if (*p1 != '<' && *p2 == '<') return -1;
3448 if (*p1 == '<' && *p2 != '<') return 1;
3449 return STRCMP(p1, p2);
3450}
3451#endif
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453/*
3454 * Return FAIL if this is not an appropriate context in which to do
3455 * completion of anything, return OK if it is (even if there are no matches).
3456 * For the caller, this means that the character is just passed through like a
3457 * normal character (instead of being expanded). This allows :s/^I^D etc.
3458 */
3459 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003460nextwild(
3461 expand_T *xp,
3462 int type,
3463 int options, /* extra options for ExpandOne() */
3464 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 int i, j;
3467 char_u *p1;
3468 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 int difflen;
3470 int v;
3471
3472 if (xp->xp_numfiles == -1)
3473 {
3474 set_expand_context(xp);
3475 cmd_showtail = expand_showtail(xp);
3476 }
3477
3478 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3479 {
3480 beep_flush();
3481 return OK; /* Something illegal on command line */
3482 }
3483 if (xp->xp_context == EXPAND_NOTHING)
3484 {
3485 /* Caller can use the character as a normal char instead */
3486 return FAIL;
3487 }
3488
3489 MSG_PUTS("..."); /* show that we are busy */
3490 out_flush();
3491
3492 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003493 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 if (type == WILD_NEXT || type == WILD_PREV)
3496 {
3497 /*
3498 * Get next/previous match for a previous expanded pattern.
3499 */
3500 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3501 }
3502 else
3503 {
3504 /*
3505 * Translate string into pattern and expand it.
3506 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003507 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3508 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 p2 = NULL;
3510 else
3511 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003512 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003513 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3514 if (escape)
3515 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003516
3517 if (p_wic)
3518 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003519 p2 = ExpandOne(xp, p1,
3520 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003521 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003523 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (p2 != NULL && type == WILD_LONGEST)
3525 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003526 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 if (ccline.cmdbuff[i + j] == '*'
3528 || ccline.cmdbuff[i + j] == '?')
3529 break;
3530 if ((int)STRLEN(p2) < j)
3531 {
3532 vim_free(p2);
3533 p2 = NULL;
3534 }
3535 }
3536 }
3537 }
3538
3539 if (p2 != NULL && !got_int)
3540 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003541 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003542 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003544 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 xp->xp_pattern = ccline.cmdbuff + i;
3546 }
3547 else
3548 v = OK;
3549 if (v == OK)
3550 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003551 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3552 &ccline.cmdbuff[ccline.cmdpos],
3553 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3554 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 ccline.cmdlen += difflen;
3556 ccline.cmdpos += difflen;
3557 }
3558 }
3559 vim_free(p2);
3560
3561 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003562 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 /* When expanding a ":map" command and no matches are found, assume that
3565 * the key is supposed to be inserted literally */
3566 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3567 return FAIL;
3568
3569 if (xp->xp_numfiles <= 0 && p2 == NULL)
3570 beep_flush();
3571 else if (xp->xp_numfiles == 1)
3572 /* free expanded pattern */
3573 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3574
3575 return OK;
3576}
3577
3578/*
3579 * Do wildcard expansion on the string 'str'.
3580 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003581 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 * Return NULL for failure.
3583 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003584 * "orig" is the originally expanded string, copied to allocated memory. It
3585 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3586 * WILD_PREV "orig" should be NULL.
3587 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003588 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3589 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 *
3591 * mode = WILD_FREE: just free previously expanded matches
3592 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3593 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3594 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3595 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3596 * mode = WILD_ALL: return all matches concatenated
3597 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003598 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 *
3600 * options = WILD_LIST_NOTFOUND: list entries without a match
3601 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3602 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3603 * options = WILD_NO_BEEP: Don't beep for multiple matches
3604 * options = WILD_ADD_SLASH: add a slash after directory names
3605 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3606 * options = WILD_SILENT: don't print warning messages
3607 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003608 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 *
3610 * The variables xp->xp_context and xp->xp_backslash must have been set!
3611 */
3612 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003613ExpandOne(
3614 expand_T *xp,
3615 char_u *str,
3616 char_u *orig, /* allocated copy of original of expanded string */
3617 int options,
3618 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 char_u *ss = NULL;
3621 static int findex;
3622 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003623 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 int i;
3625 long_u len;
3626 int non_suf_match; /* number without matching suffix */
3627
3628 /*
3629 * first handle the case of using an old match
3630 */
3631 if (mode == WILD_NEXT || mode == WILD_PREV)
3632 {
3633 if (xp->xp_numfiles > 0)
3634 {
3635 if (mode == WILD_PREV)
3636 {
3637 if (findex == -1)
3638 findex = xp->xp_numfiles;
3639 --findex;
3640 }
3641 else /* mode == WILD_NEXT */
3642 ++findex;
3643
3644 /*
3645 * When wrapping around, return the original string, set findex to
3646 * -1.
3647 */
3648 if (findex < 0)
3649 {
3650 if (orig_save == NULL)
3651 findex = xp->xp_numfiles - 1;
3652 else
3653 findex = -1;
3654 }
3655 if (findex >= xp->xp_numfiles)
3656 {
3657 if (orig_save == NULL)
3658 findex = 0;
3659 else
3660 findex = -1;
3661 }
3662#ifdef FEAT_WILDMENU
3663 if (p_wmnu)
3664 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3665 findex, cmd_showtail);
3666#endif
3667 if (findex == -1)
3668 return vim_strsave(orig_save);
3669 return vim_strsave(xp->xp_files[findex]);
3670 }
3671 else
3672 return NULL;
3673 }
3674
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003675 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3677 {
3678 FreeWild(xp->xp_numfiles, xp->xp_files);
3679 xp->xp_numfiles = -1;
3680 vim_free(orig_save);
3681 orig_save = NULL;
3682 }
3683 findex = 0;
3684
3685 if (mode == WILD_FREE) /* only release file name */
3686 return NULL;
3687
3688 if (xp->xp_numfiles == -1)
3689 {
3690 vim_free(orig_save);
3691 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003692 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694 /*
3695 * Do the expansion.
3696 */
3697 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3698 options) == FAIL)
3699 {
3700#ifdef FNAME_ILLEGAL
3701 /* Illegal file name has been silently skipped. But when there
3702 * are wildcards, the real problem is that there was no match,
3703 * causing the pattern to be added, which has illegal characters.
3704 */
3705 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3706 EMSG2(_(e_nomatch2), str);
3707#endif
3708 }
3709 else if (xp->xp_numfiles == 0)
3710 {
3711 if (!(options & WILD_SILENT))
3712 EMSG2(_(e_nomatch2), str);
3713 }
3714 else
3715 {
3716 /* Escape the matches for use on the command line. */
3717 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3718
3719 /*
3720 * Check for matching suffixes in file names.
3721 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003722 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3723 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 if (xp->xp_numfiles)
3726 non_suf_match = xp->xp_numfiles;
3727 else
3728 non_suf_match = 1;
3729 if ((xp->xp_context == EXPAND_FILES
3730 || xp->xp_context == EXPAND_DIRECTORIES)
3731 && xp->xp_numfiles > 1)
3732 {
3733 /*
3734 * More than one match; check suffix.
3735 * The files will have been sorted on matching suffix in
3736 * expand_wildcards, only need to check the first two.
3737 */
3738 non_suf_match = 0;
3739 for (i = 0; i < 2; ++i)
3740 if (match_suffix(xp->xp_files[i]))
3741 ++non_suf_match;
3742 }
3743 if (non_suf_match != 1)
3744 {
3745 /* Can we ever get here unless it's while expanding
3746 * interactively? If not, we can get rid of this all
3747 * together. Don't really want to wait for this message
3748 * (and possibly have to hit return to continue!).
3749 */
3750 if (!(options & WILD_SILENT))
3751 EMSG(_(e_toomany));
3752 else if (!(options & WILD_NO_BEEP))
3753 beep_flush();
3754 }
3755 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3756 ss = vim_strsave(xp->xp_files[0]);
3757 }
3758 }
3759 }
3760
3761 /* Find longest common part */
3762 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3763 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003764 int mb_len = 1;
3765 int c0, ci;
3766
3767 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003769#ifdef FEAT_MBYTE
3770 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003772 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3773 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3774 }
3775 else
3776#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003777 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003778 for (i = 1; i < xp->xp_numfiles; ++i)
3779 {
3780#ifdef FEAT_MBYTE
3781 if (has_mbyte)
3782 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3783 else
3784#endif
3785 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003786 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003788 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003789 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003791 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 break;
3793 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003794 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 break;
3796 }
3797 if (i < xp->xp_numfiles)
3798 {
3799 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003800 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 break;
3802 }
3803 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 ss = alloc((unsigned)len + 1);
3806 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003807 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 findex = -1; /* next p_wc gets first one */
3809 }
3810
3811 /* Concatenate all matching names */
3812 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3813 {
3814 len = 0;
3815 for (i = 0; i < xp->xp_numfiles; ++i)
3816 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3817 ss = lalloc(len, TRUE);
3818 if (ss != NULL)
3819 {
3820 *ss = NUL;
3821 for (i = 0; i < xp->xp_numfiles; ++i)
3822 {
3823 STRCAT(ss, xp->xp_files[i]);
3824 if (i != xp->xp_numfiles - 1)
3825 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3826 }
3827 }
3828 }
3829
3830 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3831 ExpandCleanup(xp);
3832
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003833 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003834 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003835 vim_free(orig);
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 return ss;
3838}
3839
3840/*
3841 * Prepare an expand structure for use.
3842 */
3843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003844ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003846 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003847 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003849#ifndef BACKSLASH_IN_FILENAME
3850 xp->xp_shell = FALSE;
3851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 xp->xp_numfiles = -1;
3853 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003854#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3855 xp->xp_arg = NULL;
3856#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003857 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858}
3859
3860/*
3861 * Cleanup an expand structure after use.
3862 */
3863 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003864ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 if (xp->xp_numfiles >= 0)
3867 {
3868 FreeWild(xp->xp_numfiles, xp->xp_files);
3869 xp->xp_numfiles = -1;
3870 }
3871}
3872
3873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003874ExpandEscape(
3875 expand_T *xp,
3876 char_u *str,
3877 int numfiles,
3878 char_u **files,
3879 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
3881 int i;
3882 char_u *p;
3883
3884 /*
3885 * May change home directory back to "~"
3886 */
3887 if (options & WILD_HOME_REPLACE)
3888 tilde_replace(str, numfiles, files);
3889
3890 if (options & WILD_ESCAPE)
3891 {
3892 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003893 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003894 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 || xp->xp_context == EXPAND_BUFFERS
3896 || xp->xp_context == EXPAND_DIRECTORIES)
3897 {
3898 /*
3899 * Insert a backslash into a file name before a space, \, %, #
3900 * and wildmatch characters, except '~'.
3901 */
3902 for (i = 0; i < numfiles; ++i)
3903 {
3904 /* for ":set path=" we need to escape spaces twice */
3905 if (xp->xp_backslash == XP_BS_THREE)
3906 {
3907 p = vim_strsave_escaped(files[i], (char_u *)" ");
3908 if (p != NULL)
3909 {
3910 vim_free(files[i]);
3911 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003912#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 p = vim_strsave_escaped(files[i], (char_u *)" ");
3914 if (p != NULL)
3915 {
3916 vim_free(files[i]);
3917 files[i] = p;
3918 }
3919#endif
3920 }
3921 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003922#ifdef BACKSLASH_IN_FILENAME
3923 p = vim_strsave_fnameescape(files[i], FALSE);
3924#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003925 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if (p != NULL)
3928 {
3929 vim_free(files[i]);
3930 files[i] = p;
3931 }
3932
3933 /* If 'str' starts with "\~", replace "~" at start of
3934 * files[i] with "\~". */
3935 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003936 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003939
3940 /* If the first file starts with a '+' escape it. Otherwise it
3941 * could be seen as "+cmd". */
3942 if (*files[0] == '+')
3943 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 else if (xp->xp_context == EXPAND_TAGS)
3946 {
3947 /*
3948 * Insert a backslash before characters in a tag name that
3949 * would terminate the ":tag" command.
3950 */
3951 for (i = 0; i < numfiles; ++i)
3952 {
3953 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3954 if (p != NULL)
3955 {
3956 vim_free(files[i]);
3957 files[i] = p;
3958 }
3959 }
3960 }
3961 }
3962}
3963
3964/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003965 * Escape special characters in "fname" for when used as a file name argument
3966 * after a Vim command, or, when "shell" is non-zero, a shell command.
3967 * Returns the result in allocated memory.
3968 */
3969 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003970vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003971{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003972 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003973#ifdef BACKSLASH_IN_FILENAME
3974 char_u buf[20];
3975 int j = 0;
3976
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003977 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003978 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003979 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003980 buf[j++] = *p;
3981 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003982 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003983#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003984 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3985 if (shell && csh_like_shell() && p != NULL)
3986 {
3987 char_u *s;
3988
3989 /* For csh and similar shells need to put two backslashes before '!'.
3990 * One is taken by Vim, one by the shell. */
3991 s = vim_strsave_escaped(p, (char_u *)"!");
3992 vim_free(p);
3993 p = s;
3994 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003995#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003996
3997 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3998 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003999 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004000 escape_fname(&p);
4001
4002 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004003}
4004
4005/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004006 * Put a backslash before the file name in "pp", which is in allocated memory.
4007 */
4008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004009escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004010{
4011 char_u *p;
4012
4013 p = alloc((unsigned)(STRLEN(*pp) + 2));
4014 if (p != NULL)
4015 {
4016 p[0] = '\\';
4017 STRCPY(p + 1, *pp);
4018 vim_free(*pp);
4019 *pp = p;
4020 }
4021}
4022
4023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * For each file name in files[num_files]:
4025 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4026 */
4027 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004028tilde_replace(
4029 char_u *orig_pat,
4030 int num_files,
4031 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
4033 int i;
4034 char_u *p;
4035
4036 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4037 {
4038 for (i = 0; i < num_files; ++i)
4039 {
4040 p = home_replace_save(NULL, files[i]);
4041 if (p != NULL)
4042 {
4043 vim_free(files[i]);
4044 files[i] = p;
4045 }
4046 }
4047 }
4048}
4049
4050/*
4051 * Show all matches for completion on the command line.
4052 * Returns EXPAND_NOTHING when the character that triggered expansion should
4053 * be inserted like a normal character.
4054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004056showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4059 int num_files;
4060 char_u **files_found;
4061 int i, j, k;
4062 int maxlen;
4063 int lines;
4064 int columns;
4065 char_u *p;
4066 int lastlen;
4067 int attr;
4068 int showtail;
4069
4070 if (xp->xp_numfiles == -1)
4071 {
4072 set_expand_context(xp);
4073 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4074 &num_files, &files_found);
4075 showtail = expand_showtail(xp);
4076 if (i != EXPAND_OK)
4077 return i;
4078
4079 }
4080 else
4081 {
4082 num_files = xp->xp_numfiles;
4083 files_found = xp->xp_files;
4084 showtail = cmd_showtail;
4085 }
4086
4087#ifdef FEAT_WILDMENU
4088 if (!wildmenu)
4089 {
4090#endif
4091 msg_didany = FALSE; /* lines_left will be set */
4092 msg_start(); /* prepare for paging */
4093 msg_putchar('\n');
4094 out_flush();
4095 cmdline_row = msg_row;
4096 msg_didany = FALSE; /* lines_left will be set again */
4097 msg_start(); /* prepare for paging */
4098#ifdef FEAT_WILDMENU
4099 }
4100#endif
4101
4102 if (got_int)
4103 got_int = FALSE; /* only int. the completion, not the cmd line */
4104#ifdef FEAT_WILDMENU
4105 else if (wildmenu)
4106 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4107#endif
4108 else
4109 {
4110 /* find the length of the longest file name */
4111 maxlen = 0;
4112 for (i = 0; i < num_files; ++i)
4113 {
4114 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004115 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 || xp->xp_context == EXPAND_BUFFERS))
4117 {
4118 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4119 j = vim_strsize(NameBuff);
4120 }
4121 else
4122 j = vim_strsize(L_SHOWFILE(i));
4123 if (j > maxlen)
4124 maxlen = j;
4125 }
4126
4127 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4128 lines = num_files;
4129 else
4130 {
4131 /* compute the number of columns and lines for the listing */
4132 maxlen += 2; /* two spaces between file names */
4133 columns = ((int)Columns + 2) / maxlen;
4134 if (columns < 1)
4135 columns = 1;
4136 lines = (num_files + columns - 1) / columns;
4137 }
4138
4139 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4140
4141 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4142 {
4143 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4144 msg_clr_eos();
4145 msg_advance(maxlen - 3);
4146 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4147 }
4148
4149 /* list the files line by line */
4150 for (i = 0; i < lines; ++i)
4151 {
4152 lastlen = 999;
4153 for (k = i; k < num_files; k += lines)
4154 {
4155 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4156 {
4157 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4158 p = files_found[k] + STRLEN(files_found[k]) + 1;
4159 msg_advance(maxlen + 1);
4160 msg_puts(p);
4161 msg_advance(maxlen + 3);
4162 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4163 break;
4164 }
4165 for (j = maxlen - lastlen; --j >= 0; )
4166 msg_putchar(' ');
4167 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004168 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 || xp->xp_context == EXPAND_BUFFERS)
4170 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004171 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004172 if (xp->xp_numfiles != -1)
4173 {
4174 char_u *halved_slash;
4175 char_u *exp_path;
4176
4177 /* Expansion was done before and special characters
4178 * were escaped, need to halve backslashes. Also
4179 * $HOME has been replaced with ~/. */
4180 exp_path = expand_env_save_opt(files_found[k], TRUE);
4181 halved_slash = backslash_halve_save(
4182 exp_path != NULL ? exp_path : files_found[k]);
4183 j = mch_isdir(halved_slash != NULL ? halved_slash
4184 : files_found[k]);
4185 vim_free(exp_path);
4186 vim_free(halved_slash);
4187 }
4188 else
4189 /* Expansion was done here, file names are literal. */
4190 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (showtail)
4192 p = L_SHOWFILE(k);
4193 else
4194 {
4195 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4196 TRUE);
4197 p = NameBuff;
4198 }
4199 }
4200 else
4201 {
4202 j = FALSE;
4203 p = L_SHOWFILE(k);
4204 }
4205 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4206 }
4207 if (msg_col > 0) /* when not wrapped around */
4208 {
4209 msg_clr_eos();
4210 msg_putchar('\n');
4211 }
4212 out_flush(); /* show one line at a time */
4213 if (got_int)
4214 {
4215 got_int = FALSE;
4216 break;
4217 }
4218 }
4219
4220 /*
4221 * we redraw the command below the lines that we have just listed
4222 * This is a bit tricky, but it saves a lot of screen updating.
4223 */
4224 cmdline_row = msg_row; /* will put it back later */
4225 }
4226
4227 if (xp->xp_numfiles == -1)
4228 FreeWild(num_files, files_found);
4229
4230 return EXPAND_OK;
4231}
4232
4233/*
4234 * Private gettail for showmatches() (and win_redr_status_matches()):
4235 * Find tail of file name path, but ignore trailing "/".
4236 */
4237 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004238sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 char_u *p;
4241 char_u *t = s;
4242 int had_sep = FALSE;
4243
4244 for (p = s; *p != NUL; )
4245 {
4246 if (vim_ispathsep(*p)
4247#ifdef BACKSLASH_IN_FILENAME
4248 && !rem_backslash(p)
4249#endif
4250 )
4251 had_sep = TRUE;
4252 else if (had_sep)
4253 {
4254 t = p;
4255 had_sep = FALSE;
4256 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004257 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 return t;
4260}
4261
4262/*
4263 * Return TRUE if we only need to show the tail of completion matches.
4264 * When not completing file names or there is a wildcard in the path FALSE is
4265 * returned.
4266 */
4267 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004268expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269{
4270 char_u *s;
4271 char_u *end;
4272
4273 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004274 if (xp->xp_context != EXPAND_FILES
4275 && xp->xp_context != EXPAND_SHELLCMD
4276 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FALSE;
4278
4279 end = gettail(xp->xp_pattern);
4280 if (end == xp->xp_pattern) /* there is no path separator */
4281 return FALSE;
4282
4283 for (s = xp->xp_pattern; s < end; s++)
4284 {
4285 /* Skip escaped wildcards. Only when the backslash is not a path
4286 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4287 if (rem_backslash(s))
4288 ++s;
4289 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4290 return FALSE;
4291 }
4292 return TRUE;
4293}
4294
4295/*
4296 * Prepare a string for expansion.
4297 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004298 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 * When expanding other names: The string will be used with regcomp(). Copy
4300 * the name into allocated memory and prepend "^".
4301 */
4302 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004303addstar(
4304 char_u *fname,
4305 int len,
4306 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
4308 char_u *retval;
4309 int i, j;
4310 int new_len;
4311 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004312 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004314 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004315 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004316 && context != EXPAND_SHELLCMD
4317 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
4319 /*
4320 * Matching will be done internally (on something other than files).
4321 * So we convert the file-matching-type wildcards into our kind for
4322 * use with vim_regcomp(). First work out how long it will be:
4323 */
4324
4325 /* For help tags the translation is done in find_help_tags().
4326 * For a tag pattern starting with "/" no translation is needed. */
4327 if (context == EXPAND_HELP
4328 || context == EXPAND_COLORS
4329 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004330 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004331 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004332 || context == EXPAND_PACKADD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 || (context == EXPAND_TAGS && fname[0] == '/'))
4334 retval = vim_strnsave(fname, len);
4335 else
4336 {
4337 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4338 for (i = 0; i < len; i++)
4339 {
4340 if (fname[i] == '*' || fname[i] == '~')
4341 new_len++; /* '*' needs to be replaced by ".*"
4342 '~' needs to be replaced by "\~" */
4343
4344 /* Buffer names are like file names. "." should be literal */
4345 if (context == EXPAND_BUFFERS && fname[i] == '.')
4346 new_len++; /* "." becomes "\." */
4347
4348 /* Custom expansion takes care of special things, match
4349 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004350 if ((context == EXPAND_USER_DEFINED
4351 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 new_len++; /* '\' becomes "\\" */
4353 }
4354 retval = alloc(new_len);
4355 if (retval != NULL)
4356 {
4357 retval[0] = '^';
4358 j = 1;
4359 for (i = 0; i < len; i++, j++)
4360 {
4361 /* Skip backslash. But why? At least keep it for custom
4362 * expansion. */
4363 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004364 && context != EXPAND_USER_LIST
4365 && fname[i] == '\\'
4366 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 break;
4368
4369 switch (fname[i])
4370 {
4371 case '*': retval[j++] = '.';
4372 break;
4373 case '~': retval[j++] = '\\';
4374 break;
4375 case '?': retval[j] = '.';
4376 continue;
4377 case '.': if (context == EXPAND_BUFFERS)
4378 retval[j++] = '\\';
4379 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004380 case '\\': if (context == EXPAND_USER_DEFINED
4381 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 retval[j++] = '\\';
4383 break;
4384 }
4385 retval[j] = fname[i];
4386 }
4387 retval[j] = NUL;
4388 }
4389 }
4390 }
4391 else
4392 {
4393 retval = alloc(len + 4);
4394 if (retval != NULL)
4395 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004396 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004399 * Don't add a star to *, ~, ~user, $var or `cmd`.
4400 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 * ~ would be at the start of the file name, but not the tail.
4402 * $ could be anywhere in the tail.
4403 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004404 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 */
4406 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004407 ends_in_star = (len > 0 && retval[len - 1] == '*');
4408#ifndef BACKSLASH_IN_FILENAME
4409 for (i = len - 2; i >= 0; --i)
4410 {
4411 if (retval[i] != '\\')
4412 break;
4413 ends_in_star = !ends_in_star;
4414 }
4415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004417 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 && vim_strchr(tail, '$') == NULL
4419 && vim_strchr(retval, '`') == NULL)
4420 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004421 else if (len > 0 && retval[len - 1] == '$')
4422 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 retval[len] = NUL;
4424 }
4425 }
4426 return retval;
4427}
4428
4429/*
4430 * Must parse the command line so far to work out what context we are in.
4431 * Completion can then be done based on that context.
4432 * This routine sets the variables:
4433 * xp->xp_pattern The start of the pattern to be expanded within
4434 * the command line (ends at the cursor).
4435 * xp->xp_context The type of thing to expand. Will be one of:
4436 *
4437 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4438 * the command line, like an unknown command. Caller
4439 * should beep.
4440 * EXPAND_NOTHING Unrecognised context for completion, use char like
4441 * a normal char, rather than for completion. eg
4442 * :s/^I/
4443 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4444 * it.
4445 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4446 * EXPAND_FILES After command with XFILE set, or after setting
4447 * with P_EXPAND set. eg :e ^I, :w>>^I
4448 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4449 * when we know only directories are of interest. eg
4450 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004451 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4453 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4454 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4455 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4456 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4457 * EXPAND_EVENTS Complete event names
4458 * EXPAND_SYNTAX Complete :syntax command arguments
4459 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4460 * EXPAND_AUGROUP Complete autocommand group names
4461 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4462 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4463 * eg :unmap a^I , :cunab x^I
4464 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4465 * eg :call sub^I
4466 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4467 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4468 * names in expressions, eg :while s^I
4469 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004470 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 */
4472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004473set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004475 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (ccline.cmdfirstc != ':'
4477#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004478 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004479 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480#endif
4481 )
4482 {
4483 xp->xp_context = EXPAND_NOTHING;
4484 return;
4485 }
4486 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4487}
4488
4489 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004490set_cmd_context(
4491 expand_T *xp,
4492 char_u *str, /* start of command line */
4493 int len, /* length of command line (excl. NUL) */
4494 int col) /* position of cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495{
4496 int old_char = NUL;
4497 char_u *nextcomm;
4498
4499 /*
4500 * Avoid a UMR warning from Purify, only save the character if it has been
4501 * written before.
4502 */
4503 if (col < len)
4504 old_char = str[col];
4505 str[col] = NUL;
4506 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004507
4508#ifdef FEAT_EVAL
4509 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004510 {
4511# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004512 /* pass CMD_SIZE because there is no real command */
4513 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004514# endif
4515 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004516 else if (ccline.input_fn)
4517 {
4518 xp->xp_context = ccline.xp_context;
4519 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004520# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004521 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004522# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004523 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004524 else
4525#endif
4526 while (nextcomm != NULL)
4527 nextcomm = set_one_cmd_context(xp, nextcomm);
4528
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004529 /* Store the string here so that call_user_expand_func() can get to them
4530 * easily. */
4531 xp->xp_line = str;
4532 xp->xp_col = col;
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 str[col] = old_char;
4535}
4536
4537/*
4538 * Expand the command line "str" from context "xp".
4539 * "xp" must have been set by set_cmd_context().
4540 * xp->xp_pattern points into "str", to where the text that is to be expanded
4541 * starts.
4542 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4543 * cursor.
4544 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4545 * key that triggered expansion literally.
4546 * Returns EXPAND_OK otherwise.
4547 */
4548 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004549expand_cmdline(
4550 expand_T *xp,
4551 char_u *str, /* start of command line */
4552 int col, /* position of cursor */
4553 int *matchcount, /* return: nr of matches */
4554 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
4556 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004557 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4560 {
4561 beep_flush();
4562 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4563 }
4564 if (xp->xp_context == EXPAND_NOTHING)
4565 {
4566 /* Caller can use the character as a normal char instead */
4567 return EXPAND_NOTHING;
4568 }
4569
4570 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004571 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4572 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (file_str == NULL)
4574 return EXPAND_UNSUCCESSFUL;
4575
Bram Moolenaar94950a92010-12-02 16:01:29 +01004576 if (p_wic)
4577 options += WILD_ICASE;
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004580 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 *matchcount = 0;
4583 *matches = NULL;
4584 }
4585 vim_free(file_str);
4586
4587 return EXPAND_OK;
4588}
4589
4590#ifdef FEAT_MULTI_LANG
4591/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004592 * Cleanup matches for help tags:
4593 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4594 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004596static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004599cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
4601 int i, j;
4602 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004603 char_u buf[4];
4604 char_u *p = buf;
4605
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004606 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004607 {
4608 *p++ = '@';
4609 *p++ = p_hlg[0];
4610 *p++ = p_hlg[1];
4611 }
4612 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
4614 for (i = 0; i < num_file; ++i)
4615 {
4616 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004617 if (len <= 0)
4618 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004619 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 /* Sorting on priority means the same item in another language may
4622 * be anywhere. Search all items for a match up to the "@en". */
4623 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004624 if (j != i && (int)STRLEN(file[j]) == len + 3
4625 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 break;
4627 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004628 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 file[i][len] = NUL;
4630 }
4631 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004632
4633 if (*buf != NUL)
4634 for (i = 0; i < num_file; ++i)
4635 {
4636 len = (int)STRLEN(file[i]) - 3;
4637 if (len <= 0)
4638 continue;
4639 if (STRCMP(file[i] + len, buf) == 0)
4640 {
4641 /* remove the default language */
4642 file[i][len] = NUL;
4643 }
4644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645}
4646#endif
4647
4648/*
4649 * Do the expansion based on xp->xp_context and "pat".
4650 */
4651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004652ExpandFromContext(
4653 expand_T *xp,
4654 char_u *pat,
4655 int *num_file,
4656 char_u ***file,
4657 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
4659#ifdef FEAT_CMDL_COMPL
4660 regmatch_T regmatch;
4661#endif
4662 int ret;
4663 int flags;
4664
4665 flags = EW_DIR; /* include directories */
4666 if (options & WILD_LIST_NOTFOUND)
4667 flags |= EW_NOTFOUND;
4668 if (options & WILD_ADD_SLASH)
4669 flags |= EW_ADDSLASH;
4670 if (options & WILD_KEEP_ALL)
4671 flags |= EW_KEEPALL;
4672 if (options & WILD_SILENT)
4673 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004674 if (options & WILD_ALLLINKS)
4675 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004677 if (xp->xp_context == EXPAND_FILES
4678 || xp->xp_context == EXPAND_DIRECTORIES
4679 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
4681 /*
4682 * Expand file or directory names.
4683 */
4684 int free_pat = FALSE;
4685 int i;
4686
4687 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4688 * space */
4689 if (xp->xp_backslash != XP_BS_NONE)
4690 {
4691 free_pat = TRUE;
4692 pat = vim_strsave(pat);
4693 for (i = 0; pat[i]; ++i)
4694 if (pat[i] == '\\')
4695 {
4696 if (xp->xp_backslash == XP_BS_THREE
4697 && pat[i + 1] == '\\'
4698 && pat[i + 2] == '\\'
4699 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004700 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 if (xp->xp_backslash == XP_BS_ONE
4702 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004703 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 }
4706
4707 if (xp->xp_context == EXPAND_FILES)
4708 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004709 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4710 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 else
4712 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004713 if (options & WILD_ICASE)
4714 flags |= EW_ICASE;
4715
Bram Moolenaard7834d32009-12-02 16:14:36 +00004716 /* Expand wildcards, supporting %:h and the like. */
4717 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (free_pat)
4719 vim_free(pat);
4720 return ret;
4721 }
4722
4723 *file = (char_u **)"";
4724 *num_file = 0;
4725 if (xp->xp_context == EXPAND_HELP)
4726 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004727 /* With an empty argument we would get all the help tags, which is
4728 * very slow. Get matches for "help" instead. */
4729 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4730 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732#ifdef FEAT_MULTI_LANG
4733 cleanup_help_tags(*num_file, *file);
4734#endif
4735 return OK;
4736 }
4737 return FAIL;
4738 }
4739
4740#ifndef FEAT_CMDL_COMPL
4741 return FAIL;
4742#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004743 if (xp->xp_context == EXPAND_SHELLCMD)
4744 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if (xp->xp_context == EXPAND_OLD_SETTING)
4746 return ExpandOldSetting(num_file, file);
4747 if (xp->xp_context == EXPAND_BUFFERS)
4748 return ExpandBufnames(pat, num_file, file, options);
4749 if (xp->xp_context == EXPAND_TAGS
4750 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4751 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4752 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004753 {
4754 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004755 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4756 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004759 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004760 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004761 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004762 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004763 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004764 {
4765 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004766 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004767 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004768 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004769 {
4770 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004771 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004772 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004773# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4774 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004775 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004776# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004777 if (xp->xp_context == EXPAND_PACKADD)
4778 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4781 if (regmatch.regprog == NULL)
4782 return FAIL;
4783
4784 /* set ignore-case according to p_ic, p_scs and pat */
4785 regmatch.rm_ic = ignorecase(pat);
4786
4787 if (xp->xp_context == EXPAND_SETTINGS
4788 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4789 ret = ExpandSettings(xp, &regmatch, num_file, file);
4790 else if (xp->xp_context == EXPAND_MAPPINGS)
4791 ret = ExpandMappings(&regmatch, num_file, file);
4792# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4793 else if (xp->xp_context == EXPAND_USER_DEFINED)
4794 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4795# endif
4796 else
4797 {
4798 static struct expgen
4799 {
4800 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004801 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004803 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 } tab[] =
4805 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004806 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4807 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004808#ifdef FEAT_CMDHIST
4809 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004812 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004813 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004814 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4815 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4816 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#endif
4818#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004819 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4820 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4821 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4822 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823#endif
4824#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004825 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4826 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#endif
4828#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004829 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004831#ifdef FEAT_PROFILE
4832 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4833#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004834 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004836 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4837 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004839#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004840 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004841#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004842#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004843 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004844#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004845#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004846 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4849 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004850 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4851 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004853 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004854 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 };
4856 int i;
4857
4858 /*
4859 * Find a context in the table and call the ExpandGeneric() with the
4860 * right function to do the expansion.
4861 */
4862 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004863 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 if (xp->xp_context == tab[i].context)
4865 {
4866 if (tab[i].ic)
4867 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004868 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004869 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 break;
4871 }
4872 }
4873
Bram Moolenaar473de612013-06-08 18:19:48 +02004874 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
4876 return ret;
4877#endif /* FEAT_CMDL_COMPL */
4878}
4879
4880#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4881/*
4882 * Expand a list of names.
4883 *
4884 * Generic function for command line completion. It calls a function to
4885 * obtain strings, one by one. The strings are matched against a regexp
4886 * program. Matching strings are copied into an array, which is returned.
4887 *
4888 * Returns OK when no problems encountered, FAIL for error (out of memory).
4889 */
4890 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004891ExpandGeneric(
4892 expand_T *xp,
4893 regmatch_T *regmatch,
4894 int *num_file,
4895 char_u ***file,
4896 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004898 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
4900 int i;
4901 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004902 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 char_u *str;
4904
4905 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004906 * round == 0: count the number of matching names
4907 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004909 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
4911 for (i = 0; ; ++i)
4912 {
4913 str = (*func)(xp, i);
4914 if (str == NULL) /* end of list */
4915 break;
4916 if (*str == NUL) /* skip empty strings */
4917 continue;
4918
4919 if (vim_regexec(regmatch, str, (colnr_T)0))
4920 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004921 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004923 if (escaped)
4924 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4925 else
4926 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 (*file)[count] = str;
4928#ifdef FEAT_MENU
4929 if (func == get_menu_names && str != NULL)
4930 {
4931 /* test for separator added by get_menu_names() */
4932 str += STRLEN(str) - 1;
4933 if (*str == '\001')
4934 *str = '.';
4935 }
4936#endif
4937 }
4938 ++count;
4939 }
4940 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004941 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 if (count == 0)
4944 return OK;
4945 *num_file = count;
4946 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4947 if (*file == NULL)
4948 {
4949 *file = (char_u **)"";
4950 return FAIL;
4951 }
4952 count = 0;
4953 }
4954 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004955
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004956 /* Sort the results. Keep menu's in the specified order. */
4957 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004958 {
4959 if (xp->xp_context == EXPAND_EXPRESSION
4960 || xp->xp_context == EXPAND_FUNCTIONS
4961 || xp->xp_context == EXPAND_USER_FUNC)
4962 /* <SNR> functions should be sorted to the end. */
4963 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4964 sort_func_compare);
4965 else
4966 sort_strings(*file, *num_file);
4967 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004968
Bram Moolenaar4f688582007-07-24 12:34:30 +00004969#ifdef FEAT_CMDL_COMPL
4970 /* Reset the variables used for special highlight names expansion, so that
4971 * they don't show up when getting normal highlight names by ID. */
4972 reset_expand_highlight();
4973#endif
4974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return OK;
4976}
4977
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004978/*
4979 * Complete a shell command.
4980 * Returns FAIL or OK;
4981 */
4982 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004983expand_shellcmd(
4984 char_u *filepat, /* pattern to match with command names */
4985 int *num_file, /* return: number of matches */
4986 char_u ***file, /* return: array with matches */
4987 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004988{
4989 char_u *pat;
4990 int i;
4991 char_u *path;
4992 int mustfree = FALSE;
4993 garray_T ga;
4994 char_u *buf = alloc(MAXPATHL);
4995 size_t l;
4996 char_u *s, *e;
4997 int flags = flagsarg;
4998 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01004999 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005000
5001 if (buf == NULL)
5002 return FAIL;
5003
5004 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5005 * space */
5006 pat = vim_strsave(filepat);
5007 for (i = 0; pat[i]; ++i)
5008 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005009 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005010
Bram Moolenaarb5971142015-03-21 17:32:19 +01005011 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005012
5013 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005014 if (mch_isFullName(pat))
5015 path = (char_u *)" ";
5016 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005017 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5018 path = (char_u *)".";
5019 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005020 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005021 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005022 if (path == NULL)
5023 path = (char_u *)"";
5024 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005025
5026 /*
5027 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005028 * collect them in "ga". When "." is not in $PATH also expand for the
5029 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005030 */
5031 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005032 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005033 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005034 if (*s == NUL)
5035 {
5036 if (did_curdir)
5037 break;
5038 /* Find directories in the current directory, path is empty. */
5039 did_curdir = TRUE;
5040 }
5041 else if (*s == '.')
5042 did_curdir = TRUE;
5043
Bram Moolenaar68c31742006-09-02 15:54:18 +00005044 if (*s == ' ')
5045 ++s; /* Skip space used for absolute path name. */
5046
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005047#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005048 e = vim_strchr(s, ';');
5049#else
5050 e = vim_strchr(s, ':');
5051#endif
5052 if (e == NULL)
5053 e = s + STRLEN(s);
5054
5055 l = e - s;
5056 if (l > MAXPATHL - 5)
5057 break;
5058 vim_strncpy(buf, s, l);
5059 add_pathsep(buf);
5060 l = STRLEN(buf);
5061 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5062
5063 /* Expand matches in one directory of $PATH. */
5064 ret = expand_wildcards(1, &buf, num_file, file, flags);
5065 if (ret == OK)
5066 {
5067 if (ga_grow(&ga, *num_file) == FAIL)
5068 FreeWild(*num_file, *file);
5069 else
5070 {
5071 for (i = 0; i < *num_file; ++i)
5072 {
5073 s = (*file)[i];
5074 if (STRLEN(s) > l)
5075 {
5076 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005077 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005078 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5079 }
5080 else
5081 vim_free(s);
5082 }
5083 vim_free(*file);
5084 }
5085 }
5086 if (*e != NUL)
5087 ++e;
5088 }
5089 *file = ga.ga_data;
5090 *num_file = ga.ga_len;
5091
5092 vim_free(buf);
5093 vim_free(pat);
5094 if (mustfree)
5095 vim_free(path);
5096 return OK;
5097}
5098
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005101static 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 +00005102
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00005104 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005105 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005107 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005108call_user_expand_func(
5109 void *(*user_expand_func)(char_u *, int, char_u **, int),
5110 expand_T *xp,
5111 int *num_file,
5112 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005114 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005115 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005118 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005119 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
Bram Moolenaarb7515462013-06-29 12:58:33 +02005121 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005122 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 *num_file = 0;
5124 *file = NULL;
5125
Bram Moolenaarb7515462013-06-29 12:58:33 +02005126 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005127 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005128 keep = ccline.cmdbuff[ccline.cmdlen];
5129 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005130 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005131
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005132 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005133 args[1] = xp->xp_line;
5134 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 args[2] = num;
5136
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005137 /* Save the cmdline, we don't know what the function may do. */
5138 save_ccline = ccline;
5139 ccline.cmdbuff = NULL;
5140 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005142
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005143 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005144
5145 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005147 if (ccline.cmdbuff != NULL)
5148 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005149
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005150 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005151 return ret;
5152}
5153
5154/*
5155 * Expand names with a function defined by the user.
5156 */
5157 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005158ExpandUserDefined(
5159 expand_T *xp,
5160 regmatch_T *regmatch,
5161 int *num_file,
5162 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005163{
5164 char_u *retstr;
5165 char_u *s;
5166 char_u *e;
5167 char_u keep;
5168 garray_T ga;
5169
5170 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5171 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 return FAIL;
5173
5174 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005175 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 e = vim_strchr(s, '\n');
5178 if (e == NULL)
5179 e = s + STRLEN(s);
5180 keep = *e;
5181 *e = 0;
5182
5183 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5184 {
5185 *e = keep;
5186 if (*e != NUL)
5187 ++e;
5188 continue;
5189 }
5190
5191 if (ga_grow(&ga, 1) == FAIL)
5192 break;
5193
5194 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5195 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
5197 *e = keep;
5198 if (*e != NUL)
5199 ++e;
5200 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005201 vim_free(retstr);
5202 *file = ga.ga_data;
5203 *num_file = ga.ga_len;
5204 return OK;
5205}
5206
5207/*
5208 * Expand names with a list returned by a function defined by the user.
5209 */
5210 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005211ExpandUserList(
5212 expand_T *xp,
5213 int *num_file,
5214 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005215{
5216 list_T *retlist;
5217 listitem_T *li;
5218 garray_T ga;
5219
5220 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5221 if (retlist == NULL)
5222 return FAIL;
5223
5224 ga_init2(&ga, (int)sizeof(char *), 3);
5225 /* Loop over the items in the list. */
5226 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5227 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005228 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5229 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005230
5231 if (ga_grow(&ga, 1) == FAIL)
5232 break;
5233
5234 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005235 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005236 ++ga.ga_len;
5237 }
5238 list_unref(retlist);
5239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 *file = ga.ga_data;
5241 *num_file = ga.ga_len;
5242 return OK;
5243}
5244#endif
5245
5246/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005247 * Expand color scheme, compiler or filetype names.
5248 * Search from 'runtimepath':
5249 * 'runtimepath'/{dirnames}/{pat}.vim
5250 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5251 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5252 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5253 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005254 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 */
5256 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005257ExpandRTDir(
5258 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005259 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260 int *num_file,
5261 char_u ***file,
5262 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 char_u *s;
5265 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005266 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005268 int i;
5269 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 *num_file = 0;
5272 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005273 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005274 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005276 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005278 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5279 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005281 ga_clear_strings(&ga);
5282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005284 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005285 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005286 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005288
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005289 if (flags & DIP_START) {
5290 for (i = 0; dirnames[i] != NULL; ++i)
5291 {
5292 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5293 if (s == NULL)
5294 {
5295 ga_clear_strings(&ga);
5296 return FAIL;
5297 }
5298 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5299 globpath(p_pp, s, &ga, 0);
5300 vim_free(s);
5301 }
5302 }
5303
5304 if (flags & DIP_OPT) {
5305 for (i = 0; dirnames[i] != NULL; ++i)
5306 {
5307 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5308 if (s == NULL)
5309 {
5310 ga_clear_strings(&ga);
5311 return FAIL;
5312 }
5313 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5314 globpath(p_pp, s, &ga, 0);
5315 vim_free(s);
5316 }
5317 }
5318
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005319 for (i = 0; i < ga.ga_len; ++i)
5320 {
5321 match = ((char_u **)ga.ga_data)[i];
5322 s = match;
5323 e = s + STRLEN(s);
5324 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5325 {
5326 e -= 4;
5327 for (s = e; s > match; mb_ptr_back(match, s))
5328 if (s < match || vim_ispathsep(*s))
5329 break;
5330 ++s;
5331 *e = NUL;
5332 mch_memmove(match, s, e - s + 1);
5333 }
5334 }
5335
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005336 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005337 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005338
5339 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005340 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005341 remove_duplicates(&ga);
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 *file = ga.ga_data;
5344 *num_file = ga.ga_len;
5345 return OK;
5346}
5347
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005348/*
5349 * Expand loadplugin names:
5350 * 'packpath'/pack/ * /opt/{pat}
5351 */
5352 static int
5353ExpandPackAddDir(
5354 char_u *pat,
5355 int *num_file,
5356 char_u ***file)
5357{
5358 char_u *s;
5359 char_u *e;
5360 char_u *match;
5361 garray_T ga;
5362 int i;
5363 int pat_len;
5364
5365 *num_file = 0;
5366 *file = NULL;
5367 pat_len = (int)STRLEN(pat);
5368 ga_init2(&ga, (int)sizeof(char *), 10);
5369
5370 s = alloc((unsigned)(pat_len + 26));
5371 if (s == NULL)
5372 {
5373 ga_clear_strings(&ga);
5374 return FAIL;
5375 }
5376 sprintf((char *)s, "pack/*/opt/%s*", pat);
5377 globpath(p_pp, s, &ga, 0);
5378 vim_free(s);
5379
5380 for (i = 0; i < ga.ga_len; ++i)
5381 {
5382 match = ((char_u **)ga.ga_data)[i];
5383 s = gettail(match);
5384 e = s + STRLEN(s);
5385 mch_memmove(match, s, e - s + 1);
5386 }
5387
5388 if (ga.ga_len == 0)
5389 return FAIL;
5390
5391 /* Sort and remove duplicates which can happen when specifying multiple
5392 * directories in dirnames. */
5393 remove_duplicates(&ga);
5394
5395 *file = ga.ga_data;
5396 *num_file = ga.ga_len;
5397 return OK;
5398}
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400#endif
5401
5402#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5403/*
5404 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005405 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408globpath(
5409 char_u *path,
5410 char_u *file,
5411 garray_T *ga,
5412 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414 expand_T xpc;
5415 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 int num_p;
5418 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 buf = alloc(MAXPATHL);
5421 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005422 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005424 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 /* Loop over all entries in {path}. */
5428 while (*path != NUL)
5429 {
5430 /* Copy one item of the path to buf[] and concatenate the file name. */
5431 copy_option_part(&path, buf, MAXPATHL, ",");
5432 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5433 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005434# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005435 /* Using the platform's path separator (\) makes vim incorrectly
5436 * treat it as an escape character, use '/' instead. */
5437 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5438 STRCAT(buf, "/");
5439# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005443 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5444 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005446 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005448 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 for (i = 0; i < num_p; ++i)
5451 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005452 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005453 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005454 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 FreeWild(num_p, p);
5459 }
5460 }
5461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464}
5465
5466#endif
5467
5468#if defined(FEAT_CMDHIST) || defined(PROTO)
5469
5470/*********************************
5471 * Command line history stuff *
5472 *********************************/
5473
5474/*
5475 * Translate a history character to the associated type number.
5476 */
5477 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005478hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 if (c == ':')
5481 return HIST_CMD;
5482 if (c == '=')
5483 return HIST_EXPR;
5484 if (c == '@')
5485 return HIST_INPUT;
5486 if (c == '>')
5487 return HIST_DEBUG;
5488 return HIST_SEARCH; /* must be '?' or '/' */
5489}
5490
5491/*
5492 * Table of history names.
5493 * These names are used in :history and various hist...() functions.
5494 * It is sufficient to give the significant prefix of a history name.
5495 */
5496
5497static char *(history_names[]) =
5498{
5499 "cmd",
5500 "search",
5501 "expr",
5502 "input",
5503 "debug",
5504 NULL
5505};
5506
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005507#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5508/*
5509 * Function given to ExpandGeneric() to obtain the possible first
5510 * arguments of the ":history command.
5511 */
5512 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005513get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005514{
5515 static char_u compl[2] = { NUL, NUL };
5516 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005517 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005518 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5519
5520 if (idx < short_names_count)
5521 {
5522 compl[0] = (char_u)short_names[idx];
5523 return compl;
5524 }
5525 if (idx < short_names_count + history_name_count)
5526 return (char_u *)history_names[idx - short_names_count];
5527 if (idx == short_names_count + history_name_count)
5528 return (char_u *)"all";
5529 return NULL;
5530}
5531#endif
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533/*
5534 * init_history() - Initialize the command line history.
5535 * Also used to re-allocate the history when the size changes.
5536 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005538init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 int newlen; /* new length of history table */
5541 histentry_T *temp;
5542 int i;
5543 int j;
5544 int type;
5545
5546 /*
5547 * If size of history table changed, reallocate it
5548 */
5549 newlen = (int)p_hi;
5550 if (newlen != hislen) /* history length changed */
5551 {
5552 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5553 {
5554 if (newlen)
5555 {
5556 temp = (histentry_T *)lalloc(
5557 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5558 if (temp == NULL) /* out of memory! */
5559 {
5560 if (type == 0) /* first one: just keep the old length */
5561 {
5562 newlen = hislen;
5563 break;
5564 }
5565 /* Already changed one table, now we can only have zero
5566 * length for all tables. */
5567 newlen = 0;
5568 type = -1;
5569 continue;
5570 }
5571 }
5572 else
5573 temp = NULL;
5574 if (newlen == 0 || temp != NULL)
5575 {
5576 if (hisidx[type] < 0) /* there are no entries yet */
5577 {
5578 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005579 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
5581 else if (newlen > hislen) /* array becomes bigger */
5582 {
5583 for (i = 0; i <= hisidx[type]; ++i)
5584 temp[i] = history[type][i];
5585 j = i;
5586 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005587 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 for ( ; j < hislen; ++i, ++j)
5589 temp[i] = history[type][j];
5590 }
5591 else /* array becomes smaller or 0 */
5592 {
5593 j = hisidx[type];
5594 for (i = newlen - 1; ; --i)
5595 {
5596 if (i >= 0) /* copy newest entries */
5597 temp[i] = history[type][j];
5598 else /* remove older entries */
5599 vim_free(history[type][j].hisstr);
5600 if (--j < 0)
5601 j = hislen - 1;
5602 if (j == hisidx[type])
5603 break;
5604 }
5605 hisidx[type] = newlen - 1;
5606 }
5607 vim_free(history[type]);
5608 history[type] = temp;
5609 }
5610 }
5611 hislen = newlen;
5612 }
5613}
5614
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005617{
5618 hisptr->hisnum = 0;
5619 hisptr->viminfo = FALSE;
5620 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005621 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005622}
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624/*
5625 * Check if command line 'str' is already in history.
5626 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5627 */
5628 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629in_history(
5630 int type,
5631 char_u *str,
5632 int move_to_front, /* Move the entry to the front if it exists */
5633 int sep,
5634 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 int i;
5637 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005638 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 if (hisidx[type] < 0)
5641 return FALSE;
5642 i = hisidx[type];
5643 do
5644 {
5645 if (history[type][i].hisstr == NULL)
5646 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005647
5648 /* For search history, check that the separator character matches as
5649 * well. */
5650 p = history[type][i].hisstr;
5651 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005652 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005653 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
5655 if (!move_to_front)
5656 return TRUE;
5657 last_i = i;
5658 break;
5659 }
5660 if (--i < 0)
5661 i = hislen - 1;
5662 } while (i != hisidx[type]);
5663
5664 if (last_i >= 0)
5665 {
5666 str = history[type][i].hisstr;
5667 while (i != hisidx[type])
5668 {
5669 if (++i >= hislen)
5670 i = 0;
5671 history[type][last_i] = history[type][i];
5672 last_i = i;
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005675 history[type][i].viminfo = FALSE;
5676 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005677 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 return TRUE;
5679 }
5680 return FALSE;
5681}
5682
5683/*
5684 * Convert history name (from table above) to its HIST_ equivalent.
5685 * When "name" is empty, return "cmd" history.
5686 * Returns -1 for unknown history name.
5687 */
5688 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
5691 int i;
5692 int len = (int)STRLEN(name);
5693
5694 /* No argument: use current history. */
5695 if (len == 0)
5696 return hist_char2type(ccline.cmdfirstc);
5697
5698 for (i = 0; history_names[i] != NULL; ++i)
5699 if (STRNICMP(name, history_names[i], len) == 0)
5700 return i;
5701
5702 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5703 return hist_char2type(name[0]);
5704
5705 return -1;
5706}
5707
5708static int last_maptick = -1; /* last seen maptick */
5709
5710/*
5711 * Add the given string to the given history. If the string is already in the
5712 * history then it is moved to the front. "histype" may be one of he HIST_
5713 * values.
5714 */
5715 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005716add_to_history(
5717 int histype,
5718 char_u *new_entry,
5719 int in_map, /* consider maptick when inside a mapping */
5720 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 histentry_T *hisptr;
5723 int len;
5724
5725 if (hislen == 0) /* no history */
5726 return;
5727
Bram Moolenaara939e432013-11-09 05:30:26 +01005728 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5729 return;
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 /*
5732 * Searches inside the same mapping overwrite each other, so that only
5733 * the last line is kept. Be careful not to remove a line that was moved
5734 * down, only lines that were added.
5735 */
5736 if (histype == HIST_SEARCH && in_map)
5737 {
5738 if (maptick == last_maptick)
5739 {
5740 /* Current line is from the same mapping, remove it */
5741 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5742 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005743 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 --hisnum[histype];
5745 if (--hisidx[HIST_SEARCH] < 0)
5746 hisidx[HIST_SEARCH] = hislen - 1;
5747 }
5748 last_maptick = -1;
5749 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005750 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (++hisidx[histype] == hislen)
5753 hisidx[histype] = 0;
5754 hisptr = &history[histype][hisidx[histype]];
5755 vim_free(hisptr->hisstr);
5756
5757 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005758 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5760 if (hisptr->hisstr != NULL)
5761 hisptr->hisstr[len + 1] = sep;
5762
5763 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005764 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005765 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (histype == HIST_SEARCH && in_map)
5767 last_maptick = maptick;
5768 }
5769}
5770
5771#if defined(FEAT_EVAL) || defined(PROTO)
5772
5773/*
5774 * Get identifier of newest history entry.
5775 * "histype" may be one of the HIST_ values.
5776 */
5777 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005778get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5781 || hisidx[histype] < 0)
5782 return -1;
5783
5784 return history[histype][hisidx[histype]].hisnum;
5785}
5786
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 * Calculate history index from a number:
5789 * num > 0: seen as identifying number of a history entry
5790 * num < 0: relative position in history wrt newest entry
5791 * "histype" may be one of the HIST_ values.
5792 */
5793 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 int i;
5797 histentry_T *hist;
5798 int wrapped = FALSE;
5799
5800 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5801 || (i = hisidx[histype]) < 0 || num == 0)
5802 return -1;
5803
5804 hist = history[histype];
5805 if (num > 0)
5806 {
5807 while (hist[i].hisnum > num)
5808 if (--i < 0)
5809 {
5810 if (wrapped)
5811 break;
5812 i += hislen;
5813 wrapped = TRUE;
5814 }
5815 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5816 return i;
5817 }
5818 else if (-num <= hislen)
5819 {
5820 i += num + 1;
5821 if (i < 0)
5822 i += hislen;
5823 if (hist[i].hisstr != NULL)
5824 return i;
5825 }
5826 return -1;
5827}
5828
5829/*
5830 * Get a history entry by its index.
5831 * "histype" may be one of the HIST_ values.
5832 */
5833 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005834get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 idx = calc_hist_idx(histype, idx);
5837 if (idx >= 0)
5838 return history[histype][idx].hisstr;
5839 else
5840 return (char_u *)"";
5841}
5842
5843/*
5844 * Clear all entries of a history.
5845 * "histype" may be one of the HIST_ values.
5846 */
5847 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005848clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 int i;
5851 histentry_T *hisptr;
5852
5853 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5854 {
5855 hisptr = history[histype];
5856 for (i = hislen; i--;)
5857 {
5858 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005859 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005860 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862 hisidx[histype] = -1; /* mark history as cleared */
5863 hisnum[histype] = 0; /* reset identifier counter */
5864 return OK;
5865 }
5866 return FAIL;
5867}
5868
5869/*
5870 * Remove all entries matching {str} from a history.
5871 * "histype" may be one of the HIST_ values.
5872 */
5873 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005874del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 regmatch_T regmatch;
5877 histentry_T *hisptr;
5878 int idx;
5879 int i;
5880 int last;
5881 int found = FALSE;
5882
5883 regmatch.regprog = NULL;
5884 regmatch.rm_ic = FALSE; /* always match case */
5885 if (hislen != 0
5886 && histype >= 0
5887 && histype < HIST_COUNT
5888 && *str != NUL
5889 && (idx = hisidx[histype]) >= 0
5890 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5891 != NULL)
5892 {
5893 i = last = idx;
5894 do
5895 {
5896 hisptr = &history[histype][i];
5897 if (hisptr->hisstr == NULL)
5898 break;
5899 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5900 {
5901 found = TRUE;
5902 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005903 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 else
5906 {
5907 if (i != last)
5908 {
5909 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005910 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
5912 if (--last < 0)
5913 last += hislen;
5914 }
5915 if (--i < 0)
5916 i += hislen;
5917 } while (i != idx);
5918 if (history[histype][idx].hisstr == NULL)
5919 hisidx[histype] = -1;
5920 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005921 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 return found;
5923}
5924
5925/*
5926 * Remove an indexed entry from a history.
5927 * "histype" may be one of the HIST_ values.
5928 */
5929 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005930del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 int i, j;
5933
5934 i = calc_hist_idx(histype, idx);
5935 if (i < 0)
5936 return FALSE;
5937 idx = hisidx[histype];
5938 vim_free(history[histype][i].hisstr);
5939
5940 /* When deleting the last added search string in a mapping, reset
5941 * last_maptick, so that the last added search string isn't deleted again.
5942 */
5943 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5944 last_maptick = -1;
5945
5946 while (i != idx)
5947 {
5948 j = (i + 1) % hislen;
5949 history[histype][i] = history[histype][j];
5950 i = j;
5951 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005952 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (--i < 0)
5954 i += hislen;
5955 hisidx[histype] = i;
5956 return TRUE;
5957}
5958
5959#endif /* FEAT_EVAL */
5960
5961#if defined(FEAT_CRYPT) || defined(PROTO)
5962/*
5963 * Very specific function to remove the value in ":set key=val" from the
5964 * history.
5965 */
5966 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005967remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 char_u *p;
5970 int i;
5971
5972 i = hisidx[HIST_CMD];
5973 if (i < 0)
5974 return;
5975 p = history[HIST_CMD][i].hisstr;
5976 if (p != NULL)
5977 for ( ; *p; ++p)
5978 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5979 {
5980 p = vim_strchr(p + 3, '=');
5981 if (p == NULL)
5982 break;
5983 ++p;
5984 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5985 if (p[i] == '\\' && p[i + 1])
5986 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005987 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 --p;
5989 }
5990}
5991#endif
5992
5993#endif /* FEAT_CMDHIST */
5994
Bram Moolenaar064154c2016-03-19 22:50:43 +01005995#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005996/*
5997 * Get pointer to the command line info to use. cmdline_paste() may clear
5998 * ccline and put the previous value in prev_ccline.
5999 */
6000 static struct cmdline_info *
6001get_ccline_ptr(void)
6002{
6003 if ((State & CMDLINE) == 0)
6004 return NULL;
6005 if (ccline.cmdbuff != NULL)
6006 return &ccline;
6007 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6008 return &prev_ccline;
6009 return NULL;
6010}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006011#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006012
Bram Moolenaar064154c2016-03-19 22:50:43 +01006013#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006014/*
6015 * Get the current command line in allocated memory.
6016 * Only works when the command line is being edited.
6017 * Returns NULL when something is wrong.
6018 */
6019 char_u *
6020get_cmdline_str(void)
6021{
6022 struct cmdline_info *p = get_ccline_ptr();
6023
6024 if (p == NULL)
6025 return NULL;
6026 return vim_strnsave(p->cmdbuff, p->cmdlen);
6027}
6028
6029/*
6030 * Get the current command line position, counted in bytes.
6031 * Zero is the first position.
6032 * Only works when the command line is being edited.
6033 * Returns -1 when something is wrong.
6034 */
6035 int
6036get_cmdline_pos(void)
6037{
6038 struct cmdline_info *p = get_ccline_ptr();
6039
6040 if (p == NULL)
6041 return -1;
6042 return p->cmdpos;
6043}
6044
6045/*
6046 * Set the command line byte position to "pos". Zero is the first position.
6047 * Only works when the command line is being edited.
6048 * Returns 1 when failed, 0 when OK.
6049 */
6050 int
6051set_cmdline_pos(
6052 int pos)
6053{
6054 struct cmdline_info *p = get_ccline_ptr();
6055
6056 if (p == NULL)
6057 return 1;
6058
6059 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6060 * changed the command line. */
6061 if (pos < 0)
6062 new_cmdpos = 0;
6063 else
6064 new_cmdpos = pos;
6065 return 0;
6066}
6067#endif
6068
6069#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6070/*
6071 * Get the current command-line type.
6072 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6073 * Only works when the command line is being edited.
6074 * Returns NUL when something is wrong.
6075 */
6076 int
6077get_cmdline_type(void)
6078{
6079 struct cmdline_info *p = get_ccline_ptr();
6080
6081 if (p == NULL)
6082 return NUL;
6083 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006084 return
6085# ifdef FEAT_EVAL
6086 (p->input_fn) ? '@' :
6087# endif
6088 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006089 return p->cmdfirstc;
6090}
6091#endif
6092
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6094/*
6095 * Get indices "num1,num2" that specify a range within a list (not a range of
6096 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6097 * Returns OK if parsed successfully, otherwise FAIL.
6098 */
6099 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006100get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 int len;
6103 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006104 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 *str = skipwhite(*str);
6107 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6108 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006109 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 *str += len;
6111 *num1 = (int)num;
6112 first = TRUE;
6113 }
6114 *str = skipwhite(*str);
6115 if (**str == ',') /* parse "to" part of range */
6116 {
6117 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006118 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (len > 0)
6120 {
6121 *num2 = (int)num;
6122 *str = skipwhite(*str + len);
6123 }
6124 else if (!first) /* no number given at all */
6125 return FAIL;
6126 }
6127 else if (first) /* only one number given */
6128 *num2 = *num1;
6129 return OK;
6130}
6131#endif
6132
6133#if defined(FEAT_CMDHIST) || defined(PROTO)
6134/*
6135 * :history command - print a history
6136 */
6137 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006138ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 histentry_T *hist;
6141 int histype1 = HIST_CMD;
6142 int histype2 = HIST_CMD;
6143 int hisidx1 = 1;
6144 int hisidx2 = -1;
6145 int idx;
6146 int i, j, k;
6147 char_u *end;
6148 char_u *arg = eap->arg;
6149
6150 if (hislen == 0)
6151 {
6152 MSG(_("'history' option is zero"));
6153 return;
6154 }
6155
6156 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6157 {
6158 end = arg;
6159 while (ASCII_ISALPHA(*end)
6160 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6161 end++;
6162 i = *end;
6163 *end = NUL;
6164 histype1 = get_histtype(arg);
6165 if (histype1 == -1)
6166 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006167 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
6169 histype1 = 0;
6170 histype2 = HIST_COUNT-1;
6171 }
6172 else
6173 {
6174 *end = i;
6175 EMSG(_(e_trailing));
6176 return;
6177 }
6178 }
6179 else
6180 histype2 = histype1;
6181 *end = i;
6182 }
6183 else
6184 end = arg;
6185 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6186 {
6187 EMSG(_(e_trailing));
6188 return;
6189 }
6190
6191 for (; !got_int && histype1 <= histype2; ++histype1)
6192 {
6193 STRCPY(IObuff, "\n # ");
6194 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6195 MSG_PUTS_TITLE(IObuff);
6196 idx = hisidx[histype1];
6197 hist = history[histype1];
6198 j = hisidx1;
6199 k = hisidx2;
6200 if (j < 0)
6201 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6202 if (k < 0)
6203 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6204 if (idx >= 0 && j <= k)
6205 for (i = idx + 1; !got_int; ++i)
6206 {
6207 if (i == hislen)
6208 i = 0;
6209 if (hist[i].hisstr != NULL
6210 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6211 {
6212 msg_putchar('\n');
6213 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6214 hist[i].hisnum);
6215 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6216 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006217 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 else
6219 STRCAT(IObuff, hist[i].hisstr);
6220 msg_outtrans(IObuff);
6221 out_flush();
6222 }
6223 if (i == idx)
6224 break;
6225 }
6226 }
6227}
6228#endif
6229
6230#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006231/*
6232 * Buffers for history read from a viminfo file. Only valid while reading.
6233 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006234static histentry_T *viminfo_history[HIST_COUNT] =
6235 {NULL, NULL, NULL, NULL, NULL};
6236static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6237static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238static int viminfo_add_at_front = FALSE;
6239
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006240static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242/*
6243 * Translate a history type number to the associated character.
6244 */
6245 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006246hist_type2char(
6247 int type,
6248 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 if (type == HIST_CMD)
6251 return ':';
6252 if (type == HIST_SEARCH)
6253 {
6254 if (use_question)
6255 return '?';
6256 else
6257 return '/';
6258 }
6259 if (type == HIST_EXPR)
6260 return '=';
6261 return '@';
6262}
6263
6264/*
6265 * Prepare for reading the history from the viminfo file.
6266 * This allocates history arrays to store the read history lines.
6267 */
6268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006269prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 int i;
6272 int num;
6273 int type;
6274 int len;
6275
6276 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006277 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (asklen > hislen)
6279 asklen = hislen;
6280
6281 for (type = 0; type < HIST_COUNT; ++type)
6282 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006283 /* Count the number of empty spaces in the history list. Entries read
6284 * from viminfo previously are also considered empty. If there are
6285 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006287 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 num++;
6289 len = asklen;
6290 if (num > len)
6291 len = num;
6292 if (len <= 0)
6293 viminfo_history[type] = NULL;
6294 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006295 viminfo_history[type] = (histentry_T *)lalloc(
6296 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (viminfo_history[type] == NULL)
6298 len = 0;
6299 viminfo_hislen[type] = len;
6300 viminfo_hisidx[type] = 0;
6301 }
6302}
6303
6304/*
6305 * Accept a line from the viminfo, store it in the history array when it's
6306 * new.
6307 */
6308 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006309read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 int type;
6312 long_u len;
6313 char_u *val;
6314 char_u *p;
6315
6316 type = hist_char2type(virp->vir_line[0]);
6317 if (viminfo_hisidx[type] < viminfo_hislen[type])
6318 {
6319 val = viminfo_readstring(virp, 1, TRUE);
6320 if (val != NULL && *val != NUL)
6321 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006322 int sep = (*val == ' ' ? NUL : *val);
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006325 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
6327 /* Need to re-allocate to append the separator byte. */
6328 len = STRLEN(val);
6329 p = lalloc(len + 2, TRUE);
6330 if (p != NULL)
6331 {
6332 if (type == HIST_SEARCH)
6333 {
6334 /* Search entry: Move the separator from the first
6335 * column to after the NUL. */
6336 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006337 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339 else
6340 {
6341 /* Not a search entry: No separator in the viminfo
6342 * file, add a NUL separator. */
6343 mch_memmove(p, val, (size_t)len + 1);
6344 p[len + 1] = NUL;
6345 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006346 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6347 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006348 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6349 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006350 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 }
6352 }
6353 }
6354 vim_free(val);
6355 }
6356 return viminfo_readline(virp);
6357}
6358
Bram Moolenaar07219f92013-04-14 23:19:36 +02006359/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006360 * Accept a new style history line from the viminfo, store it in the history
6361 * array when it's new.
6362 */
6363 void
6364handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006365 garray_T *values,
6366 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006367{
6368 int type;
6369 long_u len;
6370 char_u *val;
6371 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006372 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006373
6374 /* Check the format:
6375 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006376 if (values->ga_len < 4
6377 || vp[0].bv_type != BVAL_NR
6378 || vp[1].bv_type != BVAL_NR
6379 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6380 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006381 return;
6382
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006383 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006384 if (type >= HIST_COUNT)
6385 return;
6386 if (viminfo_hisidx[type] < viminfo_hislen[type])
6387 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006388 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006389 if (val != NULL && *val != NUL)
6390 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006391 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6392 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006393 int idx;
6394 int overwrite = FALSE;
6395
6396 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6397 {
6398 /* If lines were written by an older Vim we need to avoid
6399 * getting duplicates. See if the entry already exists. */
6400 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6401 {
6402 p = viminfo_history[type][idx].hisstr;
6403 if (STRCMP(val, p) == 0
6404 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6405 {
6406 overwrite = TRUE;
6407 break;
6408 }
6409 }
6410
6411 if (!overwrite)
6412 {
6413 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006414 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006415 p = lalloc(len + 2, TRUE);
6416 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006417 else
6418 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006419 if (p != NULL)
6420 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006421 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006422 if (!overwrite)
6423 {
6424 mch_memmove(p, val, (size_t)len + 1);
6425 /* Put the separator after the NUL. */
6426 p[len + 1] = sep;
6427 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006428 viminfo_history[type][idx].hisnum = 0;
6429 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006430 viminfo_hisidx[type]++;
6431 }
6432 }
6433 }
6434 }
6435 }
6436}
6437
6438/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006439 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006440 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006441 static void
6442concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 int idx;
6445 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006446
6447 idx = hisidx[type] + viminfo_hisidx[type];
6448 if (idx >= hislen)
6449 idx -= hislen;
6450 else if (idx < 0)
6451 idx = hislen - 1;
6452 if (viminfo_add_at_front)
6453 hisidx[type] = idx;
6454 else
6455 {
6456 if (hisidx[type] == -1)
6457 hisidx[type] = hislen - 1;
6458 do
6459 {
6460 if (history[type][idx].hisstr != NULL
6461 || history[type][idx].viminfo)
6462 break;
6463 if (++idx == hislen)
6464 idx = 0;
6465 } while (idx != hisidx[type]);
6466 if (idx != hisidx[type] && --idx < 0)
6467 idx = hislen - 1;
6468 }
6469 for (i = 0; i < viminfo_hisidx[type]; i++)
6470 {
6471 vim_free(history[type][idx].hisstr);
6472 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6473 history[type][idx].viminfo = TRUE;
6474 history[type][idx].time_set = viminfo_history[type][i].time_set;
6475 if (--idx < 0)
6476 idx = hislen - 1;
6477 }
6478 idx += 1;
6479 idx %= hislen;
6480 for (i = 0; i < viminfo_hisidx[type]; i++)
6481 {
6482 history[type][idx++].hisnum = ++hisnum[type];
6483 idx %= hislen;
6484 }
6485}
6486
6487#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6488 static int
6489#ifdef __BORLANDC__
6490_RTLENTRYF
6491#endif
6492sort_hist(const void *s1, const void *s2)
6493{
6494 histentry_T *p1 = *(histentry_T **)s1;
6495 histentry_T *p2 = *(histentry_T **)s2;
6496
6497 if (p1->time_set < p2->time_set) return -1;
6498 if (p1->time_set > p2->time_set) return 1;
6499 return 0;
6500}
6501#endif
6502
6503/*
6504 * Merge history lines from viminfo and lines typed in this Vim based on the
6505 * timestamp;
6506 */
6507 static void
6508merge_history(int type)
6509{
6510 int max_len;
6511 histentry_T **tot_hist;
6512 histentry_T *new_hist;
6513 int i;
6514 int len;
6515
6516 /* Make one long list with all entries. */
6517 max_len = hislen + viminfo_hisidx[type];
6518 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6519 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6520 if (tot_hist == NULL || new_hist == NULL)
6521 {
6522 vim_free(tot_hist);
6523 vim_free(new_hist);
6524 return;
6525 }
6526 for (i = 0; i < viminfo_hisidx[type]; i++)
6527 tot_hist[i] = &viminfo_history[type][i];
6528 len = i;
6529 for (i = 0; i < hislen; i++)
6530 if (history[type][i].hisstr != NULL)
6531 tot_hist[len++] = &history[type][i];
6532
6533 /* Sort the list on timestamp. */
6534 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6535
6536 /* Keep the newest ones. */
6537 for (i = 0; i < hislen; i++)
6538 {
6539 if (i < len)
6540 {
6541 new_hist[i] = *tot_hist[i];
6542 tot_hist[i]->hisstr = NULL;
6543 if (new_hist[i].hisnum == 0)
6544 new_hist[i].hisnum = ++hisnum[type];
6545 }
6546 else
6547 clear_hist_entry(&new_hist[i]);
6548 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006549 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006550
6551 /* Free what is not kept. */
6552 for (i = 0; i < viminfo_hisidx[type]; i++)
6553 vim_free(viminfo_history[type][i].hisstr);
6554 for (i = 0; i < hislen; i++)
6555 vim_free(history[type][i].hisstr);
6556 vim_free(history[type]);
6557 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006558 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006559}
6560
6561/*
6562 * Finish reading history lines from viminfo. Not used when writing viminfo.
6563 */
6564 void
6565finish_viminfo_history(vir_T *virp)
6566{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006568 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 for (type = 0; type < HIST_COUNT; ++type)
6571 {
6572 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006573 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006574
6575 if (merge)
6576 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006578 concat_history(type);
6579
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 vim_free(viminfo_history[type]);
6581 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006582 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
6584}
6585
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006586/*
6587 * Write history to viminfo file in "fp".
6588 * When "merge" is TRUE merge history lines with a previously read viminfo
6589 * file, data is in viminfo_history[].
6590 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006593write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 int i;
6596 int type;
6597 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006598 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600 init_history();
6601 if (hislen == 0)
6602 return;
6603 for (type = 0; type < HIST_COUNT; ++type)
6604 {
6605 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6606 if (num_saved == 0)
6607 continue;
6608 if (num_saved < 0) /* Use default */
6609 num_saved = hislen;
6610 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6611 type == HIST_CMD ? _("Command Line") :
6612 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006613 type == HIST_EXPR ? _("Expression") :
6614 type == HIST_INPUT ? _("Input Line") :
6615 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (num_saved > hislen)
6617 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006618
6619 /*
6620 * Merge typed and viminfo history:
6621 * round 1: history of typed commands.
6622 * round 2: history from recently read viminfo.
6623 */
6624 for (round = 1; round <= 2; ++round)
6625 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006626 if (round == 1)
6627 /* start at newest entry, somewhere in the list */
6628 i = hisidx[type];
6629 else if (viminfo_hisidx[type] > 0)
6630 /* start at newest entry, first in the list */
6631 i = 0;
6632 else
6633 /* empty list */
6634 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006635 if (i >= 0)
6636 while (num_saved > 0
6637 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006639 char_u *p;
6640 time_t timestamp;
6641 int c = NUL;
6642
6643 if (round == 1)
6644 {
6645 p = history[type][i].hisstr;
6646 timestamp = history[type][i].time_set;
6647 }
6648 else
6649 {
6650 p = viminfo_history[type] == NULL ? NULL
6651 : viminfo_history[type][i].hisstr;
6652 timestamp = viminfo_history[type] == NULL ? 0
6653 : viminfo_history[type][i].time_set;
6654 }
6655
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006656 if (p != NULL && (round == 2
6657 || !merge
6658 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006660 --num_saved;
6661 fputc(hist_type2char(type, TRUE), fp);
6662 /* For the search history: put the separator in the
6663 * second column; use a space if there isn't one. */
6664 if (type == HIST_SEARCH)
6665 {
6666 c = p[STRLEN(p) + 1];
6667 putc(c == NUL ? ' ' : c, fp);
6668 }
6669 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006670
6671 {
6672 char cbuf[NUMBUFLEN];
6673
6674 /* New style history with a bar line. Format:
6675 * |{bartype},{histtype},{timestamp},{separator},"text" */
6676 if (c == NUL)
6677 cbuf[0] = NUL;
6678 else
6679 sprintf(cbuf, "%d", c);
6680 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6681 type, (long)timestamp, cbuf);
6682 barline_writestring(fp, p, LSIZE - 20);
6683 putc('\n', fp);
6684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006686 if (round == 1)
6687 {
6688 /* Decrement index, loop around and stop when back at
6689 * the start. */
6690 if (--i < 0)
6691 i = hislen - 1;
6692 if (i == hisidx[type])
6693 break;
6694 }
6695 else
6696 {
6697 /* Increment index. Stop at the end in the while. */
6698 ++i;
6699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006701 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006702 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006703 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006704 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006705 vim_free(viminfo_history[type]);
6706 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006707 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
6709}
6710#endif /* FEAT_VIMINFO */
6711
6712#if defined(FEAT_FKMAP) || defined(PROTO)
6713/*
6714 * Write a character at the current cursor+offset position.
6715 * It is directly written into the command buffer block.
6716 */
6717 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006718cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6721 {
6722 EMSG(_("E198: cmd_pchar beyond the command length"));
6723 return;
6724 }
6725 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6726 ccline.cmdbuff[ccline.cmdlen] = NUL;
6727}
6728
6729 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006730cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6733 {
6734 /* EMSG(_("cmd_gchar beyond the command length")); */
6735 return NUL;
6736 }
6737 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6738}
6739#endif
6740
6741#if defined(FEAT_CMDWIN) || defined(PROTO)
6742/*
6743 * Open a window on the current command line and history. Allow editing in
6744 * the window. Returns when the window is closed.
6745 * Returns:
6746 * CR if the command is to be executed
6747 * Ctrl_C if it is to be abandoned
6748 * K_IGNORE if editing continues
6749 */
6750 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006751ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006754 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006756 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 win_T *wp;
6758 int i;
6759 linenr_T lnum;
6760 int histtype;
6761 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006762#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 int save_restart_edit = restart_edit;
6766 int save_State = State;
6767 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006768#ifdef FEAT_RIGHTLEFT
6769 int save_cmdmsg_rl = cmdmsg_rl;
6770#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006771#ifdef FEAT_FOLDING
6772 int save_KeyTyped;
6773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
6775 /* Can't do this recursively. Can't do it when typing a password. */
6776 if (cmdwin_type != 0
6777# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6778 || cmdline_star > 0
6779# endif
6780 )
6781 {
6782 beep_flush();
6783 return K_IGNORE;
6784 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006785 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787 /* Save current window sizes. */
6788 win_size_save(&winsizes);
6789
6790# ifdef FEAT_AUTOCMD
6791 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006792 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006794 /* don't use a new tab page */
6795 cmdmod.tab = 0;
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* Create a window for the command-line buffer. */
6798 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6799 {
6800 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006801# ifdef FEAT_AUTOCMD
6802 unblock_autocmds();
6803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 return K_IGNORE;
6805 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006806 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
6808 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006809 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006810 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6812 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6813 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006814#ifdef FEAT_FOLDING
6815 curwin->w_p_fen = FALSE;
6816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006818 curwin->w_p_rl = cmdmsg_rl;
6819 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006821 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
6823# ifdef FEAT_AUTOCMD
6824 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006825 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826# endif
6827
Bram Moolenaar46152342005-09-07 21:18:43 +00006828 /* Showing the prompt may have set need_wait_return, reset it. */
6829 need_wait_return = FALSE;
6830
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006831 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6833 {
6834 if (p_wc == TAB)
6835 {
6836 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6837 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6838 }
6839 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6840 }
6841
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006842 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6843 * sets 'textwidth' to 78). */
6844 curbuf->b_p_tw = 0;
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 /* Fill the buffer with the history. */
6847 init_history();
6848 if (hislen > 0)
6849 {
6850 i = hisidx[histtype];
6851 if (i >= 0)
6852 {
6853 lnum = 0;
6854 do
6855 {
6856 if (++i == hislen)
6857 i = 0;
6858 if (history[histtype][i].hisstr != NULL)
6859 ml_append(lnum++, history[histtype][i].hisstr,
6860 (colnr_T)0, FALSE);
6861 }
6862 while (i != hisidx[histtype]);
6863 }
6864 }
6865
6866 /* Replace the empty last line with the current command-line and put the
6867 * cursor there. */
6868 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6869 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6870 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006871 changed_line_abv_curs();
6872 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006873 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
6875 /* Save the command line info, can be used recursively. */
6876 save_ccline = ccline;
6877 ccline.cmdbuff = NULL;
6878 ccline.cmdprompt = NULL;
6879
6880 /* No Ex mode here! */
6881 exmode_active = 0;
6882
6883 State = NORMAL;
6884# ifdef FEAT_MOUSE
6885 setmouse();
6886# endif
6887
6888# ifdef FEAT_AUTOCMD
6889 /* Trigger CmdwinEnter autocommands. */
6890 typestr[0] = cmdwin_type;
6891 typestr[1] = NUL;
6892 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006893 if (restart_edit != 0) /* autocmd with ":startinsert" */
6894 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895# endif
6896
6897 i = RedrawingDisabled;
6898 RedrawingDisabled = 0;
6899
6900 /*
6901 * Call the main loop until <CR> or CTRL-C is typed.
6902 */
6903 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006904 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 RedrawingDisabled = i;
6907
6908# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006909
6910# ifdef FEAT_FOLDING
6911 save_KeyTyped = KeyTyped;
6912# endif
6913
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 /* Trigger CmdwinLeave autocommands. */
6915 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006916
6917# ifdef FEAT_FOLDING
6918 /* Restore KeyTyped in case it is modified by autocommands */
6919 KeyTyped = save_KeyTyped;
6920# endif
6921
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922# endif
6923
Bram Moolenaarccc18222007-05-10 18:25:20 +00006924 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 ccline = save_ccline;
6926 cmdwin_type = 0;
6927
6928 exmode_active = save_exmode;
6929
Bram Moolenaarf9821062008-06-20 16:31:07 +00006930 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006932 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
6934 cmdwin_result = Ctrl_C;
6935 EMSG(_("E199: Active window or buffer deleted"));
6936 }
6937 else
6938 {
6939# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6940 /* autocmds may abort script processing */
6941 if (aborting() && cmdwin_result != K_IGNORE)
6942 cmdwin_result = Ctrl_C;
6943# endif
6944 /* Set the new command line from the cmdline buffer. */
6945 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006946 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006948 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6949
6950 if (histtype == HIST_CMD)
6951 {
6952 /* Execute the command directly. */
6953 ccline.cmdbuff = vim_strsave((char_u *)p);
6954 cmdwin_result = CAR;
6955 }
6956 else
6957 {
6958 /* First need to cancel what we were doing. */
6959 ccline.cmdbuff = NULL;
6960 stuffcharReadbuff(':');
6961 stuffReadbuff((char_u *)p);
6962 stuffcharReadbuff(CAR);
6963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 }
6965 else if (cmdwin_result == K_XF2) /* :qa typed */
6966 {
6967 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6968 cmdwin_result = CAR;
6969 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006970 else if (cmdwin_result == Ctrl_C)
6971 {
6972 /* :q or :close, don't execute any command
6973 * and don't modify the cmd window. */
6974 ccline.cmdbuff = NULL;
6975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 else
6977 ccline.cmdbuff = vim_strsave(ml_get_curline());
6978 if (ccline.cmdbuff == NULL)
6979 cmdwin_result = Ctrl_C;
6980 else
6981 {
6982 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6983 ccline.cmdbufflen = ccline.cmdlen + 1;
6984 ccline.cmdpos = curwin->w_cursor.col;
6985 if (ccline.cmdpos > ccline.cmdlen)
6986 ccline.cmdpos = ccline.cmdlen;
6987 if (cmdwin_result == K_IGNORE)
6988 {
6989 set_cmdspos_cursor();
6990 redrawcmd();
6991 }
6992 }
6993
6994# ifdef FEAT_AUTOCMD
6995 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006996 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02006998# ifdef FEAT_CONCEAL
6999 /* Avoid command-line window first character being concealed. */
7000 curwin->w_p_cole = 0;
7001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007003 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 win_goto(old_curwin);
7005 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007006
7007 /* win_close() may have already wiped the buffer when 'bh' is
7008 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007009 if (bufref_valid(&bufref))
7010 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 /* Restore window sizes. */
7013 win_size_restore(&winsizes);
7014
7015# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007016 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017# endif
7018 }
7019
7020 ga_clear(&winsizes);
7021 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007022# ifdef FEAT_RIGHTLEFT
7023 cmdmsg_rl = save_cmdmsg_rl;
7024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
7026 State = save_State;
7027# ifdef FEAT_MOUSE
7028 setmouse();
7029# endif
7030
7031 return cmdwin_result;
7032}
7033#endif /* FEAT_CMDWIN */
7034
7035/*
7036 * Used for commands that either take a simple command string argument, or:
7037 * cmd << endmarker
7038 * {script}
7039 * endmarker
7040 * Returns a pointer to allocated memory with {script} or NULL.
7041 */
7042 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007043script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 char_u *theline;
7046 char *end_pattern = NULL;
7047 char dot[] = ".";
7048 garray_T ga;
7049
7050 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7051 return NULL;
7052
7053 ga_init2(&ga, 1, 0x400);
7054
7055 if (cmd[2] != NUL)
7056 end_pattern = (char *)skipwhite(cmd + 2);
7057 else
7058 end_pattern = dot;
7059
7060 for (;;)
7061 {
7062 theline = eap->getline(
7063#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007064 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065#endif
7066 NUL, eap->cookie, 0);
7067
7068 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007069 {
7070 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
7074 ga_concat(&ga, theline);
7075 ga_append(&ga, '\n');
7076 vim_free(theline);
7077 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007078 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080 return (char_u *)ga.ga_data;
7081}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007082
7083#ifdef FEAT_SEARCH_EXTRA
7084 static void
7085set_search_match(pos_T *t)
7086{
7087 /*
7088 * First move cursor to end of match, then to the start. This
7089 * moves the whole match onto the screen when 'nowrap' is set.
7090 */
7091 t->lnum += search_match_lines;
7092 t->col = search_match_endcol;
7093 if (t->lnum > curbuf->b_ml.ml_line_count)
7094 {
7095 t->lnum = curbuf->b_ml.ml_line_count;
7096 coladvance((colnr_T)MAXCOL);
7097 }
7098}
7099#endif